Example #1
0
 public function index()
 {
     $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array());
     try {
         //* 收集请求数据 ==根据业务逻辑定制== */
         $request_data = $this->input->get();
         //* 初始化返回数据 */
         $return_data = array('assoc' => NULL, 'count' => 0);
         $search_value = isset($request_data['keyword']) ? $request_data['keyword'] : array();
         $category_list = array();
         $brand_list = array();
         $product_ids = array();
         $relation_product_ids = array();
         $product_id = $request_data['product_id'];
         $product_service = ProductService::get_instance();
         $product = $product_service->get($product_id);
         $product_id = $product['id'];
         if (!$product_id > 0) {
             throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400);
         } else {
             $struct = product::get_struct($request_data);
             $query_struct_current = $struct['query'];
             $request_struct_current = $struct['request'];
             if ($product['type'] == ProductService::PRODUCT_TYPE_CONFIGURABLE || $product['type'] == ProductService::PRODUCT_TYPE_ASSEMBLY) {
                 $product_id_array = Product_assemblyService::get_instance()->get_good_id_array($product_id);
                 $product_id_array[] = $product_id;
             } else {
                 $product_id_array = array($product_id);
             }
             $query_struct_current['not_in']['id'] = $product_id_array;
             try {
                 $return_data = BLL_Product::index($query_struct_current);
                 if (!empty($return_data['assoc'])) {
                     // 获取该商品列表中涉及到的分类以及品牌ID
                     $category_ids = array();
                     $brand_ids = array();
                     foreach ($return_data['assoc'] as $product) {
                         if ($product['category_id'] != 0) {
                             $category_ids[$product['category_id']] = TRUE;
                         }
                         if ($product['brand_id'] != 0) {
                             $brand_ids[$product['brand_id']] = TRUE;
                         }
                         $product_ids[] = $product['id'];
                     }
                     $query_struct = array('where' => array('product_id' => $product_id));
                     foreach (Product_relationService::get_instance()->query_assoc($query_struct) as $relation) {
                         if (in_array($relation['relation_product_id'], $product_ids)) {
                             $relation_product_ids[] = $relation['relation_product_id'];
                         }
                     }
                     // 获取分类列表
                     if (!empty($category_ids)) {
                         $query_struct = array('where' => array('id' => array_keys($category_ids)));
                         $categorys = CategoryService::get_instance()->query_assoc($query_struct);
                         foreach ($categorys as $category) {
                             $category_list[$category['id']] = $category['title_manage'] ? $category['title_manage'] : $category['title'];
                         }
                     }
                     // 获取品牌列表
                     if (!empty($brand_ids)) {
                         $query_struct = array('where' => array('id' => array_keys($brand_ids)));
                         $brands = BrandService::get_instance()->query_assoc($query_struct);
                         foreach ($brands as $brand) {
                             $brand_list[$brand['id']] = $brand['name'];
                         }
                     }
                 }
                 // 模板输出 分页
                 $this->pagination = new Pagination(array('total_items' => $return_data['count'], 'items_per_page' => $query_struct_current['limit']['per_page']));
             } catch (MyRuntimeException $ex) {
                 //* ==根据业务逻辑定制== */
                 //FIXME 根据service层的异常做一些对应处理并抛出用户友好的异常Message
                 throw $ex;
             }
         }
         //* 如果是ajax请求缩减返回的字段 ==根据业务逻辑定制== */
         if ($this->is_ajax_request()) {
             $requestkeys = array('id', 'category_id', 'title', 'uri_name', 'store', 'on_sale', 'price', 'sku');
             array_walk($return_data['assoc'], 'util::simplify_return_array', $requestkeys);
         }
         //* 补充&修改返回结构体 */
         $return_struct['status'] = 1;
         $return_struct['code'] = 200;
         $return_struct['msg'] = '';
         $return_struct['content'] = $return_data;
         //exit("<div id=\"do_debug\" style=\"clear:both;display:;\"><pre>\n".var_export($return_struct,TRUE)."\n</pre></div>");
         //* 请求类型 */
         if ($this->is_ajax_request()) {
             // ajax 请求
             // json 输出
             $this->template->content = $return_struct;
         } else {
             //* html 输出 ==根据业务逻辑定制== */
             $this->template = new View('layout/commonfix_html');
             //* 模板输出 */
             $this->template->return_struct = $return_struct;
             $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__);
             //* 变量绑定 */
             $this->template->title = Kohana::config('site.name');
             $this->template->content = $content;
             //* 请求结构数据绑定 */
             $this->template->content->request_data = $request_data;
             $this->template->content->request_struct = $request_struct_current;
             //* 返回结构体绑定 */
             $this->template->content->return_struct = $return_struct;
             //:: 当前应用专用数据
             $this->template->content->title = Kohana::config('site.name');
             //$this->template->content->site_list     = Mysite::instance()->select_list($site_ids);
             $this->template->content->category_list = $category_list;
             $this->template->content->brand_list = $brand_list;
             $this->template->content->product_id = $product_id;
             $this->template->content->product_ids = $product_ids;
             $this->template->content->relation_ids = $relation_product_ids;
             $this->template->content->keyword = $search_value;
         }
         // end of request type determine
     } catch (MyRuntimeException $ex) {
         $this->template = new View('layout/commonfix_html');
         $this->_ex($ex, $return_struct, $request_data);
     }
 }
