Tools.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\Collection;
  8. class Tools implements Renderable
  9. {
  10. /**
  11. * @var Builder
  12. */
  13. protected $form;
  14. /**
  15. * Collection of tools.
  16. *
  17. * @var array
  18. */
  19. protected $tools = ['delete', 'view', 'list'];
  20. /**
  21. * Tools should be appends to default tools.
  22. *
  23. * @var Collection
  24. */
  25. protected $appends;
  26. /**
  27. * Tools should be prepends to default tools.
  28. *
  29. * @var Collection
  30. */
  31. protected $prepends;
  32. /**
  33. * @var bool
  34. */
  35. protected $showList = true;
  36. /**
  37. * @var bool
  38. */
  39. protected $showDelete = true;
  40. /**
  41. * @var bool
  42. */
  43. protected $showView = true;
  44. /**
  45. * Create a new Tools instance.
  46. *
  47. * @param Builder $builder
  48. */
  49. public function __construct(Builder $builder)
  50. {
  51. $this->form = $builder;
  52. $this->appends = new Collection();
  53. $this->prepends = new Collection();
  54. }
  55. /**
  56. * Append a tools.
  57. *
  58. * @param mixed $tool
  59. *
  60. * @return $this
  61. */
  62. public function append($tool)
  63. {
  64. $this->appends->push($tool);
  65. return $this;
  66. }
  67. /**
  68. * Prepend a tool.
  69. *
  70. * @param mixed $tool
  71. *
  72. * @return $this
  73. */
  74. public function prepend($tool)
  75. {
  76. $this->prepends->push($tool);
  77. return $this;
  78. }
  79. /**
  80. * Disable `list` tool.
  81. *
  82. * @return $this
  83. */
  84. public function disableList(bool $disable = true)
  85. {
  86. $this->showList = !$disable;
  87. return $this;
  88. }
  89. /**
  90. * Disable `delete` tool.
  91. *
  92. * @return $this
  93. */
  94. public function disableDelete(bool $disable = true)
  95. {
  96. $this->showDelete = !$disable;
  97. return $this;
  98. }
  99. /**
  100. * Disable `view` tool.
  101. *
  102. * @return $this
  103. */
  104. public function disableView(bool $disable = true)
  105. {
  106. $this->showView = !$disable;
  107. return $this;
  108. }
  109. /**
  110. * Get request path for resource list.
  111. *
  112. * @return string
  113. */
  114. protected function getListPath()
  115. {
  116. return $this->form->getResource();
  117. }
  118. /**
  119. * Get request path for edit.
  120. *
  121. * @return string
  122. */
  123. protected function getDeletePath()
  124. {
  125. return $this->getViewPath();
  126. }
  127. /**
  128. * Get request path for delete.
  129. *
  130. * @return string
  131. */
  132. protected function getViewPath()
  133. {
  134. $key = $this->form->getResourceId();
  135. if ($key) {
  136. return $this->getListPath().'/'.$key;
  137. } else {
  138. return $this->getListPath();
  139. }
  140. }
  141. /**
  142. * Get parent form of tool.
  143. *
  144. * @return Builder
  145. */
  146. public function form()
  147. {
  148. return $this->form;
  149. }
  150. /**
  151. * Render list button.
  152. *
  153. * @return string
  154. */
  155. protected function renderList()
  156. {
  157. if (!$this->showList) {
  158. return;
  159. }
  160. $text = trans('admin.list');
  161. return <<<EOT
  162. <div class="btn-group pull-right" style="margin-right: 5px">
  163. <a href="{$this->getListPath()}" class="btn btn-sm btn-default"><i class=" ti-view-list-alt"></i><span class="hidden-xs">&nbsp;$text</span></a>
  164. </div>
  165. EOT;
  166. }
  167. /**
  168. * Render list button.
  169. *
  170. * @return string
  171. */
  172. protected function renderView()
  173. {
  174. if (!$this->showView) {
  175. return;
  176. }
  177. $view = trans('admin.view');
  178. return <<<HTML
  179. <div class="btn-group pull-right" style="margin-right: 5px">
  180. <a href="{$this->getViewPath()}" class="btn btn-sm btn-primary">
  181. <i class="ti-eye"></i><span class="hidden-xs"> {$view}</span>
  182. </a>
  183. </div>
  184. HTML;
  185. }
  186. /**
  187. * Render `delete` tool.
  188. *
  189. * @return string
  190. */
  191. protected function renderDelete()
  192. {
  193. if (!$this->showDelete) {
  194. return;
  195. }
  196. $delete = trans('admin.delete');
  197. return <<<HTML
  198. <div class="btn-group pull-right" style="margin-right: 5px">
  199. <a class="btn btn-sm btn-danger " data-action="delete" data-url="{$this->getDeletePath()}" data-redirect="{$this->getListPath()}">
  200. <i class="ti-trash"></i><span class="hidden-xs"> {$delete}</span>
  201. </a>
  202. </div>
  203. HTML;
  204. }
  205. /**
  206. * Render custom tools.
  207. *
  208. * @param Collection $tools
  209. *
  210. * @return mixed
  211. */
  212. protected function renderCustomTools($tools)
  213. {
  214. if ($this->form->isCreating()) {
  215. $this->disableView();
  216. $this->disableDelete();
  217. }
  218. if (empty($tools)) {
  219. return '';
  220. }
  221. return $tools->map([Helper::class, 'render'])->implode(' ');
  222. }
  223. /**
  224. * Render tools.
  225. *
  226. * @return string
  227. */
  228. public function render()
  229. {
  230. $output = $this->renderCustomTools($this->prepends);
  231. foreach ($this->tools as $tool) {
  232. $renderMethod = 'render'.ucfirst($tool);
  233. $output .= $this->$renderMethod();
  234. }
  235. return $output.$this->renderCustomTools($this->appends);
  236. }
  237. }