Exemplo n.º 1
0
 protected function prepareSearchParam($searchParamArray)
 {
     if (!is_array($searchParamArray)) {
         throw new \InvalidArgumentException('searchParam illegal : ' . var_export($searchParamArray, true));
     }
     // 调用父类先处理
     $searchParamArray = parent::prepareSearchParam($searchParamArray);
     $resultParamArray = array();
     foreach ($searchParamArray as $searchParam) {
         $addParam = true;
         if (is_array($searchParam) && count($searchParam) == 3) {
             switch ($searchParam[0]) {
                 /** 根据过滤规则,我们构造子查询
                  *  结构 array('ga.filter', '123.321.45', '100_20.34.67_78')
                  *  其中 123.321.45 为 attr_item_id
                  *  100_20.34.67_78 为 goods_attr_id 对应的值
                  */
                 case 'ga.filter':
                     // 不加入这个参数
                     $addParam = false;
                     // 没有值,不需要过滤
                     $trimSearchParam2 = trim(str_replace('.', '', $searchParam[2]));
                     // 有可能没有值,全部为点 "..."
                     if (empty($searchParam[1]) || empty($searchParam[2]) || empty($trimSearchParam2)) {
                         break;
                     }
                     $goodsTypeService = new GoodsTypeService();
                     // 构造子查询
                     $queryJoinTable = '';
                     $firstJoinTable = '';
                     $queryCondArray = array();
                     // 构造子查询
                     $attrItemIdArray = explode('.', $searchParam[1]);
                     $goodsAttrIdStrArray = explode('.', $searchParam[2]);
                     $count = min(count($attrItemIdArray), count($goodsAttrIdStrArray));
                     for ($index = 0; $index < $count; $index++) {
                         $attrItemId = abs(intval($attrItemIdArray[$index]));
                         $goodsAttrIdArray = explode('_', $goodsAttrIdStrArray[$index]);
                         // 跳过无效值
                         if ($attrItemId <= 0 || empty($goodsAttrIdArray)) {
                             continue;
                         }
                         $goodsAttrItemCond = array();
                         foreach ($goodsAttrIdArray as $goodsAttrId) {
                             $goodsAttrId = abs(intval($goodsAttrId));
                             $goodsAttr = $goodsTypeService->loadGoodsAttrById($goodsAttrId);
                             // 无效的属性,返回
                             if ($goodsAttr->isEmpty()) {
                                 continue;
                             }
                             $goodsAttrItemCond[] = array("attr_item_value = ?", $goodsAttr['attr_item_value']);
                         }
                         if (!empty($goodsAttrItemCond)) {
                             $condArray = QueryBuilder::buildAndFilter(array(array('attr_item_id = ?', $attrItemId), QueryBuilder::buildOrFilter($goodsAttrItemCond)));
                             $tmpTableName = 'ga' . $index;
                             $tmpTable = '(select distinct(goods_id) from ' . DataMapper::tableName('goods_attr') . ' where ' . array_shift($condArray) . ') as ' . $tmpTableName;
                             $queryCondArray = array_merge($queryCondArray, $condArray);
                             if (empty($queryJoinTable)) {
                                 $queryJoinTable = $tmpTable;
                                 $firstJoinTable = $tmpTableName;
                             } else {
                                 $queryJoinTable .= ' INNER JOIN ' . $tmpTable . ' on ' . $firstJoinTable . '.goods_id = ' . $tmpTableName . '.goods_id ';
                             }
                         }
                     }
                     // 构造子查询
                     $this->searchTable = DataMapper::tableName('goods') . ' as g INNER JOIN ' . '(select distinct(' . $firstJoinTable . '.goods_id) from (' . $queryJoinTable . ')) as ga on g.goods_id = ga.goods_id';
                     /**
                      * 这里是一个很 tricky 的构造查询的方法
                      *
                      * 我们不想拼接 SQL 语句,比如 attr_item_value = $attr_item_value,
                      * 而是采用 array('attr_item_value = ?', $attr_item_value),这样可以 SQL Bind 避免 SQL 注入
                      *
                      * 由于前面的 子查询带了很多 ? 查询,所以我们需要把参数值 unshift 到第一个的位置
                      *
                      */
                     // 头部压入一个空条件
                     array_unshift($queryCondArray, '1=1');
                     // 把这个参数压入到头部
                     array_unshift($resultParamArray, $queryCondArray);
                     break;
                 default:
                     break;
             }
         }
         //  是否加入参数
         if ($addParam) {
             $resultParamArray[] = $searchParam;
         }
     }
     return $resultParamArray;
 }
