/** * Translate form fields. * * @param FormBuilder $builder */ public function translate(FormBuilder $builder) { $translations = []; $defaultLocale = config('streams::locales.default'); $enabledLocales = config('streams::locales.enabled'); /* * For each field if the assignment is translatable * then duplicate it and set a couple simple * parameters to assist in rendering. */ foreach ($builder->getFields() as $field) { if (!array_get($field, 'translatable', false)) { $translations[] = $field; continue; } foreach ($enabledLocales as $locale) { $translation = $field; array_set($translation, 'locale', $locale); array_set($translation, 'hidden', $locale !== $locale); if ($value = array_get($field, 'values.' . $locale)) { array_set($translation, 'value', $value); } if ($defaultLocale !== $locale) { array_set($translation, 'hidden', true); array_set($translation, 'required', false); array_set($translation, 'rules', array_diff(array_get($translation, 'rules', []), ['required'])); } $translations[] = $translation; } } $builder->setFields($translations); }
/** * Guess the nullable rule. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); foreach ($fields as &$field) { // Skip if nullable if (isset($field['rules']) && in_array('nullable', $field['rules'])) { continue; } /** * If the field depends on other fields we * won't add nullable here because validation * will not be performed on this field. */ if (!empty($field['rules'])) { if (preg_grep("/required_.*/", $field['rules'])) { continue; } } // If not required then nullable. if (isset($field['required']) && $field['required'] == false) { $field['rules'][] = 'nullable'; } // If not specified then it's nullable if (!isset($field['required'])) { $field['rules'][] = 'nullable'; } } $builder->setFields($fields); }
/** * Guess the field placeholders. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $prefix = $builder->getFormOption('prefix'); foreach ($fields as &$field) { array_set($field, 'prefix', array_get($field, 'prefix', $prefix)); } $builder->setFields($fields); }
/** * Fill in fields. * * @param FormBuilder $builder */ public function fill(FormBuilder $builder) { $fields = $builder->getFields(); $stream = $builder->getFormStream(); /** * If no Stream, skip it. */ if (!$stream) { if (array_search('*', $fields) !== false) { unset($fields[array_search('*', $fields)]); $builder->setFields($fields); } return; } /** * Fill with everything by default. */ $fill = $stream->getAssignments()->fieldSlugs(); /** * Loop over field configurations and unset * them from the fill fields. * * If we come across the fill marker then * set the position. */ foreach ($fields as $parameters) { if (is_string($parameters) && $parameters === '*') { continue; } unset($fill[array_search($parameters['field'], $fill)]); } /** * If we have a fill marker then splice * in the remaining fill fields in place * of the fill marker. */ if (($position = array_search('*', $fields)) !== false) { array_splice($fields, $position, null, $fill); unset($fields[array_search('*', $fields)]); } $builder->setFields($fields); }
/** * Guess the field instructions. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); if (!($readOnly = $builder->isReadOnly())) { return; } foreach ($fields as &$field) { $field['read_only'] = true; $field['disabled'] = true; } $builder->setFields($fields); }
/** * Guess the field instructions. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $mode = $builder->getFormMode(); foreach ($fields as &$field) { // Guess based on the form mode if applicable. if (in_array((string) ($disabled = array_get($field, 'disabled', null)), ['create', 'edit'])) { $field['disabled'] = $disabled === $mode; } } $builder->setFields($fields); }
/** * Guess the field instructions. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $entry = $builder->getFormEntry(); if (!is_object($entry)) { return; } foreach ($fields as &$field) { $field['translatable'] = $entry->isTranslatedAttribute($field['field']); } $builder->setFields($fields); }
/** * Guess the field unique rule. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $entry = $builder->getFormEntry(); foreach ($fields as &$field) { $unique = array_pull($field, 'rules.unique'); /* * No unique? Continue... */ if (!$unique || $unique === false) { continue; } /* * If unique is a string then * it's set explicitly. */ if ($unique && is_string($unique)) { $field['rules']['unique'] = 'unique:' . $unique; continue; } /* * If unique is true then * automate the rule. */ if ($unique && $unique === true) { $unique = 'unique:' . $entry->getTable() . ',' . $field['field']; if ($entry instanceof EntryInterface) { $unique .= ',' . $entry->getId(); } $field['rules']['unique'] = $unique; continue; } /* * If unique is an array then use * the default automation and add to it. */ if ($unique && is_array($unique)) { $unique = 'unique:' . $entry->getTable() . ',' . $field['field']; if ($entry instanceof EntryInterface) { $unique .= ',' . $entry->getId(); } $keys = array_keys($unique); $values = array_values($unique); foreach ($keys as $column) { $unique .= ',' . $column . ',' . $column . ',' . array_shift($values); } $field['rules']['unique'] = $unique; continue; } } $builder->setFields($fields); }
/** * Populate the fields with entry values. * * @param FormBuilder $builder */ public function populate(FormBuilder $builder) { $fields = $builder->getFields(); $entry = $builder->getFormEntry(); foreach ($fields as &$field) { /* * If the field is not already set * then get the value off the entry. */ if (!isset($field['value']) && $entry instanceof EloquentModel && $entry->getId()) { if ($locale = array_get($field, 'locale')) { $field['value'] = $entry->translateOrDefault($locale)->{$field['field']}; } else { $field['value'] = $entry->{$field['field']}; } } /* * If the field has a default value * and the entry does not exist yet * then use the default value. */ if (isset($field['config']['default_value']) && $entry instanceof EloquentModel && !$entry->getId()) { $field['value'] = $field['config']['default_value']; } /* * If the field has a default value * and there is no entry then * use the default value. */ if (isset($field['config']['default_value']) && !$entry) { $field['value'] = $field['config']['default_value']; } /* * If the field is an assignment then * use it's config for the default value. */ if (!isset($field['value']) && $entry instanceof EntryInterface && ($type = $entry->getFieldType($field['field']))) { $field['value'] = array_get($type->getConfig(), 'default_value'); } /* * Lastly if we have flashed data from a front end * form handler then use that value for the field. */ if ($this->session->has($field['prefix'] . $field['field'])) { $field['value'] = $this->session->pull($field['prefix'] . $field['field']); } } $builder->setFields($fields); }
/** * Normalize field input. * * @param FormBuilder $builder */ public function normalize(FormBuilder $builder) { $fields = $builder->getFields(); foreach ($fields as $slug => &$field) { /* * If the field is a wild card marker * then just continue. */ if ($field == '*') { continue; } /* * If the slug is numeric and the field * is a string then use the field as is. */ if (is_numeric($slug) && is_string($field)) { $field = ['field' => $field]; } /* * If the slug is a string and the field * is a string too then use the field as the * type and the field as well. */ if (!is_numeric($slug) && is_string($slug) && is_string($field)) { $field = ['field' => $slug, 'type' => $field]; } /* * If the field is an array and does not * have the field parameter set then * use the slug. */ if (is_array($field) && !isset($field['field'])) { $field['field'] = $slug; } /* * If the field is required then it must have * the rule as well. */ if (array_get($field, 'required') === true) { $field['rules'] = array_unique(array_merge(array_get($field, 'rules', []), ['required'])); } } $builder->setFields(array_values($fields)); }
/** * Guess the field required flag. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $mode = $builder->getFormMode(); $entry = $builder->getFormEntry(); foreach ($fields as &$field) { // Guess based on the assignment if possible. if (!isset($field['required']) && $entry instanceof EntryInterface && ($assignment = $entry->getAssignment($field['field']))) { $field['required'] = array_get($field, 'required', $assignment->isRequired()); } // Guess based on the form mode if applicable. if (in_array($required = array_get($field, 'required'), ['create', 'edit'])) { $field['required'] = $required === $mode; } // Guess based on the rules. if (in_array('required', array_get($field, 'rules', []))) { $field['required'] = true; } } $builder->setFields($fields); }
/** * Guess the field placeholders. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $stream = $builder->getFormStream(); foreach ($fields as &$field) { $locale = array_get($field, 'locale'); /** * If the placeholder are already set then use it. */ if (isset($field['placeholder'])) { if (str_is('*::*', $field['placeholder'])) { $field['placeholder'] = trans($field['placeholder'], [], null, $locale); } continue; } /** * If we don't have a field then we * can not really guess anything here. */ if (!isset($field['field'])) { continue; } /** * No stream means we can't * really do much here. */ if (!$stream instanceof StreamInterface) { continue; } $assignment = $stream->getAssignment($field['field']); $object = $stream->getField($field['field']); /** * No assignment means we still do * not have anything to do here. */ if (!$assignment instanceof AssignmentInterface) { continue; } /** * Next try using the fallback assignment * placeholder system as generated verbatim. */ $placeholder = $assignment->getPlaceholder() . '.default'; if (!isset($field['placeholder']) && str_is('*::*', $placeholder) && trans()->has($placeholder, $locale)) { $field['placeholder'] = trans($placeholder, [], null, $locale); } /** * Next try using the default assignment * placeholder system as generated verbatim. */ $placeholder = $assignment->getPlaceholder(); if (!isset($field['placeholder']) && str_is('*::*', $placeholder) && trans()->has($placeholder, $locale) && is_string($translated = trans($placeholder, [], null, $locale))) { $field['placeholder'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) { $field['placeholder'] = $placeholder; } /** * Next try using the default field * placeholder system as generated verbatim. */ $placeholder = $object->getPlaceholder(); if (!isset($field['placeholder']) && str_is('*::*', $placeholder) && trans()->has($placeholder, $locale) && is_string($translated = trans($placeholder, [], null, $locale))) { $field['placeholder'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) { $field['placeholder'] = $placeholder; } } $builder->setFields($fields); }
/** * Guess the field labels. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $stream = $builder->getFormStream(); foreach ($fields as &$field) { $locale = array_get($field, 'locale'); /** * If the label are already set then use it. */ if (isset($field['label'])) { if (str_is('*::*', $field['label'])) { $field['label'] = trans($field['label'], [], null, $locale); } continue; } /** * If we don't have a field then we * can not really guess anything here. */ if (!isset($field['field'])) { continue; } /** * No stream means we can't * really do much here. */ if (!$stream instanceof StreamInterface) { continue; } $assignment = $stream->getAssignment($field['field']); $object = $stream->getField($field['field']); /** * No assignment means we still do * not have anything to do here. */ if (!$assignment instanceof AssignmentInterface) { continue; } /** * Next try using the fallback assignment * label system as generated verbatim. */ $label = $assignment->getLabel() . '.default'; if (!isset($field['label']) && str_is('*::*', $label) && trans()->has($label, $locale)) { $field['label'] = trans($label, [], null, $locale); } /** * Next try using the default assignment * label system as generated verbatim. */ $label = $assignment->getLabel(); if (!isset($field['label']) && str_is('*::*', $label) && trans()->has($label, $locale) && is_string($translated = trans($label, [], null, $locale))) { $field['label'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['label']) && $label && !str_is('*::*', $label)) { $field['label'] = $label; } /** * Next try using the generic assignment * label system without the stream identifier. */ $label = explode('.', $assignment->getLabel()); array_pop($label); $label = implode('.', $label); if (!isset($field['label']) && str_is('*::*', $label) && trans()->has($label, $locale) && is_string($translated = trans($label, [], null, $locale))) { $field['label'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['label']) && $label && !str_is('*::*', $label)) { $field['label'] = $label; } /** * Next try using the default field * label system as generated verbatim. */ $label = $object->getName(); if (!isset($field['label']) && str_is('*::*', $label) && trans()->has($label, $locale) && is_string($translated = trans($label, [], null, $locale))) { $field['label'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['label']) && $label && !str_is('*::*', $label)) { $field['label'] = $label; } /** * If the field is still untranslated and * we're not debugging then humanize the slug * in leu of displaying an untranslated key. */ if (!isset($field['label']) && $this->config->get('streams::system.lazy_translations')) { $field['label'] = $this->string->humanize($field['field']); } } $builder->setFields($fields); }
/** * Parse the form fields. * * @param FormBuilder $builder */ public function parse(FormBuilder $builder) { $entry = $builder->getFormEntry(); $builder->setFields($this->parser->parse($builder->getFields(), compact('entry'))); }
/** * Guess the field warnings. * * @param FormBuilder $builder */ public function guess(FormBuilder $builder) { $fields = $builder->getFields(); $stream = $builder->getFormStream(); foreach ($fields as &$field) { $locale = array_get($field, 'locale'); /** * If the warning is already set then use it. */ if (isset($field['warning'])) { if (str_is('*::*', $field['warning'])) { $field['warning'] = trans($field['warning'], [], null, $locale); } continue; } /** * If we don't have a field then we * can not really guess anything here. */ if (!isset($field['field'])) { continue; } /** * No stream means we can't * really do much here. */ if (!$stream || !$stream->getAssignment($field['field'])) { $warning = "module::field.{$field['field']}.warning"; if (str_is('*::*', $warning) && trans()->has($warning)) { $field['warning'] = trans($warning, [], null, $locale); } continue; } $assignment = $stream->getAssignment($field['field']); $object = $stream->getField($field['field']); /** * No assignment means we still do * not have anything to do here. */ if (!$assignment) { continue; } /** * Next try using the fallback assignment * warning system as generated verbatim. */ $warning = $assignment->getWarning() . '.default'; if (!isset($field['warning']) && str_is('*::*', $warning) && trans()->has($warning, $locale)) { $field['warning'] = trans($warning, [], null, $locale); } /** * Next try using the default assignment * warning system as generated verbatim. */ $warning = $assignment->getWarning(); if (!isset($field['warning']) && str_is('*::*', $warning) && trans()->has($warning, $locale) && is_string($translated = trans($warning, [], null, $locale))) { $field['warning'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['warning']) && $warning && !str_is('*::*', $warning)) { $field['warning'] = $warning; } /** * Next try using the default field * warning system as generated verbatim. */ $warning = $object->getWarning(); if (!isset($field['warning']) && str_is('*::*', $warning) && trans()->has($warning, $locale) && is_string($translated = trans($warning, [], null, $locale))) { $field['warning'] = $translated; } /** * Check if it's just a standard string. */ if (!isset($field['warning']) && $warning && !str_is('*::*', $warning)) { $field['warning'] = $warning; } } $builder->setFields($fields); }
/** * Default the form fields when none are defined. * * @param FormBuilder $builder */ public function defaults(FormBuilder $builder) { if ($builder->getFields() === []) { $builder->setFields(['*']); } }
/** * Evaluate the form fields. * * @param FormBuilder $builder */ public function evaluate(FormBuilder $builder) { $builder->setFields($this->evaluator->evaluate($builder->getFields(), compact('builder'))); }