Exemple #1
0
 public function put()
 {
     role::check('product_category_add');
     $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array());
     try {
         //* 初始化返回数据 */
         $return_data = array();
         //* 收集请求数据 ==根据业务逻辑定制== */
         $request_data = $this->input->post();
         $request_data = trims::run($request_data);
         //echo "<pre>";print_r($request_data);die();
         //标签过滤
         tool::filter_strip_tags($request_data, array('description'));
         //数据验证
         $validResult = Validation::factory($request_data)->pre_filter('trim')->add_rules('pid', 'required', 'numeric')->add_rules('title', 'required', 'length[1,100]')->add_rules('title_manage', 'length[1,255]')->add_rules('uri_name', 'length[1,255]')->add_rules('description', 'length[0,1024]')->add_rules('memo', 'length[0,65535]')->add_rules('meta_title', 'length[0,255]')->add_rules('meta_keywords', 'length[0,255]')->add_rules('meta_description', 'length[0,65535]')->add_rules('is_show', 'numeric');
         if ($validResult->validate() == FALSE) {
             //* 输出错误的具体信息 ==根据业务逻辑定制== */
             $return_struct['content']['errors'] = $validResult->errors();
             throw new MyRuntimeException(Kohana::lang('o_global.input_error'), 400);
         }
         // 调用底层服务
         $category_service = CategoryService::get_instance();
         //判断title是否存在
         if ($category_service->check_exist_title($request_data['title'])) {
             throw new MyRuntimeException(Kohana::lang('o_product.category_title_has_exists'), 409);
         }
         //生成uri_name
         $request_data['uri_name'] = tool::create_uri_name($request_data['uri_name']);
         //uri_name重复判断
         if (!empty($request_data['uri_name'])) {
             while ($category_service->check_exist_uri_name($request_data['uri_name'])) {
                 $request_data['uri_name'] .= '-' . substr(uniqid(), -6);
             }
         }
         //执行添加
         $set_data = $request_data;
         $set_data['is_show'] = $request_data['is_show'];
         $set_data['pid'] = $request_data['pid'];
         $set_data['classify_id'] = $request_data['classify_id'];
         $set_data['title'] = $request_data['title'];
         $set_data['title_manage'] = $request_data['title_manage'];
         $set_data['pic_attach_id'] = $request_data['pic_attach_id'];
         $set_data['meta_title'] = $request_data['meta_title'];
         $set_data['meta_keywords'] = $request_data['meta_keywords'];
         $set_data['meta_description'] = $request_data['meta_description'];
         $set_data['description'] = $request_data['description'];
         $set_data['memo'] = $request_data['memo'];
         $set_data['create_timestamp'] = time();
         $set_data['update_timestamp'] = time();
         $return_data['id'] = $category_service->add($set_data);
         if ($set_data['pid']) {
             $category_service->update_categories();
         }
         if (!$return_data['id']) {
             throw new MyRuntimeException('Internal Error', 500);
         }
         //* 补充&修改返回结构体 */
         $return_struct['status'] = 1;
         $return_struct['code'] = 200;
         $return_struct['msg'] = '添加成功';
         $return_struct['content'] = $return_data;
         $return_struct['action'] = array('type' => 'location', 'url' => url::base() . 'product/' . $this->class_name . '/' . 'index');
         //* 请求类型 */
         if ($this->is_ajax_request()) {
             // ajax 请求
             // json 输出
             $this->template->content = $return_struct;
         } else {
             // html 输出
             $this->template->return_struct = $return_struct;
             $content = new View('info');
             $this->template->content = $content;
             //* 请求结构数据绑定 */
             $this->template->content->request_data = $request_data;
             //* 返回结构体绑定 */
             $this->template->content->return_struct = $return_struct;
         }
         // end of request type determine
     } catch (MyRuntimeException $ex) {
         $this->_ex(&$ex, $return_struct, $request_data);
     }
 }
Exemple #2
0
 /**
  * 创建商品 URI NAME
  *
  * @param array $product
  * @param array $eliminate
  * @return string
  */
 public static function crt_uri_name($product, $eliminate = array())
 {
     if (!is_array($eliminate)) {
         $eliminate = array();
     } else {
         $eliminate = array_flip($eliminate);
     }
     $uri_name = strtolower(tool::create_uri_name($product['sku']));
     if (!array_key_exists($uri_name, $eliminate) and !self::uri_name_exists($uri_name, empty($product['id']) ? 0 : $product['id'])) {
         return $uri_name;
     } else {
         if (!empty($product['sku'])) {
             return $uri_name . '-' . strtolower(tool::create_uri_name($product['sku']));
         } else {
             return $uri_name . '-' . strtolower(tool::create_uri_name(uniqid()));
         }
     }
 }