Example #2
0
 /**
  * 通过商品ID设置货品的上下架状态
  *
  * @param integer $product_id
  * @param integer $on_sale
  * @return boolean
  */
 public static function set_on_sale($product_id, $on_sale)
 {
     $goods = Product_assemblyService::get_instance()->index(array('where' => array('assembly_id' => $product_id)));
     f($goods, 1);
     if (!empty($goods)) {
         foreach ($goods as $good) {
             $data['on_sale'] = $on_sale;
             $data['update_time'] = time();
             ProductService::get_instance()->set($good['product_id'], $data);
         }
     }
     return TRUE;
 }
Example #3
0
 /**
  * 删除基本商品类型相关数据
  *
  * @param array $product
  * @return boolean
  */
 public static function delete($product_id)
 {
     $binds = Product_assemblyService::get_instance()->index(array('where' => array('product_id' => $product_id)));
     if (!empty($binds)) {
         foreach ($binds as $bind) {
             if (isset($bind['assembly_id']) && $bind['assembly_id'] > 0) {
                 $assembly_id = $bind['assembly_id'];
                 $pd = ProductService::get_instance()->get($assembly_id);
                 $pd_name = $pd['title'] . ($pd['name_manage'] ? '(' . $pd['name_manage'] . ')' : '');
                 throw new MyRuntimeException('该简单商品被捆绑在商品ID' . $assembly_id . ':' . $pd_name . '。请清除绑定后再进行删除!', 403);
             }
         }
         ORM::factory('product_assembly')->where('product_id', $product_id)->delete_all();
     }
     ORM::factory('product_attributeoption_relation')->where('product_id', $product_id)->delete_all();
     return TRUE;
 }
Example #4
0
 /**
  * 设置商品上下架
  *
  * @param integer $product_id
  * @param integer $on_sale
  * @return boolean
  */
 public static function set_on_sale($product_id, $on_sale)
 {
     $product = self::get($product_id);
     if (!$product['id'] > 0) {
         return false;
     }
     ProductService::get_instance()->set($product_id, array('on_sale' => $on_sale, 'update_time' => time()));
     if (isset($product['type']) && $product['type'] != ProductService::PRODUCT_TYPE_GOODS) {
         $goods = Product_assemblyService::get_instance()->index(array('where' => array('assembly_id' => $product_id)));
         if (!empty($goods)) {
             foreach ($goods as $good) {
                 $data['on_sale'] = $on_sale;
                 $data['update_time'] = time();
                 ProductService::get_instance()->set($good['product_id'], $data);
             }
         }
     }
     return TRUE;
 }