Footer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * @return $this
  57. */
  58. public function disableReset(bool $disable = true)
  59. {
  60. $this->buttons['reset'] = ! $disable;
  61. return $this;
  62. }
  63. /**
  64. * Disable submit button.
  65. *
  66. * @param bool $disable
  67. * @return $this
  68. */
  69. public function disableSubmit(bool $disable = true)
  70. {
  71. $this->buttons['submit'] = ! $disable;
  72. return $this;
  73. }
  74. /**
  75. * Disable View Checkbox.
  76. *
  77. * @param bool $disable
  78. * @return $this
  79. */
  80. public function disableViewCheck(bool $disable = true)
  81. {
  82. $this->checkboxes['view'] = ! $disable;
  83. return $this;
  84. }
  85. /**
  86. * Disable Editing Checkbox.
  87. *
  88. * @param bool $disable
  89. * @return $this
  90. */
  91. public function disableEditingCheck(bool $disable = true)
  92. {
  93. $this->checkboxes['continue_editing'] = ! $disable;
  94. return $this;
  95. }
  96. /**
  97. * Disable Creating Checkbox.
  98. *
  99. * @param bool $disable
  100. * @return $this
  101. */
  102. public function disableCreatingCheck(bool $disable = true)
  103. {
  104. $this->checkboxes['continue_creating'] = ! $disable;
  105. return $this;
  106. }
  107. /**
  108. * default View Checked.
  109. *
  110. * @param bool $checked
  111. * @return $this
  112. */
  113. public function defaultViewChecked(bool $checked = true)
  114. {
  115. $this->defaultcheckeds['view'] = $checked;
  116. return $this;
  117. }
  118. /**
  119. * default Editing Checked.
  120. *
  121. * @param bool $checked
  122. * @return $this
  123. */
  124. public function defaultEditingChecked(bool $checked = true)
  125. {
  126. $this->defaultcheckeds['continue_editing'] = $checked;
  127. return $this;
  128. }
  129. /**
  130. * default Creating Checked.
  131. *
  132. * @param bool $checked
  133. * @return $this
  134. */
  135. public function defaultCreatingChecked(bool $checked = true)
  136. {
  137. $this->defaultcheckeds['continue_creating'] = $checked;
  138. return $this;
  139. }
  140. /**
  141. * Build checkboxes.
  142. *
  143. * @return Checkbox|null
  144. */
  145. protected function buildCheckboxes()
  146. {
  147. if ($this->builder->isEditing()) {
  148. $this->disableCreatingCheck();
  149. }
  150. $options = [];
  151. $checked = [];
  152. if ($this->checkboxes['continue_editing']) {
  153. $options[1] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_editing'));
  154. }
  155. if ($this->checkboxes['continue_creating']) {
  156. $options[2] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_creating'));
  157. }
  158. if ($this->checkboxes['view']) {
  159. $options[3] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.view'));
  160. }
  161. if ($this->defaultcheckeds['continue_editing']) {
  162. $checked[] = 1;
  163. }
  164. if ($this->defaultcheckeds['continue_creating']) {
  165. $checked[] = 2;
  166. }
  167. if ($this->defaultcheckeds['view']) {
  168. $checked[] = 3;
  169. }
  170. if (! $options) {
  171. return;
  172. }
  173. return (new Checkbox('after-save', $options))->check($checked)->inline()->circle(true);
  174. }
  175. /**
  176. * Use custom view.
  177. *
  178. * @param string $view
  179. * @param array $data
  180. */
  181. public function view(string $view, array $data = [])
  182. {
  183. $this->view = $view;
  184. $this->data = $data;
  185. }
  186. /**
  187. * Render footer.
  188. *
  189. * @return string
  190. */
  191. public function render()
  192. {
  193. $data = [
  194. 'buttons' => $this->buttons,
  195. 'checkboxes' => $this->buildCheckboxes(),
  196. 'width' => $this->builder->getWidth(),
  197. ];
  198. $data = array_merge($data, $this->data);
  199. return view($this->view, $data)->render();
  200. }
  201. }