Footer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Form builder instance.
  15. *
  16. * @var Builder
  17. */
  18. protected $builder;
  19. /**
  20. * Available buttons.
  21. *
  22. * @var array
  23. */
  24. protected $buttons = ['reset', 'submit'];
  25. /**
  26. * Available checkboxes.
  27. *
  28. * @var array
  29. */
  30. protected $checkboxes = ['view', 'continue_editing', 'continue_creating'];
  31. /**
  32. * Footer constructor.
  33. *
  34. * @param Builder $builder
  35. */
  36. public function __construct(Builder $builder)
  37. {
  38. $this->builder = $builder;
  39. }
  40. /**
  41. * Disable reset button.
  42. *
  43. * @param bool $disable
  44. *
  45. * @return $this
  46. */
  47. public function disableReset(bool $disable = true)
  48. {
  49. if ($disable) {
  50. array_delete($this->buttons, 'reset');
  51. } elseif (! in_array('reset', $this->buttons)) {
  52. array_push($this->buttons, 'reset');
  53. }
  54. return $this;
  55. }
  56. /**
  57. * Disable submit button.
  58. *
  59. * @param bool $disable
  60. *
  61. * @return $this
  62. */
  63. public function disableSubmit(bool $disable = true)
  64. {
  65. if ($disable) {
  66. array_delete($this->buttons, 'submit');
  67. } elseif (! in_array('submit', $this->buttons)) {
  68. array_push($this->buttons, 'submit');
  69. }
  70. return $this;
  71. }
  72. /**
  73. * Disable View Checkbox.
  74. *
  75. * @param bool $disable
  76. *
  77. * @return $this
  78. */
  79. public function disableViewCheck(bool $disable = true)
  80. {
  81. if ($disable) {
  82. array_delete($this->checkboxes, 'view');
  83. } elseif (! in_array('view', $this->checkboxes)) {
  84. array_push($this->checkboxes, 'view');
  85. }
  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. if ($disable) {
  98. array_delete($this->checkboxes, 'continue_editing');
  99. } elseif (! in_array('continue_editing', $this->checkboxes)) {
  100. array_push($this->checkboxes, 'continue_editing');
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Disable Creating Checkbox.
  106. *
  107. * @param bool $disable
  108. *
  109. * @return $this
  110. */
  111. public function disableCreatingCheck(bool $disable = true)
  112. {
  113. if ($disable) {
  114. array_delete($this->checkboxes, 'continue_creating');
  115. } elseif (! in_array('continue_creating', $this->checkboxes)) {
  116. array_push($this->checkboxes, 'continue_creating');
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Build checkboxes.
  122. *
  123. * @return Checkbox|null
  124. */
  125. protected function buildCheckboxes()
  126. {
  127. if ($this->builder->isEditing()) {
  128. $this->disableCreatingCheck();
  129. }
  130. $options = [];
  131. if (in_array('continue_editing', $this->checkboxes)) {
  132. $options[1] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_editing'));
  133. }
  134. if (in_array('continue_creating', $this->checkboxes)) {
  135. $options[2] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_creating'));
  136. }
  137. if (in_array('view', $this->checkboxes)) {
  138. $options[3] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.view'));
  139. }
  140. if (! $options) {
  141. return;
  142. }
  143. return (new Checkbox('after-save', $options))->inline()->circle(true);
  144. }
  145. /**
  146. * Render footer.
  147. *
  148. * @return string
  149. */
  150. public function render()
  151. {
  152. $data = [
  153. 'buttons' => $this->buttons,
  154. 'checkboxes' => $this->buildCheckboxes(),
  155. 'width' => $this->builder->getWidth(),
  156. ];
  157. return view($this->view, $data)->render();
  158. }
  159. }