/**
  * Handle the command.
  *
  * @param ModuleCollection $modules
  * @param ThemeCollection  $themes
  */
 public function handle(ModuleCollection $modules, ThemeCollection $themes)
 {
     $theme = $themes->current();
     /*
      * Default the form view based on the request.
      */
     if (!$this->builder->getFormOption('form_view') && $this->builder->isAjax()) {
         $this->builder->setFormOption('form_view', 'streams::form/ajax');
     }
     if (!$this->builder->getFormOption('form_view') && $theme && $theme->isAdmin()) {
         $this->builder->setFormOption('form_view', 'streams::form/form');
     }
     if (!$this->builder->getFormOption('form_view') && $theme && !$theme->isAdmin()) {
         $this->builder->setFormOption('form_view', 'streams::form/standard');
     }
     if (!$this->builder->getFormOption('form_view')) {
         $this->builder->setFormOption('form_view', 'streams::form/admin/form');
     }
     /*
      * Default the form wrapper view as well.
      */
     if (!$this->builder->getFormOption('wrapper_view') && $this->builder->isAjax()) {
         $this->builder->setFormOption('wrapper_view', 'streams::ajax');
     }
     if (!$this->builder->getFormOption('wrapper_view')) {
         $this->builder->setFormOption('wrapper_view', 'streams::blank');
     }
     /*
      * If the permission is not set then
      * try and automate it.
      */
     if ($this->builder->getFormOption('permission') === null && ($module = $modules->active()) && ($stream = $this->builder->getFormStream())) {
         $this->builder->setFormOption('permission', $module->getNamespace($stream->getSlug() . '.write'));
     }
 }
 /**
  * Handle the command.
  *
  * @param Request    $request
  * @param MessageBag $messages
  * @param Translator $translator
  */
 public function handle(Request $request, MessageBag $messages, Translator $translator)
 {
     if ($this->builder->isAjax()) {
         return;
     }
     $errors = $this->builder->getFormErrors();
     $messages->error($errors->all());
     if ($request->segment(1) == 'admin' && ($stream = $this->builder->getFormStream()) && $stream->isTrashable()) {
         /* @var AssignmentInterface $assignment */
         foreach ($stream->getUniqueAssignments() as $assignment) {
             if ($this->builder->hasFormError($assignment->getFieldSlug())) {
                 $messages->warning($translator->trans('streams::validation.unique_trash', ['attribute' => '"' . $translator->trans($assignment->getFieldName()) . '"']));
             }
         }
     }
 }
 /**
  * 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);
 }
Example #4
0
 /**
  * Compile rules from form fields.
  *
  * @param  FormBuilder $builder
  * @return array
  */
 public function compile(FormBuilder $builder)
 {
     $rules = [];
     $entry = $builder->getFormEntry();
     $stream = $builder->getFormStream();
     $locale = $this->config->get('streams::locales.default');
     /* @var FieldType $field */
     foreach ($builder->getEnabledFormFields() as $field) {
         if ($field->isDisabled()) {
             continue;
         }
         if (in_array($field->getField(), $builder->getSkips())) {
             continue;
         }
         $fieldRules = array_filter(array_unique($field->getRules()));
         $fieldRules = $field->extendRules($fieldRules);
         if (!$stream instanceof StreamInterface) {
             $rules[$field->getInputName()] = implode('|', $fieldRules);
             continue;
         }
         if ($assignment = $stream->getAssignment($field->getField())) {
             $type = $assignment->getFieldType();
             if ($type->isRequired()) {
                 $fieldRules[] = 'required';
             }
             if (!isset($fieldRules['unique']) && $assignment->isUnique() && !$assignment->isTranslatable()) {
                 $unique = 'unique:' . $stream->getEntryTableName() . ',' . $field->getColumnName();
                 if ($entry && ($id = $entry->getId())) {
                     $unique .= ',' . $id;
                 }
                 $fieldRules[] = $unique;
             }
             if ($assignment->isTranslatable() && $field->getLocale() !== $locale) {
                 $fieldRules = array_diff($fieldRules, ['required']);
             }
         }
         $rules[$field->getInputName()] = implode('|', array_unique($fieldRules));
     }
     return array_filter($rules);
 }
 /**
  * Build the fields.
  *
  * @param FormBuilder $builder
  */
 public function build(FormBuilder $builder)
 {
     $skips = $builder->getSkips();
     $stream = $builder->getFormStream();
     $entry = $builder->getFormEntry();
     $this->input->read($builder);
     /**
      * Convert each field to a field object
      * and put to the forms field collection.
      */
     foreach ($builder->getFields() as $field) {
         // Continue if skipping.
         if (in_array($field['field'], $skips)) {
             continue;
         }
         // Continue if not enabled.
         if (!array_get($field, 'enabled', true)) {
             continue;
         }
         $builder->addFormField($this->factory->make($field, $stream, $entry));
     }
 }
 /**
  * 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);
 }
 /**
  * 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);
 }
 /**
  * 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);
 }
 /**
  * Handle the command.
  */
 public function handle(MessageBag $messages, Translator $translator)
 {
     // If we can't save or there are errors then skip it.
     if ($this->builder->hasFormErrors() || !$this->builder->canSave()) {
         return;
     }
     // If there is no model and there isn't anything specific to say, skip it.
     if (!$this->builder->getFormEntry() && !$this->builder->getFormOption('success_message')) {
         return;
     }
     $mode = $this->builder->getFormMode();
     // False means no message is desired.
     if ($this->builder->getFormOption('success_message') === false) {
         return;
     }
     $entry = $this->builder->getFormEntry();
     $stream = $this->builder->getFormStream();
     $parameters = ['title' => is_object($entry) ? $entry->getTitle() : null, 'name' => is_object($stream) ? $stream->getName() : null];
     // If the name doesn't exist we need to be clever.
     if (str_contains($parameters['name'], '::') && !$translator->has($parameters['name']) && $stream) {
         $parameters['name'] = ucfirst(str_singular(str_replace('_', ' ', $stream->getSlug())));
     } elseif ($parameters['name']) {
         $parameters['name'] = str_singular(trans($parameters['name']));
     } else {
         $parameters['name'] = trans('streams::entry.name');
     }
     /**
      * Set the default success message.
      */
     if ($this->builder->getFormOption('success_message') === null) {
         $this->builder->setFormOption('success_message', trans('streams::message.' . $mode . '_success', $parameters));
     }
     $messages->{$this->builder->getFormOption('success_message_type', 'success')}($this->builder->getFormOption('success_message'));
 }