Field.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. namespace Dcat\Admin\Show;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Show;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Traits\HasBuilderEvents;
  7. use Dcat\Admin\Traits\HasDefinitions;
  8. use Dcat\Admin\Widgets\Dump;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. use Illuminate\Contracts\Support\Renderable;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Collection;
  13. use Illuminate\Support\Facades\Storage;
  14. use Illuminate\Support\Fluent;
  15. use Illuminate\Support\Str;
  16. use Illuminate\Support\Traits\Macroable;
  17. class Field implements Renderable
  18. {
  19. use HasBuilderEvents,
  20. HasDefinitions,
  21. Macroable {
  22. __call as macroCall;
  23. }
  24. /**
  25. * @var array
  26. */
  27. protected static $extendedFields = [];
  28. /**
  29. * @var string
  30. */
  31. protected $view = 'admin::show.field';
  32. /**
  33. * Name of column.
  34. *
  35. * @var string
  36. */
  37. protected $name;
  38. /**
  39. * Label of column.
  40. *
  41. * @var string
  42. */
  43. protected $label;
  44. /**
  45. * Escape field value or not.
  46. *
  47. * @var bool
  48. */
  49. protected $escape = true;
  50. /**
  51. * Field value.
  52. *
  53. * @var mixed
  54. */
  55. protected $value;
  56. /**
  57. * @var Collection
  58. */
  59. protected $showAs = [];
  60. /**
  61. * Parent show instance.
  62. *
  63. * @var Show
  64. */
  65. protected $parent;
  66. /**
  67. * Relation name.
  68. *
  69. * @var string
  70. */
  71. protected $relation;
  72. /**
  73. * If show contents in box.
  74. *
  75. * @var bool
  76. */
  77. protected $border = true;
  78. /**
  79. * @var int
  80. */
  81. protected $width = 8;
  82. /**
  83. * Field constructor.
  84. *
  85. * @param string $name
  86. * @param string $label
  87. */
  88. public function __construct($name = '', $label = '')
  89. {
  90. $this->name = $name;
  91. $this->label = $this->formatLabel($label);
  92. $this->showAs = new Collection();
  93. $this->callResolving();
  94. }
  95. /**
  96. * Set parent show instance.
  97. *
  98. * @param Show $show
  99. *
  100. * @return $this
  101. */
  102. public function setParent(Show $show)
  103. {
  104. $this->parent = $show;
  105. return $this;
  106. }
  107. /**
  108. * Get name of this column.
  109. *
  110. * @return mixed
  111. */
  112. public function getName()
  113. {
  114. return $this->name;
  115. }
  116. /**
  117. * @param int $width
  118. *
  119. * @return $this|int
  120. */
  121. public function width(int $width = null)
  122. {
  123. if ($width === null) {
  124. return $this->width;
  125. }
  126. $this->width = $width;
  127. return $this;
  128. }
  129. /**
  130. * Format label.
  131. *
  132. * @param $label
  133. *
  134. * @return mixed
  135. */
  136. protected function formatLabel($label)
  137. {
  138. $label = $label ?: admin_trans_field($this->name);
  139. return str_replace(['.', '_'], ' ', $label);
  140. }
  141. /**
  142. * Get label of the column.
  143. *
  144. * @return mixed
  145. */
  146. public function getLabel()
  147. {
  148. return $this->label;
  149. }
  150. /**
  151. * Field display callback.
  152. *
  153. * @param mixed $callable
  154. *
  155. * @return $this
  156. */
  157. public function as($callable, ...$params)
  158. {
  159. $this->showAs->push([$callable, $params]);
  160. return $this;
  161. }
  162. /**
  163. * Display field using array value map.
  164. *
  165. * @param array $values
  166. * @param null $default
  167. *
  168. * @return $this
  169. */
  170. public function using(array $values, $default = null)
  171. {
  172. return $this->as(function ($value) use ($values, $default) {
  173. if (is_null($value)) {
  174. return $default;
  175. }
  176. return Arr::get($values, $value, $default);
  177. });
  178. }
  179. /**
  180. * Show field as a image.
  181. *
  182. * @param string $server
  183. * @param int $width
  184. * @param int $height
  185. *
  186. * @return $this
  187. */
  188. public function image($server = '', $width = 200, $height = 200)
  189. {
  190. return $this->unescape()->as(function ($path) use ($server, $width, $height) {
  191. if (empty($path)) {
  192. return '';
  193. }
  194. if (url()->isValidUrl($path)) {
  195. $src = $path;
  196. } elseif ($server) {
  197. $src = $server.$path;
  198. } else {
  199. $disk = config('admin.upload.disk');
  200. if (config("filesystems.disks.{$disk}")) {
  201. $src = Storage::disk($disk)->url($path);
  202. } else {
  203. return '';
  204. }
  205. }
  206. return "<img data-action='preview-img' src='$src' style='max-width:{$width}px;max-height:{$height}px' class='img' />";
  207. });
  208. }
  209. /**
  210. * Show field as a file.
  211. *
  212. * @param string $server
  213. * @param bool $download
  214. *
  215. * @return Field
  216. */
  217. public function file($server = '', $download = true)
  218. {
  219. $field = $this;
  220. return $this->unescape()->as(function ($path) use ($server, $download, $field) {
  221. $name = basename($path);
  222. $field->wrap(false);
  223. $size = $url = '';
  224. if (url()->isValidUrl($path)) {
  225. $url = $path;
  226. } elseif ($server) {
  227. $url = $server.$path;
  228. } else {
  229. $storage = Storage::disk(config('admin.upload.disk'));
  230. if ($storage->exists($path)) {
  231. $url = $storage->url($path);
  232. $size = ($storage->size($path) / 1000).'KB';
  233. }
  234. }
  235. if (! $url) {
  236. return '';
  237. }
  238. $icon = Helper::getFileIcon($name);
  239. return <<<HTML
  240. <ul class="mailbox-attachments clearfix">
  241. <li style="margin-bottom: 0;">
  242. <span class="mailbox-attachment-icon"><i class="{$icon}"></i></span>
  243. <div class="mailbox-attachment-info">
  244. <div class="mailbox-attachment-name">
  245. <i class="fa fa-paperclip"></i> {$name}
  246. </div>
  247. <span class="mailbox-attachment-size">
  248. {$size}&nbsp;
  249. <a href="{$url}" class="btn btn-white btn-xs pull-right" target="_blank"><i class="fa fa-cloud-download"></i></a>
  250. </span>
  251. </div>
  252. </li>
  253. </ul>
  254. HTML;
  255. });
  256. }
  257. /**
  258. * Show field as a link.
  259. *
  260. * @param string $href
  261. * @param string $target
  262. *
  263. * @return Field
  264. */
  265. public function link($href = '', $target = '_blank')
  266. {
  267. return $this->unescape()->as(function ($link) use ($href, $target) {
  268. $href = $href ?: $link;
  269. return "<a href='$href' target='{$target}'>{$link}</a>";
  270. });
  271. }
  272. /**
  273. * Show field as labels.
  274. *
  275. * @param string $style
  276. *
  277. * @return Field
  278. */
  279. public function label($style = 'primary')
  280. {
  281. $self = $this;
  282. return $this->unescape()->as(function ($value) use ($self, $style) {
  283. [$class, $background] = $self->formatStyle($style);
  284. return collect($value)->map(function ($name) use ($class, $background) {
  285. return "<span class='label bg-{$class}' $background>$name</span>";
  286. })->implode('&nbsp;');
  287. });
  288. }
  289. /**
  290. * Show field as badges.
  291. *
  292. * @param string $style
  293. *
  294. * @return Field
  295. */
  296. public function badge($style = 'blue')
  297. {
  298. $self = $this;
  299. return $this->unescape()->as(function ($value) use ($self, $style) {
  300. [$class, $background] = $self->formatStyle($style);
  301. return collect($value)->map(function ($name) use ($class, $background) {
  302. return "<span class='badge bg-{$class}' $background>$name</span>";
  303. })->implode('&nbsp;');
  304. });
  305. }
  306. /**
  307. * @param $style
  308. *
  309. * @return array
  310. */
  311. public function formatStyle($style)
  312. {
  313. $class = 'default';
  314. $background = '';
  315. if ($style !== 'default') {
  316. $class = '';
  317. $style = Admin::color()->get($style);
  318. $background = "style='background:{$style}'";
  319. }
  320. return [$class, $background];
  321. }
  322. /**
  323. * Show field as json code.
  324. *
  325. * @return Field
  326. */
  327. public function json()
  328. {
  329. $field = $this;
  330. return $this->unescape()->as(function ($value) use ($field) {
  331. $content = is_string($value) ? json_decode($value, true) : $value;
  332. $field->wrap(false);
  333. return Dump::make($content);
  334. });
  335. }
  336. /**
  337. * @param string $val
  338. *
  339. * @return $this
  340. */
  341. public function prepend($val)
  342. {
  343. return $this->as(function ($v) use (&$val) {
  344. if (is_array($v)) {
  345. array_unshift($v, $val);
  346. return $v;
  347. } elseif ($v instanceof Collection) {
  348. return $v->prepend($val);
  349. }
  350. return $val.$v;
  351. });
  352. }
  353. /**
  354. * @param string $val
  355. *
  356. * @return $this
  357. */
  358. public function append($val)
  359. {
  360. return $this->as(function ($v) use (&$val) {
  361. if (is_array($v)) {
  362. array_push($v, $val);
  363. return $v;
  364. } elseif ($v instanceof Collection) {
  365. return $v->push($val);
  366. }
  367. return $v.$val;
  368. });
  369. }
  370. /**
  371. * Split a string by string.
  372. *
  373. * @param string $d
  374. *
  375. * @return $this
  376. */
  377. public function explode(string $d = ',')
  378. {
  379. return $this->as(function ($v) use ($d) {
  380. if (is_array($v) || $v instanceof Arrayable) {
  381. return $v;
  382. }
  383. return $v ? explode($d, $v) : [];
  384. });
  385. }
  386. /**
  387. * Render this column with the given view.
  388. *
  389. * @param string $view
  390. *
  391. * @return $this
  392. */
  393. public function view($view)
  394. {
  395. $name = $this->name;
  396. return $this->unescape()->as(function ($value) use ($view, $name) {
  397. $model = $this;
  398. return view($view, compact('model', 'value', 'name'))->render();
  399. });
  400. }
  401. /**
  402. * Set escape or not for this field.
  403. *
  404. * @param bool $escape
  405. *
  406. * @return $this
  407. */
  408. public function escape($escape = true)
  409. {
  410. $this->escape = $escape;
  411. return $this;
  412. }
  413. /**
  414. * Unescape for this field.
  415. *
  416. * @return Field
  417. */
  418. public function unescape()
  419. {
  420. return $this->escape(false);
  421. }
  422. /**
  423. * @param Fluent $model
  424. *
  425. * @return void
  426. */
  427. public function fill(Fluent $model)
  428. {
  429. $this->value($model->get($this->name));
  430. }
  431. /**
  432. * Get or set value for this field.
  433. *
  434. * @param mixed $value
  435. *
  436. * @return $this|mixed
  437. */
  438. public function value($value = null)
  439. {
  440. if ($value === null) {
  441. return $this->value;
  442. }
  443. $this->value = $value;
  444. return $this;
  445. }
  446. /**
  447. * @return $this
  448. */
  449. public function wrap(bool $wrap = true)
  450. {
  451. $this->border = $wrap;
  452. return $this;
  453. }
  454. /**
  455. * @param string $method
  456. * @param array $arguments
  457. *
  458. * @return $this
  459. */
  460. public function __call($method, $arguments = [])
  461. {
  462. if ($class = Arr::get(static::$extendedFields, $method)) {
  463. return $this->callExtendedField($class, $arguments);
  464. }
  465. if (static::hasMacro($method)) {
  466. return $this->macroCall($method, $arguments);
  467. }
  468. return $this->callSupportDisplayer($method, $arguments);
  469. }
  470. /**
  471. * Call extended field.
  472. *
  473. * @param string|AbstractField|\Closure $abstract
  474. * @param array $arguments
  475. *
  476. * @return Field
  477. */
  478. protected function callExtendedField($abstract, $arguments = [])
  479. {
  480. if ($abstract instanceof \Closure) {
  481. return $this->as($abstract, ...$arguments);
  482. }
  483. if (is_string($abstract) && class_exists($abstract)) {
  484. /** @var AbstractField $extend */
  485. $extend = new $abstract();
  486. }
  487. if ($abstract instanceof AbstractField) {
  488. /** @var AbstractField $extend */
  489. $extend = $abstract;
  490. }
  491. if (! isset($extend)) {
  492. admin_warning("[$abstract] is not a valid Show field.");
  493. return $this;
  494. }
  495. if (! $extend->escape) {
  496. $this->unescape();
  497. }
  498. $field = $this;
  499. return $this->as(function ($value) use ($extend, $field, $arguments) {
  500. if (! $extend->border) {
  501. $field->wrap(false);
  502. }
  503. $extend->setValue($value)->setModel($this);
  504. return $extend->render(...$arguments);
  505. });
  506. }
  507. /**
  508. * Call Illuminate/Support.
  509. *
  510. * @param string $abstract
  511. * @param array $arguments
  512. *
  513. * @return $this
  514. */
  515. protected function callSupportDisplayer($abstract, $arguments)
  516. {
  517. return $this->as(function ($value) use ($abstract, $arguments) {
  518. if (is_array($value) || $value instanceof Arrayable) {
  519. return call_user_func_array([collect($value), $abstract], $arguments);
  520. }
  521. if (is_string($value)) {
  522. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  523. }
  524. return $value;
  525. });
  526. }
  527. /**
  528. * Get all variables passed to field view.
  529. *
  530. * @return array
  531. */
  532. protected function variables()
  533. {
  534. return [
  535. 'content' => $this->value,
  536. 'escape' => $this->escape,
  537. 'label' => $this->getLabel(),
  538. 'wrapped' => $this->border,
  539. 'width' => $this->width,
  540. ];
  541. }
  542. /**
  543. * Render this field.
  544. *
  545. * @return string
  546. */
  547. public function render()
  548. {
  549. if (static::hasDefinition($this->name)) {
  550. $this->useDefinedColumn();
  551. }
  552. if ($this->showAs->isNotEmpty()) {
  553. $this->showAs->each(function ($callable) {
  554. [$callable, $params] = $callable;
  555. if (! $callable instanceof \Closure) {
  556. $this->value = $callable;
  557. return;
  558. }
  559. $this->value = $callable->call(
  560. $this->parent->model(),
  561. $this->value,
  562. ...$params
  563. );
  564. });
  565. }
  566. return view($this->view, $this->variables());
  567. }
  568. /**
  569. * Use a defined column.
  570. *
  571. * @throws \Exception
  572. */
  573. protected function useDefinedColumn()
  574. {
  575. $class = static::$definitions[$this->name];
  576. if (! $class instanceof \Closure) {
  577. throw new \Exception("Invalid column definition [$class]");
  578. }
  579. $this->as($class);
  580. }
  581. /**
  582. * Register custom field.
  583. *
  584. * @param string $abstract
  585. * @param string $class
  586. *
  587. * @return void
  588. */
  589. public static function extend($abstract, $class)
  590. {
  591. static::$extendedFields[$abstract] = $class;
  592. }
  593. /**
  594. * @return array
  595. */
  596. public static function extensions()
  597. {
  598. return static::$extendedFields;
  599. }
  600. }