BlockForm.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * @return $this
  42. */
  43. public function title($title)
  44. {
  45. $this->title = $title;
  46. return $this;
  47. }
  48. /**
  49. * 显示底部内容.
  50. *
  51. * @return $this
  52. */
  53. public function showFooter()
  54. {
  55. $this->ajax(true);
  56. $this->submitButton(true);
  57. $this->resetButton(true);
  58. return $this;
  59. }
  60. /**
  61. * 在当前列增加一块表单.
  62. *
  63. * @param \Closure $callback
  64. * @return $this
  65. */
  66. public function next(\Closure $callback)
  67. {
  68. $this->layoutRow->column(
  69. 12,
  70. $form = $this->form
  71. ->builder()
  72. ->layout()
  73. ->form()
  74. );
  75. $callback($form);
  76. return $this;
  77. }
  78. public function pushField(Field $field)
  79. {
  80. $field->attribute(Field::BUILD_IGNORE, true);
  81. $this->form->builder()->pushField($field);
  82. $this->fields->push($field);
  83. if ($this->layout()->hasColumns()) {
  84. $this->layout()->addField($field);
  85. }
  86. $field->setForm($this->form);
  87. $field->setParent($this);
  88. $field->width($this->width['field'], $this->width['label']);
  89. $field::requireAssets();
  90. return $this;
  91. }
  92. public function render()
  93. {
  94. $class = $this->title ? '' : 'pt-1';
  95. $view = parent::render();
  96. return <<<HTML
  97. <div class='box {$class} mb-1'>
  98. {$this->renderHeader()} {$view}
  99. </div>
  100. HTML;
  101. }
  102. protected function renderHeader()
  103. {
  104. if (! $this->title) {
  105. return;
  106. }
  107. return <<<HTML
  108. <div class="box-header with-border" style="margin-bottom: .5rem">
  109. <h3 class="box-title">{$this->title}</h3>
  110. </div>
  111. HTML;
  112. }
  113. public function getKey()
  114. {
  115. return $this->form->getKey();
  116. }
  117. public function model()
  118. {
  119. return $this->form->model();
  120. }
  121. public function __call($method, $arguments)
  122. {
  123. try {
  124. return parent::__call($method, $arguments);
  125. } catch (RuntimeException $e) {
  126. return $this->form->$method($arguments);
  127. }
  128. }
  129. public function fillFields(array $data)
  130. {
  131. }
  132. }