functions.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. function goods_category_tree($catid = 0, $ifview = '2')
  3. {
  4. $data = read_static_cache('goods_category_tree');
  5. if ($catid == 0) {
  6. if ($data === false) {
  7. $parentid = 0;
  8. $bif_view = $ifview == '2' || $ifview == '1' ? ' AND a.if_view = \'' . $ifview . '\' AND b.if_view = \'' . $ifview . '\'' : '';
  9. $sql = 'SELECT a.catid, a.catname, a.color, a.if_view, b.catid AS childid, b.catname AS childname FROM `' . $GLOBALS['db_mymps'] . 'goods_category` AS a LEFT JOIN `' . $GLOBALS['db_mymps'] . 'goods_category` AS b ON b.parentid = a.catid WHERE a.parentid = \'' . $parentid . '\' ' . $bif_view . ' ORDER BY a.catorder ASC , catid ASC, b.catorder ASC';
  10. $res = $GLOBALS['db']->getAll($sql);
  11. $cat_arr = array();
  12. foreach ($res as $row) {
  13. if ($row['if_view']) {
  14. $cat_arr[$row['catid']]['catid'] = $row['catid'];
  15. $cat_arr[$row['catid']]['catname'] = $row['catname'];
  16. $cat_arr[$row['catid']]['color'] = $row['color'];
  17. $cat_arr[$row['catid']]['if_view'] = $row['if_view'];
  18. $cat_arr[$row['catid']]['uri'] = Rewrite('goods', array('catid' => $row['catid']));
  19. if ($row['childid'] != NULL) {
  20. $cat_arr[$row['catid']]['children'][$row['childid']]['catid'] = $row['childid'];
  21. $cat_arr[$row['catid']]['children'][$row['childid']]['catname'] = $row['childname'];
  22. $cat_arr[$row['catid']]['children'][$row['childid']]['if_view'] = $row['if_view'];
  23. $cat_arr[$row['catid']]['children'][$row['childid']]['color'] = $row['childcolor'];
  24. $cat_arr[$row['catid']]['children'][$row['childid']]['uri'] = Rewrite('goods', array('catid' => $row['childid']));
  25. }
  26. }
  27. }
  28. write_static_cache('goods_category_tree', $cat_arr);
  29. }
  30. else {
  31. $cat_arr = $data;
  32. }
  33. }
  34. else {
  35. if ($data === NULL || empty($data[$catid])) {
  36. $bif_view = $ifview == '2' || $ifview == '1' ? ' AND b.if_view = \'' . $ifview . '\'' : '';
  37. $parentid = $GLOBALS['db']->getOne('SELECT parentid FROM `' . $GLOBALS['db_mymps'] . 'goods_category` WHERE catid = \'' . $catid . '\'');
  38. if ($parentid == 0) {
  39. $sql = 'SELECT a.catid, a.catname, a.catorder, a.if_view, b.catid AS childid, b.catname AS childname, b.catorder AS childorder FROM `' . $GLOBALS['db_mymps'] . 'goods_category` AS a LEFT JOIN `' . $GLOBALS['db_mymps'] . 'goods_category` AS b ON b.parentid = a.catid ' . $bif_view . ' WHERE a.catid = \'' . $catid . '\' ORDER BY catorder ASC , catid ASC, childorder ASC';
  40. }
  41. else {
  42. $sql = 'SELECT a.catid, a.catname, a.catorder, a.if_view, a.html_dir, a.htmlpath, b.catid AS childid, b.catname AS childname, b.catorder AS childorder FROM `' . $GLOBALS['db_mymps'] . 'goods_category` AS a LEFT JOIN `' . $GLOBALS['db_mymps'] . 'goods_category` AS b ON b.parentid = a.catid ' . $bif_view . ' WHERE b.parentid = \'' . $parentid . '\' ORDER BY catorder ASC , catid ASC, childorder ASC';
  43. }
  44. $res = $GLOBALS['db']->getAll($sql);
  45. $cat_arr = array();
  46. foreach ($res as $row) {
  47. if ($row['if_view']) {
  48. $cat_arr[$row['catid']]['catid'] = $row['catid'];
  49. $cat_arr[$row['catid']]['catname'] = $row['catname'];
  50. $cat_arr[$row['catid']]['catorder'] = $row['catorder'];
  51. $cat_arr[$row['catid']]['if_view'] = $row['if_view'];
  52. $cat_arr[$row['catid']]['uri'] = Rewrite('goods', array('catid' => $row['catid']));
  53. if ($row['childid'] != NULL) {
  54. $cat_arr[$row['catid']]['children'][$row['childid']]['catid'] = $row['childid'];
  55. $cat_arr[$row['catid']]['children'][$row['childid']]['catname'] = $row['childname'];
  56. $cat_arr[$row['catid']]['children'][$row['childid']]['if_view'] = $row['if_view'];
  57. $cat_arr[$row['catid']]['children'][$row['childid']]['catorder'] = $row['childorder'];
  58. $cat_arr[$row['catid']]['children'][$row['childid']]['uri'] = Rewrite('goods', array('catid' => $row['childid']));
  59. }
  60. }
  61. }
  62. }
  63. else {
  64. $cat_arr[] = $data[$catid];
  65. }
  66. }
  67. return $cat_arr;
  68. }
  69. function goods_cat_list($catid = 0, $selected = 0, $re_type = true, $level = 0, $is_show_all = true)
  70. {
  71. global $seoset;
  72. $data = read_static_cache('goods_category_pid_releate');
  73. if ($data === false) {
  74. $sql = 'SELECT c.catid, c.catname, c.parentid, c.if_view, c.catorder, COUNT(s.catid) AS has_children FROM `' . $GLOBALS['db_mymps'] . 'goods_category` AS c LEFT JOIN `' . $GLOBALS['db_mymps'] . 'goods_category` AS s ON s.parentid=c.catid GROUP BY c.catid ORDER BY c.parentid, c.catorder ASC';
  75. $res = $GLOBALS['db']->getAll($sql);
  76. $sql = NULL;
  77. $newres = array();
  78. if (count($res) <= 1000) {
  79. write_static_cache('goods_category_pid_releate', $res);
  80. }
  81. }
  82. else {
  83. $res = $data;
  84. }
  85. if (empty($res) == true) {
  86. return $re_type ? '' : array();
  87. }
  88. $options = goods_cat_options($catid, $res);
  89. $children_level = 99999;
  90. if ($is_show_all == false) {
  91. foreach ($options as $key => $val) {
  92. if ($children_level < $val['level']) {
  93. unset($options[$key]);
  94. }
  95. else if ($val['is_show'] == 0) {
  96. unset($options[$key]);
  97. if ($val['level'] < $children_level) {
  98. $children_level = $val['level'];
  99. }
  100. }
  101. else {
  102. $children_level = 99999;
  103. }
  104. }
  105. }
  106. if (0 < $level) {
  107. if ($catid == 0) {
  108. $end_level = $level;
  109. }
  110. else {
  111. $first_item = reset($options);
  112. $end_level = $first_item['level'] + $level;
  113. }
  114. foreach ($options as $key => $val) {
  115. if ($end_level <= $val['level']) {
  116. unset($options[$key]);
  117. }
  118. }
  119. }
  120. if ($re_type == true) {
  121. $select = '';
  122. foreach ($options as $var) {
  123. $select .= '<option value="' . $var['catid'] . '" ';
  124. if (is_array($selected)) {
  125. $select .= in_array($var['catid'], $selected) ? 'selected=\'ture\' style=\'background-color:#6eb00c; color:white!important;\'' : '';
  126. }
  127. else {
  128. $select .= $selected == $var['catid'] ? 'selected=\'ture\' style=\'background-color:#6eb00c; color:white!important;\'' : '';
  129. }
  130. $select .= '>';
  131. if (0 < $var['level']) {
  132. $select .= str_repeat('&nbsp;', $var['level'] * 4);
  133. }
  134. $select .= '├ ' . mhtmlspecialchars($var['catname'], ENT_QUOTES) . '</option>';
  135. }
  136. return $select;
  137. }
  138. else {
  139. foreach ($options as $key => $value) {
  140. $options[$key]['url'] = $value['catid'];
  141. }
  142. return $options;
  143. }
  144. }
  145. function goods_cat_options($spec_catid, $arr)
  146. {
  147. $cat_options = array();
  148. if (isset($cat_options[$spec_cat_id])) {
  149. return $cat_options[$spec_cat_id];
  150. }
  151. if (!isset($cat_options[0])) {
  152. $level = $last_cat_id = 0;
  153. $options = $cat_id_array = $level_array = array();
  154. $data = read_static_cache('goods_category_option_static');
  155. if ($data === false) {
  156. while (!empty($arr)) {
  157. foreach ($arr as $key => $value) {
  158. $cat_id = $value['catid'];
  159. if ($level == 0 && $last_cat_id == 0) {
  160. if (0 < $value['parentid']) {
  161. break;
  162. }
  163. $options[$cat_id] = $value;
  164. $options[$cat_id]['level'] = $level;
  165. $options[$cat_id]['id'] = $cat_id;
  166. $options[$cat_id]['name'] = $value['catname'];
  167. unset($arr[$key]);
  168. if ($value['has_children'] == 0) {
  169. continue;
  170. }
  171. $last_cat_id = $cat_id;
  172. $cat_id_array = array($cat_id);
  173. $level_array[$last_cat_id] = ++$level;
  174. continue;
  175. }
  176. if ($value['parentid'] == $last_cat_id) {
  177. $options[$cat_id] = $value;
  178. $options[$cat_id]['level'] = $level;
  179. $options[$cat_id]['id'] = $cat_id;
  180. $options[$cat_id]['name'] = $value['catname'];
  181. unset($arr[$key]);
  182. if (0 < $value['has_children']) {
  183. if (end($cat_id_array) != $last_cat_id) {
  184. $cat_id_array[] = $last_cat_id;
  185. }
  186. $last_cat_id = $cat_id;
  187. $cat_id_array[] = $cat_id;
  188. $level_array[$last_cat_id] = ++$level;
  189. }
  190. }
  191. else if ($last_cat_id < $value['parentid']) {
  192. break;
  193. }
  194. }
  195. $count = count($cat_id_array);
  196. if (1 < $count) {
  197. $last_cat_id = array_pop($cat_id_array);
  198. }
  199. else if ($count == 1) {
  200. if ($last_cat_id != end($cat_id_array)) {
  201. $last_cat_id = end($cat_id_array);
  202. }
  203. else {
  204. $level = 0;
  205. $last_cat_id = 0;
  206. $cat_id_array = array();
  207. continue;
  208. }
  209. }
  210. if ($last_cat_id && isset($level_array[$last_cat_id])) {
  211. $level = $level_array[$last_cat_id];
  212. }
  213. else {
  214. $level = 0;
  215. }
  216. }
  217. if (count($options) <= 2000) {
  218. write_static_cache('goods_category_option_static', $options);
  219. }
  220. }
  221. else {
  222. $options = $data;
  223. }
  224. $cat_options[0] = $options;
  225. }
  226. else {
  227. $options = $cat_options[0];
  228. }
  229. if (!$spec_cat_id) {
  230. return $options;
  231. }
  232. else {
  233. if (empty($options[$spec_cat_id])) {
  234. return array();
  235. }
  236. $spec_cat_id_level = $options[$spec_cat_id]['level'];
  237. foreach ($options as $key => $value) {
  238. if ($key != $spec_cat_id) {
  239. unset($options[$key]);
  240. }
  241. else {
  242. break;
  243. }
  244. }
  245. $spec_cat_id_array = array();
  246. foreach ($options as $key => $value) {
  247. if ($spec_cat_id_level == $value['level'] && $value['catid'] != $spec_cat_id || $value['level'] < $spec_cat_id_level) {
  248. break;
  249. }
  250. else {
  251. $spec_cat_id_array[$key] = $value;
  252. }
  253. }
  254. $cat_options[$spec_cat_id] = $spec_cat_id_array;
  255. return $spec_cat_id_array;
  256. }
  257. }
  258. function goods_parent_cats($cat = '')
  259. {
  260. global $seo;
  261. if ($cat == 0) {
  262. return array();
  263. }
  264. $data = read_static_cache('goods_category_pid_releate');
  265. if ($data === false) {
  266. $arr = $GLOBALS['db']->getAll('SELECT catid, catname, parentid FROM `' . $GLOBALS['db_mymps'] . 'goods_category`');
  267. }
  268. else {
  269. $arr = $data;
  270. }
  271. if (empty($arr)) {
  272. return array();
  273. }
  274. $index = 0;
  275. $cats = array();
  276. while (1) {
  277. foreach ($arr as $row) {
  278. if ($cat == $row['catid']) {
  279. $cat = $row['parentid'];
  280. $cats[$index]['catid'] = $row['catid'];
  281. $cats[$index]['catname'] = $row['catname'];
  282. $cats[$index]['uri'] = Rewrite('goods', array('catid' => $row['catid']));
  283. $index++;
  284. break;
  285. }
  286. }
  287. if ($index == 0 || $cat == 0) {
  288. break;
  289. }
  290. }
  291. return $cats;
  292. }
  293. ?>