BlockForm.php 2.9 KB

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