/** * 设置商品的SEO信息 * * @param array $product * @return boolean */ public static function set(&$product) { $category = CategoryService::get_instance()->get($product['category_id']); $seo_inf = Seo_manageService::get_instance()->get_product_seo_struct(array('category_id' => $product['category_id'], 'product_name' => $product['title'], 'category_name' => $category['title'], 'goods_price' => $product['goods_price'])); empty($product['meta_title']) and $product['meta_title'] = $seo_inf['meta_title']; empty($product['meta_keywords']) and $product['meta_keywords'] = $seo_inf['meta_keywords']; empty($product['meta_description']) and $product['meta_description'] = $seo_inf['meta_description']; return TRUE; }
/** * 获取前台分类链接 * * @param int id 分类id * @return string 分类链接 */ public static function permalink($id, $absolute = true) { $category_service = CategoryService::get_instance(); $category = $category_service->get($id); $route = Myroute::instance()->get(); $route_type = $route['type']; $category_route = $route['category']; $category_suffix = $route['category_suffix']; $domain = Mysite::instance()->get('domain'); if ($route_type == 0) { // 0: none get category and product with id $category_permalink = $category_route . '/' . $category['id']; } if ($route_type == 1) { // 1: get product with {product}/permalink $category_permalink = $category_route . '/' . urlencode($category['uri_name']); } if ($route_type == 2 || $route_type == 4) { // 2: get category and product with {category_permalink} and {category+permalink}/{product_permalink} $category_permalink = urlencode($category['uri_name']); } if ($route_type == 3) { // 3: get category and prdouct with {category_permalink1}/.../{category_permalinkn} and {category_permalink1}/.../{category_permalinkn}/{product_permalink} $parents = $category_service->get_parents_by_category_id($id); $category_permalink = ''; foreach ($parents as $category) { if ($category == end($parents)) { $category_permalink .= urlencode($category['uri_name']); } else { $category_permalink .= urlencode($category['uri_name']) . '/'; } } } if ($absolute) { return 'http://' . $domain . '/' . $category_permalink . $category_suffix; } else { return $category_permalink . $category_suffix; } }
/** * 获得商品前台链接 * * @param int 商品id * @param String 商品链接 */ public static function permalink($product) { static $routes = array(); static $categorys = array(); if (!is_array($product)) { $product = ProductService::get_instance()->get($product); } $routes = Myroute::instance()->get(); $domain = Mysite::instance()->get('domain'); $route_type = $routes['type']; $product_suffix = $routes['product_suffix']; $product_route = $routes['product']; if ($route_type == 2 or $route_type == 3) { if (!isset($categorys[$product['category_id']])) { $categorys[$product['category_id']] = CategoryService::get_instance()->get($product['category_id']); } } $permalink = ''; $product['uri_name'] = isset($product['uri_name']) ? $product['uri_name'] : $product['id']; switch ($route_type) { case 0: //$permalink = $product_route.'/'.$product['id'].$product_suffix; $permalink = $product_route . $product['id']; break; case 1: $permalink = $product_route . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 2: $permalink = urlencode($categorys[$product['category_id']]['uri_name']) . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 3: $permalink = urlencode($categorys[$product['category_id']]['uri_name']) . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 4: $permalink = $product_route . '/' . urlencode($product['uri_name']) . $product_suffix; break; } return Kohana::config('config.front_protocol') . $domain . '/' . $permalink; }
public function search_products() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array('assoc' => NULL, 'count' => 0); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); if (empty($request_data)) { $request_data = array_merge($_POST, $_GET); } $site_ids = role::get_site_ids(); if (empty($site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } $in_site_id = site::id(); if (isset($request_data['site_id']) and $request_data['site_id'] === '0') { unset($request_data['site_id']); } if (isset($request_data['site_id']) and !in_array($request_data['site_id'], $site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } if ($in_site_id > 0) { $query_site_id = $in_site_id; } else { throw new MyRuntimeException(Kohana::lang('o_global.select_site'), 400); } $page = isset($request_data['page']) ? intval($request_data['page']) : 1; $page < 1 ? $page = 1 : ''; $per_page = 10; $query_struct = array('where' => array('site_id' => $query_site_id, 'status' => 1), 'limit' => array('page' => $page, 'per_page' => $per_page)); if (isset($request_data['type']) && trim($request_data['keyword']) != '') { $select_key = mysql_escape_string($request_data['keyword']); if ($request_data['type'] == 'sku') { $query_struct['where']['sku'] = $select_key; } elseif ($request_data['type'] == 'name_manage') { $query_struct['like']['name_manage'] = $select_key; } elseif ($request_data['type'] == 'title') { $query_struct['like']['title'] = $select_key; } elseif ($request_data['type'] == 'category_id') { $categories_select = CategoryService::get_instance()->index(array('where' => array('site_id' => $query_site_id), 'like' => array('title' => $select_key))); if (!empty($categories_select)) { $categories_ids = array(); for ($i = 0; $i < count($categories_select); $i++) { $categories_ids[] = $categories_select[$i]['id']; } $query_struct['where']['category_id'] = $categories_ids; } } elseif ($request_data['type'] == 'brand_id') { $brands_select = BrandService::get_instance()->index(array('where' => array('site_id' => $query_site_id), 'like' => array('name' => $select_key))); if (!empty($brands_select)) { $brands_ids = array(); for ($i = 0; $i < count($brands_select); $i++) { $brands_ids[] = $brands_select[$i]['id']; } $query_struct['where']['brand_id'] = $brands_ids; } else { $query_struct['where']['brand_id'] = -1; } } } //print_r($query_struct); $return_data = BLL_Product::index($query_struct); $this->pagination = new Pagination(array('total_items' => $return_data['count'], 'items_per_page' => $per_page)); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/commonblank_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->products = $return_data['assoc']; $this->template->content->pagination = $this->pagination; if (isset($request_data['type'])) { $this->template->content->search_type = $request_data['type']; } $this->template->content->pagination = $this->pagination; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $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; } } }
public function download() { role::check('promotion_coupon'); // 收集请求数据 $request_data = $this->input->get(); $coupon = Mycoupon::instance($request_data['id'])->get(); // 权限验证 if (!$coupon['id']) { remind::set(Kohana::lang('o_global.bad_request'), request::referrer(), 'error'); } if (!preg_match('/^\\d+$/', $request_data['amount']) || $request_data['amount'] >= 10000 || $request_data['amount'] <= 0) { exit; } //$used_coupons = Myused_coupon::instance()->get_used_coupon_codes($coupons['site_id']); $coupons = Mycoupon::instance()->gen_coupons($coupon['id'], $request_data['amount']); $cpn_promotion = Mycpn_promotion::instance()->get_by_couponid($coupon['id']); $coupon_scheme = Mycoupon_scheme::instance($cpn_promotion['cpns_id'])->get(); //CSV输出 $rand_name = date('Y-m-d') . '_' . mt_rand(); header("Pragma: public"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Transfer-Encoding: binary "); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=coupons_{$rand_name}.csv"); //header("Content-Disposition:attachment;filename={$filename}{$file}.xls"); header("Pragma: no-cache"); header("Expires: 0"); $output = ",,,,,\n"; switch ($cpn_promotion['cpns_id']) { case 1: case 2: $output .= "订单开始金额,订单结束金额,,,,\n"; $output .= "\${$cpn_promotion['money_from']},\${$cpn_promotion['money_to']},,,,\n"; break; case 3: $related_ids = explode(',', trim($cpn_promotion['related_ids'], ',')); $request_struct = array('where' => array('id' => $related_ids), 'orderby' => array('email' => 'ASC')); $all_users = Myuser::instance()->lists($request_struct); $output .= "用户邮箱,姓名,,,,\n"; foreach ($all_users as $key => $rs) { $output .= "{$rs['email']},{$rs['firstname']} {$rs['lastname']},,,,\n"; } break; case 4: $related_ids = explode(',', trim($cpn_promotion['related_ids'], ',')); $request_struct = array('where' => array('id' => $related_ids), 'orderby' => array('name_manage' => 'ASC')); $all_products = ProductService::get_instance()->index($request_struct); $output .= "商品SKU,名称,,,,\n"; foreach ($all_products as $key => $rs) { $output .= "{$rs['sku']},{$rs['name_manage']},,,,\n"; } break; case 5: $related_ids = explode(',', trim($cpn_promotion['related_ids'], ',')); $request_struct = array('where' => array('id' => $related_ids), 'orderby' => array('title_manage' => 'ASC')); $all_categories = CategoryService::get_instance()->index($request_struct); $output .= "分类名称,,,,,\n"; foreach ($all_categories as $key => $rs) { $output .= "{$rs['title_manage']},,,,,\n"; } break; case 6: $output .= "订单商品开始数量,订单商品结束数量,,,,\n"; $output .= "{$cpn_promotion['quantity_from']},{$cpn_promotion['quantity_to']},,,,\n"; break; } $output .= ",,,,,\n"; $output .= "打折号,开始时间,结束时间,打折值,折扣类型,\n"; foreach ($coupons as $key => $rs) { $output .= $rs . "," . $cpn_promotion['time_begin'] . "," . $cpn_promotion['time_end'] . ","; switch ($cpn_promotion['discount_type']) { case 0: $output .= '百分比 ' . $cpn_promotion['price'] * 100 . "%"; break; case 1: $output .= '减去 $' . $cpn_promotion['price']; break; case 2: $output .= '减到 $' . $cpn_promotion['price']; break; } $output .= ","; $output .= "{$coupon_scheme['cpns_memo']},\n"; } echo iconv('UTF-8', 'gb2312', $output); exit; }
protected function parse_category($l) { static $names = array(); static $last_category_id = 0; $categorys = $this->get($l, 'category'); if ($categorys !== '') { $cs_upper = strtoupper($categorys); if (isset($names[0][$cs_upper])) { return $names[0][$cs_upper]; } $cnames = explode('->', $categorys); $parent_id = 0; $parent_name = '根分类'; foreach ($cnames as $item) { $category = CategoryService::get_instance()->query_row(array('where' => array('pid' => $parent_id, 'title_manage' => trim($item)))); if (!empty($category)) { $parent_id = $category['id']; $parent_name = $category['title_manage']; } else { $this->set_error(new MyRuntimeException(sprintf('分类 "%s" 下未找到名称为 "%s" 的子分类', $parent_name, $item))); $is_error = TRUE; break; } } if (!isset($is_error) or $is_error === FALSE) { $names[][$cs_upper] = $category['id']; $last_category_id = $category['id']; return $category['id']; } } if ($last_category_id == 0) { $this->set_error(new MyRuntimeException('商品所属分类解析失败')); } return $last_category_id; }
public function check_exist_uri_name() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException('Not Implemented',501); //必须为ajax请求 if (!$this->is_ajax_request()) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } //* 权限验证 */ $site_id = site::id(); if ($site_id == 0) { throw new MyRuntimeException(Kohana::lang('o_global.select_site'), 400); } if (!isset($request_data['uri_name']) || empty($request_data['uri_name'])) { throw new MyRuntimeException(Kohana::lang('o_global.input_error'), 404); } $category_service = CategoryService::get_instance(); if (isset($request_data['category_id']) && is_numeric($request_data['category_id'])) { $category = $category_service->get($request_data['category_id']); //判断站点 if ($site_id != $category['site_id']) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 404); } if (strtolower($category['uri_name']) == strtolower($request_data['uri_name'])) { exit(Kohana::lang('o_global.true')); } } // 调用底层服务 if ($category_service->check_exist_uri_name($site_id, $request_data['uri_name'])) { exit(Kohana::lang('o_global.false')); } else { exit(Kohana::lang('o_global.true')); } } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
/** * 编辑商品分类导航 */ public function category_menu_edit($id) { $site_menu = Mysite_menu::instance($id); $temp = Mysite_menu::instance()->site_menu_queue($id); if ($_POST) { //标签过滤 tool::filter_strip_tags($_POST); /* 获取要编辑的导航的level_depth并修改其下所有的子导航的level_depth*/ $data = $_POST; $parent_id = $this->input->post('parent_id'); if ($parent_id == 0) { $data['level_depth'] = 1; } else { $parent_level_depth = Mysite_menu::instance($parent_id)->get('level_depth'); $data['level_depth'] = $parent_level_depth + 1; } if (!Mysite_menu::instance()->child_level_depth_edit($id, $data['level_depth'], $temp)) { remind::set(Kohana::lang('o_global.update_error'), 'site/menu'); } /* 获取所添加的分类的url*/ $category_id = $this->input->post('category_id'); $data['url'] = category::permalink($category_id, false); //'/category/'.$category_id; /* 标志导航类型*/ $data['memo'] = 'category'; $data['type'] = $this->MENU_TYPE_CATEGORY; $data['relation_id'] = $category_id; if ($site_menu->site_menu_edit($id, $data)) { remind::set(Kohana::lang('o_global.update_success'), 'site/menu/', 'success'); } else { remind::set(Kohana::lang('o_global.update_error'), 'site/menu'); } } $site_menu_data = $site_menu->get(); /* 得到导航列表并删除自身及自身的子目录*/ $child_ids = array(); $site_menus = Mysite_menu::instance()->site_menu_queue(); foreach ($temp as $val) { $child_ids[] = $val['id']; } foreach ($site_menus as $key => $value) { if ($value['id'] == $id || in_array($value['id'], $child_ids)) { unset($site_menus[$key]); } } /* 当前站点分类*/ $categories = CategoryService::get_instance()->get_categories(); $str = '<option value={$id} {$selected}>{$spacer}{$title_manage}</option>'; $category_list = tree::get_tree($categories, $str, 0, $site_menu_data['relation_id']); $this->template->content = new View("site/category_menu_edit"); $this->template->content->site_menus = $site_menus; $this->template->content->category_list = $category_list; $this->template->content->site_menu_data = $site_menu_data; }
public function put() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); if (empty($request_data['product_id']) or !isset($request_data['relation_ids'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //* 权限验证 ==根据业务逻辑定制== */ $product_service = ProductService::get_instance(); $product_relation_service = Product_relationService::get_instance(); $product = $product_service->get($request_data['product_id']); ORM::factory('product_relation')->where('product_id', $product['id'])->delete_all(); $query_relation_ids = explode('-', $request_data['relation_ids']); $relation_ids = array(); $products = array(); $category_ids = array(); $categorys = array(); $brand_ids = array(); $brands = array(); if (!empty($query_relation_ids)) { $query_struct = array('where' => array('id' => $query_relation_ids)); foreach ($product_service->query_assoc($query_struct) as $relation) { if ($relation['site_id'] == $product['site_id']) { $relation_ids[$relation['id']] = TRUE; $category_ids[$relation['category_id']] = TRUE; if ($relation['brand_id'] > 0) { $brand_ids[$relation['brand_id']] = TRUE; } $products[] = $relation; } } } if (!empty($category_ids)) { $query_struct = array('where' => array('id' => array_keys($category_ids))); foreach (CategoryService::get_instance()->query_assoc($query_struct) as $category) { $categorys[$category['id']] = $category['title_manage']; } } if (!empty($brand_ids)) { $query_struct = array('where' => array('id' => array_keys($brand_ids))); foreach (BrandService::get_instance()->query_assoc($query_struct) as $brand) { $brands[$brand['id']] = $brand['name']; } } if (!empty($relation_ids)) { foreach ($relation_ids as $key => $val) { $product_relation_service->create(array('product_id' => $product['id'], 'relation_product_id' => $key)); } } ProductService::get_instance()->set($product['id'], array('update_timestamp' => time())); $list = new View($this->package_name . '/' . $this->class_name . '/list'); $list->brands = $brands; $list->categorys = $categorys; $list->products = $products; $return_data['relation_ids'] = $relation_ids; $return_data['list'] = (string) $list; //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '关联成功!'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { throw new MyRuntimeException(); // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$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->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { //$return_struct['action']['type']= 'close'; // 当前应用为弹出窗口所以定义失败后续动作为关闭窗口 $this->template = new View('layout/default_html'); $this->template->return_struct = $return_struct; $content = new View('info2'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }
public function add_products() { role::check('product_auction'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); $experience = $this->input->get('experience', 0); $relation_query_struct = array('where' => array('status!=' => 2, 'experience' => $experience), 'orderby' => array('id' => 'DESC')); //$relations = Product_auctionService::get_instance()->index($relation_query_struct); $product_ids = array(); //foreach ($relations as $relation){ //$product_ids[] = $relation['product_id']; //} $struct = product::get_struct($request_data); $query_struct_current = $struct['query']; $request_struct_current = $struct['request']; $query_struct_current['where']['type'] = ProductService::PRODUCT_TYPE_GOODS; //* 调用后端服务获取数据 */ $product_service = ProductService::get_instance(); $return_data['assoc'] = $product_service->index($query_struct_current); $return_data['count'] = $product_service->count($query_struct_current); // 初始化分类列表 $category_list = array(); // 初始化品牌列表 $brand_list = array(); 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; } } // 获取分类列表 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']; } } // 获取品牌列表 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'])); //* 如果是ajax请求缩减返回的字段 ==根据业务逻辑定制== */ if ($this->is_ajax_request()) { $requestkeys = array('id', 'category_id', 'title', 'uri_name', 'store', 'on_sale', 'goods_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('product/auction/add_products', array('experience' => $experience)); //* 变量绑定 */ $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); $this->template->content->product_ids = $product_ids; $this->template->content->category_list = $category_list; $this->template->content->brand_list = $brand_list; } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
/** * 得到分类已经的SEO信息 */ public function get_category_product_seo() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); // 初始化返回数据 $return_data = array(); //请求结构体 $request_data = array(); try { /* 分类 */ $category_id = $this->input->get('category_id'); // 初始化默认查询条件 $query_struct = array('where' => array('category_id' => $category_id), 'like' => array(), 'orderby' => array('id' => 'DESC'), 'limit' => array('per_page' => 100, 'offset' => 0)); if ($category_id > 0) { //判断分类下面是否有子分类 $childrens = CategoryService::get_instance()->get_childrens_by_category_id($category_id); if (isset($childrens) && !empty($childrens)) { $is_contain_child = 1; } else { $is_contain_child = 0; } } else { $query_struct['where']['category_id'] = Seo_manageService::SEO_CATEGORY_IS_NULL; $is_contain_child = 0; } $seo_manage = Seo_manageService::get_instance()->query_assoc($query_struct); if (is_array($seo_manage) && count($seo_manage) > 0) { $return_struct['status'] = 1; $return_struct['code'] = 200; $return_data['data'] = $seo_manage; $return_struct['content'] = $return_data; $return_struct['is_contain_child'] = $is_contain_child; } //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { die('No direct access allowed.'); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); if ($this->is_ajax_request()) { $this->template = new View('layout/empty_html'); $this->template->content = $return_struct['msg']; } else { $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; } } }
public function get_category_page_by_site_id($site_id) { $category_service = CategoryService::get_instance(); $categories = $category_service->get_categories_by_site_id($site_id); return $categories; }
public function index() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); $query_struct = array('where' => array('status' => ProductService::PRODUCT_STATUS_PUBLISH), 'orderby' => array(), 'limit' => array()); switch (trim($request_data['type'])) { case 'category': $query_struct['orderby']['id'] = 'ASC'; if (!isset($request_data['id']) or !preg_match('/^\\d+$/', $request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $category = CategoryService::get_instance()->get($request_data['id']); $category['sub_ids'] = trim($category['sub_ids']); if (empty($category['sub_ids'])) { $query_struct['where']['category_id'] = $category['id']; } else { $sub_ids = explode(',', $category['sub_ids']); array_push($sub_ids, $category['id']); $query_struct['where']['category_id'] = $sub_ids; } break; case 'classify': $query_struct['orderby']['id'] = 'ASC'; if (!isset($request_data['id']) or !preg_match('/^\\d+$/', $request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $categorys = CategoryService::get_instance()->query_assoc(array('where' => array('classify_id' => $request_data['id']))); if (empty($categorys)) { throw new MyRuntimeException(Kohana::lang('o_product.export_pdt_not_found')); } $query_struct['where']['category_id'] = array(); foreach ($categorys as $category) { $query_struct['where']['category_id'][] = $category['id']; } break; default: if (!empty($request_data['product_id'])) { $request_data['product_id'] = explode('-', $request_data['product_id']); foreach ($request_data['product_id'] as $item) { if (!preg_match('/^\\d+$/', $item)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } } $query_struct['where']['id'] = $request_data['product_id']; } if (isset($request_data['category_id'])) { if (!preg_match('/^\\d+$/', $request_data['category_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['where']['category_id'] = $request_data['category_id']; } if (isset($request_data['brand_id'])) { if (!preg_match('/^\\d+$/', $request_data['brand_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['where']['brand_id'] = $request_data['brand_id']; } if (!empty($request_data['title'])) { $query_struct['where']['title'] = trim($request_data['title']); } if (!empty($request_data['name_manage'])) { $query_struct['where']['name_manage'] = trim($request_data['name_manage']); } if (!empty($request_data['sku'])) { $query_struct['where']['sku'] = trim($request_data['sku']); } if (isset($request_data['orderby'])) { if (!is_array($request_data['orderby'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } foreach ($request_data['orderby'] as $item) { $item = explode('-', $item); if (count($item) != 2) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['orderby'][$item[0]] = $item[1] === '0' ? 'ASC' : 'DESC'; } } if (isset($request_data['page'])) { if (!preg_match('/^\\d+$/', $request_data['page'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['limit']['page'] = $request_data['page']; if (!isset($request_data['per_page']) or !preg_match('/^\\d+$/', $request_data['per_page'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['limit']['per_page'] = $request_data['per_page']; } } $products = ProductService::get_instance()->query_assoc($query_struct); if (empty($products)) { throw new MyRuntimeException(Kohana::lang('o_product.export_pdt_not_found')); } $csv = ExportService::get_instance()->run($products); $csv = csv::encode($csv); $csv = iconv('UTF-8', 'GBK//IGNORE', $csv); if ($this->is_ajax_request()) { $fid = uniqid(); $dir = Kohana::config('product.export_tmp_dir'); $dir = rtrim(trim($dir), '/'); if (!is_dir($dir) && !@mkdir($dir, 0777, TRUE)) { throw new MyRuntimeException(Kohana::lang('o_product.export_cte_tmpdir_failed')); } $filename = $dir . '/' . $fid . '.csv'; if (!@file_put_contents($filename, $csv)) { throw new MyRuntimeException(Kohana::lang('o_product.export_wte_tmp_failed')); } } else { @header('Cache-control: private'); @header('Content-Disposition: attachment; filename=' . 'export-' . date('Ymd', time()) . '.csv'); @header('Content-type: text/csv; charset=GBK'); echo $csv; exit; } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = url::base() . 'product/export/download?fid=' . $fid; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 //exit('Not Implemented'); $this->template->content = $return_struct; } else { // html 输出 //* 模板输出 */ $content = new View($this->package . '/' . $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->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->site_id = $site_id; $this->template->content->sites = $sites; $this->template->content->classifies_html = $html; } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $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; } } }
/** * 获取商品列表 * * @param array $query_struct * @return array */ public static function index($query_struct) { $products = ProductService::get_instance()->index($query_struct); if (isset($query_struct['where']['sku'])) { $add_struct = array('where' => array()); $add_struct['where']['sku'] = $query_struct['where']['sku']; if (isset($query_struct['where']['on_sale'])) { $add_struct['where']['on_sale'] = $query_struct['where']['on_sale']; } if (!empty($add_struct['where'])) { $goods = ProductService::get_instance()->index($add_struct); if (!empty($goods)) { $add_ids = array(); $pdt_ids = array(); foreach ($products as $product) { $pdt_ids[$product['id']] = TRUE; } foreach ($goods as $good) { if (!isset($pdt_ids[$good['product_id']])) { $add_ids[] = $good['product_id']; $pdt_ids[$good['product_id']] = TRUE; } } if (!empty($add_ids)) { $add_pdts = ProductService::get_instance()->index(array('where' => array('id' => $add_ids))); foreach ($add_pdts as $product) { $products[] = $product; } } } } } $category_ids = array(); $classify_ids = array(); $brand_ids = array(); foreach ($products as $product) { $product['category_id'] > 0 and $category_ids[$product['category_id']] = TRUE; $product['classify_id'] > 0 and $classify_ids[$product['classify_id']] = TRUE; $product['brand_id'] > 0 and $brand_ids[$product['brand_id']] = TRUE; } $category_ids = array_keys($category_ids); $classify_ids = array_keys($classify_ids); $brand_ids = array_keys($brand_ids); $categorys = array(); if (!empty($category_ids)) { foreach (CategoryService::get_instance()->index(array('where' => array('id' => $category_ids))) as $category) { $categorys[$category['id']] = $category; } } $classifys = array(); if (!empty($classify_ids)) { foreach (ClassifyService::get_instance()->index(array('where' => array('id' => $classify_ids))) as $classify) { $classifys[$classify['id']] = $classify; } } $brands = array(); if (!empty($brand_ids)) { foreach (BrandService::get_instance()->index(array('where' => array('id' => $brand_ids))) as $brand) { $brands[$brand['id']] = $brand; } } foreach ($products as $index => $product) { $product = coding::decode_product($product); isset($categorys[$product['category_id']]) and $product['category'] = $categorys[$product['category_id']]; isset($classifys[$product['classify_id']]) and $product['classify'] = $classifys[$product['classify_id']]; isset($brands[$product['brand_id']]) and $product['brand'] = $brands[$product['brand_id']]; $product['name_manage'] || ($product['name_manage'] = $product['title']); $product['category_name'] = isset($product['category']['title_manage']) ? $product['category']['title_manage'] : ''; $products[$index] = $product; } return array('assoc' => $products, 'count' => ProductService::get_instance()->count($query_struct)); }
public static function parse($current_uri) { $current_uri = trim($current_uri); if (empty($current_uri)) { return FALSE; } $site_id = Mysite::instance()->id(); // 尝试解析 doc $doc_uris = Mydoc::get_uris($site_id); if (isset($doc_uris[$current_uri])) { return '/doc/view/' . $current_uri; } $route_type = Myroute::instance()->type(); $category_suffix = Myroute::instance()->get_action('category_suffix'); $category_key = Myroute::instance()->get_action('category'); $categorys = CategoryService::get_instance()->get_uri_name(); // 尝试解析 category $arguments = explode('/', $current_uri); switch ($route_type) { case 0: // www.example.com/category/1.html/page/2 if ($arguments[0] === $category_key and isset($arguments[1])) { if (!empty($category_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($category_suffix)); } if (preg_match('/^\\d+$/', $arguments[1])) { $result = 'category/index/' . $arguments[1]; for ($i = 2; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 1: // www.example.com/category/xxxxx.html/page/2 if ($arguments[0] === $category_key and isset($arguments[1])) { if (!empty($category_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($category_suffix)); } if (isset($categorys[$arguments[1]])) { $result = 'category/index/' . $arguments[1]; for ($i = 2; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 2: // www.example.com/xxxxx.html/page/2 if (count($arguments) == 1 or count($arguments) > 2) { if (!empty($category_suffix)) { $arguments[0] = substr($arguments[0], 0, strlen($arguments[0]) - strlen($category_suffix)); } if (isset($categorys[$arguments[0]])) { $result = 'category/index/' . $arguments[0]; for ($i = 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 3: // www.example.com/xxxxx/ooooo.html/page/2 for ($i = 0; isset($arguments[$i]); $i++) { if (!isset($categorys[$arguments[$i]])) { break; } } if ($i == 0) { break; } if (empty($category_suffix)) { $i--; } $count = count($arguments) - $i; if ($count == 1 or $count > 2) { if (!empty($category_suffix)) { $arguments[$i] = substr($arguments[$i], 0, strlen($arguments[$i]) - strlen($category_suffix)); } $result = 'category/index/' . $arguments[$i]; for ($i = $i + 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } break; case 4: // www.example.com/xxxxx.html/page/2 if (count($arguments) == 1 or count($arguments) > 2) { if (!empty($category_suffix)) { $arguments[0] = substr($arguments[0], 0, strlen($arguments[0]) - strlen($category_suffix)); } if (isset($categorys[$arguments[0]])) { $result = 'category/index/' . $arguments[0]; for ($i = 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; } $product_suffix = Myroute::instance()->get_action('product_suffix'); $product_key = Myroute::instance()->get_action('product'); // 尝试解析 product $arguments = explode('/', $current_uri); switch ($route_type) { case 0: // www.example.com/product/220.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } if (preg_match('/^\\d+$/', $arguments[1])) { return 'product/get/' . $arguments[1]; } } break; case 1: // www.example.com/product/yyyyy.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; case 2: // www.example.com/xxxxx/yyyyy.html if (isset($categorys[$arguments[0]]) and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; case 3: // www.example.com/xxxxx/ooooo/yyyyy.html for ($i = 0; isset($arguments[$i]); $i++) { if (!isset($categorys[$arguments[$i]])) { break; } } if ($i > 0 and isset($arguments[$i])) { if (!empty($product_suffix)) { $arguments[$i] = substr($arguments[$i], 0, strlen($arguments[$i]) - strlen($product_suffix)); } return 'product/get/' . $arguments[$i]; } break; case 4: // www.example.com/product/yyyyy.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; } return FALSE; }
/** * 根据 类型ID数组批量删除类型 * @param $categorys array * @return void * @throws MyRuntimeException */ public function delete_classifies($classifies) { try { $del_all = TRUE; if (!empty($classifies)) { foreach ($classifies as $val) { $query_struct = array('where' => array('classify_id' => $val)); if (CategoryService::get_instance()->count($query_struct)) { $del_all = FALSE; } if ($del_all === TRUE and ProductService::get_instance()->count($query_struct) > 0) { $del_all = FALSE; } if ($del_all === TRUE) { $where = array('classify_id' => $val); Classify_brand_relationService::get_instance()->delete_relations($where); Classify_attribute_relationService::get_instance()->delete_relations($where); //Classify_feature_relationService::get_instance()->delete_relations($where); $this->remove($val); } } if (!$del_all) { throw new MyRuntimeException('无法删除部分被关联的类型', 500); } } } catch (MyRuntimeException $ex) { throw $ex; } }
/** * 显示编辑商品表单 */ public function edit() { // 初始化返回结构体 $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { // 初始化返回数据 $return_data = array(); // 收集请求数据 $request_data = $this->input->get(); if (empty($request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $product = BLL_Product::get($request_data['id']); if (empty($product['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } // 分类列表默认关联第一个 $categorys_tree = CategoryService::get_instance()->get_tree("<option value=\\\"\$id\\\" \$selected>\$spacer\$title</option>", $product['category_id']); $categories = CategoryService::get_instance()->query_assoc(array()); $categories = tree::get_tree_array($categories); $classifies = ClassifyService::get_instance()->index(array('orderby' => array('id' => 'ASC'))); $classify_content['features'] = $this->load_features($product['classify_id'], $product['fetuoptrs']); $classify_content['brands'] = $this->load_brands($product['classify_id'], $product['brand_id']); // 处理商品类型特定的模板区块 $ptype_layout = NULL; switch ($product['type']) { case ProductService::PRODUCT_TYPE_ASSEMBLY: throw new MyRuntimeException('Coming soon ...', 400); //暂时不支持组合商品 $ptype_layout = new View($this->package_name . '/' . $this->class_name . '/assembly/layout'); break; case ProductService::PRODUCT_TYPE_CONFIGURABLE: $ptype_layout = new View($this->package_name . '/' . $this->class_name . '/configurable/layout'); break; case ProductService::PRODUCT_TYPE_GOODS: default: $ptype_layout = new View($this->package_name . '/' . $this->class_name . '/simple/layout'); break; } $ptype_layout->product = $product; $return_struct['content'] = array('product' => $product); //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // 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->return_struct = $return_struct; $this->template->content->categorys_tree = $categorys_tree; $this->template->content->categories = $categories; $this->template->content->classifies = $classifies; $this->template->content->classify_content = $classify_content; $this->template->content->ptype_layout = $ptype_layout; $this->template->content->listurl = isset($request_data['listurl']) ? $request_data['listurl'] : ''; } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
protected function get_category_names($category_id) { if (!isset($this->category_names[$category_id])) { $category_names = ''; $category = CategoryService::get_instance()->get($category_id); $category_names = $category['title_manage']; while ($category['pid'] != 0) { $category = CategoryService::get_instance()->get($category['pid']); if (!empty($category_names)) { $category_names = '->' . $category_names; } $category_names = $category['title_manage'] . $category_names; } $this->category_names[$category_id] = $category_names; } return $this->category_names[$category_id]; }
function get_category_data() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array('classify' => array(), 'brand_list' => array(), 'attribute_list' => array(), 'feature_list' => array()); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); //必须为ajax请求 if (!$this->is_ajax_request()) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } //数据验证 if (!isset($request_data['category_id']) || !is_numeric($request_data['category_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.input_error'), 404); } if ($request_data['category_id'] == 0) { //当前站点品牌 $return_data['brand_list'] = BrandService::get_instance()->get_brands(); //当前站点规格 $return_data['attribute_list'] = AttributeService::get_instance()->get_attribute_spec_options(); //当前站点品牌 $return_data['feature_list'] = AttributeService::get_instance()->get_attribute_feature_options(); } else { // 调用底层服务 $category_service = CategoryService::get_instance(); $classify_service = ClassifyService::get_instance(); //获取数据 $category = $category_service->get($request_data['category_id']); if ($category['classify_id']) { $return_data['classify'] = $classify_service->get($category['classify_id']); //获取关联品牌数组 $return_data['brand_list'] = $classify_service->get_brands_by_classify_id($category['classify_id']); //获取关联规格及规格项数组 $return_data['attribute_list'] = $classify_service->get_attribute_options_by_classify_id($category['classify_id'], AttributeService::ATTRIBUTE_SPEC); //获取关联特性及特性值数组 $return_data['feature_list'] = $classify_service->get_attribute_options_by_classify_id($category['classify_id'], AttributeService::ATTRIBUTE_FEATURE); } } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $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; } } }
public function index() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException('Not Implemented',501); //权限验证 if ($this->site_id == 0) { throw new MyRuntimeException(Kohana::lang('o_global.select_site'), 400); } role::check('default', $this->site_id, 0); // 执行业务逻辑 $category_service = CategoryService::get_instance(); $str = '<option value={$id} {$selected}>{$spacer}{$title}</option>'; $category_list = $category_service->get_tree($str); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 //* 模板输出 */ $content = new View('site/sitemap'); //* 变量绑定 */ $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->site_id = $this->site_id; $this->template->content->category_list = $category_list; } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $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; } } }
public function add_category_name(&$products) { if (!empty($products)) { $cids = array(); foreach ($products as $record) { $cids[$record['category_id']] = TRUE; } $categories = array(); $query_struct = array('where' => array('id' => array_keys($cids))); foreach ((array) CategoryService::get_instance()->query_assoc($query_struct) as $record) { $categories[$record['id']] = $record['title_manage'] ? $record['title_manage'] : $record['title']; } foreach ($products as $index => $record) { $record['category_name'] = isset($categories[$record['category_id']]) ? $categories[$record['category_id']] : ''; $products[$index] = $record; } } }
public function transport_post() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { set_time_limit(0); //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); //* 权限验证 */ $site_id = site::id(); if ($site_id == 0) { throw new MyRuntimeException(Kohana::lang('o_global.select_site'), 400); } role::check('product_classify', $site_id, 0); //数据验证 if (!isset($request_data['classify_id']) or !preg_match('/^\\d+$/', $request_data['classify_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } if (!isset($request_data['argument_group']) or !is_array($request_data['argument_group'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } if (!isset($request_data['argument']) or !is_array($request_data['argument'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } // 调用底层服务 $classify = ClassifyService::get_instance()->get($request_data['classify_id']); if ($classify['site_id'] != $site_id) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $categorys = CategoryService::get_instance()->query_assoc(array('where' => array('site_id' => $site_id, 'classify_id' => $classify['id']))); $arguments = empty($classify['argument_relation_struct']) ? array() : json_decode($classify['argument_relation_struct'], TRUE); $features = ClassifyService::get_instance()->get_features_by_classify_id($classify['id']); $transport = array(); if (!empty($features)) { foreach ($features as $feature) { if (!empty($request_data['argument_group'][$feature['id']]) and !empty($request_data['argument'][$feature['id']])) { $gname = trim($request_data['argument_group'][$feature['id']]); $aname = trim($request_data['argument'][$feature['id']]); foreach ($arguments as $group) { if ($gname === $group['name']) { foreach ($group['items'] as $argument) { if ($aname === $argument['name']) { $transport[$feature['id']] = array($gname, $aname); break; } } break; } } if (!isset($transport[$feature['id']])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } } } } if (!empty($transport)) { if (!empty($categorys)) { $category_ids = array(); foreach ($categorys as $category) { $category_ids[] = $category['id']; } $products = ProductService::get_instance()->query_assoc(array('where' => array('site_id' => $site_id, 'category_id' => $category_ids))); foreach ($products as $product) { $product = coding::decode_product($product); $product_argument_record = Product_argumentService::get_instance()->query_row(array('where' => array('product_id' => $product['id']))); $product_argument = empty($product_argument_record) ? array() : json_decode($product_argument_record['arguments'], TRUE); foreach ($transport as $feature_id => $argument) { if (!empty($product['product_feature_relation_struct']['items'])) { $key = array_search($feature_id, $product['product_feature_relation_struct']['items']); if ($key !== FALSE) { unset($product['product_feature_relation_struct']['items'][$key]); } } if (!empty($product['product_featureoption_relation_struct']['items'][$feature_id])) { $option_id = $product['product_featureoption_relation_struct']['items'][$feature_id]; if (isset($features[$feature_id]['options'][$option_id])) { if (!isset($product_argument[$argument[0]])) { $product_argument[$argument[0]] = array(); } $product_argument[$argument[0]][$argument[1]] = $features[$feature_id]['options'][$option_id]['name']; } unset($product['product_featureoption_relation_struct']['items'][$feature_id]); } } ProductService::get_instance()->set($product['id'], coding::encode_product($product)); if (!empty($product_argument)) { if (empty($product_argument_record)) { Product_argumentService::get_instance()->create(array('product_id' => $product['id'], 'arguments' => json_encode($product_argument))); } else { Product_argumentService::get_instance()->set($product_argument_record['id'], array('arguments' => json_encode($product_argument))); } } ORM::factory('product_featureoption_relation')->where('product_id', $product['id'])->delete_all(); } } ORM::factory('classify_feature_relation')->where('classify_id', $classify['id'])->in('feature_id', array_keys($transport))->delete_all(); } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 //* 模板输出 */ $content = new View($this->package . '/' . $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->return_struct = $return_struct; //:: 当前应用专用数据 } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $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; } } }