public function Edit($f3) { // 权限检查 $this->requirePrivilege('manage_goods_brand_listbrand'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $brand_id = $validator->digits()->min(1)->validate('brand_id'); if (!$brand_id) { $brand_id = 0; } $goodsBrandService = new GoodsBrandService(); $goodsBrand = $goodsBrandService->loadBrandById($brand_id); if (!$f3->get('POST')) { // 没有 post ,只是普通的显示 goto out_display; } unset($validator); $validator = new Validator($f3->get('POST')); $goodsBrand->brand_name = $validator->required()->validate('brand_name'); $goodsBrand->brand_desc = $validator->required()->validate('brand_desc'); $goodsBrand->brand_logo = $validator->validate('brand_logo'); $goodsBrand->is_custom = $validator->digits()->filter('ValidatorIntValue')->validate('is_custom'); $goodsBrand->custom_page = $f3->get('POST[custom_page]'); if (!$this->validate($validator)) { goto out_display; } $goodsBrand->save(); if (0 == $brand_id) { $this->addFlashMessage('新建商品品牌成功'); } else { $this->addFlashMessage('更新商品品牌成功'); } // 记录管理员日志 AdminLog::logAdminOperate('goods.brand.edit', '编辑品牌', $goodsBrand->brand_name); out_display: // 新建的品牌,reRoute 到编辑页面 if (!$brand_id) { RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Brand/Edit', array('brand_id' => $goodsBrand->brand_id), true)); } //给 smarty 模板赋值 $smarty->assign($goodsBrand->toArray()); $smarty->display('goods_brand_edit.tpl'); return; out_fail: // 失败从这里退出 RouteHelper::reRoute($this, '/Goods/Brand/ListBrand'); }
public function ListBrand($f3) { // 检查缓存 $cacheKey = md5(__NAMESPACE__ . '\\' . __CLASS__ . '\\' . __METHOD__); $brandArray = $f3->get($cacheKey); if (!empty($brandArray)) { goto out; } $brandService = new BrandService(); $brandArray = $brandService->fetchBrandIdNameArray(); $f3->set($cacheKey, $brandArray, 300); //缓存 5 分钟 out: $f3->expire(60); // 客户端缓存 1 分钟 Ajax::header(); echo Ajax::buildResult(null, null, $brandArray); }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits('pageNo 参数非法')->min(0, true, 'pageNo 参数非法')->validate('pageNo'); // 搜索参数数组 $searchFormQuery = array(); $searchFormQuery['g.category_id'] = $validator->required('商品分类不能为空')->digits('分类id非法')->min(1, true, '分类id非法')->filter('ValidatorIntValue')->validate('category_id'); // 这里支持多品牌查询 $searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id')); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax); $searchFormQuery['g.shop_price'] = array($shopPriceMin, $shopPriceMax); // 属性过滤 $filter = $validator->validate('filter'); // 排序 $orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy'); $orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir'); $orderByParam = array(); if (!empty($orderBy)) { $orderByParam = array(array($orderBy, $orderDir)); } //增加一些我们的缺省排序 $orderByParam[] = array('g.sort_order', 'desc'); $orderByParam[] = array('g.goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 45; // 每页固定显示 45 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Category|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $filter . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_category.tpl', $smartyCacheId)) { goto out_display; } $goodsCategoryService = new GoodsCategoryService(); $category = $goodsCategoryService->loadCategoryById($searchFormQuery['g.category_id'], 1800); if ($category->isEmpty()) { $this->addFlashMessage('分类[' . $searchFormQuery['category_id'] . ']不存在'); goto out_fail; } $smarty->assign('category', $category); $metaData = json_decode($category['meta_data'], true); $metaFilterArray = @$metaData['filterArray']; // 1. 我们需要在左侧显示分类层级结构 $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray($category['parent_meta_id'], false, 1800); $smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray); /** * 构造 Filter 数组,结构如下 * * array( * '商品品牌' => array( * filterKey => 'brand_id' * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ), * '颜色' => array( * filterKey => 'filter', * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ) * ) * */ $goodsFilterArray = array(); // filter 查询在这个条件下进行 $goodsFilterQueryCond = array_merge($this->searchExtraCondArray, array(array('g.category_id', '=', $searchFormQuery['g.category_id']))); // 2. 商品品牌查询 $goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0); $brandIdArray = array_map(function ($elem) { return $elem['brand_id']; }, $goodsBrandIdArray); if (!empty($brandIdArray)) { $goodsBrandService = new GoodsBrandService(); $goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray))); $filterBrandArray = array(); foreach ($goodsBrandArray as $brand) { $filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']); } if (!empty($filterBrandArray)) { $goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray); } } // 3. 查询属性过滤 if (!empty($metaFilterArray)) { $goodsTypeService = new GoodsTypeService(); foreach ($metaFilterArray as $filterItem) { $goodsTypeAttrItem = $goodsTypeService->loadGoodsTypeAttrItemById($filterItem['attrItemId']); if ($goodsTypeAttrItem->isEmpty()) { continue; } // 取得商品属性值列表 $goodsAttrItemValueArray = SearchHelper::search(SearchHelper::Module_GoodsAttrGoods, 'min(ga.goods_attr_id) as goods_attr_id, ga.attr_item_value', array_merge($goodsFilterQueryCond, array(array('ga.attr_item_id', '=', $filterItem['attrItemId']))), null, 0, 0, 'ga.attr_item_value'); if (!empty($goodsAttrItemValueArray)) { $filterValueArray = array(); foreach ($goodsAttrItemValueArray as $itemValue) { $filterValueArray[] = array('value' => $itemValue['goods_attr_id'], 'text' => $itemValue['attr_item_value']); } $goodsFilterArray[$goodsTypeAttrItem['meta_name']] = array('filterKey' => 'filter', 'filterValueArray' => $filterValueArray); } else { // 如果这个属性完全没有值(没有一个商品设过任何值),我们弄一个空的 $goodsFilterArray[$goodsTypeAttrItem['meta_name']] = array('filterKey' => 'filter', 'filterValueArray' => array()); } } } // 赋值给模板 if (!empty($goodsFilterArray)) { $smarty->assign('goodsFilterArray', $goodsFilterArray); } // 4. 商品查询 if (!empty($metaFilterArray)) { // 构造 attrItemId $metaFilterTypeIdArray = array(); foreach ($metaFilterArray as $metaFilterItem) { $metaFilterTypeIdArray[] = $metaFilterItem['attrItemId']; } // 构造 filter 参数,注意 filter 参数在 GoodsGoodsAttr 中具体解析 // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray, array(array('ga.filter', implode('.', $metaFilterTypeIdArray), $filter))); } else { // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); } $totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsAttr, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsAttr, 'g.goods_id, g.cat_id, g.goods_sn, g.goods_name, g.brand_id, g.goods_number, g.market_price' . ', g.shop_price, g.suppliers_id, g.virtual_buy_number, g.user_buy_number, g.user_pay_number' . ', (g.virtual_buy_number + g.user_pay_number) as total_buy_number', $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } $smarty->assign('goodsArray', $goodsArray); $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); // SEO 考虑,网页标题加上分类的名称 $smarty->assign('seo_title', $category['meta_name'] . ',' . $smarty->getTemplateVars('seo_title')); out_display: // 滑动图片广告 $goods_search_adv_slider = json_decode(bzf_get_option_value('goods_search_adv_slider'), true); if (!empty($goods_search_adv_slider)) { $smarty->assign('goods_search_adv_slider', $goods_search_adv_slider); } $smarty->display('goods_category.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits('pageNo 参数非法')->min(0, true, 'pageNo 参数非法')->validate('pageNo'); // 搜索参数数组 $searchFormQuery = array(); $searchKeywords = $validator->validate('keywords'); $searchFormQuery['g.goods_name'] = $searchKeywords; // 这里支持多品牌查询 $searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id')); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax); $searchFormQuery['g.shop_price'] = array($shopPriceMin, $shopPriceMax); // 排序 $orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy'); $orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir'); $orderByParam = array(); if (!empty($orderBy)) { $orderByParam = array(array($orderBy, $orderDir)); } //增加一些我们的缺省排序 $orderByParam[] = array('g.sort_order', 'desc'); $orderByParam[] = array('g.goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 45; // 每页固定显示 45 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) { goto out_display; } $goodsCategoryService = new GoodsCategoryService(); // 1. 我们需要在左侧显示分类层级结构 $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0, false, 1800); $smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray); /** * 构造 Filter 数组,结构如下 * * array( * '商品品牌' => array( * filterKey => 'brand_id' * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ), * '颜色' => array( * filterKey => 'filter', * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ) * ) * */ $goodsFilterArray = array(); // filter 查询在这个条件下进行 $goodsFilterQueryCond = array_merge(QueryBuilder::buildSearchParamArray(array('g.goods_name' => $searchKeywords)), $this->searchExtraCondArray); // 2. 商品品牌查询 $goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0); $brandIdArray = array_map(function ($elem) { return $elem['brand_id']; }, $goodsBrandIdArray); if (!empty($brandIdArray)) { $goodsBrandService = new GoodsBrandService(); $goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray))); $filterBrandArray = array(); foreach ($goodsBrandArray as $brand) { $filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']); } if (!empty($filterBrandArray)) { $goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray); } } if (!empty($goodsFilterArray)) { $smarty->assign('goodsFilterArray', $goodsFilterArray); } // 3. 商品属性过滤 TODO: 等以后扩展,看看 Search 怎么做属性过滤 // 4. 商品查询 // 构造 filter 参数,注意 filter 参数在 GoodsGoodsAttr 中具体解析 // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); $totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsAttr, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsAttr, 'g.goods_id, g.cat_id, g.goods_sn, g.goods_name, g.brand_id, g.goods_number, g.market_price' . ', g.shop_price, g.suppliers_id, g.virtual_buy_number, g.user_buy_number, g.user_pay_number' . ', (g.virtual_buy_number + g.user_pay_number) as total_buy_number', $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } $smarty->assign('goodsArray', $goodsArray); $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); // SEO 考虑,网页标题加上分类的名称 $smarty->assign('seo_title', '商品搜索,' . $searchKeywords . ',' . $smarty->getTemplateVars('seo_title')); out_display: // 滑动图片广告 $goods_search_adv_slider = json_decode(bzf_get_option_value('goods_search_adv_slider'), true); if (!empty($goods_search_adv_slider)) { $smarty->assign('goods_search_adv_slider', $goods_search_adv_slider); } $smarty->display('goods_search.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }