Example #1
0
 /**
  *
  *
  * @param int $widgetId
  * @param array $data
  * @return \Ip\Form
  */
 protected function createForm($widgetId, $data)
 {
     $form = new \Ip\Form();
     $form->setEnvironment(\Ip\Form::ENVIRONMENT_PUBLIC);
     if (empty($data['fields']) || !is_array($data['fields'])) {
         $data['fields'] = array();
     }
     foreach ($data['fields'] as $fieldKey => $field) {
         if (!isset($field['type']) || !isset($field['label'])) {
             continue;
         }
         if ($field['type'] == 'Fieldset') {
             $label = empty($field['label']) ? '' : $field['label'];
             $form->addFieldset(new \Ip\Form\Fieldset($label));
             continue;
         }
         if (!isset($field['options'])) {
             $field['options'] = array();
         }
         if (!isset($field['options']) || !is_array($field['options'])) {
             $field['options'] = array();
         }
         if (!isset($field['required'])) {
             $field['required'] = false;
         }
         $fieldType = Model::getFieldType($field['type']);
         if ($fieldType) {
             $fieldData = array('label' => $field['label'], 'name' => 'ipForm_field_' . $fieldKey, 'required' => $field['required'], 'options' => $field['options']);
             try {
                 $newField = $fieldType->createField($fieldData);
                 $form->addField($newField);
             } catch (\Ip\Exception\Content $e) {
                 ipLog()->error('FormWidget.failedAddField: Widget failed to add field.', array('widget' => 'Form', 'exception' => $e, 'fieldData' => $fieldData));
             }
         }
     }
     //special variable to post to widget controller
     $field = new \Ip\Form\Field\Hidden(array('name' => 'sa', 'value' => 'Content.widgetPost'));
     $form->addField($field);
     $field = new \Ip\Form\Field\Hidden(array('name' => 'widgetId', 'value' => $widgetId));
     $form->addField($field);
     //antispam
     $field = new \Ip\Form\Field\Antispam(array('name' => 'checkField'));
     $form->addField($field);
     //submit
     if (!empty($data['buttonText'])) {
         $value = $data['buttonText'];
     } else {
         $value = __('Send', 'Ip', false);
     }
     $field = new \Ip\Form\Field\Submit(array('value' => $value));
     $form->addField($field);
     return $form;
 }