Show.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. namespace Dcat\Admin;
  3. use Dcat\Admin\Contracts\Repository;
  4. use Dcat\Admin\Show\AbstractTool;
  5. use Dcat\Admin\Show\Divider;
  6. use Dcat\Admin\Show\Field;
  7. use Dcat\Admin\Show\Newline;
  8. use Dcat\Admin\Show\Panel;
  9. use Dcat\Admin\Show\Relation;
  10. use Dcat\Admin\Show\Tools;
  11. use Dcat\Admin\Traits\HasBuilderEvents;
  12. use Illuminate\Contracts\Support\Arrayable;
  13. use Illuminate\Contracts\Support\Htmlable;
  14. use Illuminate\Contracts\Support\Renderable;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Support\Arr;
  18. use Illuminate\Support\Collection;
  19. use Illuminate\Support\Fluent;
  20. use Illuminate\Support\Traits\Macroable;
  21. class Show implements Renderable
  22. {
  23. use HasBuilderEvents,
  24. Macroable {
  25. __call as macroCall;
  26. }
  27. /**
  28. * @var string
  29. */
  30. protected $view = 'admin::show.container';
  31. /**
  32. * @var Repository
  33. */
  34. protected $repository;
  35. /**
  36. * @var mixed
  37. */
  38. protected $_id;
  39. /**
  40. * @var string
  41. */
  42. protected $keyName = 'id';
  43. /**
  44. * @var Fluent
  45. */
  46. protected $model;
  47. /**
  48. * Show panel builder.
  49. *
  50. * @var callable
  51. */
  52. protected $builder;
  53. /**
  54. * Resource path for this show page.
  55. *
  56. * @var string
  57. */
  58. protected $resource;
  59. /**
  60. * Fields to be show.
  61. *
  62. * @var Collection
  63. */
  64. protected $fields;
  65. /**
  66. * Relations to be show.
  67. *
  68. * @var Collection
  69. */
  70. protected $relations;
  71. /**
  72. * @var Panel
  73. */
  74. protected $panel;
  75. /**
  76. * Show constructor.
  77. *
  78. * @param mixed $id $id
  79. * @param Model|Builder|Repository|array|Arrayable $model
  80. * @param \Closure $builder
  81. */
  82. public function __construct($id = null, $model = null, ?\Closure $builder = null)
  83. {
  84. switch (func_num_args()) {
  85. case 1:
  86. case 2:
  87. if (is_scalar($id)) {
  88. $this->setKey($id);
  89. } else {
  90. $builder = $model;
  91. $model = $id;
  92. }
  93. break;
  94. default:
  95. $this->setKey($id);
  96. }
  97. $this->builder = $builder;
  98. $this->initModel($model);
  99. $this->initPanel();
  100. $this->initContents();
  101. $this->callResolving();
  102. }
  103. protected function initModel($model)
  104. {
  105. if ($model instanceof Repository || $model instanceof Builder) {
  106. $this->repository = Admin::repository($model);
  107. } elseif ($model instanceof Model) {
  108. if ($key = $model->getKey()) {
  109. $this->key = $model->getKey();
  110. $this->keyName = $model->getKeyName();
  111. $this->model(new Fluent($model->toArray()));
  112. } else {
  113. $this->repository = Admin::repository($model);
  114. }
  115. } elseif ($model instanceof Arrayable) {
  116. $this->model(new Fluent($model->toArray()));
  117. } elseif (is_array($model)) {
  118. $this->model(new Fluent($model));
  119. } else {
  120. $this->model(new Fluent());
  121. }
  122. }
  123. /**
  124. * Create a show instance.
  125. *
  126. * @param mixed ...$params
  127. *
  128. * @return $this
  129. */
  130. public static function make(...$params)
  131. {
  132. return new static(...$params);
  133. }
  134. /**
  135. * @param string $value
  136. *
  137. * @return $this
  138. */
  139. public function setKeyName(string $value)
  140. {
  141. $this->keyName = $value;
  142. return $this;
  143. }
  144. /**
  145. * Get primary key name of model.
  146. *
  147. * @return string
  148. */
  149. public function getKeyName()
  150. {
  151. if (! $this->repository) {
  152. return $this->keyName;
  153. }
  154. return $this->keyName ?: $this->repository->getKeyName();
  155. }
  156. /**
  157. * @param mixed $id
  158. *
  159. * @return mixed
  160. */
  161. public function setKey($id)
  162. {
  163. $this->_id = $id;
  164. return $this;
  165. }
  166. /**
  167. * @return mixed
  168. */
  169. public function getKey()
  170. {
  171. return $this->_id;
  172. }
  173. /**
  174. * @param Fluent|null $model
  175. *
  176. * @return Fluent|$this
  177. */
  178. public function model(Fluent $model = null)
  179. {
  180. if ($model === null) {
  181. if (! $this->model) {
  182. $this->setupModel();
  183. }
  184. return $this->model;
  185. }
  186. $this->model = $model;
  187. return $this;
  188. }
  189. protected function setupModel()
  190. {
  191. if ($this->repository) {
  192. $this->model(new Fluent($this->repository->detail($this)));
  193. } else {
  194. $this->model(new Fluent());
  195. }
  196. }
  197. /**
  198. * Set a view to render.
  199. *
  200. * @param string $view
  201. * @param array $variables
  202. *
  203. * @return $this
  204. */
  205. public function view($view, $variables = [])
  206. {
  207. if (! empty($variables)) {
  208. $this->with($variables);
  209. }
  210. $this->panel->view($view);
  211. return $this;
  212. }
  213. /**
  214. * Add variables to show view.
  215. *
  216. * @param array $variables
  217. *
  218. * @return $this
  219. */
  220. public function with($variables = [])
  221. {
  222. $this->panel->with($variables);
  223. return $this;
  224. }
  225. /**
  226. * @return $this
  227. */
  228. public function wrap(\Closure $wrapper)
  229. {
  230. $this->panel->wrap($wrapper);
  231. return $this;
  232. }
  233. /**
  234. * Initialize the contents to show.
  235. */
  236. protected function initContents()
  237. {
  238. $this->fields = new Collection();
  239. $this->relations = new Collection();
  240. }
  241. /**
  242. * Initialize panel.
  243. */
  244. protected function initPanel()
  245. {
  246. $this->panel = new Panel($this);
  247. }
  248. /**
  249. * Get panel instance.
  250. *
  251. * @return Panel
  252. */
  253. public function panel()
  254. {
  255. return $this->panel;
  256. }
  257. /**
  258. * @param \Closure|array|AbstractTool|Renderable|Htmlable|string $callback
  259. *
  260. * @return $this|Tools
  261. */
  262. public function tools($callback = null)
  263. {
  264. if ($callback === null) {
  265. return $this->panel->tools();
  266. }
  267. if ($callback instanceof \Closure) {
  268. $callback->call($this->model, $this->panel->tools());
  269. return $this;
  270. }
  271. if (! is_array($callback)) {
  272. $callback = [$callback];
  273. }
  274. foreach ($callback as $tool) {
  275. $this->panel->tools()->append($tool);
  276. }
  277. return $this;
  278. }
  279. /**
  280. * Add a model field to show.
  281. *
  282. * @param string $name
  283. * @param string $label
  284. *
  285. * @return Field
  286. */
  287. public function field($name, $label = '')
  288. {
  289. return $this->addField($name, $label);
  290. }
  291. /**
  292. * Get fields or add multiple fields.
  293. *
  294. * @param array $fields
  295. *
  296. * @return $this|Collection
  297. */
  298. public function fields(array $fields = null)
  299. {
  300. if ($fields === null) {
  301. return $this->fields;
  302. }
  303. if (! Arr::isAssoc($fields)) {
  304. $fields = array_combine($fields, $fields);
  305. }
  306. foreach ($fields as $field => $label) {
  307. $this->field($field, $label);
  308. }
  309. return $this;
  310. }
  311. /**
  312. * @return Collection
  313. */
  314. public function relations()
  315. {
  316. return $this->relations;
  317. }
  318. /**
  319. * Show all fields.
  320. *
  321. * @return Show
  322. */
  323. public function all()
  324. {
  325. $fields = array_keys($this->model()->toArray());
  326. return $this->fields($fields);
  327. }
  328. /**
  329. * Add a relation to show.
  330. *
  331. * @param string $name
  332. * @param string|\Closure $label
  333. * @param null|\Closure $builder
  334. *
  335. * @return Relation
  336. */
  337. public function relation($name, $label, $builder = null)
  338. {
  339. if (is_null($builder)) {
  340. $builder = $label;
  341. $label = '';
  342. }
  343. return $this->addRelation($name, $builder, $label);
  344. }
  345. /**
  346. * Add a model field to show.
  347. *
  348. * @param string $name
  349. * @param string $label
  350. *
  351. * @return Field
  352. */
  353. protected function addField($name, $label = '')
  354. {
  355. $field = new Field($name, $label);
  356. $field->setParent($this);
  357. $this->overwriteExistingField($name);
  358. $this->fields->push($field);
  359. return $field;
  360. }
  361. /**
  362. * Add a relation panel to show.
  363. *
  364. * @param string $name
  365. * @param \Closure $builder
  366. * @param string $label
  367. *
  368. * @return Relation
  369. */
  370. protected function addRelation($name, $builder, $label = '')
  371. {
  372. $relation = new Relation($name, $builder, $label);
  373. $relation->setParent($this);
  374. $this->overwriteExistingRelation($name);
  375. $this->relations->push($relation);
  376. return $relation;
  377. }
  378. /**
  379. * Overwrite existing field.
  380. *
  381. * @param string $name
  382. */
  383. protected function overwriteExistingField($name)
  384. {
  385. if ($this->fields->isEmpty()) {
  386. return;
  387. }
  388. $this->fields = $this->fields->filter(
  389. function (Field $field) use ($name) {
  390. return $field->getName() != $name;
  391. }
  392. );
  393. }
  394. /**
  395. * Overwrite existing relation.
  396. *
  397. * @param string $name
  398. */
  399. protected function overwriteExistingRelation($name)
  400. {
  401. if ($this->relations->isEmpty()) {
  402. return;
  403. }
  404. $this->relations = $this->relations->filter(
  405. function (Relation $relation) use ($name) {
  406. return $relation->getName() != $name;
  407. }
  408. );
  409. }
  410. /**
  411. * @return Repository
  412. */
  413. public function repository()
  414. {
  415. return $this->repository;
  416. }
  417. /**
  418. * Show a divider.
  419. */
  420. public function divider()
  421. {
  422. $this->fields->push(new Divider());
  423. }
  424. /**
  425. * Show a divider.
  426. */
  427. public function newline()
  428. {
  429. $this->fields->push(new Newline());
  430. }
  431. /**
  432. * Disable `list` tool.
  433. *
  434. * @return $this
  435. */
  436. public function disableListButton(bool $disable = true)
  437. {
  438. $this->panel->tools()->disableList($disable);
  439. return $this;
  440. }
  441. /**
  442. * Disable `delete` tool.
  443. *
  444. * @return $this
  445. */
  446. public function disableDeleteButton(bool $disable = true)
  447. {
  448. $this->panel->tools()->disableDelete($disable);
  449. return $this;
  450. }
  451. /**
  452. * Disable `edit` tool.
  453. *
  454. * @return $this
  455. */
  456. public function disableEditButton(bool $disable = true)
  457. {
  458. $this->panel->tools()->disableEdit($disable);
  459. return $this;
  460. }
  461. /**
  462. * Show quick edit tool.
  463. *
  464. * @param null|string $width
  465. * @param null|string $height
  466. *
  467. * @return $this
  468. */
  469. public function showQuickEdit(?string $width = null, ?string $height = null)
  470. {
  471. $this->panel->tools()->showQuickEdit($width, $height);
  472. return $this;
  473. }
  474. /**
  475. * Disable quick edit tool.
  476. *
  477. * @return $this
  478. */
  479. public function disableQuickEdit()
  480. {
  481. $this->panel->tools()->disableQuickEdit();
  482. return $this;
  483. }
  484. /**
  485. * Set resource path.
  486. *
  487. * @param string $resource
  488. *
  489. * @return $this
  490. */
  491. public function resource(string $resource)
  492. {
  493. if ($resource) {
  494. $this->resource = admin_url($resource);
  495. }
  496. return $this;
  497. }
  498. /**
  499. * Get resource path.
  500. *
  501. * @return string
  502. */
  503. public function getResource()
  504. {
  505. if (empty($this->resource)) {
  506. $path = request()->path();
  507. $segments = explode('/', $path);
  508. array_pop($segments);
  509. $this->resource = url(implode('/', $segments));
  510. }
  511. return $this->resource;
  512. }
  513. /**
  514. * Add field and relation dynamically.
  515. *
  516. * @param string $method
  517. * @param array $arguments
  518. *
  519. * @return Field
  520. */
  521. public function __call($method, $arguments = [])
  522. {
  523. if (static::hasMacro($method)) {
  524. return $this->macroCall($method, $arguments);
  525. }
  526. return $this->call($method, $arguments);
  527. }
  528. /**
  529. * @param $method
  530. * @param array $arguments
  531. *
  532. * @return bool|Show|Field|Relation
  533. */
  534. protected function call($method, $arguments = [])
  535. {
  536. $label = isset($arguments[0]) ? $arguments[0] : '';
  537. if ($field = $this->handleRelationField($method, $arguments)) {
  538. return $field;
  539. }
  540. return $this->addField($method, $label);
  541. }
  542. /**
  543. * Handle relation field.
  544. *
  545. * @param string $method
  546. * @param array $arguments
  547. *
  548. * @return $this|bool|Relation|Field
  549. */
  550. protected function handleRelationField($method, $arguments)
  551. {
  552. if (count($arguments) == 1 && $arguments[0] instanceof \Closure) {
  553. return $this->addRelation($method, $arguments[0]);
  554. } elseif (count($arguments) == 2 && $arguments[1] instanceof \Closure) {
  555. return $this->addRelation($method, $arguments[1], $arguments[0]);
  556. }
  557. return false;
  558. }
  559. /**
  560. * Render the show panels.
  561. *
  562. * @return string
  563. */
  564. public function render()
  565. {
  566. try {
  567. $model = $this->model();
  568. if (is_callable($this->builder)) {
  569. call_user_func($this->builder, $this);
  570. }
  571. if ($this->fields->isEmpty()) {
  572. $this->all();
  573. }
  574. if (is_array($this->builder)) {
  575. $this->fields($this->builder);
  576. }
  577. $this->fields->each->fill($model);
  578. $this->relations->each->model($model);
  579. $this->callComposing();
  580. $data = [
  581. 'panel' => $this->panel->fill($this->fields),
  582. 'relations' => $this->relations,
  583. ];
  584. return view($this->view, $data)->render();
  585. } catch (\Throwable $e) {
  586. return Admin::makeExceptionHandler()->renderException($e);
  587. }
  588. }
  589. /**
  590. * Add a model field to show.
  591. *
  592. * @param string $name
  593. *
  594. * @return Field|Collection
  595. */
  596. public function __get($name)
  597. {
  598. return $this->call($name);
  599. }
  600. }