Footer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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' => true, 'submit' => true];
  25. /**
  26. * Available checkboxes.
  27. *
  28. * @var array
  29. */
  30. protected $checkboxes = ['view' => true, 'continue_editing' => true, 'continue_creating' => true];
  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. $this->buttons['reset'] = ! $disable;
  50. return $this;
  51. }
  52. /**
  53. * Disable submit button.
  54. *
  55. * @param bool $disable
  56. *
  57. * @return $this
  58. */
  59. public function disableSubmit(bool $disable = true)
  60. {
  61. $this->buttons['submit'] = ! $disable;
  62. return $this;
  63. }
  64. /**
  65. * Disable View Checkbox.
  66. *
  67. * @param bool $disable
  68. *
  69. * @return $this
  70. */
  71. public function disableViewCheck(bool $disable = true)
  72. {
  73. $this->checkboxes['view'] = ! $disable;
  74. return $this;
  75. }
  76. /**
  77. * Disable Editing Checkbox.
  78. *
  79. * @param bool $disable
  80. *
  81. * @return $this
  82. */
  83. public function disableEditingCheck(bool $disable = true)
  84. {
  85. $this->checkboxes['continue_editing'] = ! $disable;
  86. return $this;
  87. }
  88. /**
  89. * Disable Creating Checkbox.
  90. *
  91. * @param bool $disable
  92. *
  93. * @return $this
  94. */
  95. public function disableCreatingCheck(bool $disable = true)
  96. {
  97. $this->checkboxes['continue_creating'] = ! $disable;
  98. return $this;
  99. }
  100. /**
  101. * Build checkboxes.
  102. *
  103. * @return Checkbox|null
  104. */
  105. protected function buildCheckboxes()
  106. {
  107. if ($this->builder->isEditing()) {
  108. $this->disableCreatingCheck();
  109. }
  110. $options = [];
  111. if ($this->checkboxes['continue_editing']) {
  112. $options[1] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_editing'));
  113. }
  114. if ($this->checkboxes['continue_creating']) {
  115. $options[2] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_creating'));
  116. }
  117. if ($this->checkboxes['view']) {
  118. $options[3] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.view'));
  119. }
  120. if (! $options) {
  121. return;
  122. }
  123. return (new Checkbox('after-save', $options))->inline()->circle(true);
  124. }
  125. /**
  126. * Render footer.
  127. *
  128. * @return string
  129. */
  130. public function render()
  131. {
  132. $data = [
  133. 'buttons' => $this->buttons,
  134. 'checkboxes' => $this->buildCheckboxes(),
  135. 'width' => $this->builder->getWidth(),
  136. ];
  137. return view($this->view, $data)->render();
  138. }
  139. }