Ejemplo n.º 1
0
 /**
  * Tests the validate() method in Form Field Data
  *
  * @test
  * @dataProvider providerValidate
  * @param array $valid Valid data for the test
  * @param array $invalid Invalid data for the test
  * @param array $save Saves the record when validation succeeds
  */
 public function testValidate($valid, $invalid, $save)
 {
     // Model instance for the test
     $form_field = new Form_Field_Model();
     // Valid data test
     $this->assertEquals(TRUE, $form_field->validate($valid, $save), Kohana::debug($valid->errors()));
     // Invalid data test
     $this->assertEquals(FALSE, $form_field->validate($invalid, $save), sprintf("Expected errors not found. Error count: %d", count($invalid->errors())));
 }
Ejemplo n.º 2
0
 /**
  * Create/Edit & Save New Form Field
  */
 public function field_add()
 {
     $this->template = "";
     $this->auto_render = FALSE;
     // setup and initialize form field names
     $form = array('field_type' => '', 'field_name' => '', 'field_default' => '', 'field_required' => '', 'field_width' => '', 'field_height' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $field_add_status = "";
     $field_add_response = "";
     if ($_POST) {
         $post = Validation::factory($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         $post->add_rules('form_id', 'required', 'numeric');
         $post->add_rules('field_type', 'required', 'numeric');
         $post->add_rules('field_name', 'required', 'length[3,100]');
         $post->add_rules('field_default', 'length[3,200]');
         $post->add_rules('field_required', 'required', 'between[0,1]');
         $post->add_rules('field_width', 'between[0,300]');
         $post->add_rules('field_height', 'between[0,50]');
         $post->add_rules('field_isdate', 'required', 'between[0,1]');
         if ($post->validate()) {
             $form_id = $post->form_id;
             $custom_form = new Form_Model($form_id);
             if ($custom_form->loaded == true) {
                 $field_id = $post->field_id;
                 $new_field = true;
                 if (!empty($field_id) && is_numeric($field_id)) {
                     $exists = ORM::factory('form_field', $field_id);
                     if ($exists->loaded == true) {
                         $new_field = false;
                     } else {
                         $new_field = true;
                     }
                 }
                 $field_form = new Form_Field_Model($field_id);
                 $field_form->form_id = $form_id;
                 $field_form->field_type = $post->field_type;
                 $field_form->field_name = $post->field_name;
                 $field_form->field_default = $post->field_default;
                 $field_form->field_required = $post->field_required;
                 $field_form->field_width = $post->field_width;
                 $field_form->field_height = $post->field_height;
                 $field_form->field_isdate = $post->field_isdate;
                 if ($field_form->save()) {
                     $field_id = $field_form->id;
                 }
                 // Assign Position
                 if ($new_field) {
                     $get_position = ORM::factory('form_field')->orderby('field_position', 'desc')->find();
                     if ($get_position->loaded == true) {
                         $field_position = $get_position->field_position + 1;
                     } else {
                         $field_position = 1;
                     }
                     $field_form->field_position = $field_position;
                     $field_form->save($field_id);
                 }
                 $field_add_status = "success";
                 $field_add_response = rawurlencode($this->_get_current_fields($form_id));
             } else {
                 $field_add_status = "error";
                 $field_add_response = Kohana::lang('ui_main.form_not_exists');
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('form'));
             // populate the response to this post request
             $field_add_status = "error";
             $field_add_response = "";
             $field_add_response .= "<ul>";
             foreach ($errors as $error_item => $error_description) {
                 $field_add_response .= !$error_description ? '' : "<li>" . $error_description . "</li>";
             }
             $field_add_response .= "</ul>";
         }
     }
     echo json_encode(array("status" => $field_add_status, "response" => $field_add_response));
 }
Ejemplo n.º 3
0
 /**
  * Create/Edit & Save New Form Field
  */
 public function field_add()
 {
     $this->template = "";
     $this->auto_render = FALSE;
     // setup and initialize form field names
     $form = array('field_type' => '', 'field_name' => '', 'field_default' => '', 'field_required' => '', 'field_width' => '', 'field_height' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $field_add_status = "";
     $field_add_response = "";
     if ($_POST) {
         // @todo Manually extract the data to be validated
         $form_field_data = arr::extract($_POST, 'form_id', 'field_type', 'field_name', 'field_default', 'field_required', 'field_width', 'field_height', 'field_isdate', 'field_ispublic_visible', 'field_ispublic_submit');
         // Sanitize the default value (if provided)
         $form_field_data['field_default'] = $this->input->xss_clean($form_field_data['field_default']);
         // Form_Field_Model instance
         $form_field = Form_Field_Model::is_valid_form_field($_POST['field_id']) ? ORM::factory('form_field', $_POST['field_id']) : new Form_Field_Model();
         // Validate the form field data
         if ($form_field->validate($form_field_data)) {
             // Validation succeeded, proceed...
             // Check for new form field entry
             $new_field = $form_field->loaded;
             // Save the new/modified form field entry
             $form_field->save();
             // Get the form field id
             $field_id = $form_field->id;
             // Save optional values
             if (isset($_POST['field_options'])) {
                 foreach ($_POST['field_options'] as $name => $value) {
                     $option_exists = ORM::factory('form_field_option')->where('form_field_id', $field_id)->where('option_name', $name)->find();
                     $option_entry = $option_exists->loaded == TRUE ? ORM::factory('form_field_option', $option_exists->id) : new Form_Field_Option_Model();
                     $option_entry->form_field_id = $field_id;
                     $option_entry->option_name = $name;
                     $option_entry->option_value = $value;
                     $option_entry->save();
                 }
             }
             // If a new field, calculate the field position
             if (empty($new_field)) {
                 // Calculate the field position
                 $field_position = ORM::factory('form_field')->where(array('form_id' => $form_field->form_id, 'id != ' => $field_id))->count_all() + 1;
                 $form_field->field_position = $field_position;
                 $form_field->save();
             }
             $field_add_status = "success";
             $field_add_response = rawurlencode(customforms::get_current_fields($form_field->form_id, $this->user));
         } else {
             // Repopulate the form fields
             $form = arr::overwrite($form, $form_field_data->as_array());
             // Populate the error fields, if any
             $errors = arr::overwrite($errors, $form_field_data->errors('form'));
             // populate the response to this post request
             $field_add_status = "error";
             $field_add_response = "";
             $field_add_response .= "<ul>";
             foreach ($errors as $error_item => $error_description) {
                 $field_add_response .= !$error_description ? '' : "<li>" . $error_description . "</li>";
             }
             $field_add_response .= "</ul>";
         }
     }
     echo json_encode(array("status" => $field_add_status, "response" => $field_add_response));
 }
Ejemplo n.º 4
0
 /**
  * Import Custom Forms and their respective form fields via XML
  * @param DOMNodeList Object $customforms
  * @return bool
  */
 public function import_customforms($customforms)
 {
     $forms = $customforms->getElementsByTagName('form');
     foreach ($forms as $form) {
         // Increment forms counter
         $this->totalforms++;
         $totalfields = 0;
         // Form Title
         $title = xml::get_node_text($form, 'title');
         // If the form title is missing
         if (!$title) {
             $this->errors[] = Kohana::lang('import.xml.missing_form_title') . $this->totalforms;
         } else {
             // If the form does not already exist
             if (!isset($this->existing_forms[utf8::strtoupper($title)])) {
                 // Form Active status
                 $form_active = $form->getAttribute('active');
                 // Make sure form status value is allowable
                 $active = (isset($form_active) and in_array($form_active, $this->allowable)) ? $form_active : 1;
                 // Form Description
                 $description = xml::get_node_text($form, 'description');
                 // Save it
                 $new_form = new Form_Model();
                 $new_form->form_title = $title;
                 $new_form->form_description = $description ? $description : NULL;
                 $new_form->form_active = $active;
                 $new_form->save();
                 // Add new form to array of existing forms
                 $this->existing_forms[utf8::strtoupper($title)] = $new_form->id;
                 // Add new form to array of forms added during import
                 $this->forms_added[] = $new_form->id;
                 $this->notices[] = Kohana::lang('import.xml.new_form') . '"' . $title . '"';
             }
             // Form Fields
             $this_form = $this->existing_forms[utf8::strtoupper($title)];
             $fields = $form->getElementsByTagName('field');
             if ($fields->length > 0) {
                 foreach ($fields as $field) {
                     // Increment form fields counter for this form
                     $totalfields++;
                     // Field Name
                     $name = xml::get_node_text($field, 'name');
                     // Field Type
                     $field_type = $field->getAttribute('type');
                     $allowable_types = array(1, 2, 3, 4, 5, 6, 7);
                     // Make sure field_type value is allowable
                     $type = (isset($field_type) and in_array($field_type, $allowable_types)) ? $field_type : NULL;
                     // If field name is missing or field type is null
                     if (!$name or !isset($type)) {
                         $this->notices[] = Kohana::lang('import.xml.field_error') . '"' . $title . '" : Field #' . $totalfields;
                     } else {
                         // If the field does not already exist in this form
                         if (!isset($this->existing_fields[utf8::strtoupper($name)][$this_form])) {
                             // Field Required
                             $field_required = $field->getAttribute('required');
                             $required = (isset($field_required) and in_array($field_required, $this->allowable)) ? $field_required : 0;
                             // Field Publicly Visible
                             $field_visible = $field->getAttribute('visible_by');
                             $public_visible = (isset($field_visible) and in_array($field_visible, $this->allowable)) ? $field_visible : 0;
                             // Field Publicly submit?
                             $field_submit = $field->getAttribute('submit_by');
                             $public_submit = (isset($field_submit) and in_array($field_submit, $this->allowable)) ? $field_submit : 0;
                             // Field Default
                             $default = xml::get_node_text($field, 'default');
                             $default_values = $default ? $default : NULL;
                             // Make sure we have default values for Radio buttons, Checkboxes and drop down fields
                             // If not provided, don't import this custom field
                             $default_required = array(5, 6, 7);
                             if (!isset($default_values) and in_array($type, $default_required)) {
                                 $this->notices[] = Kohana::lang('import.xml.field_default') . '"' . $title . '" : Field "' . $name . '"';
                             } else {
                                 // Save the form field
                                 $new_field = new Form_Field_Model();
                                 $new_field->form_id = $this_form;
                                 $new_field->field_name = $name;
                                 $new_field->field_type = $type;
                                 $new_field->field_required = $required;
                                 $new_field->field_default = isset($default_values) ? $default_values : NULL;
                                 $new_field->field_ispublic_visible = $public_visible;
                                 $new_field->field_ispublic_submit = $public_submit;
                                 $new_field->save();
                                 // Add this field to array of existing fields
                                 $this->existing_fields[utf8::strtoupper($name)][$this_form] = $new_field->id;
                                 // Also add it to array of fields added during import
                                 $this->fields_added[] = $new_field->id;
                                 $this->notices[] = Kohana::lang('import.xml.new_field') . '"' . $name . '"';
                                 // Field Options exist?
                                 if ($field->hasAttribute('datatype') or $field->hasAttribute('hidden')) {
                                     // Get current field_id
                                     $fieldid = $this->existing_fields[utf8::strtoupper($name)][$this_form];
                                     if ($field->hasAttribute('datatype')) {
                                         // Does datatype option already exist for this field?
                                         $existing_datatype = ORM::factory('form_field_option')->where('form_field_id', $fieldid)->where('option_name', 'field_datatype')->find_all();
                                         // No, none exists
                                         if (count($existing_datatype) == 0) {
                                             $datatype = xml::get_node_text($field, 'datatype', FALSE);
                                             $allowed_types = array('email', 'phonenumber', 'numeric', 'text');
                                             $field_datatype = ($datatype and in_array($datatype, $allowed_types)) ? $datatype : NULL;
                                             // If field datatype is not null, save
                                             if ($field_datatype != NULL) {
                                                 $datatype_option = new Form_Field_Option_Model();
                                                 $this->_save_field_option($datatype_option, $fieldid, 'field_datatype', $field_datatype);
                                                 // Add to array of field options added during import
                                                 $this->field_options_added[] = $datatype_option->id;
                                                 $this->notices[] = Kohana::lang('import.xml.field_datatype') . '"' . $name . '"';
                                             }
                                         }
                                     }
                                     if ($field->hasAttribute('hidden')) {
                                         // Does hidden option already exist for this field?
                                         $existing_hidden = ORM::factory('form_field_option')->where('form_field_id', $fieldid)->where('option_name', 'field_hidden')->find_all();
                                         // No, none exists
                                         if (count($existing_hidden) == 0) {
                                             $hidden = $field->getAttribute('hidden');
                                             $field_hidden = ($hidden != '' and in_array($hidden, $this->allowable)) ? $hidden : NULL;
                                             // If field datatype is not null, save
                                             if ($field_hidden != NULL) {
                                                 $hidden_option = new Form_Field_Option_Model();
                                                 $this->_save_field_option($hidden_option, $fieldid, 'field_hidden', $field_hidden);
                                                 // Add to array of field options added during import
                                                 $this->field_options_added[] = $hidden_option->id;
                                                 $this->notices[] = Kohana::lang('import.xml.field_hidden') . '"' . $name . '"';
                                             }
                                         }
                                     }
                                     // End field hidden option exists
                                 }
                                 // End field options exist
                             }
                             // End defaults provided
                         }
                         // End field does not exist
                     }
                     // End field name provided
                 }
                 // End individual form field import
             }
             // End if fields exist
         }
         // End form title exists
     }
     // End individual form import
     // If we have errors, return FALSE, else TRUE
     return count($this->errors) === 0;
 }