Exemplo n.º 1
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. Uses default form (1), if none selected
  * @param bool $field_names_only Whether or not to include just fields names, or field names + data
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = 1, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     if (!$form_id) {
         $form_id = 1;
     }
     // Validation
     if (!Form_Model::is_valid_form($form_id)) {
         return $fields_array;
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     //NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Get the predicates for the public state
     $public_state = $action == "view" ? '<=' . $user_level : ' <= ' . $user_level;
     // Query to fetch the form fields and their responses
     $sql = "SELECT ff.*, '' AS form_response FROM " . $table_prefix . "form_field ff WHERE 1=1 ";
     // Check if the provided incident exists
     if (Incident_Model::is_valid_incident($incident_id)) {
         // Overwrite the previous query
         $sql = "SELECT ff.*, fr.form_response " . "FROM " . $table_prefix . "form_field ff " . "RIGHT JOIN " . $table_prefix . "form_response fr ON (fr.form_field_id = ff.id) " . "WHERE fr.incident_id = " . $incident_id . " ";
     }
     $sql .= "AND ff.form_id = " . $form_id . " " . "AND ff.field_ispublic_visible " . $public_state . " " . "ORDER BY ff.field_position ASC";
     // Execute the SQL to fetch the custom form fields
     $form_fields = Database::instance()->query($sql);
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
         } else {
             // Return Field Structure
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemplo n.º 2
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. If none selected, retrieve custom form fields from ALL custom forms
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = NULL, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     // If we have a form id but its invalid, return empty
     if (!empty($form_id) and !Form_Model::is_valid_form($form_id)) {
         return $fields_array;
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     // Get field we'll check permissions against
     $ispublic_field = $action == "view" ? 'field_ispublic_visible' : 'field_ispublic_submit';
     // NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Check if incident is valid
     // Have to do this early since we can't build 2 ORM queries at once.
     $valid_incident = Incident_Model::is_valid_incident($incident_id, FALSE);
     // Check if the provided incident exists, then fill in the data
     if ($valid_incident) {
         $sql = "SELECT ff.*, fr.form_response\n\t\t\tFROM `{$table_prefix}form_field` ff\n\t\t\tLEFT JOIN `{$table_prefix}roles` r ON (r.id = {$ispublic_field})\n\t\t\tLEFT JOIN\n\t\t\t\t`{$table_prefix}form_response` fr ON (\n\t\t\t\t\tfr.form_field_id = ff.id AND\n\t\t\t\t\tfr.incident_id = :incident_id\n\t\t\t\t)\n\t\t\tWHERE (access_level <= :user_level OR access_level IS NULL) " . (!empty($form_id) ? "AND form_id = :form_id " : '') . "ORDER BY field_position ASC";
     } else {
         $sql = "SELECT ff.*\n\t\t\tFROM `{$table_prefix}form_field` ff\n\t\t\tLEFT JOIN `{$table_prefix}roles` r ON (r.id = {$ispublic_field})\n\t\t\tWHERE (access_level <= :user_level OR access_level IS NULL) " . (!empty($form_id) ? "AND form_id = :form_id " : '') . "ORDER BY field_position ASC";
     }
     $form_fields = Database::instance()->query($sql, array(':form_id' => $form_id, ':user_level' => $user_level, ':incident_id' => $incident_id));
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = isset($custom_formfield->form_response) ? $custom_formfield->form_response : '';
         } else {
             // Return Field Structure
             // JP: added field description
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'form_id' => $custom_formfield->form_id, 'field_name' => $custom_formfield->field_name, 'field_description' => $custom_formfield->field_description, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => isset($custom_formfield->form_response) ? $custom_formfield->form_response : '');
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemplo n.º 3
0
 /**
  * Lists the forms.
  */
 public function index()
 {
     $this->template->content = new View('admin/forms');
     // setup and initialize form field names
     $form = array('action' => '', 'form_id' => '', 'form_title' => '', 'form_description' => '', 'form_active' => '', 'field_type' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $form_id = "";
     if ($_POST) {
         $post = Validation::factory($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         if ($post->action == 'a') {
             // Add some rules, the input field, followed by a list of checks, carried out in order
             $post->add_rules('form_title', 'required', 'length[3,100]');
             $post->add_rules('form_description', 'required');
         } elseif ($post->action == 'd') {
             if ($_POST['form_id'] == 1) {
                 // Default Form Cannot Be Deleted
                 $post->add_error('form_id', 'default');
             }
         }
         if ($post->validate()) {
             $form_id = $post->form_id;
             $custom_form = new Form_Model($form_id);
             if ($post->action == 'd') {
                 // Delete Action
                 $custom_form->delete($form_id);
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             } else {
                 if ($post->action == 'h') {
                     // Active/Inactive Action
                     if ($custom_form->loaded == true) {
                         if ($custom_form->form_active == 1) {
                             $custom_form->form_active = 0;
                         } else {
                             $custom_form->form_active = 1;
                         }
                         $custom_form->save();
                         $form_saved = TRUE;
                         $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                     }
                 } else {
                     // Save Action
                     $custom_form->form_title = $post->form_title;
                     $custom_form->form_description = $post->form_description;
                     $custom_form->save();
                     $form_saved = TRUE;
                     $form_action = strtoupper(Kohana::lang('ui_admin.created_edited'));
                 }
             }
             // Empty $form array
             array_fill_keys($form, '');
         } 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'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('form')->count_all()));
     $forms = ORM::factory('form')->orderby('id', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     // Form Field Types
     $form_field_types = array('' => Kohana::lang('ui_admin.select_field_type'), 1 => Kohana::lang('ui_admin.text_field'), 2 => Kohana::lang('ui_admin.free_text_field'));
     $this->template->content->form = $form;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->forms = $forms;
     $this->template->content->form_field_types = $form_field_types;
     $this->template->content->errors = $errors;
     // Javascript Header
     $this->template->js = new View('admin/forms_js');
     $this->template->js->form_id = $form_id;
     $this->template->form_error = $form_error;
 }
Exemplo n.º 4
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. If none selected, retrieve custom form fields from ALL custom forms
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = NULL, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     if ($form_id != null and $form_id != '') {
         // Validation
         if (!Form_Model::is_valid_form($form_id)) {
             return $fields_array;
         }
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     // Get field we'll check permissions against
     $ispublic_field = $action == "view" ? 'field_ispublic_visible' : 'field_ispublic_submit';
     // Query to fetch the form fields associated with the given form id
     $sql = "SELECT ff.*, '' AS form_response FROM " . $table_prefix . "form_field ff LEFT JOIN roles r ON (r.id = {$ispublic_field}) WHERE 1=1 ";
     if ($form_id != null and $form_id != '') {
         $sql .= "AND ff.form_id = " . $form_id . " ";
     }
     // NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Check access_level
     $sql .= 'AND (r.access_level <= ' . $user_level . ' OR r.access_level IS NULL)';
     $sql .= " ORDER BY ff.field_position ASC";
     // Execute the SQL to fetch the custom form fields
     $form_fields = Database::instance()->query($sql);
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
         } else {
             // Return Field Structure
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
         }
     }
     // Garbage collection
     unset($form_fields);
     // Check if the provided incident exists, then fill in the data
     if (Incident_Model::is_valid_incident($incident_id)) {
         // Overwrite the previous query
         $sql = "SELECT ff.*, fr.form_response " . "FROM " . $table_prefix . "form_field ff " . "RIGHT JOIN " . $table_prefix . "form_response fr ON (fr.form_field_id = ff.id) " . "LEFT JOIN roles r ON (r.id = {$ispublic_field})" . "WHERE fr.incident_id = " . $incident_id . " ";
         if ($form_id != null and $form_id != '') {
             $sql .= "AND ff.form_id = " . $form_id . " ";
         }
         $sql .= 'AND (r.access_level <= ' . $user_level . ' OR r.access_level IS NULL)';
         $sql .= " ORDER BY ff.field_position ASC";
         // Execute the SQL to fetch the custom form fields
         $form_fields = Database::instance()->query($sql);
         foreach ($form_fields as $custom_formfield) {
             if ($data_only) {
                 // Return Data Only
                 $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
             } else {
                 // Return Field Structure
                 $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
             }
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemplo n.º 5
0
 /**
  * Lists the forms.
  */
 public function index()
 {
     $this->template->content = new View('admin/manage/forms/main');
     // Setup and initialize form field names
     $form = array('action' => '', 'form_id' => '', 'form_title' => '', 'form_description' => '', 'form_active' => '', 'field_type' => '');
     // Copy the form as errors, so the errors will be stored with keys
     // corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $form_id = "";
     if ($_POST) {
         $post = Validation::factory($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         if ($post->action == 'a') {
             // Add some rules, the input field, followed by a list of checks, carried out in order
             $post->add_rules('form_title', 'required', 'length[1,1000]');
             $post->add_rules('form_description', 'required');
         } elseif ($post->action == 'd') {
             if ($_POST['form_id'] == 1) {
                 // Default Form Cannot Be Deleted
                 $post->add_error('form_id', 'default');
             }
         }
         if ($post->validate()) {
             $form_id = $post->form_id;
             $custom_form = new Form_Model($form_id);
             if ($post->action == 'd') {
                 // Delete Action
                 $custom_form->delete($form_id);
                 $form_saved = TRUE;
                 $form_action = utf8::strtoupper(Kohana::lang('ui_admin.deleted'));
             } elseif ($post->action == 'h') {
                 // Active/Inactive Action
                 if ($custom_form->loaded) {
                     // @todo Doesn't make sense, find out what the logic for this is
                     // Customary values for active and inactive are 1 and 0 respectively
                     $custom_form->form_active = $custom_form->form_active == 1 ? 0 : 1;
                     $custom_form->save();
                     $form_saved = TRUE;
                     $form_action = utf8::strtoupper(Kohana::lang('ui_admin.modified'));
                 }
             } else {
                 // Save Action
                 $custom_form->form_title = $post->form_title;
                 $custom_form->form_description = $post->form_description;
                 $custom_form->save();
                 $form_saved = TRUE;
                 $form_action = utf8::strtoupper(Kohana::lang('ui_admin.created_edited'));
             }
             // Empty $form array
             array_fill_keys($form, '');
         } 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'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('form')->count_all()));
     $forms = ORM::factory('form')->orderby('id', 'asc')->find_all($this->items_per_page, $pagination->sql_offset);
     // Form Field Types
     $form_field_types = array('' => Kohana::lang('ui_admin.select_field_type'), 1 => Kohana::lang('ui_admin.text_field'), 2 => Kohana::lang('ui_admin.free_text_field'), 3 => Kohana::lang('ui_admin.date_field'), 5 => Kohana::lang('ui_admin.radio_field'), 6 => Kohana::lang('ui_admin.checkbox_field'), 7 => Kohana::lang('ui_admin.dropdown_field'), 8 => Kohana::lang('ui_admin.divider_start_field'), 9 => Kohana::lang('ui_admin.divider_end_field'));
     $this->template->content->form = $form;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->forms = $forms;
     $this->template->content->form_field_types = $form_field_types;
     $this->template->content->errors = $errors;
     // Javascript Header
     $this->template->js = new View('admin/manage/forms/forms_js');
     $this->template->js->form_id = $form_id;
     $this->template->form_error = $form_error;
 }
Exemplo n.º 6
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;
 }
Exemplo n.º 7
0
 /**
  * Function to import a report form a row in the CSV file
  * @param array $row
  * @return bool
  */
 function import_report($row)
 {
     // If the date is not in proper date format
     if (!strtotime($row['INCIDENT DATE'])) {
         $this->errors[] = Kohana::lang('import.incident_date') . ($this->rownumber + 1) . ': ' . $row['INCIDENT DATE'];
     }
     // If a value of Yes or No is NOT set for approval status for the imported row
     if (isset($row["APPROVED"]) and !in_array(utf8::strtoupper($row["APPROVED"]), array('NO', 'YES'))) {
         $this->errors[] = Kohana::lang('import.csv.approved') . ($this->rownumber + 1);
     }
     // If a value of Yes or No is NOT set for verified status for the imported row
     if (isset($row["VERIFIED"]) and !in_array(utf8::strtoupper($row["VERIFIED"]), array('NO', 'YES'))) {
         $this->errors[] = Kohana::lang('import.csv.verified') . ($this->rownumber + 1);
     }
     if (count($this->errors)) {
         return false;
     }
     // STEP 1: SAVE LOCATION
     if (isset($row['LOCATION'])) {
         $location = new Location_Model();
         $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : '';
         // For Geocoding purposes
         $location_geocoded = map::geocode($location->location_name);
         // If we have LATITUDE and LONGITUDE use those
         if (isset($row['LATITUDE']) and isset($row['LONGITUDE'])) {
             $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : 0;
             $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : 0;
         } else {
             $location->latitude = $location_geocoded ? $location_geocoded['latitude'] : 0;
             $location->longitude = $location_geocoded ? $location_geocoded['longitude'] : 0;
         }
         $location->country_id = $location_geocoded ? $location_geocoded['country_id'] : 0;
         $location->location_date = $this->time;
         $location->save();
         $this->locations_added[] = $location->id;
     }
     // STEP 2: SAVE INCIDENT
     $incident = new Incident_Model();
     $incident->location_id = isset($row['LOCATION']) ? $location->id : 0;
     $incident->user_id = 0;
     $incident->form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1;
     $incident->incident_title = $row['INCIDENT TITLE'];
     $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : '';
     $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE']));
     $incident->incident_dateadd = $this->time;
     $incident->incident_active = (isset($row['APPROVED']) and utf8::strtoupper($row['APPROVED']) == 'YES') ? 1 : 0;
     $incident->incident_verified = (isset($row['VERIFIED']) and utf8::strtoupper($row['VERIFIED']) == 'YES') ? 1 : 0;
     $incident->save();
     $this->incidents_added[] = $incident->id;
     // STEP 3: Save Personal Information
     if (isset($row['FIRST NAME']) or isset($row['LAST NAME']) or isset($row['EMAIL'])) {
         $person = new Incident_Person_Model();
         $person->incident_id = $incident->id;
         $person->person_first = isset($row['FIRST NAME']) ? $row['FIRST NAME'] : '';
         $person->person_last = isset($row['LAST NAME']) ? $row['LAST NAME'] : '';
         $person->person_email = (isset($row['EMAIL']) and valid::email($row['EMAIL'])) ? $row['EMAIL'] : '';
         $person->person_date = date("Y-m-d H:i:s", time());
         // Make sure that you're not importing an empty record i.e at least one field has been recorded
         // If all fields are empty i.e you have an empty record, don't save
         if (!empty($person->person_first) or !empty($person->person_last) or !empty($person->person_email)) {
             $person->save();
             // Add to array of incident persons added
             $this->incident_persons_added[] = $person->id;
         }
     }
     // STEP 4: SAVE CATEGORIES
     // If CATEGORY column exists
     if (isset($row['CATEGORY'])) {
         $categorynames = explode(',', trim($row['CATEGORY']));
         // Trim whitespace from array values
         $categorynames = array_map('trim', $categorynames);
         // Get rid of duplicate category entries in a row
         $categories = array_unique(array_map('strtolower', $categorynames));
         // Add categories to incident
         foreach ($categories as $categoryname) {
             // Convert the first string character of the category name to Uppercase
             $categoryname = utf8::ucfirst($categoryname);
             // For purposes of adding an entry into the incident_category table
             $incident_category = new Incident_Category_Model();
             $incident_category->incident_id = $incident->id;
             // If category name exists, add entry in incident_category table
             if ($categoryname != '') {
                 // Check if the category exists (made sure to convert to uppercase for comparison)
                 if (!isset($this->existing_categories[utf8::strtoupper($categoryname)])) {
                     $this->notices[] = Kohana::lang('import.new_category') . $categoryname;
                     $category = new Category_Model();
                     $category->category_title = $categoryname;
                     // We'll just use black for now. Maybe something random?
                     $category->category_color = '000000';
                     // because all current categories are of type '5'
                     $category->category_visible = 1;
                     $category->category_description = $categoryname;
                     $category->category_position = count($this->existing_categories);
                     $category->save();
                     $this->categories_added[] = $category->id;
                     // Now category_id is known: This time, and for the rest of the import.
                     $this->existing_categories[utf8::strtoupper($categoryname)] = $category->id;
                 }
                 $incident_category->category_id = $this->existing_categories[utf8::strtoupper($categoryname)];
                 $incident_category->save();
                 $this->incident_categories_added[] = $incident_category->id;
             }
         }
     }
     // STEP 5: Save Custom form fields responses
     // Check for form_id
     $form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1;
     // Get custom form fields for this particular form
     $custom_titles = customforms::get_custom_form_fields('', $form_id, false);
     // Do custom form fields exist on this deployment?
     if (!empty($custom_titles)) {
         foreach ($custom_titles as $field_name) {
             // Check if the column exists in the CSV
             $rowname = utf8::strtoupper($field_name['field_name']);
             if (isset($row[$rowname . '-' . $form_id])) {
                 $response = $row[$rowname . '-' . $form_id];
                 // Grab field_id and field_type
                 $field_id = $field_name['field_id'];
                 $field_type = $field_name['field_type'];
                 // Initialize form response model
                 $form_response = new Form_Response_Model();
                 $form_response->incident_id = $incident->id;
                 $form_response->form_field_id = $field_id;
                 // If form response exists
                 if ($response != '') {
                     /* Handling case sensitivity issues with custom form field upload */
                     // Check if the field is a radio button, checkbox OR dropdown field
                     if ($field_type == '5' or $field_type == '6' or $field_type == '7') {
                         // Get field option values
                         $field_values = $field_name['field_default'];
                         // Split field options into individual values
                         $options = explode(",", $field_values);
                         // Since radio button and dropdown fields take single responses
                         if ($field_type == '5' or $field_type == '7') {
                             foreach ($options as $option) {
                                 // Carry out a case insensitive comparison between individual field options and csv response
                                 // If there's a match, store field option value from the db
                                 if (strcasecmp($option, $response) == 0) {
                                     $form_response->form_response = $option;
                                 }
                             }
                         }
                         // For checkboxes, which accomodate multiple responses
                         if ($field_type == '6') {
                             // Split user responses into single values
                             $csvresponses = explode(",", $response);
                             $values = array();
                             foreach ($options as $option) {
                                 foreach ($csvresponses as $csvresponse) {
                                     // Carry out a case insensitive comparison between individual field options and csv response
                                     // If there's a match
                                     if (strcasecmp($option, $csvresponse) == 0) {
                                         // Store field option value from the db
                                         $values[] = $option;
                                     }
                                 }
                             }
                             // Concatenate checkbox values into a string, separated by a comma
                             $form_response->form_response = implode(",", $values);
                         }
                     } else {
                         $form_response->form_response = $response;
                     }
                     // If form_response is provided based on conditions set above, Save the form response
                     if ($form_response->form_response != '') {
                         $form_response->save();
                         // Add to array of field responses added
                         $this->incident_responses_added[] = $form_response->id;
                     }
                 }
             }
         }
     }
     return true;
 }
Exemplo n.º 8
0
 /**
  * JP: Edit & Save Advanced Form Field
  */
 public function advanced_field_edit()
 {
     $this->template = "";
     $this->auto_render = FALSE;
     // setup and initialize form field names
     $form = array('report_title_name' => '', 'description_name' => '', 'description_active' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $advanced_edit_status = "";
     $advanced_edit_response = "";
     if ($_POST) {
         // @todo Manually extract the data to be validated
         $form_data = arr::extract($_POST, 'advanced_form_id', 'advanced_form_title', 'advanced_form_description', 'advanced_form_active', 'report_title_name', 'description_name', 'description_active');
         // Form Model instance
         $custom_form = Form_Model::is_valid_form($_POST['advanced_form_id']) ? ORM::factory('form', $_POST['advanced_form_id']) : new Form_Model();
         // Validate the form data
         if ($custom_form->validate(Validation::factory($form_data))) {
             // Validation succeeded, proceed...
             // Save the new or modified entries
             // JP: The Report Title and Description fields are saved as null in the database if they match the default names or are empty.
             if (strcmp(trim($form_data['report_title_name']), Kohana::lang('ui_main.reports_title')) === 0 or empty(trim($form_data['report_title_name']))) {
                 $custom_form->report_title_name = null;
             } else {
                 $custom_form->report_title_name = trim($form_data['report_title_name']);
             }
             if (strcmp(trim($form_data['description_name']), Kohana::lang('ui_main.reports_description')) === 0 or empty(trim($form_data['description_name']))) {
                 $custom_form->description_name = null;
             } else {
                 $custom_form->description_name = trim($form_data['description_name']);
             }
             $custom_form->description_active = $form_data['description_active'];
             $custom_form->save();
             $advanced_edit_status = "success";
             $advanced_edit_response = rawurlencode(customforms::get_current_fields($custom_form->id, $this->user));
         } else {
             // Repopulate the form fields
             $form = arr::overwrite($form, $form_data->as_array());
             // Populate the error fields, if any
             $errors = arr::overwrite($errors, $form_data->errors('form'));
             // populate the response to this post request
             $advanced_edit_status = "error";
             $advanced_edit_response = "";
             $advanced_edit_response .= "<ul>";
             foreach ($errors as $error_item => $error_description) {
                 $advanced_edit_response .= !$error_description ? '' : "<li>" . $error_description . "</li>";
             }
             $advanced_edit_response .= "</ul>";
         }
     }
     echo json_encode(array("status" => $advanced_edit_status, "response" => $advanced_edit_response));
 }