Footer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Widgets\Checkbox;
  4. use Illuminate\Contracts\Support\Renderable;
  5. class Footer implements Renderable
  6. {
  7. /**
  8. * Footer view.
  9. *
  10. * @var string
  11. */
  12. protected $view = 'admin::form.footer';
  13. /**
  14. * Footer view data.
  15. *
  16. * @var array
  17. */
  18. protected $data = [];
  19. /**
  20. * Form builder instance.
  21. *
  22. * @var Builder
  23. */
  24. protected $builder;
  25. /**
  26. * Available buttons.
  27. *
  28. * @var array
  29. */
  30. protected $buttons = ['reset' => true, 'submit' => true];
  31. /**
  32. * Available checkboxes.
  33. *
  34. * @var array
  35. */
  36. protected $checkboxes = ['view' => true, 'continue_editing' => true, 'continue_creating' => true];
  37. /**
  38. * Default checked.
  39. *
  40. * @var arrays
  41. */
  42. protected $defaultcheckeds = ['view' => false, 'continue_editing' => false, 'continue_creating' => false];
  43. /**
  44. * Footer constructor.
  45. *
  46. * @param Builder $builder
  47. */
  48. public function __construct(Builder $builder)
  49. {
  50. $this->builder = $builder;
  51. }
  52. /**
  53. * Disable reset button.
  54. *
  55. * @param bool $disable
  56. *
  57. * @return $this
  58. */
  59. public function disableReset(bool $disable = true)
  60. {
  61. $this->buttons['reset'] = ! $disable;
  62. return $this;
  63. }
  64. /**
  65. * Disable submit button.
  66. *
  67. * @param bool $disable
  68. *
  69. * @return $this
  70. */
  71. public function disableSubmit(bool $disable = true)
  72. {
  73. $this->buttons['submit'] = ! $disable;
  74. return $this;
  75. }
  76. /**
  77. * Disable View Checkbox.
  78. *
  79. * @param bool $disable
  80. *
  81. * @return $this
  82. */
  83. public function disableViewCheck(bool $disable = true)
  84. {
  85. $this->checkboxes['view'] = ! $disable;
  86. return $this;
  87. }
  88. /**
  89. * Disable Editing Checkbox.
  90. *
  91. * @param bool $disable
  92. *
  93. * @return $this
  94. */
  95. public function disableEditingCheck(bool $disable = true)
  96. {
  97. $this->checkboxes['continue_editing'] = ! $disable;
  98. return $this;
  99. }
  100. /**
  101. * Disable Creating Checkbox.
  102. *
  103. * @param bool $disable
  104. *
  105. * @return $this
  106. */
  107. public function disableCreatingCheck(bool $disable = true)
  108. {
  109. $this->checkboxes['continue_creating'] = ! $disable;
  110. return $this;
  111. }
  112. /**
  113. * default View Checked.
  114. *
  115. * @param bool $checked
  116. *
  117. * @return $this
  118. */
  119. public function defaultViewChecked(bool $checked = true)
  120. {
  121. $this->defaultcheckeds['view'] = $checked;
  122. return $this;
  123. }
  124. /**
  125. * default Editing Checked.
  126. *
  127. * @param bool $checked
  128. *
  129. * @return $this
  130. */
  131. public function defaultEditingChecked(bool $checked = true)
  132. {
  133. $this->defaultcheckeds['continue_editing'] = $checked;
  134. return $this;
  135. }
  136. /**
  137. * default Creating Checked.
  138. *
  139. * @param bool $checked
  140. *
  141. * @return $this
  142. */
  143. public function defaultCreatingChecked(bool $checked = true)
  144. {
  145. $this->defaultcheckeds['continue_creating'] = $checked;
  146. return $this;
  147. }
  148. /**
  149. * Build checkboxes.
  150. *
  151. * @return Checkbox|null
  152. */
  153. protected function buildCheckboxes()
  154. {
  155. if ($this->builder->isEditing()) {
  156. $this->disableCreatingCheck();
  157. }
  158. $options = [];
  159. $checked = [];
  160. if ($this->checkboxes['continue_editing']) {
  161. $options[1] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_editing'));
  162. }
  163. if ($this->checkboxes['continue_creating']) {
  164. $options[2] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_creating'));
  165. }
  166. if ($this->checkboxes['view']) {
  167. $options[3] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.view'));
  168. }
  169. if ($this->defaultcheckeds['continue_editing']) {
  170. $checked[] = 1;
  171. }
  172. if ($this->defaultcheckeds['continue_creating']) {
  173. $checked[] = 2;
  174. }
  175. if ($this->defaultcheckeds['view']) {
  176. $checked[] = 3;
  177. }
  178. if (! $options) {
  179. return;
  180. }
  181. return (new Checkbox('after-save', $options))->check($checked)->inline()->circle(true);
  182. }
  183. /**
  184. * Use custom view.
  185. *
  186. * @param string $view
  187. * @param array $data
  188. */
  189. public function view(string $view, array $data = [])
  190. {
  191. $this->view = $view;
  192. $this->data = $data;
  193. }
  194. /**
  195. * Render footer.
  196. *
  197. * @return string
  198. */
  199. public function render()
  200. {
  201. $data = [
  202. 'buttons' => $this->buttons,
  203. 'checkboxes' => $this->buildCheckboxes(),
  204. 'width' => $this->builder->getWidth(),
  205. ];
  206. $data = array_merge($data, $this->data);
  207. return view($this->view, $data)->render();
  208. }
  209. }