Goodsclass.php 30 KB

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