Exemplo n.º 2
0
 public function post($f3)
 {
     // 权限检查
     $this->requirePrivilege('manage_goods_edit_edit_post');
     global $smarty;
     // 参数验证
     $validator = new Validator($f3->get('GET'));
     $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id');
     if (!$this->validate($validator)) {
         goto out_fail;
     }
     unset($validator);
     $goodsBasicService = new GoodsBasicService();
     $goods = $goodsBasicService->loadGoodsById($goods_id);
     if ($goods->isEmpty()) {
         $this->addFlashMessage('商品ID[' . $goods_id . ']非法');
         goto out_fail;
     }
     // 商品类型属性做验证
     $validator = new Validator($f3->get('POST'));
     //表单数据验证、过滤
     $type_id = $validator->digits()->min(1)->validate('type_id');
     if (!$this->validate($validator)) {
         goto out_fail;
     }
     $goodsTypeService = new GoodsTypeService();
     // 商品类型发生了变化,清除所有旧的属性
     if ($goods['type_id'] != $type_id) {
         $goodsTypeService->removeAllGoodsAttrItemValue($goods_id);
         $goods->type_id = $type_id;
         $goods->save();
     }
     // 获得属性值列表
     $goodsAttrValueArray = $f3->get('POST[goodsAttrValueArray]');
     if (!empty($goodsAttrValueArray)) {
         foreach ($goodsAttrValueArray as $goodsAttrValueInfo) {
             $goodsAttrValueInfo = @json_decode($goodsAttrValueInfo, true);
             if (empty($goodsAttrValueInfo)) {
                 continue;
             }
             // 更新属性值
             $goodsAttrValue = $goodsTypeService->loadGoodsAttrById(intval($goodsAttrValueInfo['goods_attr_id']));
             $goodsAttrValue->goods_id = $goods_id;
             $goodsAttrValue->attr_item_id = $goodsAttrValueInfo['meta_id'];
             $goodsAttrValue->attr_item_value = $goodsAttrValueInfo['attr_item_value'];
             $goodsAttrValue->save();
         }
     }
     // 成功,显示商品详情
     $this->addFlashMessage('商品类型属性保存成功');
     //清除缓存,确保商品显示正确
     ClearHelper::clearGoodsCacheById($goods_id);
     RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Type', array('goods_id' => $goods_id), true));
     return;
     out_fail:
     RouteHelper::reRoute($this, '/Goods/Search');
 }
