Show.php 15 KB

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