Пример #1
0
 /**
  * 删除联系人分组
  * @param int $id 分组ID
  */
 public function destroy($id = NULL)
 {
     $id = (int) $id;
     if ($this->get_method() != 'POST') {
         $this->send_response(405, NULL, Kohana::lang('contact.method_not_exist'));
     } elseif (empty($id)) {
         $this->send_response(400, NULL, Kohana::lang('contact.group_id_empty'));
     } elseif ($this->model->get_category_name($this->user_id, $id)) {
         $status = $this->model->delete($this->user_id, $id);
         if ($status == FALSE) {
             $this->send_response(404, NULL, Kohana::lang('contact.group_not_exist'));
         } else {
             $this->send_response(200, NULL, '', FALSE);
         }
     } else {
         $this->send_response(404, NULL, Kohana::lang('contact.group_not_exist'));
     }
 }
Пример #2
0
 function index()
 {
     $this->template->content = new View('admin/categories');
     $this->template->content->title = Kohana::lang('ui_admin.categories');
     // setup and initialize form field names
     $form = array('action' => '', 'category_id' => '', 'parent_id' => '', 'category_title' => '', 'category_description' => '', 'category_color' => '', 'category_image' => '');
     // 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 = "";
     $parents_array = array();
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  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('parent_id', 'required', 'numeric');
             $post->add_rules('category_title', 'required', 'length[3,80]');
             $post->add_rules('category_description', 'required');
             $post->add_rules('category_color', 'required', 'length[6,6]');
             $post->add_rules('category_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]');
             $post->add_callbacks('parent_id', array($this, 'parent_id_chk'));
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $category_id = $post->category_id;
             $category = new Category_Model($category_id);
             if ($post->action == 'd') {
                 // Delete Action
                 $category->delete($category_id);
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             } else {
                 if ($post->action == 'v') {
                     // Show/Hide Action
                     if ($category->loaded == true) {
                         if ($category->category_visible == 1) {
                             $category->category_visible = 0;
                         } else {
                             $category->category_visible = 1;
                         }
                         $category->save();
                         $form_saved = TRUE;
                         $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                     }
                 } else {
                     if ($post->action == 'i') {
                         // Delete Image/Icon Action
                         if ($category->loaded == true) {
                             $category_image = $category->category_image;
                             if (!empty($category_image) && file_exists(Kohana::config('upload.directory', TRUE) . $category_image)) {
                                 unlink(Kohana::config('upload.directory', TRUE) . $category_image);
                             }
                             $category->category_image = null;
                             $category->save();
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                         }
                     } else {
                         if ($post->action == 'a') {
                             // Save Action
                             $category->parent_id = $post->parent_id;
                             $category->category_title = $post->category_title;
                             $category->category_description = $post->category_description;
                             $category->category_color = $post->category_color;
                             $category->save();
                             // Upload Image/Icon
                             $filename = upload::save('category_image');
                             if ($filename) {
                                 $new_filename = "category_" . $category->id . "_" . time();
                                 // Resize Image to 32px if greater
                                 Image::factory($filename)->resize(32, 32, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".png");
                                 // Remove the temporary file
                                 unlink($filename);
                                 // Delete Old Image
                                 $category_old_image = $category->category_image;
                                 if (!empty($category_old_image) && file_exists(Kohana::config('upload.directory', TRUE) . $category_old_image)) {
                                     unlink(Kohana::config('upload.directory', TRUE) . $category_old_image);
                                 }
                                 // Save
                                 $category->category_image = $new_filename . ".png";
                                 $category->save();
                             }
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
                         }
                     }
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('category'));
             $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('category')->where('parent_id', '0')->count_all()));
     $categories = ORM::factory('category')->where('parent_id', '0')->orderby('category_title', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $parents_array = ORM::factory('category')->where('parent_id', '0')->select_list('id', 'category_title');
     // add none to the list
     $parents_array[0] = "--- Top Level Category ---";
     $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->categories = $categories;
     $this->template->content->parents_array = $parents_array;
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/categories_js');
 }
 /**
  * Delete existing category
  *
  * @param string response_type - XML or JSON
  *
  * @return string
  */
 private function _del_category()
 {
     // setup and initialize form field names
     $form = array('category_id' => '');
     // copy the form as errors, so the errors will be stored
     //with keys corresponding to the form field names
     $errors = $form;
     $ret_value = 0;
     // Check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't
         //overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  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('category_id', 'required', 'numeric');
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $category_id = $post->category_id;
             $category = new Category_Model($category_id);
             $category->delete($category_id);
         } else {
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('category'));
             foreach ($errors as $error_item => $error_description) {
                 if (!is_array($error_description)) {
                     $this->error_messages .= $error_description;
                     if ($error_description != end($errors)) {
                         $this->error_messages .= " - ";
                     }
                 }
             }
             $ret_value = 1;
             // validation error
         }
     } else {
         $ret_value = 3;
     }
     // Set the response data
     $this->response_data = $this->response($ret_value, $this->error_messages);
 }
Пример #4
0
 function index()
 {
     $this->template->content = new View('admin/categories');
     $this->template->content->title = 'Categories';
     // setup and initialize form field names
     $form = array('action' => '', 'locale' => '', 'category_id' => '', 'category_title' => '', 'category_description' => '', 'category_color' => '');
     //  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) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $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('locale', 'required', 'alpha_dash', 'length[5]');
             $post->add_rules('category_title', 'required', 'length[3,80]');
             $post->add_rules('category_description', 'required');
             $post->add_rules('category_color', 'required', 'length[6,6]');
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $category_id = $post->category_id;
             $category = new Category_Model($category_id);
             if ($post->action == 'd') {
                 $category->delete($category_id);
                 $form_saved = TRUE;
                 $form_action = "DELETED";
             } else {
                 if ($post->action == 'v') {
                     if ($category->loaded == true) {
                         if ($category->category_visible == 1) {
                             $category->category_visible = 0;
                         } else {
                             $category->category_visible = 1;
                         }
                         $category->save();
                         $form_saved = TRUE;
                         $form_action = "MODIFIED";
                     }
                 } else {
                     if ($post->action == 'a') {
                         // SAVE Category
                         $category->locale = $post->locale;
                         $category->category_title = $post->category_title;
                         $category->category_description = $post->category_description;
                         $category->category_color = $post->category_color;
                         $category->save();
                         $form_saved = TRUE;
                         $form_action = "ADDED/EDITED";
                     }
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('category'));
             $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('category')->count_all()));
     $categories = ORM::factory('category')->orderby('category_title', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $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->categories = $categories;
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/categories_js');
 }