PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Admin/Lib/Model/CategoryModel.class.php

http://nblog-thinkphp.googlecode.com/
PHP | 366 lines | 245 code | 41 blank | 80 comment | 66 complexity | 78569424e74377a47c2ea827a3cd984d MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ??????
  4. // +----------------------------------------------------------------------
  5. // | @link ( http://www.yurnero.net )
  6. // +----------------------------------------------------------------------
  7. // | @copyright
  8. // +----------------------------------------------------------------------
  9. // | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | @author Haijun Wu <nicholasinlove@126.com>
  12. // +----------------------------------------------------------------------
  13. // | $Id: CategoryModel.class.php 26 2011-02-15 05:48:40Z nicholasinlove1986@gmail.com $
  14. // +----------------------------------------------------------------------
  15. class CategoryModel extends Model {
  16. protected $catString = '';
  17. protected $catUrlString = array();
  18. public $_validate = array(
  19. array('category_title','require','{%category_title_require}'),
  20. array('category_url','require','{%category_url_require}'),
  21. array('category_sort','require','{%category_sort_require}'),
  22. array('category_title','','{%category_title_unique}',0,'unique',1),
  23. array('category_url','','{%category_url_unique}',0,'unique',1),
  24. );
  25. public $_auto = array(
  26. array('category_addtime','time',3,'function'),
  27. array('category_url','gdToEncode',3,'callback'),
  28. );
  29. //??
  30. protected function gdToEncode() {
  31. $str = $_POST['category_url'];
  32. if (is_gb($str)) {
  33. return strtolower(urlencode($str));
  34. } else {
  35. return strtolower($str);
  36. }
  37. }
  38. /**
  39. * ??????????????????
  40. *
  41. * @access public
  42. * @return array
  43. */
  44. public function getCatArray() {
  45. $sql = "select c.category_id, c.parent_id, c.category_url, c.category_title, c.category_sort, c.category_isshow,
  46. c.category_seoti, c.category_seokw, c.category_seodesc, count(s.category_id) as has_children ".
  47. " from ".C('DB_PREFIX')."category as c ".
  48. " left join ".C('DB_PREFIX')."category as s on s.parent_id = c.category_id ".
  49. " group by c.category_id ".
  50. ' order by c.parent_id, c.category_sort asc ';
  51. $res = $this->query($sql);
  52. /*$sql = "select category_id, count(*) as posts_num " .
  53. " from ".C('DB_PREFIX')."posts ".
  54. " where posts_status <> 0 " .
  55. " group by category_id";
  56. $res2 = $this->query($sql);*/
  57. $sql = "select p.category_id, count(*) as posts_num " .
  58. " from ".C('DB_PREFIX')."posts as p , ".C('DB_PREFIX')."category as c ".
  59. " where p.category_id = c.category_id and posts_status <> 0 " .
  60. " group by p.category_id";
  61. $res2 = $this->query($sql);
  62. $newres = array();
  63. foreach( $res as $k=>$v) {
  64. $newres[$v['category_id']] = $v['posts_num'];
  65. foreach($res2 as $ks=>$vs) {
  66. if ($v['category_id'] == $vs['category_id']) {
  67. $newres[$v['category_id']] += $vs['posts_num'];
  68. }
  69. }
  70. }
  71. foreach ($res as $k=>$v) {
  72. $res[$k]['posts_num'] = !empty($newres[$v['category_id']]) ? $newres[$v['category_id']] : 0;
  73. }
  74. return $res;
  75. }
  76. /**
  77. * ???????????????????????
  78. *
  79. * @access private
  80. * @param int $cat_id ????ID
  81. * @param array $arr ?????????
  82. * @param int $level ??
  83. * @return void
  84. */
  85. public function getCatOptions($arr, $spec_cat_id = 0) {
  86. static $cat_options = array();
  87. $path = ROOT_PATH.STATIC_CACHES_DIR;
  88. if (isset($cat_options[$spec_cat_id])) {
  89. return $cat_options[$spec_cat_id];
  90. }
  91. if (!isset($cat_options[0])) {
  92. $level = $last_cat_id = 0;
  93. $options = $cat_id_array = $level_array = array();
  94. /*if (file_exists($path.'cat_option_static.php')) {
  95. $data = require_once ($path.'cat_option_static.php');
  96. }*/
  97. //if ($data == false) {
  98. while (!empty($arr))
  99. {
  100. foreach ($arr as $key => $value)
  101. {
  102. $cat_id = $value['category_id'];
  103. if ($level == 0 && $last_cat_id == 0) {
  104. if ($value['parent_id'] > 0) {
  105. break;
  106. }
  107. $options[$cat_id] = $value;
  108. $options[$cat_id]['level'] = $level;
  109. $options[$cat_id]['id'] = $cat_id;
  110. $options[$cat_id]['name'] = $value['category_title'];
  111. unset($arr[$key]);
  112. if ($value['has_children'] == 0) {
  113. continue;
  114. }
  115. $last_cat_id = $cat_id;
  116. $cat_id_array = array($cat_id);
  117. $level_array[$last_cat_id] = ++$level;
  118. continue;
  119. }
  120. if ($value['parent_id'] == $last_cat_id) {
  121. $options[$cat_id] = $value;
  122. $options[$cat_id]['level'] = $level;
  123. $options[$cat_id]['id'] = $cat_id;
  124. $options[$cat_id]['name'] = $value['category_title'];
  125. unset($arr[$key]);
  126. if ($value['has_children'] > 0) {
  127. if (end($cat_id_array) != $last_cat_id) {
  128. $cat_id_array[] = $last_cat_id;
  129. }
  130. $last_cat_id = $cat_id;
  131. $cat_id_array[] = $cat_id;
  132. $level_array[$last_cat_id] = ++$level;
  133. }
  134. }
  135. elseif ($value['parent_id'] > $last_cat_id) {
  136. break;
  137. }
  138. }
  139. $count = count($cat_id_array);
  140. if ($count > 1) {
  141. $last_cat_id = array_pop($cat_id_array);
  142. }
  143. elseif ($count == 1) {
  144. if ($last_cat_id != end($cat_id_array)) {
  145. $last_cat_id = end($cat_id_array);
  146. } else {
  147. $level = 0;
  148. $last_cat_id = 0;
  149. $cat_id_array = array();
  150. continue;
  151. }
  152. }
  153. if ($last_cat_id && isset($level_array[$last_cat_id])) {
  154. $level = $level_array[$last_cat_id];
  155. } else {
  156. $level = 0;
  157. }
  158. }
  159. //????????
  160. f_static_cache('cat_option_static',$options ,$path);
  161. //} else {
  162. //$options = $data;
  163. //}
  164. $cat_options[0] = $options;
  165. } else {
  166. $options = $cat_options[0];
  167. }
  168. if (!$spec_cat_id) {
  169. return $options;
  170. } else {
  171. if (empty($options[$spec_cat_id])) {
  172. return array();
  173. }
  174. $spec_cat_id_level = $options[$spec_cat_id]['level'];
  175. foreach ($options as $key => $value) {
  176. if ($key != $spec_cat_id) {
  177. unset($options[$key]);
  178. } else {
  179. break;
  180. }
  181. }
  182. $spec_cat_id_array = array();
  183. foreach ($options as $key => $value) {
  184. if (($spec_cat_id_level == $value['level'] && $value['category_id'] != $spec_cat_id) || ($spec_cat_id_level > $value['level'])) {
  185. break;
  186. } else {
  187. $spec_cat_id_array[$key] = $value;
  188. }
  189. }
  190. $cat_options[$spec_cat_id] = $spec_cat_id_array;
  191. return $spec_cat_id_array;
  192. }
  193. }
  194. /**
  195. * ??????
  196. *
  197. * @access public
  198. * @param
  199. * @return array
  200. */
  201. public function getCatDate($cat_id) {
  202. $path = ROOT_PATH.STATIC_CACHES_DIR;
  203. if (file_exists($path.'cat_option_static.php')) {
  204. $cat_option = f_static_cache('cat_option_static','',$path);
  205. } else {
  206. if (file_exists($path.'category_static.php')) {
  207. $data = f_static_cache('category_static','',$path);
  208. } else {
  209. $data = $this->getCatArray();
  210. }
  211. f_static_cache('category_static',$data ,$path);
  212. $cat_option = $this->getCatOptions($data,$cat_id);
  213. //f_static_cache('cat_option_static',$cat_option ,$path);
  214. }
  215. return $cat_option;
  216. }
  217. /**
  218. * ????????(disabled)
  219. *
  220. * @access public
  221. * @param
  222. * @return void
  223. */
  224. public function rewriteStatic() {
  225. $data = $this->getCatArray();
  226. $cat_option = $this->getCatOptions($data);
  227. f_static_cache('category_static',$data ,$path);
  228. //f_static_cache('cat_option_static',$cat_option ,$path);
  229. }
  230. /**
  231. * ???????????
  232. *
  233. * @access public
  234. * @param array $arr ???????????
  235. * @param int $cat_id ???ID,????ID
  236. * @param int $selected ???????ID
  237. * @param int $level ?????????0???????
  238. * @param boolean $re_type ?????: ??????????,??????
  239. * @param int $is_show_all ???true??????????false????????
  240. * @return mix
  241. */
  242. public function getCatList($cat_id = 0, $selected = 0, $level = 0, $re_type = true, $is_show_all = true) {
  243. $arr = $this->getCatDate($cat_id);
  244. $children_level = 99999; //???????????
  245. /*???????*/
  246. if ($is_show_all == false) {
  247. foreach ($arr as $key => $val) {
  248. if ($val['level'] > $children_level) {
  249. unset($arr[$key]);
  250. } else {
  251. if ($val['category_isshow'] == 0) {
  252. unset($arr[$key]);
  253. if ($children_level > $val['level']) {
  254. $children_level = $val['level']; //??????????????
  255. }
  256. } else {
  257. $children_level = 99999; //?????
  258. }
  259. }
  260. }
  261. }
  262. /* ?????????? */
  263. if ($level > 0) {
  264. if ($cat_id == 0) {
  265. $end_level = $level;
  266. } else {
  267. $first_item = reset($arr); // ???????
  268. $end_level = $first_item['level'] + $level;
  269. }
  270. /* ??level??end_level??? */
  271. foreach ($arr as $key => $val) {
  272. if ($val['level'] >= $end_level) {
  273. unset($arr[$key]);
  274. }
  275. }
  276. }
  277. if ($re_type) {
  278. foreach ($arr as $cat) {
  279. $this->catString .= "<option value='{$cat['category_id']}' ";
  280. $this->catString .= ($cat['category_id'] == $selected) ? "selected='selected'" : '';
  281. $this->catString .= ">";
  282. if ($cat['level'] > 0) {
  283. $this->catString .= str_repeat('&nbsp;', $cat['level'] * 4);
  284. }
  285. $this->catString .= "{$cat['category_title']}</option>";
  286. }
  287. return $this->catString;
  288. } else {
  289. foreach ($arr as $key => $cat) {
  290. $arr[$key]['url'] = C('blog_domain').$cat['category_url'].'/';
  291. }
  292. return $arr;
  293. }
  294. }
  295. /**
  296. * ?????????????????????
  297. * ??: html-table-tr-td
  298. * @access public
  299. * @param int $cat_id ????ID
  300. * @return string
  301. */
  302. public function getCatUrl($cat_id) {
  303. if (!isset($cat_id)) return false;
  304. $path = ROOT_PATH.STATIC_CACHES_DIR;
  305. if (file_exists($path.'category_static.php')) {
  306. $data = f_static_cache('category_static','',$path);
  307. } else {
  308. $data = $this->getCatArray();
  309. }
  310. $arr = $this->getCatOptions($data,$cat_id);
  311. if ($p = $arr[$cat_id]['parent_id']) {
  312. $this->catUrlString[] = $arr[$cat_id]['name'];
  313. $this->getCatUrl($p);
  314. } else {
  315. $this->catUrlString[] = $arr[$cat_id]['name'];
  316. }
  317. krsort($this->catUrlString);
  318. foreach ($this->catUrlString as $cat) {
  319. $string .= $cat.'-';
  320. }
  321. $string = (strrpos($string, '-')) ? $string : '';
  322. return $string;
  323. }
  324. }
  325. ?>