Field.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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\HasVariables;
  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. use HasVariables;
  21. use 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 = ['field' => 8, 'label' => 2];
  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|array
  120. */
  121. public function width(int $field = 8, int $label = 2)
  122. {
  123. $this->width = [
  124. 'label' => $label,
  125. 'field' => $field,
  126. ];
  127. return $this;
  128. }
  129. /**
  130. * Format label.
  131. *
  132. * @param $label
  133. *
  134. * @return mixed
  135. */
  136. protected function formatLabel($label)
  137. {
  138. if ($label) {
  139. return $label;
  140. }
  141. $label = admin_trans_field($this->name);
  142. return str_replace('_', ' ', $label);
  143. }
  144. /**
  145. * Get label of the column.
  146. *
  147. * @return mixed
  148. */
  149. public function getLabel()
  150. {
  151. return $this->label;
  152. }
  153. /**
  154. * Field display callback.
  155. *
  156. * @param mixed $callable
  157. *
  158. * @return $this
  159. */
  160. public function as($callable, ...$params)
  161. {
  162. $this->showAs->push([$callable, $params]);
  163. return $this;
  164. }
  165. /**
  166. * Display field using array value map.
  167. *
  168. * @param array $values
  169. * @param null $default
  170. *
  171. * @return $this
  172. */
  173. public function using(array $values, $default = null)
  174. {
  175. return $this->as(function ($value) use ($values, $default) {
  176. if (is_null($value)) {
  177. return $default;
  178. }
  179. return Arr::get($values, $value, $default);
  180. });
  181. }
  182. /**
  183. * Show field as a image.
  184. *
  185. * @param string $server
  186. * @param int $width
  187. * @param int $height
  188. *
  189. * @return $this
  190. */
  191. public function image($server = '', $width = 200, $height = 200)
  192. {
  193. return $this->unescape()->as(function ($path) use ($server, $width, $height) {
  194. if (empty($path)) {
  195. return '';
  196. }
  197. return collect((array) $path)->transform(function ($path) use ($server, $width, $height) {
  198. if (url()->isValidUrl($path)) {
  199. $src = $path;
  200. } elseif ($server) {
  201. $src = rtrim($server, '/').'/'.ltrim($path, '/');
  202. } else {
  203. $disk = config('admin.upload.disk');
  204. if (config("filesystems.disks.{$disk}")) {
  205. $src = Storage::disk($disk)->url($path);
  206. } else {
  207. return '';
  208. }
  209. }
  210. return "<img data-action='preview-img' src='$src' style='max-width:{$width}px;max-height:{$height}px' class='img' />";
  211. })->implode('&nbsp;');
  212. });
  213. }
  214. /**
  215. * Show field as a file.
  216. *
  217. * @param string $server
  218. * @param bool $download
  219. *
  220. * @return Field
  221. */
  222. public function file($server = '', $download = true)
  223. {
  224. $field = $this;
  225. return $this->unescape()->as(function ($path) use ($server, $field) {
  226. $name = basename($path);
  227. $field->wrap(false);
  228. $size = $url = '';
  229. if (url()->isValidUrl($path)) {
  230. $url = $path;
  231. } elseif ($server) {
  232. $url = $server.$path;
  233. } else {
  234. $storage = Storage::disk(config('admin.upload.disk'));
  235. if ($storage->exists($path)) {
  236. $url = $storage->url($path);
  237. $size = ($storage->size($path) / 1000).'KB';
  238. }
  239. }
  240. if (! $url) {
  241. return '';
  242. }
  243. $icon = Helper::getFileIcon($name);
  244. return <<<HTML
  245. <ul class="mailbox-attachments clearfix">
  246. <li style="margin-bottom: 0;">
  247. <span class="mailbox-attachment-icon"><i class="{$icon}"></i></span>
  248. <div class="mailbox-attachment-info">
  249. <div class="mailbox-attachment-name">
  250. <i class="fa fa-paperclip"></i> {$name}
  251. </div>
  252. <span class="mailbox-attachment-size">
  253. {$size}&nbsp;
  254. <a href="{$url}" class="btn btn-white btn-xs pull-right" target="_blank"><i class="fa fa-cloud-download"></i></a>
  255. </span>
  256. </div>
  257. </li>
  258. </ul>
  259. HTML;
  260. });
  261. }
  262. /**
  263. * Show field as a link.
  264. *
  265. * @param string $href
  266. * @param string $target
  267. *
  268. * @return Field
  269. */
  270. public function link($href = '', $target = '_blank')
  271. {
  272. return $this->unescape()->as(function ($link) use ($href, $target) {
  273. $href = $href ?: $link;
  274. return "<a href='$href' target='{$target}'>{$link}</a>";
  275. });
  276. }
  277. /**
  278. * Show field as labels.
  279. *
  280. * @param string $style
  281. *
  282. * @return Field
  283. */
  284. public function label($style = 'primary')
  285. {
  286. $self = $this;
  287. return $this->unescape()->as(function ($value) use ($self, $style) {
  288. [$class, $background] = $self->formatStyle($style);
  289. return collect($value)->map(function ($name) use ($class, $background) {
  290. return "<span class='label bg-{$class}' $background>$name</span>";
  291. })->implode('&nbsp;');
  292. });
  293. }
  294. /**
  295. * Add a `dot` before column text.
  296. *
  297. * @param array $options
  298. * @param string $default
  299. *
  300. * @return $this
  301. */
  302. public function dot($options = [], $default = 'default')
  303. {
  304. return $this->unescape()->prepend(function ($_, $original) use ($options, $default) {
  305. $style = is_null($original) ? $default : Arr::get((array) $options, $original, $default);
  306. $style = $style === 'default' ? 'dark70' : $style;
  307. $background = Admin::color()->get($style, $style);
  308. return "<i class='fa fa-circle' style='font-size: 13px;color: {$background}'></i>&nbsp;&nbsp;";
  309. });
  310. }
  311. /**
  312. * Show field as badges.
  313. *
  314. * @param string $style
  315. *
  316. * @return Field
  317. */
  318. public function badge($style = 'blue')
  319. {
  320. $self = $this;
  321. return $this->unescape()->as(function ($value) use ($self, $style) {
  322. [$class, $background] = $self->formatStyle($style);
  323. return collect($value)->map(function ($name) use ($class, $background) {
  324. return "<span class='badge bg-{$class}' $background>$name</span>";
  325. })->implode('&nbsp;');
  326. });
  327. }
  328. /**
  329. * @param $style
  330. *
  331. * @return array
  332. */
  333. public function formatStyle($style)
  334. {
  335. $class = 'default';
  336. $background = '';
  337. if ($style !== 'default') {
  338. $class = '';
  339. $style = Admin::color()->get($style, $style);
  340. $background = "style='background:{$style}'";
  341. }
  342. return [$class, $background];
  343. }
  344. /**
  345. * Show field as json code.
  346. *
  347. * @return Field
  348. */
  349. public function json()
  350. {
  351. $field = $this;
  352. return $this->unescape()->as(function ($value) use ($field) {
  353. $content = is_string($value) ? json_decode($value, true) : $value;
  354. $field->wrap(false);
  355. return Dump::make($content);
  356. });
  357. }
  358. /**
  359. * @param string $val
  360. *
  361. * @return $this
  362. */
  363. public function prepend($val)
  364. {
  365. $name = $this->name;
  366. return $this->as(function ($v) use (&$val, $name) {
  367. if ($val instanceof \Closure) {
  368. $val = $val->call($this, $v, Arr::get($this, $name));
  369. }
  370. if (is_array($v)) {
  371. array_unshift($v, $val);
  372. return $v;
  373. } elseif ($v instanceof Collection) {
  374. return $v->prepend($val);
  375. }
  376. return $val.$v;
  377. });
  378. }
  379. /**
  380. * @param string $val
  381. *
  382. * @return $this
  383. */
  384. public function append($val)
  385. {
  386. $name = $this->name;
  387. return $this->as(function ($v) use (&$val, $name) {
  388. if ($val instanceof \Closure) {
  389. $val = $val->call($this, $v, Arr::get($this, $name));
  390. }
  391. if (is_array($v)) {
  392. array_push($v, $val);
  393. return $v;
  394. } elseif ($v instanceof Collection) {
  395. return $v->push($val);
  396. }
  397. return $v.$val;
  398. });
  399. }
  400. /**
  401. * Split a string by string.
  402. *
  403. * @param string $d
  404. *
  405. * @return $this
  406. */
  407. public function explode(string $d = ',')
  408. {
  409. return $this->as(function ($v) use ($d) {
  410. if (is_array($v) || $v instanceof Arrayable) {
  411. return $v;
  412. }
  413. return $v ? explode($d, $v) : [];
  414. });
  415. }
  416. /**
  417. * Render this column with the given view.
  418. *
  419. * @param string $view
  420. *
  421. * @return $this
  422. */
  423. public function view($view)
  424. {
  425. $name = $this->name;
  426. return $this->unescape()->as(function ($value) use ($view, $name) {
  427. $model = $this;
  428. return view($view, compact('model', 'value', 'name'))->render();
  429. });
  430. }
  431. /**
  432. * Set escape or not for this field.
  433. *
  434. * @param bool $escape
  435. *
  436. * @return $this
  437. */
  438. public function escape($escape = true)
  439. {
  440. $this->escape = $escape;
  441. return $this;
  442. }
  443. /**
  444. * Unescape for this field.
  445. *
  446. * @return Field
  447. */
  448. public function unescape()
  449. {
  450. return $this->escape(false);
  451. }
  452. /**
  453. * @param Fluent|\Illuminate\Database\Eloquent\Model $model
  454. *
  455. * @return void
  456. */
  457. public function fill($model)
  458. {
  459. $this->value(Arr::get($model->toArray(), $this->name));
  460. }
  461. /**
  462. * Get or set value for this field.
  463. *
  464. * @param mixed $value
  465. *
  466. * @return $this|mixed
  467. */
  468. public function value($value = null)
  469. {
  470. if ($value === null) {
  471. return $this->value;
  472. }
  473. $this->value = $value;
  474. return $this;
  475. }
  476. /**
  477. * @return $this
  478. */
  479. public function wrap(bool $wrap = true)
  480. {
  481. $this->border = $wrap;
  482. return $this;
  483. }
  484. /**
  485. * @param mixed $value
  486. * @param callable $callback
  487. *
  488. * @return $this|mixed
  489. */
  490. public function when($value, $callback)
  491. {
  492. if ($value) {
  493. return $callback($this, $value) ?: $this;
  494. }
  495. return $this;
  496. }
  497. /**
  498. * @param string $method
  499. * @param array $arguments
  500. *
  501. * @return $this
  502. */
  503. public function __call($method, $arguments = [])
  504. {
  505. if ($class = Arr::get(static::$extendedFields, $method)) {
  506. return $this->callExtendedField($class, $arguments);
  507. }
  508. if (static::hasMacro($method)) {
  509. return $this->macroCall($method, $arguments);
  510. }
  511. return $this->callSupportDisplayer($method, $arguments);
  512. }
  513. /**
  514. * Call extended field.
  515. *
  516. * @param string|AbstractField|\Closure $abstract
  517. * @param array $arguments
  518. *
  519. * @return Field
  520. */
  521. protected function callExtendedField($abstract, $arguments = [])
  522. {
  523. if ($abstract instanceof \Closure) {
  524. return $this->as($abstract, ...$arguments);
  525. }
  526. if (is_string($abstract) && class_exists($abstract)) {
  527. /** @var AbstractField $extend */
  528. $extend = new $abstract();
  529. }
  530. if ($abstract instanceof AbstractField) {
  531. /** @var AbstractField $extend */
  532. $extend = $abstract;
  533. }
  534. if (! isset($extend)) {
  535. admin_warning("[$abstract] is not a valid Show field.");
  536. return $this;
  537. }
  538. if (! $extend->escape) {
  539. $this->unescape();
  540. }
  541. $field = $this;
  542. return $this->as(function ($value) use ($extend, $field, $arguments) {
  543. if (! $extend->border) {
  544. $field->wrap(false);
  545. }
  546. $extend->setValue($value)->setModel($this);
  547. return $extend->render(...$arguments);
  548. });
  549. }
  550. /**
  551. * Call Illuminate/Support.
  552. *
  553. * @param string $abstract
  554. * @param array $arguments
  555. *
  556. * @return $this
  557. */
  558. protected function callSupportDisplayer($abstract, $arguments)
  559. {
  560. return $this->as(function ($value) use ($abstract, $arguments) {
  561. if (is_array($value) || $value instanceof Arrayable) {
  562. return call_user_func_array([collect($value), $abstract], $arguments);
  563. }
  564. if (is_string($value)) {
  565. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  566. }
  567. return $value;
  568. });
  569. }
  570. /**
  571. * Get all variables passed to field view.
  572. *
  573. * @return array
  574. */
  575. protected function defaultVariables()
  576. {
  577. return [
  578. 'content' => $this->value,
  579. 'escape' => $this->escape,
  580. 'label' => $this->getLabel(),
  581. 'wrapped' => $this->border,
  582. 'width' => $this->width,
  583. ];
  584. }
  585. /**
  586. * Render this field.
  587. *
  588. * @return string
  589. */
  590. public function render()
  591. {
  592. if ($this->showAs->isNotEmpty()) {
  593. $this->showAs->each(function ($callable) {
  594. [$callable, $params] = $callable;
  595. if (! $callable instanceof \Closure) {
  596. $this->value = $callable;
  597. return;
  598. }
  599. $this->value = $callable->call(
  600. $this->parent->model(),
  601. $this->value,
  602. ...$params
  603. );
  604. });
  605. }
  606. return view($this->view, $this->variables());
  607. }
  608. /**
  609. * Register custom field.
  610. *
  611. * @param string $abstract
  612. * @param string $class
  613. *
  614. * @return void
  615. */
  616. public static function extend($abstract, $class)
  617. {
  618. static::$extendedFields[$abstract] = $class;
  619. }
  620. /**
  621. * @return array
  622. */
  623. public static function extensions()
  624. {
  625. return static::$extendedFields;
  626. }
  627. }