Search.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. *
  5. *
  6. * ----------------------------------------------------------------------------
  7. *
  8. * 数据层模型
  9. */
  10. class Search extends BaseModel
  11. {
  12. public function __get($key)
  13. {
  14. return $this->$key;
  15. }
  16. /**
  17. * 取得商品分类详细信息
  18. * @access public
  19. * @author csdeshang
  20. * @param type $get 需要的参数内容
  21. * @param type $default_classid 默认分类id
  22. * @return type
  23. */
  24. public function getAttribute($get, $default_classid)
  25. {
  26. if (!empty($get['a_id'])) {
  27. $attr_ids = explode('_', $get['a_id']);
  28. }
  29. $data = array();
  30. // 获取当前的分类内容
  31. $class_array = model('goodsclass')->getGoodsclassForCacheModel();
  32. $data['class'] = isset($get['cate_id']) && isset($class_array[$get['cate_id']]) ? $class_array[$get['cate_id']] : array();
  33. if (empty($data['class']['child']) && empty($data['class']['childchild'])) {
  34. // 根据属性查找商品
  35. if (isset($attr_ids)) {
  36. // 商品id数组
  37. $goodsid_array = array();
  38. $data['sign'] = false;
  39. foreach ($attr_ids as $val) {
  40. $where = array();
  41. $where[] = array('attrvalue_id', '=', $val);
  42. if ($data['sign']) {
  43. $where[] = array('goods_id', 'in', $goodsid_array);
  44. }
  45. $goodsattrindex_list = model('goodsattrindex')->getGoodsAttrIndexList($where, 'goods_id');
  46. if (!empty($goodsattrindex_list)) {
  47. $data['sign'] = true;
  48. $tpl_goodsid_array = array();
  49. foreach ($goodsattrindex_list as $val) {
  50. $tpl_goodsid_array[] = $val['goods_id'];
  51. }
  52. $goodsid_array = $tpl_goodsid_array;
  53. } else {
  54. $data['goodsid_array'] = $goodsid_array = array();
  55. $data['sign'] = false;
  56. break;
  57. }
  58. }
  59. if ($data['sign']) {
  60. $data['goodsid_array'] = $goodsid_array;
  61. }
  62. }
  63. }
  64. $class = isset($class_array[$default_classid]) ? $class_array[$default_classid] : "";
  65. $brand_array = array();
  66. $initial_array = array();
  67. $attr_array = array();
  68. $checked_brand = array();
  69. $checked_attr = array();
  70. if (empty($class['child']) && empty($class['childchild'])) {
  71. //获得分类对应的类型ID
  72. $type_id = isset($class['type_id']) ? $class['type_id'] : '';
  73. if (!empty($type_id)) {
  74. //品牌列表
  75. $typebrand_list = model('type')->getTypebrandList(array('type_id' => $type_id), 'brand_id');
  76. if (!empty($typebrand_list)) {
  77. $brandid_array = array();
  78. foreach ($typebrand_list as $val) {
  79. $brandid_array[] = $val['brand_id'];
  80. }
  81. $brand_array = model('brand')->getBrandPassedList(array(array('brand_id', 'in', $brandid_array)), 'brand_id,brand_name,brand_initial,brand_pic,brand_showtype', 0, 'brand_showtype asc,brand_recommend desc,brand_sort asc');
  82. if (!empty($brand_array)) {
  83. $brand_list = array();
  84. foreach ($brand_array as $val) {
  85. $brand_list[$val['brand_id']] = $val;
  86. $initial_array[] = $val['brand_initial'];
  87. }
  88. $brand_array = $brand_list;
  89. $initial_array = array_unique($initial_array);
  90. sort($initial_array);
  91. }
  92. }
  93. // 被选中的品牌
  94. $brand_id = isset($get['b_id']) ? intval($get['b_id']) : "";
  95. if ($brand_id > 0 && !empty($brand_array)) {
  96. if (isset($brand_array[$brand_id])) {
  97. $checked_brand[$brand_id]['brand_name'] = $brand_array[$brand_id]['brand_name'];
  98. }
  99. }
  100. //属性列表
  101. $attribute_model = model('attribute');
  102. $attribute_list = $attribute_model->getAttributeShowList(array(array('type_id', '=', $type_id)), 'attr_id,attr_name');
  103. $attributevalue_list = $attribute_model->getAttributeValueList(array('type_id' => $type_id), 'attrvalue_id,attrvalue_name,attr_id');
  104. $attributevalue_list = array_under_reset($attributevalue_list, 'attr_id', 2);
  105. if (!empty($attribute_list)) {
  106. foreach ($attribute_list as $val) {
  107. $attr_array[$val['attr_id']]['name'] = $val['attr_name'];
  108. if (isset($attributevalue_list[$val['attr_id']])) {
  109. $tpl_array = array_under_reset($attributevalue_list[$val['attr_id']], 'attrvalue_id');
  110. $attr_array[$val['attr_id']]['value'] = $tpl_array;
  111. } else {
  112. $attr_array[$val['attr_id']]['value'] = array();
  113. }
  114. }
  115. }
  116. // 被选中的属性
  117. if (isset($attr_ids) && is_array($attr_ids) && !empty($attr_array)) {
  118. foreach ($attr_ids as $s) {
  119. foreach ($attr_array as $k => $d) {
  120. if (isset($d['value'][$s])) {
  121. $checked_attr[$k]['attr_name'] = $d['name'];
  122. $checked_attr[$k]['attrvalue_id'] = $s;
  123. $checked_attr[$k]['attrvalue_name'] = $d['value'][$s]['attrvalue_name'];
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. return array($data, $brand_array, $initial_array, $attr_array, $checked_brand, $checked_attr);
  131. }
  132. /**
  133. * 从TAG中查找分类
  134. * @access public
  135. * @author csdeshang
  136. * @param type $keyword
  137. * @return type
  138. */
  139. public function getTagCategory($keyword = '')
  140. {
  141. $data = array();
  142. if ($keyword != '') {
  143. // 跟据class_tag缓存搜索出与keyword相关的分类
  144. $tag_list = rkcache('classtag', true);
  145. if (!empty($tag_list) && is_array($tag_list)) {
  146. foreach ($tag_list as $key => $val) {
  147. $tag_value = $val['gctag_value'];
  148. if (strpos($tag_value, $keyword) !== false) {
  149. $data[] = $val['gc_id'];
  150. }
  151. }
  152. }
  153. }
  154. return $data;
  155. }
  156. /**
  157. * 获取父级分类,递归调用
  158. * @access public
  159. * @author csdeshang
  160. * @param type $gc_id 分类id
  161. * @param type $goods_class 商品分类
  162. * @param type $data 数据
  163. * @return type
  164. */
  165. private function _getParentCategory($gc_id, $goods_class, $data)
  166. {
  167. array_unshift($data, $gc_id);
  168. if (isset($goods_class[$gc_id]['gc_parent_id']) && $goods_class[$gc_id]['gc_parent_id'] != 0) {
  169. return $this->_getParentCategory($goods_class[$gc_id]['gc_parent_id'], $goods_class, $data);
  170. } else {
  171. return $data;
  172. }
  173. }
  174. /**
  175. * 显示左侧商品分类
  176. * @access public
  177. * @author csdeshang
  178. * @param type $param 分类id
  179. * @param type $sign 0为取得最后一级的同级分类,1为不取得
  180. * @return type
  181. */
  182. public function getLeftCategory($param, $sign = 0)
  183. {
  184. $data = array();
  185. if (!empty($param)) {
  186. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  187. foreach ($param as $val) {
  188. $data[] = $this->_getParentCategory($val, $goods_class, array());
  189. }
  190. }
  191. $tpl_data = array();
  192. $gc_list = model('goodsclass')->get_all_category();
  193. foreach ($data as $value) {
  194. //$tpl_data[$val[0]][$val[1]][$val[2]] = $val[2];
  195. if (!empty($gc_list[$value[0]])) { // 一级
  196. $tpl_data[$value[0]]['gc_id'] = $gc_list[$value[0]]['gc_id'];
  197. $tpl_data[$value[0]]['gc_name'] = $gc_list[$value[0]]['gc_name'];
  198. if (isset($value[0]) && isset($value[1]) && isset($gc_list[$value[0]]['class2'][$value[1]])) { // 二级
  199. $tpl_data[$value[0]]['class2'][$value[1]]['gc_id'] = $gc_list[$value[0]]['class2'][$value[1]]['gc_id'];
  200. $tpl_data[$value[0]]['class2'][$value[1]]['gc_name'] = $gc_list[$value[0]]['class2'][$value[1]]['gc_name'];
  201. if (isset($value[0]) && isset($value[1]) && isset($value[2]) && isset($gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]])) { // 三级
  202. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_id'] = $gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_id'];
  203. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_name'] = $gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_name'];
  204. if (!$sign) { // 取得全部三级分类
  205. foreach ($gc_list[$value[0]]['class2'][$value[1]]['class3'] as $val) {
  206. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['gc_id'] = $val['gc_id'];
  207. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['gc_name'] = $val['gc_name'];
  208. if ($value[2] == $val['gc_id']) {
  209. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['default'] = 1;
  210. }
  211. }
  212. }
  213. } else { // 取得全部二级分类
  214. if (!$sign) { // 取得同级分类
  215. if (!empty($gc_list[$value[0]]['class2'])) {
  216. foreach ($gc_list[$value[0]]['class2'] as $gc2) {
  217. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['gc_id'] = $gc2['gc_id'];
  218. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['gc_name'] = $gc2['gc_name'];
  219. if (!empty($gc2['class3'])) {
  220. foreach ($gc2['class3'] as $gc3) {
  221. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_id'] = $gc3['gc_id'];
  222. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_name'] = $gc3['gc_name'];
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. } else { // 取得全部一级分类
  230. if (!$sign) { // 取得同级分类
  231. if (!empty($gc_list)) {
  232. foreach ($gc_list as $gc1) {
  233. $tpl_data[$gc1['gc_id']]['gc_id'] = $gc1['gc_id'];
  234. $tpl_data[$gc1['gc_id']]['gc_name'] = $gc1['gc_name'];
  235. if (!empty($gc1['class2'])) {
  236. foreach ($gc1['class2'] as $gc2) {
  237. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['gc_id'] = $gc2['gc_id'];
  238. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['gc_name'] = $gc2['gc_name'];
  239. if (!empty($gc2['class3'])) {
  240. foreach ($gc2['class3'] as $gc3) {
  241. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_id'] = $gc3['gc_id'];
  242. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_name'] = $gc3['gc_name'];
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. return $tpl_data;
  254. }
  255. }