Show.php 14 KB

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