public function get_product_page_by_site_id($site_id, $on_sale = 0) { $products = array(); $product_service = ProductService::get_instance(); $request_struct = array('where' => array('site_id' => $site_id, 'status' => 1)); if ($on_sale == 1) { $request_struct['where']['on_sale'] = 1; } $products = $product_service->query_assoc($request_struct); return $products; }
/** * 通过商品SKU创建货品SKU * * @param string $product_sku * @return string */ public static function create_good_sku($product_sku) { $good_service = ProductService::get_instance(); do { $good_sku = strtoupper($product_sku . '-' . substr(uniqid(), -4)); if (strlen($good_sku) > 32) { $good_sku = substr($good_sku, -32); } } while ($good_service->query_count(array('where' => array('sku' => $good_sku))) > 0); return $good_sku; }
/** * 设置商品的关联商品 * * @param array $product * @param array $relation_ids * @return boolean */ public static function set(&$product) { ORM::factory('product_relation')->where('product_id', $product['id'])->delete_all(); if (isset($product['pdt_relation_ids']) && count($product['pdt_relation_ids']) > 0) { $relations = ProductService::get_instance()->index(array('where' => array('id' => $product['pdt_relation_ids']))); foreach ($relations as $relation) { if ($relation['id'] != $product['id']) { Product_relationService::get_instance()->add(array('product_id' => $product['id'], 'relation_product_id' => $relation['id'])); } } } return TRUE; }
/** * 使用商品模板创建一个新的商品 * * @return array */ public static function get($site_id, $type = 0) { if (Product_templateService::get_instance()->is_template_exist($site_id)) { $template = Product_templateService::get_instance()->get_template_by_site($site_id); unset($template['id']); unset($template['create_timestamp']); unset($template['update_timestamp']); unset($template['descriptions']); $product = $template; $product['type'] = $type; $product['sku'] = strtoupper(uniqid()); } else { $product = array('site_id' => $site_id, 'status' => 0, 'sku' => strtoupper(uniqid()), 'category_id' => 0, 'on_sale' => 1, 'type' => $type); } $product_id = ProductService::get_instance()->add($product); return BLL_Product::get($product_id); }
public function search_good() { // 收集请求数据 $request_data = $this->input->get(); $request_struct_current = array('where' => array('type' => ProductService::PRODUCT_TYPE_GOODS, 'on_sale' => 1), 'limit' => array('per_page' => 6, 'page' => 1), 'like' => array(), 'orderby' => array('id' => 'DESC')); if (isset($request_data['page']) && !empty($request_data['page'])) { $request_struct_current['limit']['page'] = $request_data['page']; $returnData['page'] = $request_struct_current['limit']['page']; } // 当前支持的查询业务逻辑 if (isset($request_data['type']) && isset($request_data['keyword']) && !empty($request_data['keyword'])) { switch ($request_data['type']) { case 'id': $request_struct_current['where'][$request_data['type']] = intval(trim($request_data['keyword'])); break; case 'title': $request_struct_current['like'][$request_data['type']] = trim($request_data['keyword']); break; case 'sku': $request_struct_current['where'][$request_data['type']] = trim($request_data['keyword']); break; } } $goods = ProductService::get_instance()->index($request_struct_current); $returnData['content'] = $goods; $returnData['count'] = ceil(ProductService::get_instance()->count($request_struct_current) / $request_struct_current['limit']['per_page']); $returnData['page'] = isset($returnData['page']) ? $returnData['page'] : 1; //header('Content-Type: text/javascript; charset=UTF-8'); echo json_encode($returnData); exit; }
public function delete_all() { $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); //* 权限验证,数据验证,逻辑验证 ==根据业务逻辑定制== */ $site_ids = role::get_site_ids(); if (empty($site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } if (empty($request_data['product_id']) or empty($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']); if (!in_array($product['site_id'], $site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } $relation_ids = array(); $query_struct = array('where' => array('product_id' => $product, 'relation_product_id' => explode('-', $request_data['relation_ids']))); foreach (Product_relationService::get_instance()->query_assoc($query_struct) as $relation) { $relation_ids[$relation['relation_product_id']] = TRUE; } ORM::factory('product_relation')->where('product_id', $request_data['product_id'])->in('relation_product_id', array_keys($relation_ids))->delete_all(); ProductService::get_instance()->set($product['id'], array('update_timestamp' => time())); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $relation_ids; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 404); // 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 put() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { /* 初始化返回数据 */ $return_data = array(); /* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); if (!isset($request_data['relation_ids'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //* 权限验证 ==根据业务逻辑定制== */ $good_service = ProductService::get_instance(); $query_relation_ids = explode('-', $request_data['relation_ids']); $relation_ids = array(); $goods = array(); $query_struct = array('where' => array('id' => $query_relation_ids)); foreach ($good_service->query_assoc($query_struct) as $relation) { $relation_ids[$relation['id']] = true; $goods[] = $relation; } $list = new View($this->package_name . '/' . $this->class_name . '/list'); $list->goods = $goods; $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; } } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); 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('info'); $this->template->content = $content; /* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; /* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }
/** * 检查货品SKU是否重复 * * @param integer $site_id * @param string $sku * @param integer $good_id * @return boolean */ public static function good_sku_exists($sku, $good_id = 0) { $query_struct = array('where' => array('sku' => trim($sku))); if ($good_id > 0) { $query_struct['where']['id !='] = $good_id; } return ProductService::get_instance()->count($query_struct) > 0 ? TRUE : FALSE; }
/** * 通过商品ID设置货品的上下架状态 * * @param integer $product_id * @param integer $on_sale * @return boolean */ public static function set_on_sale($product_id, $on_sale) { $goods = ProductService::get_instance()->index(array('where' => array('product_id' => $product_id))); if (!empty($goods)) { foreach ($goods as $good) { $good['on_sale'] = $on_sale; ProductService::get_instance()->set($good['id'], $good); } } return TRUE; }
/** * 根据 类型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 add_products() { role::check('product_collection'); $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['collection_id']) || !is_numeric($request_data['collection_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $collection_service = CollectionService::get_instance(); $collection = $collection_service->get($request_data['collection_id']); $relation_query_struct = array('where' => array('collection_id' => $collection['id'])); $relations = Collection_product_relationService::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']; //* 调用后端服务获取数据 */ $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', 'site_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($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'); $this->template->content->product_ids = $product_ids; $this->template->content->category_list = $category_list; $this->template->content->brand_list = $brand_list; $this->template->content->collection_id = $collection['id']; } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
public function put_products() { role::check('product_rush'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = $ids = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); $relation_id = explode(',', $request_data['relation_id']); if (empty($relation_id) || !is_array($relation_id)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $products = ProductService::get_instance()->index(array('where' => array('id' => $relation_id))); $product_rush_service = Product_rushService::get_instance(); $product_rush = $product_rush_service->index(array()); foreach ($product_rush as $p) { $ids[] = $p['product_id']; } foreach ($products as $product) { if (!in_array($product['id'], $ids)) { $data = array(); $data['product_id'] = $product['id']; $data['store'] = $product['store']; $data['price_rush'] = $product['price']; $data['price'] = $product['price']; $data['title'] = $product['title']; $data['max_buy'] = 1; $data['start_time'] = date('Y-m-d') . ' 00:00:00'; $data['end_time'] = date('Y-m-d') . ' 23:59:59'; $result = $product_rush_service->add($data); if (!$result) { throw new MyRuntimeException('Internal Error', 500); } } } //* 补充&修改返回结构体 */ $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/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) { $this->_ex($ex, $return_struct, $request_data); } }
public function edit() { role::check('promotion_coupon'); // 收集请求数据 $request_data = $this->input->get(); $promotion = Mycpn_promotion::instance($request_data['id'])->get(); $cpns_id = $promotion['cpns_id']; if (!$cpns_id) { remind::set(Kohana::lang('o_global.access_denied'), request::referrer(), 'error'); } $promotion['money_from'] = sprintf('%.3f', $promotion['money_from']); $promotion['money_to'] = sprintf('%.3f', $promotion['money_to']); $coupon_schemes = Mycoupon_scheme::instance($cpns_id)->get(); //实例化session $session = Session::instance(); $this->template->content = new View("promotion/edit_cpn_promotion_" . $cpns_id); // extra process needed for IDs switch ($cpns_id) { case 3: // discount_cart_user_buy $all_ids = array(); if (isset($promotion['related_ids'])) { $query_struct = array('where' => array('id' => explode(',', $promotion['related_ids']))); $all_ids = Myuser::instance()->lists($query_struct); } $this->template->content->users_area = new View("promotion/partial/edit_users"); if ($session->get('sessionErrorData') === false) { $this->template->content->users_area->related_ids = explode(',', $promotion['related_ids']); } $this->template->content->users_area->thing = 'users'; $this->template->content->thing = 'users'; $this->template->content->users_area->all_ids = $all_ids; $this->template->content->users_area->relatedUsers = "relatedUsers"; $this->template->content->users_area->related_idsu = "related_ids"; $this->template->content->users_area->addUser = "******"; //$this->template->content->all_ids = $all_ids; //dialog start $this->template->content->dialog = new View("promotion/partial/dialog_user"); $userDiaStruct = array('dialog_form' => 'dialog-form-user', 'userSearchType' => 'userSearchType', 'userKeyword' => 'userKeyword', 'userSearchbtn' => 'userSearchbtn', 'userTable' => 'userTable', 'checkAll' => 'checkAll', 'users' => 'users'); $this->template->content->dialog->userDiaStruct = $userDiaStruct; //$this->template->content->dialog->all_ids = $all_ids; //显示字段设置 $js_user_field = 'var user_field = {"用户登录邮箱":"email"};'; $user_field = array('email' => "用户登录邮箱"); $this->template->content->js_user_field = $js_user_field; $this->template->content->users_area->user_field = $user_field; //dialog end break; case 4: // discount_cart_buy_product if ($session->get('sessionErrorData') === false) { // $this->template->content->products_area->related_ids = explode(',', $promotion['related_ids']); $related_ids = explode(',', $promotion['related_ids']); } else { $related_ids = $session->get('sessionErrorData'); $related_ids = $related_ids['related_ids']; } $request_struct = array('where' => array('on_sale' => 1, 'id' => $related_ids), 'orderby' => array('name_manage' => 'ASC')); $all_ids = ProductService::get_instance()->index($request_struct); $this->add_category_name($all_ids); $this->template->content->products_area = new View("promotion/partial/edit_products"); $this->template->content->products_area->all_ids = $all_ids; $this->template->content->products_area->thing = 'products'; $this->template->content->thing = 'products'; //dialog start $this->template->content->dialog = new View("promotion/partial/dialog_product"); $productDiaStruct = array('dialog_form' => 'dialog-form-product', 'productSearchType' => 'productSearchType', 'productKeyword' => 'productKeyword', 'productSearchbtn' => 'productSearchbtn', 'productTable' => 'productTable', 'checkAll' => 'checkAll', 'products' => 'products'); $this->template->content->dialog->productDiaStruct = $productDiaStruct; //显示字段设置 $js_product_field = 'var product_field = {\'SKU\':"sku","中文名称":"name_manage","商品名称":"title","分类名称":"category_name"};'; $product_field = array('sku' => 'SKU', "name_manage" => "中文名称", "title" => "商品名称", "category_name" => "分类名称"); $this->template->content->js_product_field = $js_product_field; $this->template->content->products_area->product_field = $product_field; //dialog end break; case 5: // discount_category $request_struct = array('where' => array(), 'orderby' => array('title_manage' => 'ASC')); $all_ids = CategoryService::get_instance()->index($request_struct); $all_ids = promotion::convert($all_ids); //dialog start $related_ids = 'related_ids'; $this->template->content->dialog = new View("promotion/partial/dialog_category"); $this->template->content->dialog->related_ids_name = $related_ids; if ($session->get('sessionErrorData') === false) { $this->template->content->dialog->related_ids = explode(',', $promotion['related_ids']); } $categoryDiaStruct = array('dialog_form' => 'dialog-form', 'categoryTable' => 'categoryTable'); $this->template->content->dialog->categoryDiaStruct = $categoryDiaStruct; $this->template->content->dialog->all_ids = $all_ids; //显示字段设置 $category_field = array("title_manage" => "中文名称", "title" => "分类名"); $tree = promotion::generate_tree($all_ids, 1, 0, $related_ids, $category_field, 'checkAll'); $this->template->content->dialog->tree = $tree; //dialog end break; } // 变量绑定 $this->template->content->promotion = $promotion; $this->template->content->coupon_schemes = $coupon_schemes; $this->template->content->coupon = Mycoupon::instance($promotion['cpn_id'])->get(); }
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; } } }
/** * 通过category_id获取该分类下的所有商品的指定字段 * @param $category_id int * @param $requestkeys array 为空时表示显示全部字段 * @return array */ public function get_products_by_category_id($category_id, $requestkeys = array()) { $result = array(); $return_array = array(); $query_struct = array('where' => array('category_id' => $category_id, 'status' => ProductService::PRODUCT_STATUS_PUBLISH), 'orderby' => array('update_time' => 'DESC'), 'limit' => array()); $result = ProductService::get_instance()->query_assoc($query_struct); if ($result) { foreach ($result as $val) { if (!empty($requestkeys)) { $return_array[$val['id']] = $val; util::filter_keys($return_array[$val['id']], $requestkeys); } else { $return_array[$val['id']] = $val; } } } return $return_array; }
public function do_edit() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); $request_data = $this->input->post(); /* 数据验证 ==根据业务逻辑定制== */ if (empty($request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 403); } $validResult = Validation::factory($request_data)->pre_filter('trim')->add_rules('id', 'required', 'digit')->add_rules('is_receive', 'digit')->add_rules('is_show', 'digit')->add_rules('reply_content', 'length[0,1024]'); if (!$validResult->validate()) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 403); } //* 逻辑验证 ==根据业务逻辑定制== */ // 调用底层服务 $productinquiry_service = ProductinquiryService::get_instance(); //* 调用后端服务获取数据 */ try { $productinquiry = $productinquiry_service->get($request_data['id']); if (!isset($productinquiry) || empty($productinquiry)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } $set_data = array('reply_content' => $request_data['reply_content'], 'is_show' => $request_data['is_show'], 'update_timestamp' => date('Y-m-d H:i:s'), 'status' => 1); if (!empty($request_data['is_receive']) && $productinquiry['is_receive'] != 1) { $product = ProductService::get_instance()->get($productinquiry['product_id']); if (!empty($productinquiry['user_id'])) { $email = Myuser::instance($productinquiry['user_id'])->get('email'); } else { $email = $productinquiry['email']; } $email_flag = 'reply_inquiry'; $title_param = array(); $title_param['{title}'] = strip_tags($product['title']); $content_param = array(); $content_param['{user_name}'] = strip_tags($productinquiry['user_name']); $content_param['{reply_content}'] = strip_tags($request_data['reply_content']); $content_param['{product_title}'] = strip_tags($product['title']); if (!mail::send_mail($email_flag, $email, '', $title_param, $content_param)) { throw new MyRuntimeException(Kohana::lang('o_global.mail_send_error'), 500); } else { $set_data['is_receive'] = 1; } //不套用邮件模板的方式 /* $subject = 'Reply to Inquiry About '.$product['title']; $content = ''; $content .= 'Dear '.$productinquiry['user_name'].' :<br>'; $content .= 'Having received your letter regarding the inquiry about '.$product['title'].'.<br>'; $content .= $productinquiry['reply_content'].'<br>'; $content .= 'If we may be of further service, please feel free to contact us.<br>'; $content .= 'Sincerely yours,<br>'; $content .= $site['name']; if(!mail::send($email,$subject,$content)) { throw new MyRuntimeException(Kohana::lang('o_global.mail_send_error'), 500); } else { $set_data['is_receive'] = 1; } */ } $productinquiry_service->set($productinquiry['id'], $set_data); } catch (MyRuntimeException $ex) { throw $ex; } //* 补充&修改返回结构体 */ $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()) { $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) { $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; } } }
/** * 批量更新商品的name_url */ public function update_product_name_url() { $site = Mysite::instance($this->site_id)->get(); if ($_POST) { $domain = $this->input->post('domain'); if (empty($domain) || $domain != $site['domain']) { remind::set(Kohana::lang('o_global.input_error'), 'site/update/update_product_name_url'); } $query_struct = array('where' => array('site_id' => $this->site_id), 'like' => array(), 'orderby' => array(), 'limit' => array('per_page' => 20, 'page' => 1)); $product_count = ProductService::get_instance()->count($query_struct); //1000个商品以下直接读取所有的商品 if ($product_count < 1000) { $query_struct['limit']['per_page'] = $product_count; $products = ProductService::get_instance()->index($query_struct); foreach ($products as $key => $value) { $product_name = $value['title']; if (!empty($product_name)) { //判断是否为中文的商品 if (preg_match('/[\\x7f-\\xff]/', $product_name)) { continue; } //过滤重复的NAME URL $product_name_url = product::create_uri_name($product_name, $value['site_id'], $value['id']); echo $product_name_url . "<br/>"; $data = array(); $data['uri_name'] = $product_name_url; $return_sturct = ProductService::get_instance()->set($value['id'], $data); } } } else { remind::set(Kohana::lang('o_site.product_request_check'), 'site/update/update_product_name_url'); } remind::set(Kohana::lang('o_site.sku_update_success'), 'site/update/update_product_name_url', 'success'); } $this->template->content = new View("site/update_product_name_url"); $this->template->content->site = $site; }
/** * 更新商品SEO信息 */ public function index() { $site = Mysite::instance()->get(); if ($_POST) { // 收集请求数据 $domain = $this->input->post('domain'); $category_id = $this->input->post('category_id'); $is_children = $this->input->post('is_children'); $meta_title = $this->input->post('meta_title'); $meta_keywords = $this->input->post('meta_keywords'); $meta_description = $this->input->post('meta_description'); if (empty($domain) || $domain != $site['domain']) { remind::set(Kohana::lang('o_site.seo_domain_cannot_null'), 'site/seo_manage', 'error'); } //分类ID数组 $category_ids = array(); //分类ID和名称的对应关系 $category_names = array(); //分类id不为空 if ($category_id > 0) { $category = CategoryService::get_instance()->get($category_id); //先处理选择分类的信息 $result = Seo_manageService::get_instance()->get_by_category_id($category_id); $set_data = array('category_id' => $category_id, 'is_children' => $is_children, 'meta_title' => $meta_title, 'meta_keywords' => $meta_keywords, 'meta_description' => $meta_description); if (isset($result) && $result['id'] > 0) { $set_data['update_timestamp'] = date('Y-m-d H:i:s', time()); Seo_manageService::get_instance()->set($result['id'], $set_data); } else { Seo_manageService::get_instance()->add($set_data); } //包含子分类 if ($is_children == Seo_manageService::SEO_CATEGORY_IS_CHILDREN) { //得到分类下的子分类 $childrens = CategoryService::get_instance()->get_childrens_by_category_id($category_id); foreach ($childrens as $key => $value) { //记录更新过的分类ID $category_ids[] = $value; $category_child = CategoryService::get_instance()->get($value); $category_names[$value] = $category_child['title']; //查看分类信息已经存在的SEO信息 $result_child = Seo_manageService::get_instance()->get_by_category_id($value); $set_data = array('category_id' => $value, 'is_children' => Seo_manageService::SEO_CATEGORY_IS_CHILDREN, 'meta_title' => $meta_title, 'meta_keywords' => $meta_keywords, 'meta_description' => $meta_description); if (isset($result_child) && $result_child['id'] > 0) { $set_data['update_timestamp'] = date('Y-m-d H:i:s', time()); Seo_manageService::get_instance()->set($result_child['id'], $set_data); } else { Seo_manageService::get_instance()->add($set_data); } } $category_ids[] = $category_id; $category_names[$category_id] = $category['title']; // 初始化默认查询条件[商品表] $reqeust_query_struct = array('where' => array('category_id' => $category_ids)); } else { $category_names[$category_id] = $category['title']; $reqeust_query_struct = array('where' => array('category_id' => $category_id)); } } else { //没有选择分类,默认更新全部分类 $result = Seo_manageService::get_instance()->get_by_category_id(Seo_manageService::SEO_CATEGORY_IS_NULL); $set_data = array('is_children' => Seo_manageService::SEO_CATEGORY_IS_CHILDREN, 'meta_title' => $meta_title, 'meta_keywords' => $meta_keywords, 'meta_description' => $meta_description); if (isset($result['id']) && $result['id'] > 0) { $set_data['update_timestamp'] = date('Y-m-d H:i:s', time()); Seo_manageService::get_instance()->set($result['id'], $set_data); } else { Seo_manageService::get_instance()->add($set_data); } //得到站点所有分类 $categories = CategoryService::get_instance()->get_categories(); foreach ($categories as $cate_key => $cate_value) { if ($cate_value['id'] <= 0) { continue; } $category_ids[] = $cate_value['id']; $category_names[$cate_value['id']] = $cate_value['title']; $result = Seo_manageService::get_instance()->get_by_category_id($cate_value['id']); $set_data = array('category_id' => $cate_value['id'], 'is_children' => Seo_manageService::SEO_CATEGORY_IS_CHILDREN, 'meta_title' => $meta_title, 'meta_keywords' => $meta_keywords, 'meta_description' => $meta_description, 'update_timestamp' => date('Y-m-d H:i:s', time())); if (isset($result['id']) && $result['id'] > 0) { Seo_manageService::get_instance()->set($result['id'], $set_data); } else { $set_data['create_timestamp'] = date('Y-m-d H:i:s', time()); Seo_manageService::get_instance()->add($set_data); } } $reqeust_query_struct = array('where' => array('category_id' => $category_ids)); } //获得总数 $count = ProductService::get_instance()->count($reqeust_query_struct); if ($count < 10000) { $products = ProductService::get_instance()->query_assoc($reqeust_query_struct); foreach ($products as $key => $value) { $product_name = $value['title']; $category_name = isset($category_names[$value['category_id']]) ? $category_names[$value['category_id']] : ''; $site_domain = $site['domain']; $goods_price = isset($value['goods_price']) ? $value['goods_price'] : 0; $title = str_replace('{product_name}', $product_name, $meta_title); $keywords = str_replace('{product_name}', $product_name, $meta_keywords); $description = str_replace('{product_name}', $product_name, $meta_description); $title = str_replace('{category_name}', $category_name, $title); $keywords = str_replace('{category_name}', $category_name, $keywords); $description = str_replace('{category_name}', $category_name, $description); $title = str_replace('{site_domain}', $site_domain, $title); $keywords = str_replace('{site_domain}', $site_domain, $keywords); $description = str_replace('{site_domain}', $site_domain, $description); $title = str_replace('{price}', $goods_price, $title); $keywords = str_replace('{price}', $goods_price, $keywords); $description = str_replace('{price}', $goods_price, $description); $data = array('meta_title' => $title, 'meta_keywords' => $keywords, 'meta_description' => $description); Product_detailService::get_instance()->set_by_product_id($value['id'], $data); } remind::set(Kohana::lang('o_global.update_success'), 'site/seo_manage', 'success'); } else { remind::set(Kohana::lang('o_site.product_request_check'), 'site/seo_manage'); } } //没有选择分类,默认更新全部分类 $data = Seo_manageService::get_instance()->get_by_category_id(Seo_manageService::SEO_CATEGORY_IS_NULL); // 分类列表默认关联第一个 $categorys_tree = CategoryService::get_instance()->get_tree('<option value={$id} {$selected}>{$spacer}{$title}</option>'); $this->template->content = new View("site/update_product_seo"); $this->template->content->data = $data; $this->template->content->site = $site; $this->template->content->categorys_tree = $categorys_tree; }
public function put_products() { role::check('product_auction'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); $this->template = new View('layout/commonfix_html'); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); $experiences = $this->input->post('experiences', array()); if (empty($request_data['relation_ids'])) { throw new MyRuntimeException('Input Error', 500); } if (isset($request_data['relation_ids']) && !empty($request_data['relation_ids'])) { $query_struct = array('where' => array('id' => $request_data['relation_ids'])); $pdata = ProductService::get_instance()->query_assoc($query_struct); $product_data = array(); foreach ($pdata as $p) { $product_data[$p['id']] = $p; } $auction_service = Product_auctionService::get_instance(); foreach ($request_data['relation_ids'] as $pid => $val) { $relation_query_struct = array('where' => array('product_id' => $val)); $pdata = $data = array(); $pdata = array_shift($auction_service->query_assoc($relation_query_struct)); $data['experience'] = isset($experiences[$pid]) ? 1 : 0; if (empty($pdata)) { $data['qty'] = 1; $data['status'] = 0; $data['product_id'] = $val; $data['price_start'] = '0.00'; $data['price_increase'] = '0.01'; $data['time_end'] = 3600 * 24; $data['time_reset'] = 10; if (isset($product_data[$val])) { $data['price'] = $product_data[$val]['price']; $data['name'] = $product_data[$val]['title']; if (!empty($product_data[$val]['default_image_id'])) { $data['default_image'] = '/att/product/' . $product_data[$val]['default_image_id']; } elseif (!empty($product_data[$val]['goods_productpic_relation_struct'])) { $img = json_decode($product_data[$val]['goods_productpic_relation_struct'], 1); if (isset($img['items'][0])) { $img = ProductpicService::get_instance()->get($img['items'][0]); $data['default_image'] = '/att/product/' . $img['image_id']; } } else { $data['default_image'] = '/att/product/'; } $data['category_ids'] = ',' . $product_data[$val]['category_id'] . ',' . $product_data[$val]['category_ids'] . ','; } $result = $auction_service->add($data); if (!$result) { throw new MyRuntimeException('Internal Error', 500); } } elseif ($pdata['id'] > 0) { $data['id'] = $pdata['id']; $data['status'] = $pdata['status'] == 2 ? 0 : $pdata['status']; if (isset($product_data[$val])) { $data['price'] = $product_data[$val]['price']; $data['name'] = $product_data[$val]['title']; if (!empty($product_data[$val]['default_image_id'])) { $data['default_image'] = '/att/product/' . $product_data[$val]['default_image_id']; } elseif (!empty($product_data[$val]['goods_productpic_relation_struct'])) { $img = json_decode($product_data[$val]['goods_productpic_relation_struct'], 1); if (isset($img['items'][0])) { $img = ProductpicService::get_instance()->get($img['items'][0]); $data['default_image'] = '/att/product/' . $img['image_id']; } } else { $data['default_image'] = '/att/product/'; } $data['category_ids'] = ',' . $product_data[$val]['category_id'] . ',' . $product_data[$val]['category_ids'] . ','; } $auction_service->update($data); } } } //* 补充&修改返回结构体 */ $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/empty_html'); //* 变量绑定 */ $this->template->content = new View('product/auction/put_products'); //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
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; }
/** * 设置商品特性关联 * * @param array $product * @return boolean */ public static function set_fetuoptrs(&$product) { $fetuoptrs = isset($product['pdt_fetuoptrs']) ? $product['pdt_fetuoptrs'] : array(); if ($product['classify_id'] > 0 && $fetuoptrs) { $product['product_featureoption_relation_struct'] = array(); $features = self::get_clsfeturs($product['classify_id']); if ($features) { $rs = array(); foreach ($fetuoptrs as $fetu_id => $opti_name) { if (isset($features[$fetu_id])) { $opti_name = trim($opti_name); if (!empty($opti_name)) { //输入项目 if ($features[$fetu_id]['type'] == 1) { //组合商品属性保存 Product_attributeoption_relationService::get_instance()->add(array('apply' => AttributeService::ATTRIBUTE_FEATURE, 'product_id' => $product['id'], 'attribute_id' => $fetu_id, 'attributeoption_id' => '0', 'attribute_value' => $opti_name)); $rs[$fetu_id] = '0'; } else { //选择项目 foreach ($features[$fetu_id]['options'] as $option) { if (strtolower(trim($option['name'])) === strtolower($opti_name)) { Product_attributeoption_relationService::get_instance()->add(array('apply' => AttributeService::ATTRIBUTE_FEATURE, 'product_id' => $product['id'], 'attribute_id' => $fetu_id, 'attributeoption_id' => $option['id'], 'attribute_value' => $opti_name)); $rs[$fetu_id] = (string) $option['id']; break; } } } } } } if (!empty($rs)) { $product_feature_relation_struct = json_encode(array('items' => array_keys($rs))); $product_featureoption_relation_struct = json_encode(array('items' => $rs)); //更新商品数据 $data = array('id' => $product['id'], 'product_feature_relation_struct' => $product_feature_relation_struct, 'product_featureoption_relation_struct' => $product_featureoption_relation_struct, 'update_time' => time()); ProductService::get_instance()->update($data); //更新货品数据 if (!empty($product['pdt_goods'])) { foreach ($product['pdt_goods'] as $index => $good) { $data = array('id' => $good['id'], 'product_feature_relation_struct' => $product_feature_relation_struct, 'product_featureoption_relation_struct' => $product_featureoption_relation_struct, 'update_time' => time()); ProductService::get_instance()->update($data); } } } } } return TRUE; }
protected function parse_sku($l, &$product) { static $skus = array(); $sku = $this->get($l, 'sku'); if ($sku !== '') { if (strlen($sku) <= 32) { $record = ProductService::get_instance()->query_row(array('where' => array('sku' => $sku))); if (empty($record)) { if (!isset($skus[$sku])) { $skus[$sku] = TRUE; return $sku; } else { $this->set_error(new MyRuntimeException('商品 SKU 不可重复')); } } else { $product['id'] = $record['id']; return $sku; } } else { $this->set_error(new MyRuntimeException('商品 SKU 长度不可超过 32 字节')); } } else { $this->set_error(new MyRuntimeException('商品 SKU 不可为空')); } return ''; }
/** * 提交商品修改 */ public function post() { // 初始化返回结构体 $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); $url_redirect = url::base() . 'product/gift_card'; $id = isset($request_data['id']) ? (int) $request_data['id'] : 0; $on_sale = isset($request_data['on_sale']) ? (int) $request_data['on_sale'] : 0; $price = (int) $request_data['price']; if ($price <= 0) { throw new MyRuntimeException("价格必须为整数", 500); } $data = array('front_visible' => 0, 'type' => ProductService::PRODUCT_TYPE_GIFT_CARD, 'price' => $price, 'title' => $request_data['title'], 'sku' => $request_data['sku'], 'on_sale' => $on_sale); if ($id > 0) { $data['id'] = $id; $data['update_time'] = time(); ProductService::get_instance()->update($data); } else { $data['create_time'] = time(); $id = ProductService::get_instance()->add($data); } if ($id <= 0) { throw new MyRuntimeException(Kohana::lang('o_product.no_save'), 500); } remind::set(Kohana::lang('o_product.edit_product_success'), $url_redirect, 'success'); } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data); } }
/** * 首页 */ public function index() { // 初始化返回数据 $return_data = array(); //请求结构体 $request_data = array(); try { /* 清空商品搜索表中的脏数据 */ /* 初始化默认查询结构体 */ $query_struct = array('where' => array(), 'like' => array(), 'orderby' => array(), 'limit' => array('per_page' => 100, 'page' => 1)); /* 清理已经删除的产品 */ do { $productsearches = ProductsearchService::get_instance()->query_assoc($query_struct); if (is_array($productsearches) && count($productsearches) > 0) { foreach ($productsearches as $value) { try { $product = ProductService::get_instance()->get($value['product_id']); if (!is_array($product) || $product['id'] < 1) { ProductsearchService::get_instance()->delete_by_productsearch_id($value['id']); } } catch (MyRuntimeException $ex) { Kohana::log('error', 'Productsearch error:' . $value['product_id'] . ' product not found.'); } } $query_struct['limit']['page']++; } else { break; } } while (1); //更新产品内容到产品搜索快照 获取产品描述 $query_struct = array('where' => array()); $productdescsections = array(); $descsections = Product_detailService::get_instance()->query_assoc($query_struct); if (!empty($descsections)) { foreach ($descsections as $val) { if (!empty($productdescsections[$val['product_id']])) { $productdescsections[$val['product_id']] .= ' ' . $val['content']; } else { $productdescsections[$val['product_id']] = $val['content']; } } } /* 初始化默认查询结构体 */ $query_struct = array('where' => array(), 'like' => array(), 'orderby' => array(), 'limit' => array('per_page' => 100, 'page' => 1)); do { $products = ProductService::get_instance()->query_assoc($query_struct); if (is_array($products) && count($products) > 0) { foreach ($products as $key => $value) { $productsearch = ProductsearchService::get_instance()->get_by_product_id($value['id']); $productsearch_data = array(); $productsearch_data['product_id'] = $value['id']; $productsearch_data['category_id'] = $value['category_id']; $productsearch_data['brand_id'] = $value['brand_id']; $productsearch_data['title'] = $value['title']; $productsearch_data['brief'] = $value['brief']; if (!empty($productdescsections[$value['id']])) { $productsearch_data['description'] = $productdescsections[$value['id']]; } if ($productsearch['id'] > 0) { ProductsearchService::get_instance()->set($productsearch['id'], $productsearch_data); } else { ProductsearchService::get_instance()->add($productsearch_data); } } $query_struct['limit']['page']++; } else { break; } } while (1); remind::set(Kohana::lang('o_global.set_success'), '/index/desktop', 'success'); } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 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 $product * @param array $wholesales * @return boolean */ public static function set(&$product) { self::delete($product['id']); $wholesales = isset($product['wholesales']) ? $product['wholesales'] : array(); if ($product['is_wholesale'] > 0 and isset($wholesales['items'])) { foreach ($wholesales['items'] as $index => $wholesale) { $wholesale['product_id'] = $product['id']; $wholesale['type'] = $wholesales['type']; $wholesales['items'][$index] = $wholesale; } while (TRUE) { $order = FALSE; foreach ($wholesales['items'] as $index => $wholesale) { $next = $index + 1; if (isset($wholesales['items'][$next])) { if ($wholesale['num_begin'] > $wholesales['items'][$next]['num_begin']) { $wholesales['items'][$index] = $wholesales['items'][$next]; $wholesales['items'][$next] = $wholesale; $order = TRUE; } elseif ($wholesale['num_begin'] == $wholesales['items'][$next]['num_begin']) { unset($wholesales['items'][$index]); } } } if ($order == FALSE) { break; } } foreach ($wholesales['items'] as $index => $wholesale) { $prev = $index - 1; if (isset($wholesales['items'][$prev])) { $wholesales['items'][$prev]['num_end'] = $wholesale['num_begin'] - 1; } } isset($wholesales['items'][0]['num_begin']) and $product['lowest_wholesale_num'] = $wholesales['items'][0]['num_begin']; foreach ($wholesales['items'] as $wholesale) { Product_wholesaleService::get_instance()->add($wholesale); } } else { $product['is_wholesale'] = 0; $product['lowest_wholesale_num'] = 0; } //更新商品数据 $data = array('id' => $product['id'], 'is_wholesale' => $product['is_wholesale'], 'lowest_wholesale_num' => $product['lowest_wholesale_num'], 'update_time' => time()); ProductService::get_instance()->update($data); //更新货品数据 if (isset($product['pdt_goods']) && is_array($product['pdt_goods'])) { foreach ($product['pdt_goods'] as $good) { if (isset($good['id'])) { $data['id'] = $good['id']; ProductService::get_instance()->update($data); } } } return TRUE; }
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 update_product($product_id) { try { $grade_count = 0; $grade_score = 0; $comments = ProductcommentService::get_instance()->query_assoc(array('where' => array('product_id' => $product_id, 'status' => ProductcommentService::COMMENT_EXAMINE_TRUE))); foreach ($comments as $comment) { if ($comment['grade'] > 0) { $grade_count++; $grade_score += $comment['grade']; } } ProductService::get_instance()->set($product_id, array('comments_count' => count($comments), 'graded_count' => $grade_count, 'star_average' => $grade_count > 0 ? round($grade_score / $grade_count, 1) : 0, 'update_time' => time())); } catch (MyRuntimeException $ex) { throw $ex; } }
/** * 添加订单 */ function do_add() { /*权限检查*/ role::check('order_add'); if ($_POST) { $post = new Validation($_POST); $post->pre_filter('trim'); $post->add_rules('shipping_firstname', 'required', 'length[1,200]'); $post->add_rules('shipping_lastname', 'required', 'length[1,200]'); $post->add_rules('shipping_country', 'required', 'length[1,200]'); $post->add_rules('shipping_state', 'length[1,200]'); $post->add_rules('shipping_city', 'required', 'length[1,200]'); $post->add_rules('shipping_address', 'required', 'length[1,200]'); $post->add_rules('shipping_zip', 'required', 'length[1,200]'); $post->add_rules('shipping_phone', 'required', 'length[1,200]'); $post->add_rules('shipping_mobile', 'length[1,200]'); $post->add_rules('billing_firstname', 'length[1,200]'); $post->add_rules('billing_lastname', 'length[1,200]'); $post->add_rules('billing_country', 'length[1,200]'); $post->add_rules('billing_state', 'length[1,200]'); $post->add_rules('billing_city', 'length[1,200]'); $post->add_rules('billing_address', 'length[1,200]'); $post->add_rules('billing_zip', 'length[1,200]'); $post->add_rules('billing_phone', 'length[1,200]'); $post->add_rules('billing_mobile', 'length[1,200]'); $post->add_rules('good_price', 'required', 'length[1,200]'); $post->add_rules('shipping_price', 'required', 'length[1,200]'); if (!$post->validate()) { $errors = $post->errors(); log::write('form_error', $errors, __FILE__, __LINE__); remind::set(Kohana::lang('o_order.user_address_wrong'), 'order/order_add/add_again', 'error'); } /* 添加主订单详情*/ $order_data = array(); $user_id = $this->input->post('user_id'); $email = $this->input->post('email'); $carrier = $this->input->post('carrier'); $currency_code = $this->input->post('code'); if ($user_id && $email && $currency_code) { /* 订单用户信息*/ $order_data['user_id'] = $user_id; $order_data['email'] = $email; /* 订单币种信息*/ $currency = Mycurrency::instance()->get_by_code($currency_code); $order_data['currency'] = $currency_code; $order_data['conversion_rate'] = $currency['conversion_rate']; /* 订单国家*/ $order_data['shipping_country'] = Mycountry::instance($post->shipping_country)->get('iso_code'); $order_data['billing_country'] = Mycountry::instance($post->billing_country)->get('iso_code'); /* 订单时间和IP信息*/ $order_data['data_add'] = date('Y-m-d H:i:s', time()); $order_data['IP'] = tool::get_long_ip(); /* 订单号生成*/ $order_num = ''; do { $temp = sprintf("%14.0f", date('ymd') . "00000" + rand(0, 99999) . "0000"); $exist_data = array(); $exist_data['order_num'] = $temp; if (!Myorder::instance()->exist($exist_data)) { $order_num = $temp; break; } } while (1); $order_data['order_num'] = $order_num; $order_data['order_status'] = '1'; $order_data['pay_status'] = '1'; $order_data['ship_status'] = '1'; $order_data['user_status'] = 'NULL'; $order_data['order_source'] = 'manual'; $order_data['total'] = $post->good_price + $post->shipping_price; $order_data['total_products'] = $post->good_price; $order_data['total_shipping'] = $post->shipping_price; $order_data['total_real'] = $order_data['total'] / $order_data['conversion_rate']; $order_data['total_discount'] = '0.00'; $order_data['total_paid'] = '0.00'; $order_data['shipping_firstname'] = $post->shipping_firstname; $order_data['shipping_lastname'] = $post->shipping_lastname; $order_data['shipping_state'] = $post->shipping_state; $order_data['shipping_city'] = $post->shipping_city; $order_data['shipping_address'] = $post->shipping_address; $order_data['shipping_zip'] = $post->shipping_zip; $order_data['shipping_phone'] = $post->shipping_phone; $order_data['shipping_mobile'] = $post->shipping_mobile; $order_data['billing_firstname'] = $post->billing_firstname; $order_data['billing_lastname'] = $post->billing_lastname; $order_data['billing_state'] = $post->billing_state; $order_data['billing_city'] = $post->billing_city; $order_data['billing_address'] = $post->billing_address; $order_data['billing_zip'] = $post->billing_zip; $order_data['billing_phone'] = $post->billing_phone; $order_data['billing_mobile'] = $post->billing_mobile; $order_data['carrier'] = $carrier; $order_data['active'] = 1; } else { remind::set(Kohana::lang('o_order.data_trans_wrong'), 'order/order_add', 'error'); } /* 添加订单,返回订单数据*/ $order_id = Myorder::instance()->add($order_data); $order = Myorder::instance($order_id)->get(); /* 添加订单产品信息*/ $session = Session::instance(); $cart_data = $session->get('cart_data'); if (isset($cart_data) && is_array($cart_data) && count($cart_data) && !empty($order['order_num'])) { foreach ($cart_data as $key => $rs) { $good_full_data = ProductService::get_instance()->get($key); $order_product_detail_data = array(); $order_product_detail_data['order_id'] = $order['id']; $order_product_detail_data['product_type'] = ProductService::PRODUCT_TYPE_GOODS; $order_product_detail_data['dly_status'] = 'storage'; //$order_product_detail_data['product_id'] = $good_full_data['product_id']; $order_product_detail_data['good_id'] = $key; $order_product_detail_data['quantity'] = $rs; $order_product_detail_data['sendnum'] = '0'; $order_product_detail_data['price'] = $good_full_data['price']; $order_product_detail_data['discount_price'] = $good_full_data['price']; $order_product_detail_data['weight'] = $good_full_data['weight']; //$order_product_detail_data['name'] = $good_full_data['name_manage']; $order_product_detail_data['name'] = $good_full_data['title']; $order_product_detail_data['SKU'] = $good_full_data['sku']; $order_product_detail_data['brief'] = $good_full_data['brief']; $order_product_detail_data['date_add'] = date('Y-m-d H:i:s', time()); $order_product_detail_data['link'] = product::permalink($good_full_data); order::do_order_product_detail_data_by_good(&$order_product_detail_data, $good_full_data, $good_full_data['default_image_id']); $order_product_detail = Myorder_product::instance()->add($order_product_detail_data); } } /*验证是否添加成功,添加成功返回订单号*/ if (!empty($order['order_num']) && $order_product_detail) { remind::set(Kohana::lang('o_order.add_order_success') . $order['order_num'], 'order/order', 'success'); } else { remind::set(Kohana::lang('o_order.add_order_wrong'), 'order/order_add', 'error'); } } }
public function post() { $request_data = $this->input->post(); $total_products = 0; $total = 0; if ($_POST) { if (!$request_data['id']) { remind::set(Kohana::lang('o_global.access_denied'), request::referrer()); } if (!is_numeric($request_data['discount_price']) || $request_data['discount_price'] < 0 || !is_numeric($request_data['amount']) || $request_data['amount'] < 0) { remind::set(Kohana::lang('o_global.illegal_data'), request::referrer()); } $data = Myorder_product::instance($request_data['id'])->get(); if (!$data['id']) { remind::set(Kohana::lang('o_global.access_denied'), request::referrer()); } $good = ProductService::get_instance()->get($data['good_id']); $order = Myorder::instance($data['order_id'])->get(); if ($good['store'] == '0') { remind::set(Kohana::lang('o_global.bad_request'), request::referrer()); } //得到合理的价格数值 if ($good['store'] == -1 && $request_data['amount'] > 999) { $request_data['amount'] = 999; } if ($good['store'] != -1 && $request_data['amount'] > $good['store']) { $request_data['amount'] = $good['store']; } $final_price = $request_data['discount_price'] * $order['conversion_rate']; $set_data = array('discount_price' => $final_price, 'quantity' => $request_data['amount']); if (Myorder_product::instance($data['id'])->edit($set_data)) { //重新查询数据库,计算价格 if (Myorder::instance($order['id'])->update_total()) { remind::set(Kohana::lang('o_global.update_success'), 'order/order/edit/id/' . $data['order_id'], 'success'); } else { remind::set(Kohana::lang('o_global.update_error'), 'order/order/edit/id/' . $data['order_id'], 'error'); } } else { remind::set(Kohana::lang('o_global.update_error'), 'order/order/edit/id/' . $data['order_id']); } } else { remind::set(Kohana::lang('o_global.update_error'), request::referrer()); } }
/** * 设置商品前台可见 * * @param integer $product_id * @param integer $front_visible * @return boolean */ public static function set_front_visible($product_id, $front_visible) { $product = self::get($product_id); if (!$product['id'] > 0) { return false; } ProductService::get_instance()->set($product_id, array('front_visible' => $front_visible, 'update_time' => time())); return TRUE; }