Tools.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\Collection;
  7. class Tools implements Renderable
  8. {
  9. /**
  10. * @var Builder
  11. */
  12. protected $form;
  13. /**
  14. * Collection of tools.
  15. *
  16. * @var array
  17. */
  18. protected $tools = ['delete' => true, 'view' => true, 'list' => true];
  19. /**
  20. * Tools should be appends to default tools.
  21. *
  22. * @var Collection
  23. */
  24. protected $appends;
  25. /**
  26. * Tools should be prepends to default tools.
  27. *
  28. * @var Collection
  29. */
  30. protected $prepends;
  31. /**
  32. * Create a new Tools instance.
  33. *
  34. * @param Builder $builder
  35. */
  36. public function __construct(Builder $builder)
  37. {
  38. $this->form = $builder;
  39. $this->appends = new Collection();
  40. $this->prepends = new Collection();
  41. }
  42. /**
  43. * Append a tools.
  44. *
  45. * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
  46. *
  47. * @return $this
  48. */
  49. public function append($tool)
  50. {
  51. $this->prepareTool($tool);
  52. $this->appends->push($tool);
  53. return $this;
  54. }
  55. /**
  56. * Prepend a tool.
  57. *
  58. * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
  59. *
  60. * @return $this
  61. */
  62. public function prepend($tool)
  63. {
  64. $this->prepareTool($tool);
  65. $this->prepends->push($tool);
  66. return $this;
  67. }
  68. /**
  69. * @param mixed $tool
  70. *
  71. * @return void
  72. */
  73. protected function prepareTool($tool)
  74. {
  75. if ($tool instanceof AbstractTool) {
  76. $tool->setForm($this->form->form());
  77. }
  78. }
  79. /**
  80. * Disable `list` tool.
  81. *
  82. * @return $this
  83. */
  84. public function disableList(bool $disable = true)
  85. {
  86. $this->tools['list'] = ! $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->tools['delete'] = ! $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->tools['view'] = ! $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. if ($key = $this->form->getResourceId()) {
  135. return $this->getListPath().'/'.$key;
  136. }
  137. return $this->getListPath();
  138. }
  139. /**
  140. * Get parent form of tool.
  141. *
  142. * @return Builder
  143. */
  144. public function form()
  145. {
  146. return $this->form;
  147. }
  148. /**
  149. * Render list button.
  150. *
  151. * @return string
  152. */
  153. protected function renderList()
  154. {
  155. $text = trans('admin.list');
  156. return <<<EOT
  157. <div class="btn-group pull-right" style="margin-right: 5px">
  158. <a href="{$this->getListPath()}" class="btn btn-sm btn-white "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;$text</span></a>
  159. </div>
  160. EOT;
  161. }
  162. /**
  163. * Render list button.
  164. *
  165. * @return string
  166. */
  167. protected function renderView()
  168. {
  169. $view = trans('admin.view');
  170. return <<<HTML
  171. <div class="btn-group pull-right" style="margin-right: 5px">
  172. <a href="{$this->getViewPath()}" class="btn btn-sm btn-primary">
  173. <i class="feather icon-eye"></i><span class="d-none d-sm-inline"> {$view}</span>
  174. </a>
  175. </div>
  176. HTML;
  177. }
  178. /**
  179. * Render `delete` tool.
  180. *
  181. * @return string
  182. */
  183. protected function renderDelete()
  184. {
  185. $delete = trans('admin.delete');
  186. return <<<HTML
  187. <div class="btn-group pull-right" style="margin-right: 5px">
  188. <a class="btn btn-sm btn-danger text-white" data-action="delete" data-url="{$this->getDeletePath()}" data-redirect="{$this->getListPath()}">
  189. <i class="feather icon-trash"></i><span class="d-none d-sm-inline"> {$delete}</span>
  190. </a>
  191. </div>
  192. HTML;
  193. }
  194. /**
  195. * Render custom tools.
  196. *
  197. * @param Collection $tools
  198. *
  199. * @return mixed
  200. */
  201. protected function renderCustomTools($tools)
  202. {
  203. if ($this->form->isCreating()) {
  204. $this->disableView();
  205. $this->disableDelete();
  206. }
  207. if (empty($tools)) {
  208. return '';
  209. }
  210. return $tools->map([Helper::class, 'render'])->implode(' ');
  211. }
  212. /**
  213. * Render tools.
  214. *
  215. * @return string
  216. */
  217. public function render()
  218. {
  219. $output = $this->renderCustomTools($this->prepends);
  220. foreach ($this->tools as $tool => $enable) {
  221. if ($enable) {
  222. $renderMethod = 'render'.ucfirst($tool);
  223. $output .= $this->$renderMethod();
  224. }
  225. }
  226. return $output.$this->renderCustomTools($this->appends);
  227. }
  228. }