public static function create_good_title($product_title, $options) { static $attributes = array(); $attribute_ids = array(); foreach ($options as $attribute_id => $option_id) { if (!isset($attributes[$attribute_id])) { $attribute_ids[] = $attribute_id; } } if (!empty($attribute_ids)) { $query_struct = array('where' => array('id' => $attribute_ids)); foreach (AttributeService::get_instance()->get_attribute_options($query_struct) as $attribute) { $attributes[$attribute['id']] = $attribute; } } $title = ''; foreach ($options as $attribute_id => $option_id) { if (isset($attributes[$attribute_id]['options'][$option_id])) { if ($title != '') { $title .= ','; } $title .= $attributes[$attribute_id]['options'][$option_id]['name']; } } return $product_title . ' - ' . $title; }
/** * 根据规格ID获取商品规格 * * @param integer $attribute_id * @return array */ public static function get($attribute_id) { $attribute = AttributeService::get_instance()->get($attribute_id); $attribute = coding::decode_attribute($attribute); $options = Attribute_valueService::get_instance()->index(array('where' => array('attribute_id' => $attribute['id']), 'orderby' => 'order')); $attribute['options'] = array(); foreach ($options as $option) { unset($option['attribute_id']); unset($option['order']); $attribute['options'][$option['id']] = $option; } return $attribute; }
/** * 根据 attributeoption_id 获取该 attributeoption 的兄弟记录 * * @param int $attributeoption_id 规格项ID * @param bool $clear_self 是否清除自身 * @return array * @throws MyRuntimeException */ public function get_brothers_by_attributeoption_id($attributeoption_id, $clear_self = FALSE) { try { $attributeoption = $this->get($attributeoption_id); $attribute_id = $attributeoption['attribute_id']; $attributeoptions = AttributeService::get_instance()->get_attributeoptions_by_attribute_id($attribute_id); if ($clear_self == TRUE) { foreach ($attributeoptions as $index => $attributeoption) { if ($attributeoption['id'] == $attributeoption_id) { unset($attributeoptions[$index]); } } } return $attributeoptions; } catch (MyRuntimeException $ex) { throw $ex; } }
/** * 根据 WHERE 条件获取商品特性列表 * * @param array $where * @return array */ public static function index($where) { $features = array(); $feature_arr = AttributeService::get_instance()->index(array('where' => $where, 'orderby' => 'order')); foreach ($feature_arr as $feature) { $feature = coding::decode_feature($feature); $feature['options'] = array(); $features[$feature['id']] = $feature; } if (!empty($features)) { $options = Attribute_valueService::get_instance()->index(array('where' => array('attribute_id' => array_keys($features)), 'orderby' => 'order')); foreach ($options as $option) { //$option = coding::decode_featureoption($option); $featid = $option['attribute_id']; unset($option['attribute_id']); unset($option['order']); $features[$featid]['options'][$option['id']] = $option; } } return $features; }
/** * 设定菜单的排序 */ public function set_order() { //初始化返回数组 $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); /* 验证是否可以操作 */ if (!role::verify('product_feature')) { $return_struct['msg'] = Kohana::lang('o_global.permission_enough'); exit(json_encode($return_struct)); } $request_data = $this->input->get(); $id = isset($request_data['id']) ? $request_data['id'] : ''; $order = isset($request_data['order']) ? $request_data['order'] : ''; if (empty($id) || empty($order) && $order != 0) { $return_struct['msg'] = Kohana::lang('o_global.bad_request'); exit(json_encode($return_struct)); } if (!is_numeric($order) || $order < 0) { $return_struct['msg'] = Kohana::lang('o_global.position_rule'); exit(json_encode($return_struct)); } $attribute_service = AttributeService::get_instance(); $attribute_service->set($id, array('order' => $order)); $return_struct = array('status' => 1, 'code' => 200, 'msg' => Kohana::lang('o_global.position_success'), 'content' => array('order' => $order)); exit(json_encode($return_struct)); }
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; } } }
/** * 根据 classify_id 获取关联的规格及规格项 * * @param int $classify_id * @return array * @throws MyRuntimeException */ public function get_attribute_options_by_classify_id($classify_id, $apply = NULL) { $return_array = array(); try { /* 判断 classify 是否存在 $classify = $this->get($classify_id); if(!$classify){ throw new MyRuntimeException('Object not found.', 500); }*/ // 获取关联表数据 $attribute_ids = $this->get_attribute_ids_by_classify_id($classify_id, $apply); if ($attribute_ids) { // 获取所有关联的attribute $query_struct = array('where' => array('id' => $attribute_ids), 'orderby' => array('id' => 'ASC')); $attributes = AttributeService::get_instance()->query_assoc($query_struct); // 每个ID必须都是有效的 if (count($attributes) != count($attribute_ids)) { throw new MyRuntimeException('Internal error.', 500); } // 组装返回数组 foreach ($attributes as $attribute) { //$attribute = coding::decode_attribute($attribute); $attribute['options'] = array(); $return_array[$attribute['id']] = $attribute; } // 获取关联项 $query_struct = array('where' => array('attribute_id' => $attribute_ids)); $attributeoptions = Attribute_valueService::get_instance()->query_assoc($query_struct); foreach ($attributeoptions as $attributeoption) { //$attributeoption = coding::decode_attributeoption($attributeoption); $attribute_id = $attributeoption['attribute_id']; $return_array[$attribute_id]['options'][$attributeoption['id']] = $attributeoption; } } return $return_array; } catch (MyRuntimeException $ex) { throw $ex; } }
protected function parse_attributes($l, &$product) { $attributes = $this->get($l, 'attributes'); if ($attributes !== '') { $return_assoc = array(); $sign = '规格:'; if ($sign === substr($attributes, 0, strlen($sign))) { $attributes = explode('|', str_replace('/\\|+/', '|', substr($attributes, strlen($sign)))); foreach ($attributes as $index => $item) { $attributes[$index] = trim($item); } $records = AttributeService::get_instance()->get_attribute_options(array('where' => array('name_manage' => $attributes))); foreach ($attributes as $item) { $upper = strtoupper($item); $search = FALSE; foreach ($records as $record) { if (strtoupper($record['name_manage']) === $upper) { $return_assoc[] = $record; $search = TRUE; break; } } if ($search === FALSE) { $this->set_error(new MyRuntimeException(sprintf('站点 "%s" 下未找到名称为 "%s" 的规格', $this->site_name, $item))); } } } else { $num = 1; $attributes = explode('|', $attributes); $relations = array(); foreach ($attributes as $index => $item) { $item = explode('/', trim($item)); if (empty($item)) { $this->set_error(new MyRuntimeException(sprintf('规格 "%s" 的值不可为空', $this->attributes[$index]['name_manage']))); } else { if (isset($this->attributes[$index])) { foreach ($item as $option_name) { $uppercase = strtoupper(trim($option_name)); foreach ($this->attributes[$index]['options'] as $option) { if ($uppercase === strtoupper($option['name_manage'])) { if (!isset($relations[$this->attributes[$index]['id']])) { $relations[$this->attributes[$index]['id']] = array(); } $relations[$this->attributes[$index]['id']][] = $option['id']; } } } if (isset($relations[$this->attributes[$index]['id']]) and count($relations[$this->attributes[$index]['id']] === count($item))) { $num *= count($item); } else { $this->set_error(new MyRuntimeException(sprintf('规格 "%s" 的值填写错误:"%s"', $this->attributes[$index]['name_manage'], $item))); } } } } if (!empty($relations)) { if (!function_exists('array_assembly')) { function array_assembly($arrays) { $result = array(); $array = array_shift($arrays); if (empty($arrays)) { foreach ($array as $key => $val) { $array[$key] = array($val); } return $array; } else { foreach ($array as $val) { foreach (array_assembly($arrays) as $item) { array_unshift($item, $val); $result[] = $item; } } return $result; } } } $struct = array(); $index = 0; foreach ($relations as $aid => $oids) { if (!isset($struct[$index])) { $struct[$index] = array(); } foreach ($oids as $oid) { $struct[$index][] = array($aid, $oid); } $index++; } $struct = array_assembly($struct); foreach ($struct as $item) { $good = array('sku' => product::create_good_sku($product['sku']), 'store' => '-1', 'on_sale' => $product['on_sale'], 'price' => $product['goods_price'], 'market_price' => $product['goods_market_price'], 'cost' => $product['goods_cost'], 'weight' => $product['weight'], 'update_timestamp' => time(), 'create_timestamp' => time()); if (!isset($product['goods'])) { $product['store'] = '-1'; $product['goods'] = array(); $good['is_default'] = '1'; } else { $good['is_default'] = '0'; } $good['goods_attributeoption_relation_struct'] = array('items' => array()); foreach ($item as $aoid) { $good['goods_attributeoption_relation_struct']['items'][$aoid[0]] = $aoid[1]; } $good['title'] = product::create_good_title($product['title'], $good['goods_attributeoption_relation_struct']['items']); $product['goods'][] = $good; } } } return $return_assoc; } else { return array(); } }
function get_site_data() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array('brand_list' => NULL, 'attribute_list' => NULL, 'feature_list' => NULL); //* 收集请求数据 ==根据业务逻辑定制== */ $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_list = role::check('product_classify', 0, 0); if (empty($site_id_list)) { throw new MyRuntimeException('Access Denied', 403); } if (isset($request_data['site_id']) && is_numeric($request_data['site_id'])) { if (!in_array($request_data['site_id'], $site_id_list)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } } //数据验证 if (!isset($request_data['site_id']) || !is_numeric($request_data['site_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } // 调用底层服务 $classify_service = ClassifyService::get_instance(); //请求站点品牌列表 $brands = BrandService::get_instance()->get_brands_by_site_id($request_data['site_id']); foreach ($brands as $val) { $return_data['brand_list'] .= '<option value=' . $val['id'] . '>' . $val['name'] . '</option>'; } //请求站点规格列表 $attributes = AttributeService::get_instance()->get_attributes_by_site_id($request_data['site_id']); foreach ($attributes as $val) { $return_data['attribute_list'] .= '<option value=' . $val['id'] . '>' . $val['name'] . '</option>'; } //请求站点规格列表 $features = FeatureService::get_instance()->get_features_by_site_id($request_data['site_id']); foreach ($features as $val) { $return_data['feature_list'] .= '<option value=' . $val['id'] . '>' . $val['name'] . '</option>'; } //* 补充&修改返回结构体 */ $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; } } }