BlockForm.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Exception\RuntimeException;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Widgets\Form as WidgetForm;
  7. /**
  8. * Class BlockForm
  9. *
  10. * @package Dcat\Admin\Form
  11. *
  12. * @mixin Form
  13. */
  14. class BlockForm extends WidgetForm
  15. {
  16. /**
  17. * @var Form
  18. */
  19. protected $form;
  20. /**
  21. * @var Builder
  22. */
  23. protected $builder;
  24. /**
  25. * @var string
  26. */
  27. protected $title;
  28. /**
  29. * @var \Dcat\Admin\Layout\Row
  30. */
  31. public $layoutRow;
  32. public function __construct(Form $form)
  33. {
  34. $this->form = $form;
  35. $this->builder = $form->builder();
  36. $this->initFields();
  37. $this->initFormAttributes();
  38. }
  39. /**
  40. * 设置标题.
  41. *
  42. * @param string $title
  43. *
  44. * @return $this
  45. */
  46. public function title($title)
  47. {
  48. $this->title = $title;
  49. return $this;
  50. }
  51. /**
  52. * 显示底部内容.
  53. *
  54. * @return $this
  55. */
  56. public function showFooter()
  57. {
  58. $this->ajax(true);
  59. $this->disableSubmitButton(false);
  60. $this->disableResetButton(false);
  61. return $this;
  62. }
  63. /**
  64. * 在当前列增加一块表单.
  65. *
  66. * @param \Closure $callback
  67. *
  68. * @return $this
  69. */
  70. public function next(\Closure $callback)
  71. {
  72. $this->layoutRow->column(
  73. 12,
  74. $form = $this->form
  75. ->builder()
  76. ->layout()
  77. ->form()
  78. );
  79. $callback($form);
  80. return $this;
  81. }
  82. public function pushField(Field $field)
  83. {
  84. $this->form->builder()->fields()->push($field);
  85. $this->fields->push($field);
  86. if ($this->layout()->hasColumns()) {
  87. $this->layout()->addField($field);
  88. }
  89. $field->attribute(Builder::BUILD_IGNORE, true);
  90. $field->setForm($this->form);
  91. $field->width($this->width['field'], $this->width['label']);
  92. $field::requireAssets();
  93. return $this;
  94. }
  95. public function render()
  96. {
  97. $class = $this->title ? '' : 'pt-1';
  98. $view = parent::render();
  99. return <<<HTML
  100. <div class='box {$class} mb-1'>
  101. {$this->renderHeader()} {$view}
  102. </div>
  103. HTML;
  104. }
  105. protected function renderHeader()
  106. {
  107. if (! $this->title) {
  108. return;
  109. }
  110. return <<<HTML
  111. <div class="box-header with-border" style="margin-bottom: .5rem">
  112. <h3 class="box-title">{$this->title}</h3>
  113. </div>
  114. HTML;
  115. }
  116. public function getKey()
  117. {
  118. return $this->form->getKey();
  119. }
  120. public function __call($method, $arguments)
  121. {
  122. try {
  123. return parent::__call($method, $arguments);
  124. } catch (RuntimeException $e) {
  125. return $this->form->$method($arguments);
  126. }
  127. }
  128. public function fillFields(array $data)
  129. {
  130. }
  131. }