Example #1
0
 public function post_create()
 {
     $val = Validation::forge();
     $val->add_callable('MyRules');
     $val->add_field('category_name', Lang::get('label.category_name'), 'required|max_length[255]');
     $val->add_field('id', Lang::get('label.category'), 'trim|valid_numeric|valid_category');
     $val->add_field('parent_category_id', Lang::get('label.parent_category'), 'trim|valid_numeric|valid_category');
     if ($val->run()) {
         DB::start_transaction();
         $category_id = $val->validated('id');
         $parent_category_id = (int) $val->validated('parent_category_id');
         $category_props = array('category_name' => Model_Service_Util::mb_trim($val->validated('category_name')), 'parent_category_id' => $parent_category_id, 'level' => !empty($parent_category_id) ? Model_Category::find($parent_category_id)->level + 1 : 1);
         $create_id = !empty($category_id) ? Model_Base_Category::update($category_id, $category_props) : Model_Base_Category::insert($category_props);
         if ($create_id) {
             $type = !empty($category_id) ? 'update' : 'new';
             $category_id = !empty($category_id) ? $category_id : $create_id;
             $photo_props = array('type' => $type, 'category_id' => $category_id);
             if ($type === 'new' || Input::file('category_photo')) {
                 $upload = Model_Service_Upload::run('category', $photo_props);
             }
             if (empty($upload['error'])) {
                 DB::commit_transaction();
                 $this->data['category'] = array('type' => $type, 'id' => $category_id, 'parent_category_id' => $parent_category_id, 'category_name' => $val->validated('category_name'), 'category_name_display' => Model_Base_Category::get_parent($category_id), 'category_photo_display' => empty($upload['photo_name']) ?: $upload['photo_name'], 'no_image' => _PATH_NO_IMAGE_);
                 $this->data['success'] = true;
                 $this->data['msg'] = Lang::get($this->controller . '.' . $this->action . '.success');
             } else {
                 DB::rollback_transaction();
                 $this->data['msg'] = $upload['error'];
             }
         } else {
             DB::rollback_transaction();
             $this->data['msg'] = Lang::get($this->controller . '.' . $this->action . '.error');
         }
     } else {
         $this->data['errors'] = $val->error_message();
     }
     return $this->response($this->data);
 }