Show.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. *
  205. * @return $this
  206. */
  207. public function view($view)
  208. {
  209. $this->panel->view($view);
  210. return $this;
  211. }
  212. /**
  213. * Add variables to show view.
  214. *
  215. * @param array $variables
  216. *
  217. * @return $this
  218. */
  219. public function with($variables = [])
  220. {
  221. $this->panel->with($variables);
  222. return $this;
  223. }
  224. /**
  225. * @return $this
  226. */
  227. public function wrap(\Closure $wrapper)
  228. {
  229. $this->panel->wrap($wrapper);
  230. return $this;
  231. }
  232. /**
  233. * Initialize the contents to show.
  234. */
  235. protected function initContents()
  236. {
  237. $this->fields = new Collection();
  238. $this->relations = new Collection();
  239. }
  240. /**
  241. * Initialize panel.
  242. */
  243. protected function initPanel()
  244. {
  245. $this->panel = new Panel($this);
  246. }
  247. /**
  248. * Get panel instance.
  249. *
  250. * @return Panel
  251. */
  252. public function panel()
  253. {
  254. return $this->panel;
  255. }
  256. /**
  257. * @param \Closure|array|AbstractTool|Renderable|Htmlable|string $callback
  258. *
  259. * @return $this|Tools
  260. */
  261. public function tools($callback = null)
  262. {
  263. if ($callback === null) {
  264. return $this->panel->tools();
  265. }
  266. if ($callback instanceof \Closure) {
  267. $callback->call($this->model, $this->panel->tools());
  268. return $this;
  269. }
  270. if (! is_array($callback)) {
  271. $callback = [$callback];
  272. }
  273. foreach ($callback as $tool) {
  274. $this->panel->tools()->append($tool);
  275. }
  276. return $this;
  277. }
  278. /**
  279. * Add a model field to show.
  280. *
  281. * @param string $name
  282. * @param string $label
  283. *
  284. * @return Field
  285. */
  286. public function field($name, $label = '')
  287. {
  288. return $this->addField($name, $label);
  289. }
  290. /**
  291. * Get fields or add multiple fields.
  292. *
  293. * @param array $fields
  294. *
  295. * @return $this|Collection
  296. */
  297. public function fields(array $fields = null)
  298. {
  299. if ($fields === null) {
  300. return $this->fields;
  301. }
  302. if (! Arr::isAssoc($fields)) {
  303. $fields = array_combine($fields, $fields);
  304. }
  305. foreach ($fields as $field => $label) {
  306. $this->field($field, $label);
  307. }
  308. return $this;
  309. }
  310. /**
  311. * @return Collection
  312. */
  313. public function relations()
  314. {
  315. return $this->relations;
  316. }
  317. /**
  318. * Show all fields.
  319. *
  320. * @return Show
  321. */
  322. public function all()
  323. {
  324. $fields = array_keys($this->model()->toArray());
  325. return $this->fields($fields);
  326. }
  327. /**
  328. * Add a relation to show.
  329. *
  330. * @param string $name
  331. * @param string|\Closure $label
  332. * @param null|\Closure $builder
  333. *
  334. * @return Relation
  335. */
  336. public function relation($name, $label, $builder = null)
  337. {
  338. if (is_null($builder)) {
  339. $builder = $label;
  340. $label = '';
  341. }
  342. return $this->addRelation($name, $builder, $label);
  343. }
  344. /**
  345. * Add a model field to show.
  346. *
  347. * @param string $name
  348. * @param string $label
  349. *
  350. * @return Field
  351. */
  352. protected function addField($name, $label = '')
  353. {
  354. $field = new Field($name, $label);
  355. $field->setParent($this);
  356. $this->overwriteExistingField($name);
  357. $this->fields->push($field);
  358. return $field;
  359. }
  360. /**
  361. * Add a relation panel to show.
  362. *
  363. * @param string $name
  364. * @param \Closure $builder
  365. * @param string $label
  366. *
  367. * @return Relation
  368. */
  369. protected function addRelation($name, $builder, $label = '')
  370. {
  371. $relation = new Relation($name, $builder, $label);
  372. $relation->setParent($this);
  373. $this->overwriteExistingRelation($name);
  374. $this->relations->push($relation);
  375. return $relation;
  376. }
  377. /**
  378. * Overwrite existing field.
  379. *
  380. * @param string $name
  381. */
  382. protected function overwriteExistingField($name)
  383. {
  384. if ($this->fields->isEmpty()) {
  385. return;
  386. }
  387. $this->fields = $this->fields->filter(
  388. function (Field $field) use ($name) {
  389. return $field->getName() != $name;
  390. }
  391. );
  392. }
  393. /**
  394. * Overwrite existing relation.
  395. *
  396. * @param string $name
  397. */
  398. protected function overwriteExistingRelation($name)
  399. {
  400. if ($this->relations->isEmpty()) {
  401. return;
  402. }
  403. $this->relations = $this->relations->filter(
  404. function (Relation $relation) use ($name) {
  405. return $relation->getName() != $name;
  406. }
  407. );
  408. }
  409. /**
  410. * @return Repository
  411. */
  412. public function repository()
  413. {
  414. return $this->repository;
  415. }
  416. /**
  417. * Show a divider.
  418. */
  419. public function divider()
  420. {
  421. $this->fields->push(new Divider());
  422. }
  423. /**
  424. * Show a divider.
  425. */
  426. public function newline()
  427. {
  428. $this->fields->push(new Newline());
  429. }
  430. /**
  431. * Show the content of html.
  432. *
  433. * @param string $html
  434. */
  435. public function html($html = '')
  436. {
  437. $this->fields->push((new Html($html))->setParent($this));
  438. }
  439. /**
  440. * Disable `list` tool.
  441. *
  442. * @return $this
  443. */
  444. public function disableListButton(bool $disable = true)
  445. {
  446. $this->panel->tools()->disableList($disable);
  447. return $this;
  448. }
  449. /**
  450. * Disable `delete` tool.
  451. *
  452. * @return $this
  453. */
  454. public function disableDeleteButton(bool $disable = true)
  455. {
  456. $this->panel->tools()->disableDelete($disable);
  457. return $this;
  458. }
  459. /**
  460. * Disable `edit` tool.
  461. *
  462. * @return $this
  463. */
  464. public function disableEditButton(bool $disable = true)
  465. {
  466. $this->panel->tools()->disableEdit($disable);
  467. return $this;
  468. }
  469. /**
  470. * Show quick edit tool.
  471. *
  472. * @param null|string $width
  473. * @param null|string $height
  474. *
  475. * @return $this
  476. */
  477. public function showQuickEdit(?string $width = null, ?string $height = null)
  478. {
  479. $this->panel->tools()->showQuickEdit($width, $height);
  480. return $this;
  481. }
  482. /**
  483. * Disable quick edit tool.
  484. *
  485. * @return $this
  486. */
  487. public function disableQuickEdit()
  488. {
  489. $this->panel->tools()->disableQuickEdit();
  490. return $this;
  491. }
  492. /**
  493. * @return string
  494. */
  495. public function resource()
  496. {
  497. if (empty($this->resource)) {
  498. $path = request()->path();
  499. $segments = explode('/', $path);
  500. array_pop($segments);
  501. $this->resource = url(implode('/', $segments));
  502. }
  503. return $this->resource;
  504. }
  505. /**
  506. * Set resource path.
  507. *
  508. * @param string $path
  509. *
  510. * @return $this
  511. */
  512. public function setResource($path)
  513. {
  514. if ($path) {
  515. $this->resource = admin_url($path);
  516. }
  517. return $this;
  518. }
  519. /**
  520. * Add field and relation dynamically.
  521. *
  522. * @param string $method
  523. * @param array $arguments
  524. *
  525. * @return Field
  526. */
  527. public function __call($method, $arguments = [])
  528. {
  529. if (static::hasMacro($method)) {
  530. return $this->macroCall($method, $arguments);
  531. }
  532. return $this->call($method, $arguments);
  533. }
  534. /**
  535. * @param $method
  536. * @param array $arguments
  537. *
  538. * @return bool|Show|Field|Relation
  539. */
  540. protected function call($method, $arguments = [])
  541. {
  542. $label = isset($arguments[0]) ? $arguments[0] : '';
  543. if ($field = $this->handleRelationField($method, $arguments)) {
  544. return $field;
  545. }
  546. return $this->addField($method, $label);
  547. }
  548. /**
  549. * Handle relation field.
  550. *
  551. * @param string $method
  552. * @param array $arguments
  553. *
  554. * @return $this|bool|Relation|Field
  555. */
  556. protected function handleRelationField($method, $arguments)
  557. {
  558. if (count($arguments) == 1 && $arguments[0] instanceof \Closure) {
  559. return $this->addRelation($method, $arguments[0]);
  560. } elseif (count($arguments) == 2 && $arguments[1] instanceof \Closure) {
  561. return $this->addRelation($method, $arguments[1], $arguments[0]);
  562. }
  563. return false;
  564. }
  565. /**
  566. * Render the show panels.
  567. *
  568. * @return string
  569. */
  570. public function render()
  571. {
  572. $model = $this->model();
  573. if (is_callable($this->builder)) {
  574. call_user_func($this->builder, $this);
  575. }
  576. if ($this->fields->isEmpty()) {
  577. $this->all();
  578. }
  579. if (is_array($this->builder)) {
  580. $this->fields($this->builder);
  581. }
  582. $this->fields->each->fill($model);
  583. $this->relations->each->model($model);
  584. $this->callComposing();
  585. $data = [
  586. 'panel' => $this->panel->fill($this->fields),
  587. 'relations' => $this->relations,
  588. ];
  589. return view($this->view, $data)->render();
  590. }
  591. /**
  592. * Add a row in Show.
  593. *
  594. * @param Closure $callback
  595. *
  596. * @return $this
  597. */
  598. public function row(Closure $callback)
  599. {
  600. $this->rows->push(new Row($callback, $this));
  601. return $this;
  602. }
  603. /**
  604. * @return \Illuminate\Support\Collection
  605. */
  606. public function rows()
  607. {
  608. return $this->rows;
  609. }
  610. /**
  611. * Add a model field to show.
  612. *
  613. * @param string $name
  614. *
  615. * @return Field|Collection
  616. */
  617. public function __get($name)
  618. {
  619. return $this->call($name);
  620. }
  621. }