/** * 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; }
/** * 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; }
/** * 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; }