/** * 设置商品规格 * * @param array $product * @param array $arguments * @return boolean */ public static function set_argumrs(&$product) { self::rmv_arguments($product['id']); $arguments = isset($product['pdt_argumrs']) ? $product['pdt_argumrs'] : array(); if ($product['classify_id'] > 0 and $arguments) { $relation = array(); foreach (self::get_clsargurs($product['classify_id']) as $group) { isset($relation[$group['name']]) or $relation[$group['name']] = array(); foreach ($group['items'] as $argument) { $relation[$group['name']][$argument['name']] = TRUE; } } if (!empty($relation)) { foreach ($arguments as $group => $argument) { if (isset($relation[$group])) { foreach ($argument as $key => $value) { $value = trim($value); if (!isset($relation[$group][$key]) or empty($value)) { unset($arguments[$group][$key]); } } } else { unset($arguments[$group]); } } } if (!empty($arguments)) { Product_argumentService::get_instance()->add(array('product_id' => $product['id'], 'arguments' => json_encode($arguments))); } } //Cache::remove(self::$cache_key.$product['id']); return TRUE; }
public function run($products) { $csv_array = array(); if ($products) { $result = $this->format($products); foreach ($result as $classify_id => $products) { foreach ($products as $product) { $csv_array[] = $this->get_titlebar($classify_id); //dump($csv_array); //foreach ($products as $product){ $product = coding::decode_product($product); $desc = Product_detailService::get_instance()->get_by_product_id($product['id']); $goods = ProductService::get_instance()->query_assoc(array('where' => array('product_id' => $product['id']), 'orderby' => array('default_goods' => 'DESC'))); if (!empty($this->arguments)) { $arguments = Product_argumentService::get_instance()->query_row(array('where' => array('product_id' => $product['id']))); $arguments = empty($arguments) ? array() : json_decode($arguments['arguments'], TRUE); } $has_goods = FALSE; $default_good = array(); foreach ($goods as $good) { if (!empty($good['attribute_struct']['items'])) { $has_goods = TRUE; } if ($good['default_goods'] == 1) { $default_good = $good; break; } } /*if ($default_good AND $goods) { $default_good = $goods[key($goods)]; }*/ $p_row = array(); $p_row[] = ''; $p_row[] = ''; $p_row[] = $this->get_category_names($product['category_id']); $p_row[] = $product['sku']; $p_row[] = ''; $p_row[] = $product['name_manage']; $p_row[] = $product['title']; $p_row[] = $product['brand_id'] > 0 ? $this->get_brand_name($product['brand_id']) : ''; if (!$has_goods) { $p_row[] = $product['on_sale'] == 1 ? 'Y' : 'N'; $p_row[] = $product['price']; $p_row[] = $product['market_price']; } else { $p_row[] = ''; $p_row[] = ''; $p_row[] = ''; } $p_row[] = ''; // 商品图片 /** * 处理商品规格 */ if ($has_goods) { /*$attribute_option_ids = $product['argumrs_struct_default']['items']; $same_attributes = TRUE; if (count($this->attributes) == count($attribute_option_ids)) { foreach ($this->attributes as $attribute) { if (!isset($attribute_option_ids[$attribute['id']])) { $same_attributes = FALSE; break; } } } else { $same_attributes = FALSE; } if ($same_attributes == TRUE) { $attributes = $this->attributes; $p_row[] = ''; } else { $attributes = AttributeService::get_instance()->get_attribute_options(array( 'where' => array( 'id' => array_keys($attribute_option_ids), ), 'orderby' => array( 'id' => 'ASC', ), ));*/ $attribute_names = ''; foreach ($this->attributes as $attribute) { if (!empty($attribute_names)) { $attribute_names .= '|'; } $attribute_names .= $attribute['name']; } $p_row[] = '规格:' . $attribute_names; //} } else { $p_row[] = ''; } $p_row[] = $product['brief']; /** * 处理商品的详细描述 */ if ($desc) { $p_row[] = $desc['description']; } else { $p_row[] = ''; } /** * 处理商品的成本、库存、重量,当商品有规格时,在商品行不显示此三项信息 */ if (!$has_goods) { $p_row[] = $product['cost']; $p_row[] = $product['store']; $p_row[] = empty($default_good) ? '0' : $default_good['weight']; } else { $p_row[] = ''; $p_row[] = ''; $p_row[] = ''; } $p_row[] = $desc['meta_title']; $p_row[] = $desc['meta_keywords']; $p_row[] = $desc['meta_description']; /** * 处理货品与特性的关联 */ $feature_option_ids = empty($product['product_featureoption_relation_struct']['items']) ? array() : $product['product_featureoption_relation_struct']['items']; foreach ($this->features as $feature) { if (isset($feature_option_ids[$feature['id']])) { $p_row[] = $feature['options'][$feature_option_ids[$feature['id']]]['name']; } else { $p_row[] = ''; } } foreach ($this->arguments as $argument_group) { foreach ($argument_group['items'] as $argument) { if (isset($arguments[$argument_group['name']]) and isset($arguments[$argument_group['name']][$argument['name']])) { $p_row[] = $arguments[$argument_group['name']][$argument['name']]; } else { $p_row[] = ''; } } } $csv_array[] = $p_row; /** * 处理货品 */ if ($has_goods) { foreach ($goods as $good) { $is_err = FALSE; $good = coding::decode_good($good); $g_row = array(); $g_row[] = ''; $g_row[] = ''; $g_row[] = ''; $g_row[] = ''; $g_row[] = $good['sku']; $g_row[] = ''; $g_row[] = $good['title']; $g_row[] = ''; $g_row[] = $good['on_sale'] == 1 ? 'Y' : 'N'; $g_row[] = $good['price']; $g_row[] = $good['market_price']; $g_row[] = ''; $option_name = ''; foreach ($this->attributes as $attribute) { if ($option_name != '') { $option_name .= '|'; } if (!isset($good['argumrs_struct']['items'][$attribute['id']])) { $is_err = TRUE; log::write('product_import_data_error', print_r($product, true), __FILE__, __LINE__); } else { $option_id = $good['argumrs_struct']['items'][$attribute['id']]; $option_name .= $attribute['options'][$option_id]['name']; } } $g_row[] = $option_name; if ($is_err == TRUE) { break; } $g_row[] = ''; $g_row[] = ''; $g_row[] = $good['cost']; $g_row[] = $good['store']; $g_row[] = $good['weight']; $g_row[] = ''; $g_row[] = ''; $g_row[] = ''; foreach ($this->features as $feature) { $g_row[] = ''; } foreach ($this->arguments as $argument_group) { foreach ($argument_group['items'] as $argument) { $g_row[] = ''; } } $csv_array[] = $g_row; } } //} } } } return $csv_array; }
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; } } }