コード例 #1
0
ファイル: manage.php プロジェクト: kjgarza/ushahidi
 /**
  * Add Edit Reporter Levels
  */
 public function levels()
 {
     $this->template->content = new View('admin/levels');
     $this->template->content->title = Kohana::lang('ui_admin.reporter_levels');
     // setup and initialize form field names
     $form = array('level_id' => '', 'level_title' => '', 'level_description' => '', 'level_weight' => '');
     // 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 = "";
     // Check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Level_Model instance for the opertation
         $level = (isset($_POST['level_id']) and Level_Model::is_valid_level($_POST['level_id'])) ? new Level_Model($_POST['level_id']) : new Level_Model();
         if ($_POST['action'] == 'a') {
             // Manually extract the data to be validated
             $data = arr::extract($_POST, 'level_title', 'level_description', 'level_weight');
             if ($level->validate($data)) {
                 $level->save();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
             } else {
                 // Repopulate the form fields
                 $form = arr::overwrite($form, $data->as_array());
                 // Ropulate the error fields, if any
                 $errors = arr::overwrite($errors, $data->errors('level'));
                 $form_error = TRUE;
             }
         } elseif ($_POST['action'] == 'd') {
             if ($level->loaded) {
                 // Levels are tied to reporters, therefore nullify
                 // @todo Reporter_Model::delink_level($level_id)
                 $level->delete();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             }
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('level')->count_all()));
     $levels = ORM::factory('level')->orderby('level_weight', 'asc')->find_all($this->items_per_page, $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $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->levels = $levels;
 }