Exemplo n.º 3
0
 public function get($f3)
 {
     // 权限检查
     $this->requirePrivilege('manage_goods_create');
     // 参数验证
     $validator = new Validator($f3->get('GET'));
     $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id');
     if (!$this->validate($validator)) {
         goto out_fail;
     }
     // 取得商品信息
     $goodsBasicService = new GoodsBasicService();
     $goods = $goodsBasicService->loadGoodsById($goods_id);
     if ($goods->isEmpty()) {
         $this->addFlashMessage('非法商品ID');
         goto out_fail;
     }
     $authAdminUser = AuthHelper::getAuthUser();
     // 1. 复制 goods 信息
     $goodsArray = $goods->toArray();
     unset($goodsArray['goods_id']);
     // 清除主键
     // 新商品缺省为下线状态
     $goodsArray['is_on_sale'] = 0;
     // 清除购买数量统计
     $goodsArray['user_buy_number'] = 0;
     $goodsArray['user_pay_number'] = 0;
     // 设置复制人
     $goodsArray['admin_user_id'] = $authAdminUser['user_id'];
     $goodsArray['admin_user_name'] = $authAdminUser['user_name'];
     // 处理商品的规格
     if (!empty($goodsArray['goods_spec'])) {
         $goodsSpecService = new GoodsSpecService();
         $goodsSpecService->initWithJson($goodsArray['goods_spec']);
         $goodsSpecService->clearGoodsSpecImgIdArray();
         // 清除图片 ID 的关联
         $goodsArray['goods_spec'] = $goodsSpecService->getJsonStr();
         unset($goodsSpecService);
     }
     $goodsArray['add_time'] = Time::gmTime();
     $newGoods = $goodsBasicService->loadGoodsById(0);
     $newGoods->copyFrom($goodsArray);
     $newGoods->save();
     // 更新 goods_sn
     $newGoods->goods_sn = $f3->get('sysConfig[goods_sn_prefix]') . $newGoods['goods_id'];
     $newGoods->save();
     unset($goodsArray);
     // 2. 复制 goods_attr 信息
     if ($goods->type_id > 0) {
         $goodsTypeService = new GoodsTypeService();
         $goodsAttrValueArray = $goodsTypeService->fetchGoodsAttrItemValueArray($goods->goods_id, $goods->type_id);
         foreach ($goodsAttrValueArray as $goodsAttrValue) {
             $goodsAttr = $goodsTypeService->loadGoodsAttrById(0);
             $goodsAttr->goods_id = $newGoods->goods_id;
             $goodsAttr->attr_item_id = $goodsAttrValue['meta_id'];
             $goodsAttr->attr_item_value = $goodsAttrValue['attr_item_value'];
             $goodsAttr->save();
             unset($goodsAttr);
         }
         unset($goodsAttrValueArray);
         unset($goodsTypeService);
     }
     // 3. 复制 goods_gallery 信息
     $goodsGalleryService = new GoodsGalleryService();
     $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsId($goods_id);
     foreach ($goodsGalleryArray as $goodsGalleryItem) {
         // 新建一个 goods_gallery 记录
         $goodsGallery = $goodsGalleryService->loadGoodsGalleryById(0);
         unset($goodsGalleryItem['img_id']);
         $goodsGallery->copyFrom($goodsGalleryItem);
         $goodsGallery->goods_id = $newGoods['goods_id'];
         $goodsGallery->save();
         unset($goodsGallery);
     }
     unset($goodsGalleryArray);
     unset($goodsGalleryService);
     // 4. 复制 goods_team 信息
     $goodsTeam = $goodsBasicService->loadGoodsTeamByGoodsId($goods_id);
     if (!$goodsTeam->isEmpty()) {
         $goodsTeamInfo = $goodsTeam->toArray();
         unset($goodsTeamInfo['team_id']);
         $goodsTeamInfo['goods_id'] = $newGoods['goods_id'];
         $newGoodsTeam = new DataMapper('goods_team');
         $newGoodsTeam->copyFrom($goodsTeamInfo);
         $newGoodsTeam->save();
         unset($newGoodsTeam);
         unset($goodsTeamInfo);
         unset($goodsTeam);
     }
     // 5. 复制 link_goods 信息
     $linkGoodsArray = $goodsBasicService->fetchSimpleLinkGoodsArray($goods_id);
     foreach ($linkGoodsArray as $linkGoodsItem) {
         unset($linkGoodsItem['link_id']);
         $linkGoodsItem['goods_id'] = $newGoods['goods_id'];
         $linkGoodsItem['admin_id'] = $authAdminUser['user_id'];
         $linkGoods = new DataMapper('link_goods');
         $linkGoods->copyFrom($linkGoodsItem);
         $linkGoods->save();
         unset($linkGoods);
     }
     unset($linkGoodsArray);
     // 6. 复制 goods_promote 信息
     $goodsPromote = $goodsBasicService->loadGoodsPromoteByGoodsId($goods_id);
     if (!$goodsPromote->isEmpty()) {
         $goodsPromoteInfo = $goodsPromote->toArray();
         unset($goodsPromoteInfo['promote_id']);
         $goodsPromoteInfo['goods_id'] = $newGoods['goods_id'];
         $newGoodspromote = new DataMapper('goods_promote');
         $newGoodspromote->copyFrom($goodsPromoteInfo);
         $newGoodspromote->save();
         unset($newGoodspromote);
     }
     unset($goodsPromote);
     // 记录编辑日志
     $goodsLogContent = '从 [' . $goods_id . '] 复制过来';
     $goodsLogService = new GoodsLogService();
     $goodsLogService->addGoodsLog($newGoods['goods_id'], $authAdminUser['user_id'], $authAdminUser['user_name'], '复制商品', $goodsLogContent);
     $this->addFlashMessage('复制新建商品成功');
     RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Edit', array('goods_id' => $newGoods['goods_id']), true));
     return;
     //正常返回
     out_fail:
     RouteHelper::reRoute($this, '/Goods/Search');
 }