goods_class.model.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. /**
  3. * 商品类别模型
  4. *
  5. *
  6. *
  7. *
  8. * by abc 网店技术交流中心 www.abc.com 开发
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. class goods_classModel extends Model
  12. {
  13. /**
  14. * 缓存数据
  15. */
  16. protected $cachedData;
  17. /**
  18. * 缓存数据 原H('goods_class')形式
  19. */
  20. protected $gcForCacheModel;
  21. public function __construct() {
  22. parent::__construct('goods_class');
  23. }
  24. /**
  25. * 获取缓存数据
  26. *
  27. * @return array
  28. * array(
  29. * 'data' => array(
  30. * // Id => 记录
  31. * ),
  32. * 'parent' => array(
  33. * // 子Id => 父Id
  34. * ),
  35. * 'children' => array(
  36. * // 父Id => 子Id数组
  37. * ),
  38. * 'children2' => array(
  39. * // 1级Id => 3级Id数组
  40. * ),
  41. * )
  42. */
  43. public function getCache()
  44. {
  45. if ($this->cachedData) {
  46. return $this->cachedData;
  47. }
  48. $data = rkcache('gc_class');
  49. if (!$data)
  50. {
  51. $data = array();
  52. foreach ((array) $this->getGoodsClassList(array()) as $v) {
  53. $id = $v['gc_id'];
  54. $pid = $v['gc_parent_id'];
  55. $data['data'][$id] = $v;
  56. $data['parent'][$id] = $pid;
  57. $data['children'][$pid][] = $id;
  58. }
  59. foreach ((array) $data['children'][0] as $id)
  60. {
  61. foreach ((array) $data['children'][$id] as $cid)
  62. {
  63. foreach ((array) $data['children'][$cid] as $ccid) {
  64. $data['children2'][$id][] = $ccid;
  65. }
  66. }
  67. }
  68. wkcache('gc_class', $data);
  69. }
  70. return $this->cachedData = $data;
  71. }
  72. /**
  73. * 删除缓存数据
  74. */
  75. public function dropCache() {
  76. $this->cachedData = null;
  77. $this->gcForCacheModel = null;
  78. dkcache('gc_class');
  79. dkcache('all_categories');
  80. }
  81. /**
  82. * 类别列表
  83. *
  84. * @param array $condition 检索条件
  85. * @return array 返回二位数组
  86. */
  87. public function getGoodsClassList($condition, $field = '*') {
  88. $result = $this->table('goods_class')->field($field)->where($condition)->order('gc_parent_id asc,gc_sort asc,gc_id asc')->limit(false)->select();
  89. return $result;
  90. }
  91. /**
  92. * 从缓存获取全部分类
  93. */
  94. public function getGoodsClassListAll() {
  95. $data = $this->getCache();
  96. return array_values((array) $data['data']);
  97. }
  98. /**
  99. * 从缓存获取全部分类 分类id作为数组的键
  100. */
  101. public function getGoodsClassIndexedListAll() {
  102. $data = $this->getCache();
  103. return (array) $data['data'];
  104. }
  105. /**
  106. * 从缓存获取分类 通过分类id数组
  107. *
  108. * @param array $ids 分类id数组
  109. */
  110. public function getGoodsClassListByIds($ids) {
  111. $data = $this->getCache();
  112. $ret = array();
  113. foreach ((array) $ids as $i) {
  114. if ($data['data'][$i]) {
  115. $ret[] = $data['data'][$i];
  116. }
  117. }
  118. return $ret;
  119. }
  120. /**
  121. * 从缓存获取分类 通过上级分类id
  122. *
  123. * @param int $pid 上级分类id 若传0则返回1级分类
  124. */
  125. public function getGoodsClassListByParentId($pid)
  126. {
  127. $data = $this->getCache();
  128. $ret = array();
  129. foreach ((array) $data['children'][$pid] as $i)
  130. {
  131. if ($data['data'][$i]) {
  132. $ret[] = $data['data'][$i];
  133. }
  134. }
  135. return $ret;
  136. }
  137. /**
  138. * 从缓存获取分类 通过分类id
  139. *
  140. * @param int $id 分类id
  141. */
  142. public function getGoodsClassInfoById($id) {
  143. $data = $this->getCache();
  144. return $data['data'][$id];
  145. }
  146. /**
  147. * 返回缓存数据 原H('goods_class')形式
  148. */
  149. public function getGoodsClassForCacheModel()
  150. {
  151. if ($this->gcForCacheModel)
  152. return $this->gcForCacheModel;
  153. $data = $this->getCache();
  154. $r = $data['data'];
  155. $p = $data['parent'];
  156. $c = $data['children'];
  157. $c2 = $data['children2'];
  158. $r = (array) $r;
  159. foreach ($r as $k => & $v)
  160. {
  161. if ((string) $p[$k] == '0')
  162. {
  163. $v['depth'] = 1;
  164. if ($data['children'][$k]) {
  165. $v['child'] = implode(',', $c[$k]);
  166. }
  167. if ($data['children2'][$k]) {
  168. $v['childchild'] = implode(',', $c2[$k]);
  169. }
  170. }
  171. elseif ((string) $p[$p[$k]] == '0')
  172. {
  173. $v['depth'] = 2;
  174. if ($data['children'][$k]) {
  175. $v['child'] = implode(',', $c[$k]);
  176. }
  177. }
  178. elseif ((string) $p[$p[$p[$k]]] == '0')
  179. {
  180. $v['depth'] = 3;
  181. }
  182. }
  183. return $this->gcForCacheModel = $r;
  184. }
  185. /**
  186. * 更新信息
  187. * @param unknown $data
  188. * @param unknown $condition
  189. */
  190. public function editGoodsClass($data = array(), $condition = array()) {
  191. // 删除缓存
  192. $this->dropCache();
  193. return $this->where($condition)->update($data);
  194. }
  195. /**
  196. * 取得店铺绑定的分类
  197. *
  198. * @param number $store_id 店铺id
  199. * @param number $pid 父级分类id
  200. * @param number $deep 深度
  201. * @return array 二维数组
  202. */
  203. public function getGoodsClass($store_id, $pid = 0, $deep = 1)
  204. {
  205. // 读取商品分类 by 33 hao .com 批量添加分类修改
  206. $gc_list_o = $gc_list = $this->getGoodsClassListByParentId($pid);
  207. // 如果不是自营店铺或者自营店铺未绑定全部商品类目,读取绑定分类
  208. if (!checkPlatformStoreBindingAllGoodsClass())
  209. {
  210. $gc_list = array_under_reset($gc_list, 'gc_id');
  211. $model_storebindclass = Model('store_bind_class');
  212. $gcid_array = $model_storebindclass->getStoreBindClassList(array(
  213. 'store_id' => $store_id,
  214. 'state' => array('in', array(1, 2)),
  215. ), '', "class_{$deep} asc", "distinct class_{$deep}");
  216. if (!empty($gcid_array))
  217. {
  218. $tmp_gc_list = array();
  219. foreach ($gcid_array as $value)
  220. {
  221. if($value["class_{$deep}"] == 0)
  222. return $gc_list_o;
  223. if (isset($gc_list[$value["class_{$deep}"]])) {
  224. $tmp_gc_list[] = $gc_list[$value["class_{$deep}"]];
  225. }
  226. }
  227. $gc_list = $tmp_gc_list;
  228. } else {
  229. return array();
  230. }
  231. }
  232. return $gc_list;
  233. }
  234. /**
  235. * 删除商品分类
  236. * @param unknown $condition
  237. * @return boolean
  238. */
  239. public function delGoodsClass($condition) {
  240. // 删除缓存
  241. $this->dropCache();
  242. return $this->where($condition)->delete();
  243. }
  244. /**
  245. * 删除商品分类
  246. *
  247. * @param array $gcids
  248. * @return boolean
  249. */
  250. public function delGoodsClassByGcIdString($gcids)
  251. {
  252. $gcids = explode(',', $gcids);
  253. if (empty($gcids)) {
  254. return false;
  255. }
  256. $goods_class = $this->getGoodsClassForCacheModel();
  257. $gcid_array = array();
  258. foreach ($gcids as $gc_id) {
  259. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  260. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  261. $gcid_array = array_merge($gcid_array, array($gc_id), $child, $childchild);
  262. }
  263. // 删除商品分类
  264. $this->delGoodsClass(array('gc_id' => array('in', $gcid_array)));
  265. // 删除常用商品分类
  266. Model('goods_class_staple')->delStaple(array('gc_id_1|gc_id_2|gc_id_3' => array('in', $gcid_array)));
  267. // 删除分类tag表
  268. Model('goods_class_tag')->delGoodsClassTag(array('gc_id_1|gc_id_2|gc_id_3' => array('in', $gcid_array)));
  269. // 删除店铺绑定分类
  270. Model('store_bind_class')->delStoreBindClass(array('class_1|class_2|class_3' => array('in', $gcid_array)));
  271. // 商品下架
  272. Model('goods')->editProducesLockUp(array('goods_stateremark' => '商品分类被删除,需要重新选择分类'), array('gc_id' => array('in', $gcid_array)));
  273. return true;
  274. }
  275. /**
  276. * 前台头部的商品分类
  277. *
  278. * @param number $update_all 更新
  279. * @return array 数组
  280. */
  281. public function get_all_category($update_all = 0)
  282. {
  283. // 不存在时更新或者强制更新时执行
  284. if ($update_all == 1 || !($gc_list = rkcache('all_categories')))
  285. {
  286. $class_list = $this->getGoodsClassListAll();
  287. $gc_list = array();
  288. $class1_deep = array();//第1级关联第3级数组
  289. $class2_ids = array();//第2级关联第1级ID数组
  290. $type_ids = array();//第2级分类关联类型
  291. if (is_array($class_list) && !empty($class_list))
  292. {
  293. foreach ($class_list as $key => $value)
  294. {
  295. $p_id = $value['gc_parent_id'];//父级ID
  296. $gc_id = $value['gc_id'];
  297. $sort = $value['gc_sort'];
  298. if ($p_id == 0) {//第1级分类
  299. $gc_list[$gc_id] = $value;
  300. } elseif (array_key_exists($p_id,$gc_list)) {//第2级
  301. $class2_ids[$gc_id] = $p_id;
  302. $type_ids[] = $value['type_id'];
  303. $gc_list[$p_id]['class2'][$gc_id] = $value;
  304. } elseif (array_key_exists($p_id,$class2_ids)) {//第3级
  305. $parent_id = $class2_ids[$p_id];//取第1级ID
  306. $gc_list[$parent_id]['class2'][$p_id]['class3'][$gc_id] = $value;
  307. $class1_deep[$parent_id][$sort][] = $value;
  308. }
  309. }
  310. $type_brands = $this->get_type_brands($type_ids);//类型关联品牌
  311. foreach ($gc_list as $key => $value)
  312. {
  313. $gc_id = $value['gc_id'];
  314. $pic_name = BASE_UPLOAD_PATH.'/'.ATTACH_COMMON.'/category-pic-'.$gc_id.'.jpg';
  315. if (file_exists($pic_name)) {
  316. $gc_list[$gc_id]['pic'] = UPLOAD_SITE_URL.'/'.ATTACH_COMMON.'/category-pic-'.$gc_id.'.jpg';
  317. }
  318. $class3s = $class1_deep[$gc_id];
  319. if (is_array($class3s) && !empty($class3s))
  320. {//取关联的第3级
  321. $class3_n = 0;//已经找到的第3级分类个数
  322. ksort($class3s);//排序取到分类
  323. foreach ($class3s as $k3 => $v3)
  324. {
  325. if ($class3_n >= 5) {//最多取5个
  326. break;
  327. }
  328. foreach ($v3 as $k => $v)
  329. {
  330. if ($class3_n >= 5) {
  331. break;
  332. }
  333. if (is_array($v) && !empty($v)) {
  334. $p_id = $v['gc_parent_id'];
  335. $gc_id = $v['gc_id'];
  336. $parent_id = $class2_ids[$p_id];//取第1级ID
  337. $gc_list[$parent_id]['class3'][$gc_id] = $v;
  338. $class3_n += 1;
  339. }
  340. }
  341. }
  342. }
  343. $class2s = $value['class2'];
  344. if (is_array($class2s) && !empty($class2s))
  345. {//第2级关联品牌
  346. foreach ($class2s as $k2 => $v2) {
  347. $p_id = $v2['gc_parent_id'];
  348. $gc_id = $v2['gc_id'];
  349. $type_id = $v2['type_id'];
  350. $gc_list[$p_id]['class2'][$gc_id]['brands'] = $type_brands[$type_id];
  351. }
  352. }
  353. }
  354. }
  355. wkcache('all_categories', $gc_list);
  356. }
  357. return $gc_list;
  358. }
  359. /**
  360. * 类型关联品牌
  361. *
  362. * @param array $type_ids 类型
  363. * @return array 数组
  364. */
  365. public function get_type_brands($type_ids = array())
  366. {
  367. $brands = array();//品牌
  368. $type_brands = array();//类型关联品牌
  369. if (is_array($type_ids) && !empty($type_ids))
  370. {
  371. $type_ids = array_unique($type_ids);
  372. $type_list = $this->table('type_brand')->where(array('type_id'=>array('in',$type_ids)))->limit(10000)->select();
  373. if (is_array($type_list) && !empty($type_list))
  374. {
  375. $brand_list = $this->table('brand')->field('brand_id,brand_name,brand_pic')->where(array('brand_apply'=>1))->limit(10000)->select();
  376. if (is_array($brand_list) && !empty($brand_list))
  377. {
  378. foreach ($brand_list as $key => $value) {
  379. $brand_id = $value['brand_id'];
  380. $brands[$brand_id] = $value;
  381. }
  382. foreach ($type_list as $key => $value)
  383. {
  384. $type_id = $value['type_id'];
  385. $brand_id = $value['brand_id'];
  386. $brand = $brands[$brand_id];
  387. if (is_array($brand) && !empty($brand)) {
  388. $type_brands[$type_id][$brand_id] = $brand;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. return $type_brands;
  395. }
  396. /**
  397. * 新增商品分类
  398. * @param array $insert
  399. * @return boolean
  400. */
  401. public function addGoodsClass($insert) {
  402. // 删除缓存
  403. $this->dropCache();
  404. return $this->insert($insert);
  405. }
  406. /**
  407. * 取分类列表,最多为三级
  408. *
  409. * @param int $show_deep 显示深度
  410. * @param array $condition 检索条件
  411. * @return array 数组类型的返回结果
  412. */
  413. public function getTreeClassList($show_deep='3',$condition=array())
  414. {
  415. $class_list = $this->getGoodsClassList($condition);
  416. $goods_class = array();//分类数组
  417. if(is_array($class_list) && !empty($class_list))
  418. {
  419. $show_deep = intval($show_deep);
  420. if ($show_deep == 1)
  421. {//只显示第一级时用循环给分类加上深度deep号码
  422. foreach ($class_list as $val)
  423. {
  424. if($val['gc_parent_id'] == 0) {
  425. $val['deep'] = 1;
  426. $goods_class[] = $val;
  427. } else {
  428. break;//父类编号不为0时退出循环
  429. }
  430. }
  431. } else {//显示第二和三级时用递归
  432. $goods_class = $this->_getTreeClassList($show_deep,$class_list);
  433. }
  434. }
  435. return $goods_class;
  436. }
  437. /**
  438. * 递归 整理分类
  439. *
  440. * @param int $show_deep 显示深度
  441. * @param array $class_list 类别内容集合
  442. * @param int $deep 深度
  443. * @param int $parent_id 父类编号
  444. * @param int $i 上次循环编号
  445. * @return array $show_class 返回数组形式的查询结果
  446. */
  447. private function _getTreeClassList($show_deep,$class_list,$deep=1,$parent_id=0,$i=0)
  448. {
  449. static $show_class = array();//树状的平行数组
  450. if(is_array($class_list) && !empty($class_list))
  451. {
  452. $size = count($class_list);
  453. if($i == 0) $show_class = array();//从0开始时清空数组,防止多次调用后出现重复
  454. for ($i;$i < $size;$i++)
  455. {//$i为上次循环到的分类编号,避免重新从第一条开始
  456. $val = $class_list[$i];
  457. $gc_id = $val['gc_id'];
  458. $gc_parent_id = $val['gc_parent_id'];
  459. if($gc_parent_id == $parent_id)
  460. {
  461. $val['deep'] = $deep;
  462. $show_class[] = $val;
  463. if($deep < $show_deep && $deep < 3) {//本次深度小于显示深度时执行,避免取出的数据无用
  464. $this->_getTreeClassList($show_deep,$class_list,$deep+1,$gc_id,$i+1);
  465. }
  466. }
  467. if($gc_parent_id > $parent_id) break;//当前分类的父编号大于本次递归的时退出循环
  468. }
  469. }
  470. return $show_class;
  471. }
  472. /**
  473. * 取指定分类ID下的所有子类
  474. *
  475. * @param int/array $parent_id 父ID 可以单一可以为数组
  476. * @return array $rs_row 返回数组形式的查询结果
  477. */
  478. public function getChildClass($parent_id)
  479. {
  480. static $_cache;
  481. if ($_cache !== null) return $_cache;
  482. $all_class = $this->getGoodsClassListAll();
  483. if (is_array($all_class))
  484. {
  485. if (!is_array($parent_id)){
  486. $parent_id = array($parent_id);
  487. }
  488. $result = array();
  489. foreach ($all_class as $k => $v)
  490. {
  491. $gc_id = $v['gc_id'];//返回的结果包括父类
  492. $gc_parent_id = $v['gc_parent_id'];
  493. if (in_array($gc_id,$parent_id) || in_array($gc_parent_id,$parent_id)) {
  494. $parent_id[] = $v['gc_id'];
  495. $result[] = $v;
  496. }
  497. }
  498. $return = $result;
  499. }
  500. else
  501. {
  502. $return = false;
  503. }
  504. return $_cache = $return;
  505. }
  506. /**
  507. * 取指定分类ID的导航链接
  508. *
  509. * @param int $id 父类ID/子类ID
  510. * @param int $sign 1、0 1为最后一级不加超链接,0为加超链接
  511. * @return array $nav_link 返回数组形式类别导航连接
  512. */
  513. public function getGoodsClassNav($id = 0, $sign = 1)
  514. {
  515. if (intval ( $id ) > 0) {
  516. $data = $this->getGoodsClassIndexedListAll();
  517. // 当前分类不加超链接
  518. if ($sign == 1) {
  519. $nav_link [] = array(
  520. 'title' => $data[$id]['gc_name']
  521. );
  522. } else {
  523. $nav_link [] = array(
  524. 'title' => $data[$id]['gc_name'],
  525. 'link' => urlShop('search', 'index', array('cate_id' => $data[$id]['gc_id']))
  526. );
  527. }
  528. // 最多循环4层
  529. for($i = 1; $i < 5; $i ++)
  530. {
  531. if ($data[$id]['gc_parent_id'] == '0') {
  532. break;
  533. }
  534. $id = $data[$id]['gc_parent_id'];
  535. $nav_link[] = array(
  536. 'title' => $data[$id]['gc_name'],
  537. 'link' => urlShop('search', 'index', array('cate_id' => $data[$id]['gc_id']))
  538. );
  539. }
  540. } else {
  541. // 加上 首页 商品分类导航
  542. $nav_link[] = array('title' => Language::get('goods_class_index_search_results'));
  543. }
  544. // 首页导航
  545. $nav_link[] = array('title' => Language::get('homepage'), 'link' => SHOP_SITE_URL);
  546. krsort ( $nav_link );
  547. return $nav_link;
  548. }
  549. /**
  550. * 取指定分类ID的所有父级分类
  551. *
  552. * @param int $id 父类ID/子类ID
  553. * @return array $nav_link 返回数组形式类别导航连接
  554. */
  555. public function getGoodsClassLineForTag($id = 0)
  556. {
  557. if (intval($id)> 0) {
  558. $gc_line = array();
  559. /**
  560. * 取当前类别信息
  561. */
  562. $class = $this->getGoodsClassInfoById(intval($id));
  563. $gc_line['gc_id'] = $class['gc_id'];
  564. $gc_line['type_id'] = $class['type_id'];
  565. $gc_line['gc_virtual'] = $class['gc_virtual'];
  566. /**
  567. * 是否是子类
  568. */
  569. if ($class['gc_parent_id'] != 0)
  570. {
  571. $parent_1 = $this->getGoodsClassInfoById($class['gc_parent_id']);
  572. if ($parent_1['gc_parent_id'] != 0) {
  573. $parent_2 = $this->getGoodsClassInfoById($parent_1['gc_parent_id']);
  574. $gc_line['gc_id_1'] = $parent_2['gc_id'];
  575. $gc_line['gc_tag_name'] = trim($parent_2['gc_name']) . ' >';
  576. $gc_line['gc_tag_value'] = trim($parent_2['gc_name']) . ',';
  577. }
  578. if (!isset($gc_line['gc_id_1'])) {
  579. $gc_line['gc_id_1'] = $parent_1['gc_id'];
  580. } else {
  581. $gc_line['gc_id_2'] = $parent_1['gc_id'];
  582. }
  583. $gc_line['gc_tag_name'] .= trim($parent_1['gc_name']) . ' >';
  584. $gc_line['gc_tag_value'] .= trim($parent_1['gc_name']) . ',';
  585. }
  586. if (!isset($gc_line['gc_id_1'])) {
  587. $gc_line['gc_id_1'] = $class['gc_id'];
  588. } else if (!isset($gc_line['gc_id_2'])) {
  589. $gc_line['gc_id_2'] = $class['gc_id'];
  590. } else {
  591. $gc_line['gc_id_3'] = $class['gc_id'];
  592. }
  593. $gc_line['gc_tag_name'] .= trim($class['gc_name']) . ' >';
  594. $gc_line['gc_tag_value'] .= trim($class['gc_name']) . ',';
  595. }
  596. $gc_line['gc_tag_name'] = trim($gc_line['gc_tag_name'], ' >');
  597. $gc_line['gc_tag_value'] = trim($gc_line['gc_tag_value'], ',');
  598. return $gc_line;
  599. }
  600. /**
  601. * 取得分类关键词,方便SEO
  602. */
  603. public function getKeyWords($gc_id = null)
  604. {
  605. if (empty($gc_id)) return false;
  606. $keywrods = rkcache('goods_class_seo',true);
  607. $seo_title = $keywrods[$gc_id]['title'];
  608. $seo_key = '';
  609. $seo_desc = '';
  610. if ($gc_id > 0)
  611. {
  612. if (isset($keywrods[$gc_id])){
  613. $seo_key .= $keywrods[$gc_id]['key'].',';
  614. $seo_desc .= $keywrods[$gc_id]['desc'].',';
  615. }
  616. $goods_class = Model('goods_class')->getGoodsClassIndexedListAll();
  617. if(($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0)
  618. {
  619. if (isset($keywrods[$gc_id])){
  620. $seo_key .= $keywrods[$gc_id]['key'].',';
  621. $seo_desc .= $keywrods[$gc_id]['desc'].',';
  622. }
  623. }
  624. if(($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0)
  625. {
  626. if (isset($keywrods[$gc_id])){
  627. $seo_key .= $keywrods[$gc_id]['key'].',';
  628. $seo_desc .= $keywrods[$gc_id]['desc'].',';
  629. }
  630. }
  631. }
  632. return array(1=>$seo_title,2=>trim($seo_key,','),3=>trim($seo_desc,','));
  633. }
  634. /**
  635. * 获得商品分类缓存
  636. * @param int $choose_gcid 选择分类ID
  637. * @param int $show_depth 需要展示分类深度
  638. * @return array 返回分类数组和选择分类id数组
  639. */
  640. public function getGoodsclassCache($choose_gcid, $show_depth=3)
  641. {
  642. $gc_list = $this->getGoodsClassForCacheModel();
  643. //获取需要展示的分类数组
  644. $show_gc_list = array();
  645. foreach ((array)$gc_list as $k=>$v)
  646. {
  647. if ($v['depth'] < $show_depth){
  648. $show_gc_list[$v['gc_id']] = $v;
  649. } elseif ($v['depth'] == $show_depth) {
  650. unset($v['child'],$v['childchild']);
  651. $show_gc_list[$v['gc_id']] = $v;
  652. }
  653. }
  654. if ($choose_gcid > 0)
  655. {
  656. //遍历出选择商品分类的上下级ID
  657. $gc_depth = $gc_list[$choose_gcid]['depth'];
  658. $choose_gcidarr = array();
  659. $parentid = $choose_gcid;
  660. for ($i=$gc_depth-1; $i>=0; $i--){
  661. $choose_gcidarr[$i] = $parentid;
  662. $parentid = $gc_list[$parentid]['gc_parent_id'];
  663. }
  664. }
  665. return array('showclass'=>$show_gc_list,'choose_gcid'=>$choose_gcidarr);
  666. }
  667. }