Footer.php 3.9 KB

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