| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form\NestedForm;
- class Table extends HasMany
- {
- /**
- * @var string
- */
- protected $viewMode = 'table';
- /**
- * Table constructor.
- *
- * @param string $column
- * @param array $arguments
- */
- public function __construct($column, $arguments = [])
- {
- $this->column = $column;
- if (count($arguments) == 1) {
- $this->label = $this->formatLabel();
- $this->builder = $arguments[0];
- }
- if (count($arguments) == 2) {
- list($this->label, $this->builder) = $arguments;
- }
- }
- /**
- * @return array
- */
- protected function buildRelatedForms()
- {
- if (is_null($this->form)) {
- return [];
- }
- $forms = [];
- if ($values = old($this->column)) {
- foreach ($values as $key => $data) {
- if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) {
- continue;
- }
- $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
- }
- } else {
- foreach ($this->value() as $key => $data) {
- if (isset($data['pivot'])) {
- $data = array_merge($data, $data['pivot']);
- }
- $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
- }
- }
- return $forms;
- }
- protected function prepareToSave($input)
- {
- $form = $this->buildNestedForm($this->column, $this->builder);
- $prepare = $form->prepare($input);
- return array_values(
- collect($prepare)->reject(function ($item) {
- return $item[NestedForm::REMOVE_FLAG_NAME] == 1;
- })->map(function ($item) {
- unset($item[NestedForm::REMOVE_FLAG_NAME]);
- return $item;
- })->toArray()
- );
- }
- protected function getKeyName()
- {
- if (is_null($this->form)) {
- return;
- }
- return 'id';
- }
- protected function buildNestedForm($column, \Closure $builder, $key = null)
- {
- $form = new NestedForm($column);
- $form->setForm($this->form)
- ->setKey($key);
- call_user_func($builder, $form);
- $form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS);
- return $form;
- }
- public function render()
- {
- Admin::style(
- <<<'CSS'
- .table-has-many .fields-group .form-group {
- margin-bottom:0;
- }
- CSS
- );
- return $this->renderTable();
- }
- }
|