BlockForm.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $this->form->builder()->fields()->push($field);
  83. $this->fields->push($field);
  84. if ($this->layout()->hasColumns()) {
  85. $this->layout()->addField($field);
  86. }
  87. $field->attribute(Builder::BUILD_IGNORE, true);
  88. $field->setForm($this->form);
  89. $field->width($this->width['field'], $this->width['label']);
  90. $field::requireAssets();
  91. return $this;
  92. }
  93. public function render()
  94. {
  95. $class = $this->title ? '' : 'pt-1';
  96. $view = parent::render();
  97. return <<<HTML
  98. <div class='box {$class} mb-1'>
  99. {$this->renderHeader()} {$view}
  100. </div>
  101. HTML;
  102. }
  103. protected function renderHeader()
  104. {
  105. if (! $this->title) {
  106. return;
  107. }
  108. return <<<HTML
  109. <div class="box-header with-border" style="margin-bottom: .5rem">
  110. <h3 class="box-title">{$this->title}</h3>
  111. </div>
  112. HTML;
  113. }
  114. public function getKey()
  115. {
  116. return $this->form->getKey();
  117. }
  118. public function model()
  119. {
  120. return $this->form->model();
  121. }
  122. public function __call($method, $arguments)
  123. {
  124. try {
  125. return parent::__call($method, $arguments);
  126. } catch (RuntimeException $e) {
  127. return $this->form->$method($arguments);
  128. }
  129. }
  130. public function fillFields(array $data)
  131. {
  132. }
  133. }