Goodsclass.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Goodsclass extends BaseModel
  15. {
  16. /**
  17. * 缓存数据
  18. */
  19. protected $cachedData;
  20. /**
  21. * 缓存数据 原H('goods_class')形式
  22. */
  23. protected $gcForCacheModel;
  24. /**
  25. * 获取缓存数据
  26. * @access public
  27. * @author csdeshang
  28. * @return array
  29. * array(
  30. * 'data' => array(
  31. * // Id => 记录
  32. * ),
  33. * 'parent' => array(
  34. * // 子Id => 父Id
  35. * ),
  36. * 'children' => array(
  37. * // 父Id => 子Id数组
  38. * ),
  39. * 'children2' => array(
  40. * // 1级Id => 3级Id数组
  41. * ),
  42. * )
  43. */
  44. public function getCache()
  45. {
  46. if ($this->cachedData) {
  47. return $this->cachedData;
  48. }
  49. $data = rkcache('gc_class');
  50. if (!$data) {
  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. if (!empty($data['children'][$id])) {
  61. foreach ((array)$data['children'][$id] as $cid) {
  62. if (!empty($data['children'][$cid])) {
  63. foreach ((array)$data['children'][$cid] as $ccid) {
  64. $data['children2'][$id][] = $ccid;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. wkcache('gc_class', $data);
  71. }
  72. return $this->cachedData = $data;
  73. }
  74. /**
  75. * 删除缓存数据
  76. * @access public
  77. * @author csdeshang
  78. * @return bool
  79. */
  80. public function dropCache()
  81. {
  82. $this->cachedData = null;
  83. $this->gcForCacheModel = null;
  84. dkcache('gc_class');
  85. dkcache('all_categories');
  86. }
  87. /**
  88. * 类别列表
  89. * @access public
  90. * @author csdeshang
  91. * @param array $condition 检索条件
  92. * @param string $field 字段
  93. * @return array 返回二位数组
  94. */
  95. public function getGoodsclassList($condition, $field = '*')
  96. {
  97. $result = Db::name('goodsclass')->field($field)->where($condition)->order('gc_parent_id asc,gc_sort asc,gc_id asc')->select()->toArray();
  98. return $result;
  99. }
  100. /**
  101. * 从缓存获取全部分类
  102. * @access public
  103. * @author csdeshang
  104. * @return type
  105. */
  106. public function getGoodsclassListAll()
  107. {
  108. $data = $this->getCache();
  109. return array_values((array)@$data['data']);
  110. }
  111. /**
  112. * 从缓存获取全部分类 分类id作为数组的键
  113. * @access public
  114. * @author csdeshang
  115. * @return array
  116. */
  117. public function getGoodsclassIndexedListAll()
  118. {
  119. $data = $this->getCache();
  120. if($data){
  121. return (array)$data['data'];
  122. }
  123. return '';
  124. }
  125. /**
  126. * 从缓存获取分类 通过分类id数组
  127. * @access public
  128. * @author csdeshang
  129. * @param array $ids 分类id数组
  130. * @return array
  131. */
  132. public function getGoodsclassListByIds($ids)
  133. {
  134. $data = $this->getCache();
  135. $ret = array();
  136. foreach ((array)$ids as $i) {
  137. if (isset($data['data'][$i]) && $data['data'][$i]) {
  138. $ret[] = $data['data'][$i];
  139. }
  140. }
  141. return $ret;
  142. }
  143. /**
  144. * 从缓存获取分类 通过上级分类id
  145. * @access public
  146. * @author csdeshang
  147. * @param type $pid 上级分类id 若传0则返回1级分类
  148. * @return type
  149. */
  150. public function getGoodsclassListByParentId($pid)
  151. {
  152. $data = $this->getCache();
  153. $ret = array();
  154. if (!empty($data['children'][$pid])) {
  155. foreach ((array)$data['children'][$pid] as $i) {
  156. if ($data['data'][$i]) {
  157. $ret[] = $data['data'][$i];
  158. }
  159. }
  160. }
  161. return $ret;
  162. }
  163. /**
  164. * 从缓存获取分类 通过分类id
  165. * @access public
  166. * @author csdeshang
  167. * @param type $id 分类id
  168. * @return type
  169. */
  170. public function getGoodsclassInfoById($id)
  171. {
  172. $data = $this->getCache();
  173. return @$data['data'][$id];
  174. }
  175. /**
  176. * 返回缓存数据 原H('goods_class')形式
  177. * @access public
  178. * @author csdeshang
  179. * @return type
  180. */
  181. public function getGoodsclassForCacheModel()
  182. {
  183. if ($this->gcForCacheModel)
  184. return $this->gcForCacheModel;
  185. $data = $this->getCache();
  186. $r = isset($data['data'])?$data['data']:'';
  187. $p = isset($data['parent'])?$data['parent']:'';
  188. $c = isset($data['children'])?$data['children']:'';
  189. $c2 = empty($data['children2']) ? '' : $data['children2'];
  190. if(!empty($r)) {
  191. $r = (array)$r;
  192. foreach ($r as $k => & $v) {
  193. if ((string)$p[$k] == '0') {
  194. $v['depth'] = 1;
  195. if (!empty($data['children'][$k])) {
  196. $v['child'] = implode(',', $c[$k]);
  197. }
  198. if (!empty($data['children2'][$k])) {
  199. $v['childchild'] = implode(',', $c2[$k]);
  200. }
  201. }
  202. else if (isset($p[$p[$k]]) && (string)$p[$p[$k]] == '0') {
  203. $v['depth'] = 2;
  204. if (isset($data['children'][$k])) {
  205. $v['child'] = implode(',', $c[$k]);
  206. }
  207. }
  208. else if (isset($p[$p[$k]]) && isset($p[$p[$p[$k]]]) && (string)$p[$p[$p[$k]]] == '0') {
  209. $v['depth'] = 3;
  210. }
  211. }
  212. }
  213. return $this->gcForCacheModel = $r;
  214. }
  215. /**
  216. * 更新信息
  217. * @access public
  218. * @author csdeshang
  219. * @param type $data 更新数据
  220. * @param type $condition 条件
  221. * @return bool
  222. */
  223. public function editGoodsclass($data = array(), $condition = array())
  224. {
  225. // 删除缓存
  226. $this->dropCache();
  227. return Db::name('goodsclass')->where($condition)->update($data);
  228. }
  229. /**
  230. * 取得店铺绑定的分类
  231. * @access public
  232. * @author csdeshang
  233. * @param number $store_id 店铺id
  234. * @param number $pid 父级分类id
  235. * @param number $deep 深度
  236. * @return array 二维数组
  237. */
  238. public function getGoodsclass($store_id, $pid = 0, $deep = 1)
  239. {
  240. // 读取商品分类
  241. $gc_list_o = $gc_list = $this->getGoodsclassListByParentId($pid);
  242. // 如果不是自营店铺或者自营店铺未绑定全部商品类目,读取绑定分类
  243. if (!check_platform_store_bindingall_goodsclass()) {
  244. $gc_list = array_under_reset($gc_list, 'gc_id');
  245. $storebindclass_model = model('storebindclass');
  246. $condition = array();
  247. $condition[] = array('store_id','=',$store_id);
  248. $condition[] = array('storebindclass_state','in',array(1, 2));
  249. $gcid_array = $storebindclass_model->getStorebindclassList($condition, '', "class_{$deep} asc", "distinct class_{$deep}");
  250. if (!empty($gcid_array)) {
  251. $tmp_gc_list = array();
  252. foreach ($gcid_array as $value) {
  253. if ($value["class_{$deep}"] == 0)
  254. return $gc_list_o;
  255. if (isset($gc_list[$value["class_{$deep}"]])) {
  256. $tmp_gc_list[] = $gc_list[$value["class_{$deep}"]];
  257. }
  258. }
  259. $gc_list = $tmp_gc_list;
  260. }
  261. else {
  262. return array();
  263. }
  264. }
  265. return $gc_list;
  266. }
  267. /**
  268. * 删除商品分类
  269. * @access public
  270. * @author csdeshang
  271. * @param array $condition 条件
  272. * @return boolean
  273. */
  274. public function delGoodsclass($condition)
  275. {
  276. // 删除缓存
  277. $this->dropCache();
  278. return Db::name('goodsclass')->where($condition)->delete();
  279. }
  280. /**
  281. * 删除商品分类
  282. * @access public
  283. * @author csdeshang
  284. * @param array $gcids 分类ID
  285. * @return boolean
  286. */
  287. public function delGoodsclassByGcIdString($gcids)
  288. {
  289. $gcids = explode(',', $gcids);
  290. if (empty($gcids)) {
  291. return false;
  292. }
  293. $goods_class = $this->getGoodsclassForCacheModel();
  294. $gcid_array = array();
  295. foreach ($gcids as $gc_id) {
  296. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  297. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  298. $gcid_array = array_merge($gcid_array, array($gc_id), $child, $childchild);
  299. }
  300. // 删除商品分类
  301. $this->delGoodsclass(array(array('gc_id','in', $gcid_array)));
  302. // 删除常用商品分类
  303. model('goodsclassstaple')->delGoodsclassstaple(array(array('gc_id_1|gc_id_2|gc_id_3','in', $gcid_array)));
  304. // 删除分类tag表
  305. model('goodsclasstag')->delGoodsclasstag(array(array('gc_id_1|gc_id_2|gc_id_3','in', $gcid_array)));
  306. // 删除店铺绑定分类
  307. model('storebindclass')->delStorebindclass(array(array('class_1|class_2|class_3','in', $gcid_array)));
  308. // 商品下架
  309. model('goods')->editProducesLockUp(array('goods_stateremark' => '商品分类被删除,需要重新选择分类'), array(array('gc_id','in', $gcid_array)));
  310. return true;
  311. }
  312. /**
  313. * 前台头部的商品分类
  314. * @access public
  315. * @author csdeshang
  316. * @param number $update_all 更新
  317. * @return array 数组
  318. */
  319. public function get_all_category($update_all = 0)
  320. {
  321. // 不存在时更新或者强制更新时执行
  322. if ($update_all == 1 || !($gc_list = rkcache('all_categories'))) {
  323. $class_list = $this->getTreeClassList(3);
  324. $gc_list = [];
  325. $type_ids = array(); //第2级分类关联类型
  326. if (is_array($class_list) && !empty($class_list)) {
  327. //遍历一级商品分类
  328. foreach ($class_list as $k => $v) {
  329. if ($v[ 'deep' ] == 1) {
  330. $nav_info = $this->_getGoodsclassnavById($v['gc_id']);//一级分类推荐品牌
  331. $gc_list[$v['gc_id']] = array_merge($v, $nav_info);
  332. unset($class_list[ $k ]);
  333. }
  334. }
  335. $class_list = array_values($class_list);
  336. //遍历二级商品分类
  337. foreach ($class_list as $k => $v) {
  338. foreach ($gc_list as $ck => $cv) {
  339. if ($v[ 'deep' ] == 2 && $cv[ 'gc_id' ] == $v[ 'gc_parent_id' ]) {
  340. $gc_list[$cv['gc_id']][ 'class2' ][$v['gc_id']] = $v;
  341. $type_ids[] = $cv['type_id'];
  342. unset($class_list[ $k ]);
  343. }
  344. }
  345. }
  346. $class_list = array_values($class_list);
  347. //遍历三级商品分类
  348. foreach ($class_list as $k => $v) {
  349. foreach ($gc_list as $ck => $cv) {
  350. if (!empty($cv[ 'class2' ])) {
  351. foreach ($cv[ 'class2' ] as $third_k => $third_v) {
  352. if ($v[ 'deep' ] == 3 && $third_v[ 'gc_id' ] == $v[ 'gc_parent_id' ]) {
  353. $gc_list[$cv['gc_id']][ 'class2' ][$third_v['gc_id']][ 'class3' ][] = $v;
  354. unset($class_list[ $k ]);
  355. }
  356. }
  357. }
  358. }
  359. }
  360. $type_brands = $this->get_type_brands($type_ids); //类型关联品牌
  361. foreach ($gc_list as $key => $value) {
  362. $class2s = isset($value['class2']) ? $value['class2'] : '';
  363. if (is_array($class2s) && !empty($class2s)) {//第2级关联品牌
  364. foreach ($class2s as $k2 => $v2) {
  365. $p_id = $v2['gc_parent_id'];
  366. $gc_id = $v2['gc_id'];
  367. $type_id = $v2['type_id'];
  368. $gc_list[$p_id]['class2'][$gc_id]['brands'] = isset($type_brands[$type_id]) ? $type_brands[$type_id] : '';
  369. }
  370. }
  371. }
  372. }
  373. wkcache('all_categories', $gc_list);
  374. }
  375. return $gc_list;
  376. }
  377. /**
  378. * 类型关联品牌
  379. * @access public
  380. * @author csdeshang
  381. * @param array $type_ids 类型
  382. * @return array 数组
  383. */
  384. public function get_type_brands($type_ids = array())
  385. {
  386. $brands = array(); //品牌
  387. $type_brands = array(); //类型关联品牌
  388. if (is_array($type_ids) && !empty($type_ids)) {
  389. $type_ids = array_unique($type_ids);
  390. $type_list = Db::name('typebrand')->where('type_id','in', $type_ids)->limit(10000)->select()->toArray();
  391. if (is_array($type_list) && !empty($type_list)) {
  392. $brand_mod=model('brand');
  393. $brand_list = $brand_mod->getBrandList(array('brand_apply' => 1),'brand_id,brand_name,brand_pic',10000);
  394. if (is_array($brand_list) && !empty($brand_list)) {
  395. foreach ($brand_list as $key => $value) {
  396. $brand_id = $value['brand_id'];
  397. $brands[$brand_id] = $value;
  398. }
  399. foreach ($type_list as $key => $value) {
  400. $type_id = $value['type_id'];
  401. $brand_id = $value['brand_id'];
  402. if(isset($brands[$brand_id])){
  403. $brand = $brands[$brand_id];
  404. if (is_array($brand) && !empty($brand)) {
  405. $type_brands[$type_id][$brand_id] = $brand;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. return $type_brands;
  413. }
  414. /**
  415. * 获取商品分类导航
  416. * @access public
  417. * @author csdeshang
  418. * @param int $gc_id 商品分类id
  419. * @return type
  420. */
  421. private function _getGoodsclassnavById($gc_id)
  422. {
  423. $classnav_model = model('goodsclassnav');
  424. $brand_model = model('brand');
  425. $nav_info = $classnav_model->getGoodsclassnavInfoByGcId($gc_id);
  426. if (empty($nav_info)) {
  427. return array();
  428. }
  429. if (!empty($nav_info['goodscn_pic'])) {
  430. $nav_info['goodscn_pic'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_pic']);
  431. }
  432. else {
  433. unset($nav_info['goodscn_pic']);
  434. }
  435. if (!empty($nav_info['goodscn_adv1'])) {
  436. $nav_info['goodscn_adv1'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_adv1']);
  437. }
  438. else {
  439. unset($nav_info['goodscn_adv1']);
  440. }
  441. if (!empty($nav_info['goodscn_adv2'])) {
  442. $nav_info['goodscn_adv2'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_adv2']);
  443. }
  444. else {
  445. unset($nav_info['goodscn_adv2']);
  446. }
  447. if ($nav_info['goodscn_brandids'] != '') {
  448. $nav_info['cn_brands'] = $brand_model->getBrandList(array(array('brand_id','in', $nav_info['goodscn_brandids'])));
  449. unset($nav_info['goodscn_brandids']);
  450. }
  451. if ($nav_info['goodscn_classids'] != '') {
  452. $nav_info['cn_classs'] = $this->getGoodsclassList(array(array('gc_id','in', $nav_info['goodscn_classids'])));
  453. unset($nav_info['goodscn_classids']);
  454. }
  455. if ($nav_info['goodscn_alias'] != '') {
  456. $nav_info['gc_name'] = $nav_info['goodscn_alias'];
  457. unset($nav_info['goodscn_alias']);
  458. }
  459. return $nav_info;
  460. }
  461. /**
  462. * 新增商品分类
  463. * @access public
  464. * @author csdeshang
  465. * @param array $data 参数数据
  466. * @return boolean
  467. */
  468. public function addGoodsclass($data)
  469. {
  470. // 删除缓存
  471. $this->dropCache();
  472. return Db::name('goodsclass')->insertGetId($data);
  473. }
  474. /**
  475. * 取分类列表,最多为三级
  476. * @access public
  477. * @author csdeshang
  478. * @param int $show_deep 显示深度
  479. * @param array $condition 检索条件
  480. * @return array 数组类型的返回结果
  481. */
  482. public function getTreeClassList($show_deep = '3', $condition = array())
  483. {
  484. $class_list = $this->getGoodsclassList($condition);
  485. $goods_class = array(); //分类数组
  486. if (is_array($class_list) && !empty($class_list)) {
  487. $show_deep = intval($show_deep);
  488. if ($show_deep == 1) {//只显示第一级时用循环给分类加上深度deep号码
  489. foreach ($class_list as $val) {
  490. if ($val['gc_parent_id'] == 0) {
  491. $val['deep'] = 1;
  492. $goods_class[] = $val;
  493. }
  494. else {
  495. break; //父类编号不为0时退出循环
  496. }
  497. }
  498. }
  499. else {//显示第二和三级时用递归
  500. $goods_class = $this->_getTreeClassList($show_deep, $class_list);
  501. }
  502. }
  503. return $goods_class;
  504. }
  505. /**
  506. * 递归 整理分类
  507. * @access public
  508. * @author csdeshang
  509. * @param int $show_deep 显示深度
  510. * @param array $class_list 类别内容集合
  511. * @param int $deep 深度
  512. * @param int $parent_id 父类编号
  513. * @param int $i 上次循环编号
  514. * @return array $show_class 返回数组形式的查询结果
  515. */
  516. private function _getTreeClassList($show_deep, $class_list, $deep = 1, $parent_id = 0, $i = 0)
  517. {
  518. static $show_class = array(); //树状的平行数组
  519. if (is_array($class_list) && !empty($class_list)) {
  520. $size = count($class_list);
  521. if ($i == 0)
  522. $show_class = array(); //从0开始时清空数组,防止多次调用后出现重复
  523. for ($i; $i < $size; $i++) {//$i为上次循环到的分类编号,避免重新从第一条开始
  524. $val = $class_list[$i];
  525. $gc_id = $val['gc_id'];
  526. $gc_parent_id = $val['gc_parent_id'];
  527. if ($gc_parent_id == $parent_id) {
  528. $val['deep'] = $deep;
  529. $show_class[] = $val;
  530. if ($deep < $show_deep && $deep < 3) {//本次深度小于显示深度时执行,避免取出的数据无用
  531. $this->_getTreeClassList($show_deep, $class_list, $deep + 1, $gc_id, 1);
  532. }
  533. }
  534. if ($gc_parent_id > $parent_id)
  535. break; //当前分类的父编号大于本次递归的时退出循环
  536. }
  537. }
  538. return $show_class;
  539. }
  540. /**
  541. * 取指定分类ID下的所有子类
  542. * @access public
  543. * @author csdeshang
  544. * @staticvar type $_cache
  545. * @param int $parent_id 父ID 可以单一可以为数组
  546. * @return array 返回数组形式的查询结果
  547. */
  548. public function getChildClass($parent_id)
  549. {
  550. static $_cache;
  551. if ($_cache !== null)
  552. return $_cache;
  553. $all_class = $this->getGoodsclassListAll();
  554. if (is_array($all_class)) {
  555. if (!is_array($parent_id)) {
  556. $parent_id = array($parent_id);
  557. }
  558. $result = array();
  559. foreach ($all_class as $k => $v) {
  560. $gc_id = $v['gc_id']; //返回的结果包括父类
  561. $gc_parent_id = $v['gc_parent_id'];
  562. if (in_array($gc_id, $parent_id) || in_array($gc_parent_id, $parent_id)) {
  563. $parent_id[] = $v['gc_id'];
  564. $result[] = $v;
  565. }
  566. }
  567. $return = $result;
  568. }
  569. else {
  570. $return = false;
  571. }
  572. return $_cache = $return;
  573. }
  574. /**
  575. * 取指定分类ID的导航链接
  576. * @access public
  577. * @author csdeshang
  578. * @param int $id 父类ID/子类ID
  579. * @param int $sign 1、0 1为最后一级不加超链接,0为加超链接
  580. * @return array $nav_link 返回数组形式类别导航连接
  581. */
  582. public function getGoodsclassnav($id = 0, $sign = 1)
  583. {
  584. if (intval($id) > 0) {
  585. $data = $this->getGoodsclassIndexedListAll();
  586. // 当前分类不加超链接
  587. if ($sign == 1) {
  588. if (isset($data[$id])) {
  589. $nav_link [] = array(
  590. 'title' => $data[$id]['gc_name']
  591. );
  592. }
  593. }
  594. else {
  595. if (isset($data[$id])) {
  596. $nav_link [] = array(
  597. 'title' => isset($data[$id]['gc_name']) ? $data[$id]['gc_name'] : '..',
  598. 'link' => (string)url('/home/Search/index', ['cate_id' => $data[$id]['gc_id']]),
  599. );
  600. }
  601. }
  602. if (isset($data[$id])) {
  603. // 最多循环4层
  604. for ($i = 1; $i < 5; $i++) {
  605. if ($data[$id]['gc_parent_id'] == '0') {
  606. break;
  607. }
  608. $id = $data[$id]['gc_parent_id'];
  609. if(!isset($data[$id])){
  610. break;
  611. }
  612. $nav_link[] = array(
  613. 'title' => $data[$id]['gc_name'],
  614. 'link' => (string)url('/home/Search/index', ['cate_id' => $data[$id]['gc_id']])
  615. );
  616. }
  617. }
  618. }
  619. else {
  620. // 加上 首页 商品分类导航
  621. $nav_link[] = array('title' => lang('goods_class_index_search_results'));
  622. }
  623. // 首页导航
  624. $nav_link[] = array('title' => lang('homepage'), 'link' => (string)url('home/Index/index'));
  625. krsort($nav_link);
  626. return $nav_link;
  627. }
  628. /**
  629. * 根据一级分类id取得所有三级分类
  630. * @access public
  631. * @author csdeshang
  632. * @param type $id 分类ID
  633. * @return type
  634. */
  635. public function getChildClassByFirstId($id)
  636. {
  637. $data = $this->getCache();
  638. $result = array();
  639. if (!empty($data['children2'][$id])) {
  640. foreach ($data['children2'][$id] as $val) {
  641. $child = $data['data'][$val];
  642. $result[$child['gc_parent_id']]['class'][$child['gc_id']] = $child['gc_name'];
  643. $result[$child['gc_parent_id']]['name'] = $data['data'][$child['gc_parent_id']]['gc_name'];
  644. }
  645. }
  646. return $result;
  647. }
  648. /**
  649. * 取指定分类ID的所有父级分类
  650. * @access public
  651. * @author csdeshang
  652. * @param int $id 父类ID/子类ID
  653. * @return array
  654. */
  655. public function getGoodsclassLineForTag($id = 0)
  656. {
  657. if (intval($id) > 0) {
  658. $gc_line = array();
  659. /**
  660. * 取当前类别信息
  661. */
  662. $class = $this->getGoodsclassInfoById(intval($id));
  663. $gc_line['gc_id'] = $class['gc_id'];
  664. $gc_line['type_id'] = $class['type_id'];
  665. $gc_line['gc_virtual'] = $class['gc_virtual'];
  666. $gc_line['gctag_name']='>';
  667. $gc_line['gctag_value']=',';
  668. /**
  669. * 是否是子类
  670. */
  671. if ($class['gc_parent_id'] != 0) {
  672. $parent_1 = $this->getGoodsclassInfoById($class['gc_parent_id']);
  673. if ($parent_1['gc_parent_id'] != 0) {
  674. $parent_2 = $this->getGoodsclassInfoById($parent_1['gc_parent_id']);
  675. $gc_line['gc_id_1'] = $parent_2['gc_id'];
  676. $gc_line['gctag_name'] = trim($parent_2['gc_name']) . ' >';
  677. $gc_line['gctag_value'] = trim($parent_2['gc_name']) . ',';
  678. }
  679. if (!isset($gc_line['gc_id_1'])) {
  680. $gc_line['gc_id_1'] = $parent_1['gc_id'];
  681. }
  682. else {
  683. $gc_line['gc_id_2'] = $parent_1['gc_id'];
  684. }
  685. $gc_line['gctag_name'] .= trim($parent_1['gc_name']) . ' >';
  686. $gc_line['gctag_value'] .= trim($parent_1['gc_name']) . ',';
  687. }
  688. if (!isset($gc_line['gc_id_1'])) {
  689. $gc_line['gc_id_1'] = $class['gc_id'];
  690. }
  691. else if (!isset($gc_line['gc_id_2'])) {
  692. $gc_line['gc_id_2'] = $class['gc_id'];
  693. }
  694. else {
  695. $gc_line['gc_id_3'] = $class['gc_id'];
  696. }
  697. $gc_line['gctag_name'] .= trim($class['gc_name']) . ' >';
  698. $gc_line['gctag_value'] .= trim($class['gc_name']) . ',';
  699. }
  700. $gc_line['gctag_name'] = trim($gc_line['gctag_name'], ' >');
  701. $gc_line['gctag_value'] = trim($gc_line['gctag_value'], ',');
  702. return $gc_line;
  703. }
  704. /**
  705. * 取得分类关键词,方便SEO
  706. * @access public
  707. * @author csdeshang
  708. * @param type $gc_id 商品分类ID
  709. * @return boolean
  710. */
  711. public function getKeyWords($gc_id = null)
  712. {
  713. if (empty($gc_id))
  714. return false;
  715. $keywrods = rkcache('goodsclassseo', true);
  716. if (empty($keywrods)) {
  717. return array(1 => '', 2 => trim('', ','), 3 => trim('', ','));
  718. }
  719. $seo_title = $keywrods[$gc_id]['title'];
  720. $seo_key = '';
  721. $seo_desc = '';
  722. if ($gc_id > 0) {
  723. if (isset($keywrods[$gc_id])) {
  724. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  725. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  726. }
  727. $goods_class = model('goodsclass')->getGoodsclassIndexedListAll();
  728. if (($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0) {
  729. if (isset($keywrods[$gc_id])) {
  730. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  731. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  732. }
  733. }
  734. if (($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0) {
  735. if (isset($keywrods[$gc_id])) {
  736. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  737. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  738. }
  739. }
  740. }
  741. return array(1 => $seo_title, 2 => trim($seo_key, ','), 3 => trim($seo_desc, ','));
  742. }
  743. /**
  744. * 获得商品分类缓存
  745. * @access public
  746. * @author csdeshang
  747. * @param int $choose_gcid 选择分类ID
  748. * @param int $show_depth 需要展示分类深度
  749. * @return array 返回分类数组和选择分类id数组
  750. */
  751. public function getGoodsclassCache($choose_gcid, $show_depth = 3)
  752. {
  753. $gc_list = $this->getGoodsclassForCacheModel();
  754. //获取需要展示的分类数组
  755. $show_gc_list = array();
  756. if(!empty($gc_list)) {
  757. foreach ((array)$gc_list as $k => $v) {
  758. if(isset($v['depth'])){
  759. if ($v['depth'] < $show_depth) {
  760. $show_gc_list[$v['gc_id']] = $v;
  761. }
  762. elseif ($v['depth'] == $show_depth) {
  763. unset($v['child'], $v['childchild']);
  764. $show_gc_list[$v['gc_id']] = $v;
  765. }
  766. }
  767. }
  768. }
  769. $choose_gcidarr = array();
  770. if ($choose_gcid > 0) {
  771. //遍历出选择商品分类的上下级ID
  772. if(isset($gc_list[$choose_gcid])){
  773. $gc_depth = $gc_list[$choose_gcid]['depth'];
  774. $parentid = $choose_gcid;
  775. for ($i = $gc_depth - 1; $i >= 0; $i--) {
  776. $choose_gcidarr[$i] = $parentid;
  777. $parentid = $gc_list[$parentid]['gc_parent_id'];
  778. }
  779. }
  780. }
  781. return array('showclass' => $show_gc_list, 'choose_gcid' => $choose_gcidarr);
  782. }
  783. }