/** * 加载基本商品类型相关数据 * * @param array $product * @return boolean */ public static function load(&$product) { $product['classify_id'] && ($product['attributes'] = BLL_Product_Attribute::get_clsattrrs($product['classify_id'])); //处理简单商品是否属于可配置商品或者组合商品的一项 $p_configurable = Product_assemblyService::get_instance()->query_assoc(array('where' => array('product_id' => $product['id'], 'assembly_type' => ProductService::PRODUCT_TYPE_CONFIGURABLE), 'orderby' => array('id' => 'asc'), 'limit' => array('per_page' => 1))); $configurable_id = isset($p_configurable[0]['assembly_id']) && $p_configurable[0]['assembly_id'] > 0 ? $p_configurable[0]['assembly_id'] : 0; if ($configurable_id > 0) { $product['configurable_id'] = $configurable_id; !$product['descsections'] && ($product['descsections'] = BLL_Product_Detail::get($configurable_id)); !$product['fetuoptrs'] && ($product['fetuoptrs'] = BLL_Product_Feature::get_fetuoptrs($configurable_id)); !$product['relations'] && ($product['relations'] = BLL_Product_Relation::get($configurable_id)); !$product['wholesales'] && ($product['wholesales'] = BLL_Product_Wholesale::get($configurable_id)); //可配置商品的关联图片更新检查 if (!empty($product['goods_productpic_relation_struct'])) { $configurable_pictures = BLL_Product_Picture::get($configurable_id); $productpicService = ProductpicService::get_instance(); $product = coding::decode_attribute($product, 'goods_productpic_relation_struct'); foreach ($product['goods_productpic_relation_struct']['items'] as $pic_id) { if (isset($configurable_pictures[$pic_id])) { $query_struct = array('where' => array('product_id' => $product['id'], 'image_id' => $configurable_pictures[$pic_id]['image_id'])); if (!$productpicService->count($query_struct) > 0) { $productpic_data = array('product_id' => $product['id'], 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'title' => $configurable_pictures[$pic_id]['title'], 'image_id' => $configurable_pictures[$pic_id]['image_id']); $productpic_row_id = $productpicService->add($productpic_data); $product['pictures'][$pic_id] = $configurable_pictures[$pic_id]; } } } } } //获取商品规格 $attroptrs = array(); if ($product['attribute_struct_default']) { $attroptrs = json_decode($product['attribute_struct_default'], TRUE); $attroptrs = $attroptrs['items']; if (is_array($attroptrs)) { foreach ($attroptrs as $aid => $oid) { $attroptrs[$aid] = $oid[0]; } } unset($product['attribute_struct_default']); } else { $attroptrs = self::get_pdt_attroptrs($product['id']); } if (!empty($attroptrs)) { $product['attroptrs'] = $attroptrs; $product['attrrs'] = array_keys($attroptrs); } return TRUE; }
/** * 通过商品ID删除商品图片 * * @param integer $product_id * @return boolean */ public static function delete($product_id) { $ProductpicService = ProductpicService::get_instance(); $pictures = $ProductpicService->index(array('where' => array('product_id' => $product_id))); if (empty($pictures)) { return FALSE; } foreach ($pictures as $picture) { if (empty($picture['image_id'])) { continue; } $pic_count = $ProductpicService->count(array('where' => array('image_id' => $picture['image_id']))); if ($pic_count <= 1) { $ProductpicService->delete_productpic_attachment($picture); } } ORM::factory('productpic')->where('product_id', $product_id)->delete_all(); return TRUE; }
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 pic_delete() { $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['product_id']) or empty($request_data['picture_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } // 调用底层服务 $productpic_service = ProductpicService::get_instance(); // 验证图片是否存在 //$picture = $productpic_service->get($request_data['picture_id']); /* 验证图片是否正在被关联 $query_struct = array('where'=>array( 'productpic_id' => $picture['id'], ));*/ //if (Product_attributeoption_productpic_relationService::get_instance()->query_count($query_struct) > 0) { // throw new MyRuntimeException(Kohana::lang('o_product.pic_has_relation'), 400); //} /*delete by zhu $good_pic_relations = Goods_productpic_relationService::get_instance()->query_assoc($query_struct); if (!empty($good_pic_relations)) { $good_ids = array(); foreach ($good_pic_relations as $good_pic_relation) { $good_ids[$good_pic_relation['goods_id']] = TRUE; } $query_struct = array('where' => array( 'id' => array_keys($good_ids), )); $goods = ProductService::get_instance()->query_assoc($query_struct); if (count($goods) != count($good_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } foreach ($goods as $key => $good) { $good = coding::decode_good($good); if (!empty($good['goods_productpic_relation_struct']['items'])) { $idx = array_search($picture['id'], $good['goods_productpic_relation_struct']['items']); if ($idx !== FALSE) { unset($good['goods_productpic_relation_struct']['items'][$idx]); if (empty($good['goods_productpic_relation_struct']['items'])) { unset($good['goods_productpic_relation_struct']['items']); } } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $good['update_timestamp'] = time(); $goods[$key] = coding::encode_good($good); } foreach ($goods as $good) { ProductService::get_instance()->set($good['id'], $good); } ORM::factory('goods_productpic_relation')->where('productpic_id', $picture['id'])->delete_all(); } ORM::factory('product_attributeoption_productpic_relation') ->where('productpic_id', $request_data['picture_id'])->delete_all();*/ $productpic_service->delete_productpic($request_data['picture_id'], $request_data['product_id']); $pictures = $productpic_service->get_stand_pic_list_by_product_id($request_data['product_id']); $current_picture = 0; $default_picture = 0; foreach ($pictures as $idx => $picture) { if ($picture['is_default'] == ProductpicService::PRODUCTPIC_IS_DEFAULT_TRUE) { $current_picture = $idx; $default_picture = $idx; break; } } $return_data['pictures'] = $pictures; $return_data['current_picture'] = $current_picture; $return_data['default_picture'] = $default_picture; //Cache::remove('product_pictures'.$request_data['id']); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = Kohana::lang('o_global.delete_success'); $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // 仅允许AJAX调用 //throw new MyRuntimeException('Not Implemented',501); // html 输出 //* 模板输出 */ $this->template->return_struct = $return_struct; $content = new View('info'); //* 变量绑定 */ $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) { $this->_ex($ex, $return_struct, $request_data); } }
protected function set_product($l) { $product_id = 0; $str_bad = array(" ", '(', ')', '(', ')'); $sku = str_replace($str_bad, '', $l['2']); $title = trim($l['5']); $datas = array('status' => 0, 'front_visible' => 1, 'classify_id' => (int) $l['0'], 'category_id' => (int) $l['1'], 'sku' => $sku, 'name_manage' => $l['4'], 'title' => $title, 'brand_id' => (int) $l['6'], 'on_sale' => (int) $l['7'], 'price' => $l['8'], 'market_price' => $l['9'], 'cost' => $l['14'], 'brief' => $l['12'], 'store' => (int) $l['15'], 'weight' => (int) $l['16'], 'meta_title' => $l['17'], 'meta_keywords' => $l['18'], 'meta_description' => $l['19'], 'create_time' => time(), 'update_timestamp' => time()); //save data $query_struct = array('orderby' => array('id' => 'ASC'), 'limit' => array('page' => 1, 'per_page' => 1)); if (!empty($sku)) { $query_struct['where'] = array('sku' => $sku); } else { $query_struct['where'] = array('title' => $title); } $product = ProductService::get_instance()->query_assoc($query_struct); if (!empty($product)) { $product_id = $product[0]['id']; $datas['id'] = $product_id; } if ($product_id > 0) { ProductService::get_instance()->update($datas); } else { $product_id = ProductService::get_instance()->create($datas); } //save pic $pictures = str_replace(',', ',', $l['10']); $pictures = trim($pictures, ','); if ($product_id > 0 && !empty($pictures)) { $i = 0; $pictures = explode(',', $pictures); foreach ($pictures as $pic_name) { $str_bad = array(" ", ",", "\t", "\r", "\n", "\r\n"); $pic_name = str_replace($str_bad, '', $pic_name); if (empty($pic_name)) { continue; } $img_file = ATTPATH . 'upimg/' . iconv('UTF-8', 'GBK//IGNORE', $pic_name); if (!file_exists($img_file)) { $this->set_error(new MyRuntimeException("图片不存在:" . $pic_name)); continue; } $str_bad = array('(', ')', '(', ')'); $picname = str_replace($str_bad, '', $pic_name); $default_img_id = strtolower(substr($picname, 0, strrpos($picname, '.'))); if (file_exists(ATTPATH . 'product/' . $default_img_id . '/default.jpg')) { $img_id = $default_img_id; } else { $att = AttService::get_instance("product"); $img_id = $att->save_default_img($img_file, $default_img_id); } if (empty($img_id)) { $this->set_error(new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed') . $pic_name)); continue; } $query_struct = array('where' => array('product_id' => $product_id, 'image_id' => $img_id), 'orderby' => array('id' => 'ASC'), 'limit' => array('page' => 1, 'per_page' => 1)); $pic_data = ProductpicService::get_instance()->query_assoc($query_struct); if (empty($pic_data)) { $productpic_data = array('product_id' => $product_id, 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'image_id' => $img_id); $productpic_id = ProductpicService::get_instance()->add($productpic_data); if ($i == 0 && $productpic_id > 0) { ProductpicService::get_instance()->set_default_pic_by_productpic_id($productpic_id, $product_id); } } $i++; } } }