Backend\Modules\FormBuilder\Engine\Helper::parseField PHP Method

parseField() public static method

Parse a field and return the HTML.
public static parseField ( array $field ) : string
$field array Field data.
return string
    public static function parseField(array $field)
    {
        if (!empty($field)) {
            // init
            $frm = new BackendForm('tmp', '');
            $tpl = BackendModel::getContainer()->has('template') ? BackendModel::getContainer()->get('template') : new BackendTemplate();
            $fieldHTML = '';
            $fieldName = 'field' . $field['id'];
            $values = isset($field['settings']['values']) ? $field['settings']['values'] : null;
            $defaultValues = isset($field['settings']['default_values']) ? $field['settings']['default_values'] : null;
            $placeholder = isset($field['settings']['placeholder']) ? $field['settings']['placeholder'] : null;
            /*
             * Create form and parse to HTML
             */
            // dropdown
            if ($field['type'] == 'dropdown') {
                // values and labels are the same
                $values = array_combine($values, $values);
                // get index of selected item
                $defaultIndex = array_search($defaultValues, $values, true);
                if ($defaultIndex === false) {
                    $defaultIndex = null;
                }
                // create element
                $ddm = $frm->addDropdown($fieldName, $values, $defaultIndex);
                // empty default element
                $ddm->setDefaultElement('');
                // get content
                $fieldHTML = $ddm->parse();
            } elseif ($field['type'] == 'datetime') {
                // create element
                if ($field['settings']['input_type'] == 'date') {
                    // calculate default value
                    $amount = $field['settings']['value_amount'];
                    $type = $field['settings']['value_type'];
                    if ($type != '') {
                        switch ($type) {
                            case 'today':
                                $defaultValues = date('d/m/Y');
                                break;
                            case 'day':
                            case 'week':
                            case 'month':
                            case 'year':
                                if ($amount != '') {
                                    $defaultValues = date('d/m/Y', strtotime('+' . $amount . ' ' . $type));
                                }
                                break;
                        }
                    }
                    $datetime = $frm->addText($fieldName, $defaultValues);
                } else {
                    $datetime = $frm->addTime($fieldName, $defaultValues);
                }
                $datetime->setAttribute('disabled', 'disabled');
                // get content
                $fieldHTML = $datetime->parse();
            } elseif ($field['type'] == 'radiobutton') {
                // create element
                $rbt = $frm->addRadiobutton($fieldName, $values, $defaultValues);
                // get content
                $fieldHTML = $rbt->parse();
            } elseif ($field['type'] == 'checkbox') {
                // rebuild values
                foreach ($values as $value) {
                    $newValues[] = array('label' => $value, 'value' => $value);
                }
                // create element
                $chk = $frm->addMultiCheckbox($fieldName, $newValues, $defaultValues);
                // get content
                $fieldHTML = $chk->parse();
            } elseif ($field['type'] == 'textbox') {
                // create element
                $txt = $frm->addText($fieldName, $defaultValues);
                $txt->setAttribute('disabled', 'disabled');
                $txt->setAttribute('placeholder', $placeholder);
                // get content
                $fieldHTML = $txt->parse();
            } elseif ($field['type'] == 'textarea') {
                // create element
                $txt = $frm->addTextarea($fieldName, $defaultValues);
                $txt->setAttribute('cols', 30);
                $txt->setAttribute('disabled', 'disabled');
                $txt->setAttribute('placeholder', $placeholder);
                // get content
                $fieldHTML = $txt->parse();
            } elseif ($field['type'] == 'heading') {
                $fieldHTML = '<h3>' . $values . '</h3>';
            } elseif ($field['type'] == 'paragraph') {
                $fieldHTML = $values;
            }
            /*
             * Parse the field into the template
             */
            // init
            $tpl->assign('plaintext', false);
            $tpl->assign('simple', false);
            $tpl->assign('multiple', false);
            $tpl->assign('id', $field['id']);
            $tpl->assign('required', isset($field['validations']['required']));
            // plaintext items
            if ($field['type'] == 'heading' || $field['type'] == 'paragraph') {
                // assign
                $tpl->assign('content', $fieldHTML);
                $tpl->assign('plaintext', true);
            } elseif ($field['type'] == 'checkbox' || $field['type'] == 'radiobutton') {
                // name (prefixed by type)
                $name = $field['type'] == 'checkbox' ? 'chk' . \SpoonFilter::ucfirst($fieldName) : 'rbt' . \SpoonFilter::ucfirst($fieldName);
                // rebuild so the html is stored in a general name (and not rbtName)
                foreach ($fieldHTML as &$item) {
                    $item['field'] = $item[$name];
                }
                // show multiple
                $tpl->assign('label', $field['settings']['label']);
                $tpl->assign('items', $fieldHTML);
                $tpl->assign('multiple', true);
            } else {
                // assign
                $tpl->assign('label', $field['settings']['label']);
                $tpl->assign('field', $fieldHTML);
                $tpl->assign('simple', true);
            }
            return $tpl->getContent(BACKEND_MODULES_PATH . '/FormBuilder/Layout/Templates/Field.html.twig');
        } else {
            // empty field so return empty string
            return '';
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $formId = \SpoonFilter::getPostValue('form_id', null, '', 'int');
     $fieldId = \SpoonFilter::getPostValue('field_id', null, '', 'int');
     $type = \SpoonFilter::getPostValue('type', array('checkbox', 'dropdown', 'datetime', 'heading', 'paragraph', 'radiobutton', 'submit', 'textarea', 'textbox'), '', 'string');
     $label = trim(\SpoonFilter::getPostValue('label', null, '', 'string'));
     $values = trim(\SpoonFilter::getPostValue('values', null, '', 'string'));
     // this is somewhat a nasty hack, but it makes special chars work.
     $values = \SpoonFilter::htmlspecialcharsDecode($values);
     $defaultValues = trim(\SpoonFilter::getPostValue('default_values', null, '', 'string'));
     $placeholder = trim(\SpoonFilter::getPostValue('placeholder', null, '', 'string'));
     $required = \SpoonFilter::getPostValue('required', array('Y', 'N'), 'N', 'string');
     $requiredErrorMessage = trim(\SpoonFilter::getPostValue('required_error_message', null, '', 'string'));
     $validation = \SpoonFilter::getPostValue('validation', array('email', 'numeric', 'time'), '', 'string');
     $validationParameter = trim(\SpoonFilter::getPostValue('validation_parameter', null, '', 'string'));
     $errorMessage = trim(\SpoonFilter::getPostValue('error_message', null, '', 'string'));
     // special field for textbox: reply to
     $replyTo = \SpoonFilter::getPostValue('reply_to', array('Y', 'N'), 'N', 'string');
     // special fields for datetime
     $inputType = \SpoonFilter::getPostValue('input_type', array('date', 'time'), 'date', 'string');
     $valueAmount = trim(\SpoonFilter::getPostValue('value_amount', null, '', 'string'));
     $valueType = trim(\SpoonFilter::getPostValue('value_type', null, '', 'string'));
     // invalid form id
     if (!BackendFormBuilderModel::exists($formId)) {
         $this->output(self::BAD_REQUEST, null, 'form does not exist');
     } else {
         // invalid fieldId
         if ($fieldId !== 0 && !BackendFormBuilderModel::existsField($fieldId, $formId)) {
             $this->output(self::BAD_REQUEST, null, 'field does not exist');
         } else {
             // invalid type
             if ($type == '') {
                 $this->output(self::BAD_REQUEST, null, 'invalid type provided');
             } else {
                 // extra validation is only possible for textfields & datetime fields
                 if ($type != 'textbox' && $type != 'datetime') {
                     $validation = '';
                     $validationParameter = '';
                     $errorMessage = '';
                 }
                 // init
                 $errors = array();
                 // validate textbox
                 if ($type == 'textbox') {
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($validation != '' && $errorMessage == '') {
                         $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($replyTo == 'Y' && $validation != 'email') {
                         $errors['reply_to_error_message'] = BL::getError('EmailValidationIsRequired');
                     }
                 } elseif ($type == 'textarea') {
                     // validate textarea
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($validation != '' && $errorMessage == '') {
                         $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                 } elseif ($type == 'datetime') {
                     // validate datetime
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if (in_array($valueType, array('day', 'week', 'month', 'year')) && $valueAmount == '') {
                         $errors['default_value_error_message'] = BL::getError('ValueIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($validation != '' && $errorMessage == '') {
                         $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                 } elseif ($type == 'heading' && $values == '') {
                     // validate heading
                     $errors['values'] = BL::getError('ValueIsRequired');
                 } elseif ($type == 'paragraph' && $values == '') {
                     // validate paragraphs
                     $errors['values'] = BL::getError('ValueIsRequired');
                 } elseif ($type == 'submit' && $values == '') {
                     // validate submitbuttons
                     $errors['values'] = BL::getError('ValueIsRequired');
                 } elseif ($type == 'dropdown') {
                     // validate dropdown
                     $values = trim($values, ',');
                     // validate
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($values == '') {
                         $errors['values'] = BL::getError('ValueIsRequired');
                     }
                 } elseif ($type == 'radiobutton') {
                     // validate radiobutton
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                     if ($values == '') {
                         $errors['values'] = BL::getError('ValueIsRequired');
                     }
                 } elseif ($type == 'checkbox') {
                     // validate checkbox
                     if ($label == '') {
                         $errors['label'] = BL::getError('LabelIsRequired');
                     }
                     if ($required == 'Y' && $requiredErrorMessage == '') {
                         $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
                     }
                 }
                 // got errors
                 if (!empty($errors)) {
                     $this->output(self::OK, array('errors' => $errors), 'form contains errors');
                 } else {
                     // htmlspecialchars except for paragraphs
                     if ($type != 'paragraph') {
                         if ($values != '') {
                             $values = \SpoonFilter::htmlspecialchars($values);
                         }
                         if ($defaultValues != '') {
                             $defaultValues = \SpoonFilter::htmlspecialchars($defaultValues);
                         }
                     }
                     // split
                     if ($type == 'dropdown' || $type == 'checkbox') {
                         $values = (array) explode('|', $values);
                     } elseif ($type == 'radiobutton') {
                         $postedValues = (array) explode('|', $values);
                         $values = array();
                         foreach ($postedValues as $postedValue) {
                             $values[] = array('value' => CommonUri::getUrl($postedValue), 'label' => $postedValue);
                         }
                     }
                     /**
                      * Save!
                      */
                     // settings
                     $settings = array();
                     if ($label != '') {
                         $settings['label'] = \SpoonFilter::htmlspecialchars($label);
                     }
                     if (isset($values)) {
                         $settings['values'] = $values;
                     }
                     if ($defaultValues != '') {
                         $settings['default_values'] = $defaultValues;
                     }
                     if ($placeholder != '') {
                         $settings['placeholder'] = \SpoonFilter::htmlspecialchars($placeholder);
                     }
                     // reply-to, only for textboxes
                     if ($type == 'textbox') {
                         $settings['reply_to'] = $replyTo == 'Y';
                     }
                     // only for datetime input
                     if ($type == 'datetime') {
                         $settings['input_type'] = $inputType;
                         if ($inputType == 'date') {
                             $settings['value_amount'] = $valueAmount;
                             $settings['value_type'] = $valueType;
                         }
                     }
                     // build array
                     $field = array();
                     $field['form_id'] = $formId;
                     $field['type'] = $type;
                     $field['settings'] = !empty($settings) ? serialize($settings) : null;
                     // existing field
                     if ($fieldId !== 0) {
                         // update field
                         BackendFormBuilderModel::updateField($fieldId, $field);
                         // delete all validation (added again later)
                         BackendFormBuilderModel::deleteFieldValidation($fieldId);
                     } else {
                         // sequence
                         $field['sequence'] = BackendFormBuilderModel::getMaximumSequence($formId) + 1;
                         // insert
                         $fieldId = BackendFormBuilderModel::insertField($field);
                     }
                     // required
                     if ($required == 'Y') {
                         // build array
                         $validate['field_id'] = $fieldId;
                         $validate['type'] = 'required';
                         $validate['error_message'] = \SpoonFilter::htmlspecialchars($requiredErrorMessage);
                         // add validation
                         BackendFormBuilderModel::insertFieldValidation($validate);
                         // add to field (for parsing)
                         $field['validations']['required'] = $validate;
                     }
                     // other validation
                     if ($validation != '') {
                         // build array
                         $validate['field_id'] = $fieldId;
                         $validate['type'] = $validation;
                         $validate['error_message'] = \SpoonFilter::htmlspecialchars($errorMessage);
                         $validate['parameter'] = $validationParameter != '' ? \SpoonFilter::htmlspecialchars($validationParameter) : null;
                         // add validation
                         BackendFormBuilderModel::insertFieldValidation($validate);
                         // add to field (for parsing)
                         $field['validations'][$type] = $validate;
                     }
                     // get item from database (i do this call again to keep the pof as low as possible)
                     $field = BackendFormBuilderModel::getField($fieldId);
                     // submit button isnt parsed but handled directly via javascript
                     if ($type == 'submit') {
                         $fieldHTML = '';
                     } else {
                         // parse field to html
                         $fieldHTML = FormBuilderHelper::parseField($field);
                     }
                     // success output
                     $this->output(self::OK, array('field_id' => $fieldId, 'field_html' => $fieldHTML), 'field saved');
                 }
             }
         }
     }
 }
All Usage Examples Of Backend\Modules\FormBuilder\Engine\Helper::parseField