BlockForm.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Widgets\Form as WidgetForm;
  6. class BlockForm extends WidgetForm
  7. {
  8. /**
  9. * @var Form
  10. */
  11. protected $form;
  12. /**
  13. * @var Builder
  14. */
  15. protected $builder;
  16. /**
  17. * @var string
  18. */
  19. protected $title;
  20. public function __construct(Form $form)
  21. {
  22. $this->form = $form;
  23. $this->builder = $form->builder();
  24. $this->initFields();
  25. $this->initFormAttributes();
  26. }
  27. public function title($title)
  28. {
  29. $this->title = $title;
  30. return $this;
  31. }
  32. /**
  33. * Add a form field to form.
  34. *
  35. * @param Field $field
  36. *
  37. * @return $this
  38. */
  39. public function pushField(Field &$field)
  40. {
  41. $this->form->builder()->fields()->push($field);
  42. $this->fields->push($field);
  43. $field->attribute(Builder::BUILD_IGNORE, true);
  44. $field->setForm($this->form);
  45. $field->width($this->width['field'], $this->width['label']);
  46. $field::collectAssets();
  47. return $this;
  48. }
  49. public function render()
  50. {
  51. $view = Helper::render(parent::render());
  52. $style = $this->title ? '' : 'padding-top: 13px';
  53. return <<<HTML
  54. <div class='box' style="{$style}">
  55. {$this->renderHeader()}
  56. {$view}
  57. </div>
  58. HTML;
  59. }
  60. protected function renderHeader()
  61. {
  62. if (! $this->title) {
  63. return;
  64. }
  65. return <<<HTML
  66. <div class="box-header with-border mb-1">
  67. <h3 class="box-title">{$this->title}</h3>
  68. </div>
  69. HTML;
  70. }
  71. }