123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace Dcat\Admin\Form;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- /**
- * Class EmbeddedForm.
- *
- * @method Field\Text text($column, $label = '')
- * @method Field\Checkbox checkbox($column, $label = '')
- * @method Field\Radio radio($column, $label = '')
- * @method Field\Select select($column, $label = '')
- * @method Field\MultipleSelect multipleSelect($column, $label = '')
- * @method Field\Textarea textarea($column, $label = '')
- * @method Field\Hidden hidden($column, $label = '')
- * @method Field\Id id($column, $label = '')
- * @method Field\Ip ip($column, $label = '')
- * @method Field\Url url($column, $label = '')
- * @method Field\Color color($column, $label = '')
- * @method Field\Email email($column, $label = '')
- * @method Field\Mobile mobile($column, $label = '')
- * @method Field\Slider slider($column, $label = '')
- * @method Field\Map map($latitude, $longitude, $label = '')
- * @method Field\Editor editor($column, $label = '')
- * @method Field\Date date($column, $label = '')
- * @method Field\Datetime datetime($column, $label = '')
- * @method Field\Time time($column, $label = '')
- * @method Field\Year year($column, $label = '')
- * @method Field\Month month($column, $label = '')
- * @method Field\DateRange dateRange($start, $end, $label = '')
- * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
- * @method Field\TimeRange timeRange($start, $end, $label = '')
- * @method Field\Number number($column, $label = '')
- * @method Field\Currency currency($column, $label = '')
- * @method Field\HasMany hasMany($relationName, $callback)
- * @method Field\SwitchField switch($column, $label = '')
- * @method Field\Display display($column, $label = '')
- * @method Field\Rate rate($column, $label = '')
- * @method Field\Divide divider()
- * @method Field\Password password($column, $label = '')
- * @method Field\Decimal decimal($column, $label = '')
- * @method Field\Html html($html, $label = '')
- * @method Field\Tags tags($column, $label = '')
- * @method Field\Icon icon($column, $label = '')
- * @method Field\Embeds embeds($column, $label = '')
- * @method Field\Captcha captcha($column, $label = '')
- * @method Field\Listbox listbox($column, $label = '')
- * @method Field\SelectResource selectResource($column, $label = '')
- * @method Field\File file($column, $label = '')
- * @method Field\Image image($column, $label = '')
- * @method Field\MultipleFile multipleFile($column, $label = '')
- * @method Field\MultipleImage multipleImage($column, $label = '')
- * @method Field\Tree tree($column, $label = '')
- *
- * @method Field\BootstrapFile bootstrapFile($column, $label = '')
- * @method Field\BootstrapImage bootstrapImage($column, $label = '')
- * @method Field\BootstrapMultipleImage bootstrapMultipleImage($column, $label = '')
- * @method Field\BootstrapMultipleFile bootstrapMultipleFile($column, $label = '')
- */
- class EmbeddedForm
- {
- /**
- * @var Form
- */
- protected $parent = null;
- /**
- * Fields in form.
- *
- * @var Collection
- */
- protected $fields;
- /**
- * Original data for this field.
- *
- * @var array
- */
- protected $original = [];
- /**
- * Column name for this form.
- *
- * @var string
- */
- protected $column;
- /**
- * EmbeddedForm constructor.
- *
- * @param string $column
- */
- public function __construct($column)
- {
- $this->column = $column;
- $this->fields = new Collection();
- }
- /**
- * Get all fields in current form.
- *
- * @return Collection
- */
- public function fields()
- {
- return $this->fields;
- }
- /**
- * Set parent form for this form.
- *
- * @param Form $parent
- *
- * @return $this
- */
- public function setParent(Form $parent)
- {
- $this->parent = $parent;
- return $this;
- }
- /**
- * Set original values for fields.
- *
- * @param array $data
- *
- * @return $this
- */
- public function setOriginal($data)
- {
- if (empty($data)) {
- return $this;
- }
- if (is_string($data)) {
- $data = json_decode($data, true);
- }
- $this->original = $data;
- return $this;
- }
- /**
- * Prepare for insert or update.
- *
- * @param array $input
- *
- * @return mixed
- */
- public function prepare($input)
- {
- foreach ($input as $key => $record) {
- $this->setFieldOriginalValue($key);
- $input[$key] = $this->prepareValue($key, $record);
- }
- return $input;
- }
- /**
- * Do prepare work for each field.
- *
- * @param string $key
- * @param string $record
- *
- * @return mixed
- */
- protected function prepareValue($key, $record)
- {
- $field = $this->fields->first(function (Field $field) use ($key) {
- return in_array($key, (array) $field->column());
- });
- if (method_exists($field, 'prepareInputValue')) {
- return $field->prepareInputValue($record);
- }
- return $record;
- }
- /**
- * Set original data for each field.
- *
- * @param string $key
- *
- * @return void
- */
- protected function setFieldOriginalValue($key)
- {
- if (array_key_exists($key, $this->original)) {
- $values = $this->original[$key];
- $this->fields->each(function (Field $field) use ($values) {
- $field->setOriginal($values);
- });
- }
- }
- /**
- * Fill data to all fields in form.
- *
- * @param array $data
- *
- * @return $this
- */
- public function fill(array $data)
- {
- $this->fields->each(function (Field $field) use ($data) {
- $field->fill($data);
- });
- return $this;
- }
- /**
- * Format form, set `element name` `error key` and `element class`.
- *
- * @param Field $field
- *
- * @return Field
- */
- protected function formatField(Field $field)
- {
- $jsonKey = $field->column();
- $elementName = $elementClass = $errorKey = [];
- if (is_array($jsonKey)) {
- foreach ($jsonKey as $index => $name) {
- $elementName[$index] = "{$this->column}[$name]";
- $errorKey[$index] = "{$this->column}.$name";
- $elementClass[$index] = "{$this->column}_$name";
- }
- } else {
- $elementName = "{$this->column}[$jsonKey]";
- $errorKey = "{$this->column}.$jsonKey";
- $elementClass = "{$this->column}_$jsonKey";
- }
- $field->setElementName($elementName)
- ->setErrorKey($errorKey)
- ->setElementClass($elementClass);
- return $field;
- }
- /**
- * Add a field to form.
- *
- * @param Field $field
- *
- * @return $this
- */
- public function pushField(Field $field)
- {
- $field = $this->formatField($field);
- $this->fields->push($field);
- $field::collectAssets();
- return $this;
- }
- /**
- * Add nested-form fields dynamically.
- *
- * @param string $method
- * @param array $arguments
- *
- * @return Field|$this
- */
- public function __call($method, $arguments)
- {
- if ($className = Form::findFieldClass($method)) {
- $column = Arr::get($arguments, 0, '');
- /** @var Field $field */
- $field = new $className($column, array_slice($arguments, 1));
- $field->setForm($this->parent);
- $this->pushField($field);
- return $field;
- }
- return $this;
- }
- }
|