PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/models/posts.php

https://gitlab.com/Ltaimao/wecenter
PHP | 514 lines | 397 code | 105 blank | 12 comment | 62 complexity | 10ae76c48008b42085a05d9816b0a673 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. if (!defined('IN_ANWSION'))
  15. {
  16. die;
  17. }
  18. class posts_class extends AWS_MODEL
  19. {
  20. public function set_posts_index($post_id, $post_type, $data = null)
  21. {
  22. if ($data)
  23. {
  24. $result = $data;
  25. }
  26. else
  27. {
  28. switch ($post_type)
  29. {
  30. case 'question':
  31. $result = $this->fetch_row('question', 'question_id = ' . intval($post_id));
  32. break;
  33. case 'article':
  34. $result = $this->fetch_row('article', 'id = ' . intval($post_id));
  35. break;
  36. case 'project':
  37. $result = $this->fetch_row('project', 'id = ' . intval($post_id));
  38. break;
  39. }
  40. if (!$result)
  41. {
  42. return false;
  43. }
  44. }
  45. switch ($post_type)
  46. {
  47. case 'question':
  48. $data = array(
  49. 'add_time' => $result['add_time'],
  50. 'update_time' => $result['update_time'],
  51. 'category_id' => $result['category_id'],
  52. 'is_recommend' => $result['is_recommend'],
  53. 'view_count' => $result['view_count'],
  54. 'anonymous' => $result['anonymous'],
  55. 'popular_value' => $result['popular_value'],
  56. 'uid' => $result['published_uid'],
  57. 'lock' => $result['lock'],
  58. 'agree_count' => $result['agree_count'],
  59. 'answer_count' => $result['answer_count']
  60. );
  61. break;
  62. case 'article':
  63. $data = array(
  64. 'add_time' => $result['add_time'],
  65. 'update_time' => $result['add_time'],
  66. 'category_id' => $result['category_id'],
  67. 'view_count' => $result['views'],
  68. 'anonymous' => 0,
  69. 'uid' => $result['uid'],
  70. 'agree_count' => $result['votes'],
  71. 'answer_count' => $result['comments'],
  72. 'lock' => $result['lock'],
  73. 'is_recommend' => $result['is_recommend'],
  74. );
  75. break;
  76. case 'project':
  77. $data = array(
  78. 'add_time' => $result['add_time'],
  79. 'update_time' => $result['update_time'],
  80. 'category_id' => 0,
  81. 'is_recommend' => 0,
  82. 'view_count' => $result['views'],
  83. 'anonymous' => 0,
  84. 'popular_value' => 0,
  85. 'uid' => $result['uid'],
  86. 'lock' => 0,
  87. 'agree_count' => 0,
  88. 'answer_count' => 0
  89. );
  90. break;
  91. }
  92. if ($posts_index = $this->fetch_all('posts_index', "post_id = " . intval($post_id) . " AND post_type = '" . $this->quote($post_type) . "'"))
  93. {
  94. $post_index = end($posts_index);
  95. $this->update('posts_index', $data, 'id = ' . intval($post_index['id']));
  96. if (sizeof($posts_index) > 1)
  97. {
  98. $this->delete('posts_index', "post_id = " . intval($post_id) . " AND post_type = '" . $this->quote($post_type) . "' AND id != " . intval($post_index['id']));
  99. }
  100. }
  101. else
  102. {
  103. $data = array_merge($data, array(
  104. 'post_id' => intval($post_id),
  105. 'post_type' => $post_type
  106. ));
  107. $this->remove_posts_index($post_id, $post_type);
  108. $this->insert('posts_index', $data);
  109. }
  110. }
  111. public function remove_posts_index($post_id, $post_type)
  112. {
  113. return $this->delete('posts_index', "post_id = " . intval($post_id) . " AND post_type = '" . $this->quote($post_type) . "'");
  114. }
  115. public function get_posts_list($post_type, $page = 1, $per_page = 10, $sort = null, $topic_ids = null, $category_id = null, $answer_count = null, $day = 30, $is_recommend = false)
  116. {
  117. $order_key = 'add_time DESC';
  118. switch ($sort)
  119. {
  120. case 'responsed':
  121. $answer_count = 1;
  122. break;
  123. case 'unresponsive':
  124. $answer_count = 0;
  125. break;
  126. case 'new' :
  127. $order_key = 'update_time DESC';
  128. break;
  129. }
  130. if (is_array($topic_ids))
  131. {
  132. foreach ($topic_ids AS $key => $val)
  133. {
  134. if (!$val)
  135. {
  136. unset($topic_ids[$key]);
  137. }
  138. }
  139. }
  140. if ($topic_ids)
  141. {
  142. $posts_index = $this->get_posts_list_by_topic_ids($post_type, $post_type, $topic_ids, $category_id, $answer_count, $order_key, $is_recommend, $page, $per_page);
  143. }
  144. else
  145. {
  146. $where = array();
  147. if (isset($answer_count))
  148. {
  149. $answer_count = intval($answer_count);
  150. if ($answer_count == 0)
  151. {
  152. $where[] = "answer_count = " . $answer_count;
  153. }
  154. else if ($answer_count > 0)
  155. {
  156. $where[] = "answer_count >= " . $answer_count;
  157. }
  158. }
  159. if ($is_recommend)
  160. {
  161. $where[] = 'is_recommend = 1';
  162. }
  163. if ($category_id)
  164. {
  165. $where[] = 'category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
  166. }
  167. if ($post_type)
  168. {
  169. $where[] = "post_type = '" . $this->quote($post_type) . "'";
  170. }
  171. $posts_index = $this->fetch_page('posts_index', implode(' AND ', $where), $order_key, $page, $per_page);
  172. $this->posts_list_total = $this->found_rows();
  173. }
  174. return $this->process_explore_list_data($posts_index);
  175. }
  176. public function get_hot_posts($post_type, $category_id = 0, $topic_ids = null, $day = 30, $page = 1, $per_page = 10)
  177. {
  178. if ($day)
  179. {
  180. $add_time = strtotime('-' . $day . ' Day');
  181. }
  182. $where[] = 'add_time > ' . intval($add_time);
  183. if ($post_type)
  184. {
  185. $where[] = "post_type = '" . $this->quote($post_type) . "'";
  186. }
  187. if ($category_id)
  188. {
  189. $where[] = 'category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
  190. }
  191. if (is_array($topic_ids))
  192. {
  193. foreach ($topic_ids AS $key => $val)
  194. {
  195. if (!$val)
  196. {
  197. unset($topic_ids[$key]);
  198. }
  199. }
  200. }
  201. if ($topic_ids)
  202. {
  203. array_walk_recursive($topic_ids, 'intval_string');
  204. if (!$post_type)
  205. {
  206. if ($question_post_ids = $this->model('topic')->get_item_ids_by_topics_ids($topic_ids, 'question') OR $article_post_ids = $this->model('topic')->get_item_ids_by_topics_ids($topic_ids, 'article'))
  207. {
  208. if ($question_post_ids)
  209. {
  210. $topic_where[] = 'post_id IN(' . implode(',', $question_post_ids) . ") AND post_type = 'question'";
  211. }
  212. if ($article_post_ids)
  213. {
  214. $topic_where[] = 'post_id IN(' . implode(',', $article_post_ids) . ") AND post_type = 'article'";
  215. }
  216. if ($topic_where)
  217. {
  218. $where[] = '(' . implode(' OR ', $topic_where) . ')';
  219. }
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. else if ($post_ids = $this->model('topic')->get_item_ids_by_topics_ids($topic_ids, $post_type))
  227. {
  228. $where[] = 'post_id IN(' . implode(',', $post_ids) . ") AND post_type = '" . $post_type . "'";
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. $posts_index = $this->fetch_page('posts_index', implode(' AND ', $where), 'popular_value DESC', $page, $per_page);
  236. $this->posts_list_total = $this->found_rows();
  237. return $this->process_explore_list_data($posts_index);
  238. }
  239. public function get_posts_list_total()
  240. {
  241. return $this->posts_list_total;
  242. }
  243. public function process_explore_list_data($posts_index)
  244. {
  245. if (!$posts_index)
  246. {
  247. return false;
  248. }
  249. foreach ($posts_index as $key => $data)
  250. {
  251. switch ($data['post_type'])
  252. {
  253. case 'question':
  254. $question_ids[] = $data['post_id'];
  255. break;
  256. case 'article':
  257. $article_ids[] = $data['post_id'];
  258. break;
  259. case 'project':
  260. continue 2;
  261. $project_ids[] = $data['post_id'];
  262. break;
  263. }
  264. $data_list_uids[$data['uid']] = $data['uid'];
  265. }
  266. if ($question_ids)
  267. {
  268. if ($last_answers = $this->model('answer')->get_last_answer_by_question_ids($question_ids))
  269. {
  270. foreach ($last_answers as $key => $val)
  271. {
  272. $data_list_uids[$val['uid']] = $val['uid'];
  273. }
  274. }
  275. $topic_infos['question'] = $this->model('topic')->get_topics_by_item_ids($question_ids, 'question');
  276. $question_infos = $this->model('question')->get_question_info_by_ids($question_ids);
  277. }
  278. if ($article_ids)
  279. {
  280. $topic_infos['article'] = $this->model('topic')->get_topics_by_item_ids($article_ids, 'article');
  281. $article_infos = $this->model('article')->get_article_info_by_ids($article_ids);
  282. }
  283. if ($project_ids)
  284. {
  285. $topic_infos['project'] = $this->model('topic')->get_topics_by_item_ids($project_ids, 'project');
  286. $project_infos = $this->model('project')->get_project_info_by_ids($project_ids);
  287. }
  288. $users_info = $this->model('account')->get_user_info_by_uids($data_list_uids);
  289. foreach ($posts_index as $key => $data)
  290. {
  291. switch ($data['post_type'])
  292. {
  293. case 'question':
  294. $explore_list_data[$key] = $question_infos[$data['post_id']];
  295. $explore_list_data[$key]['answer_info'] = $last_answers[$data['post_id']];
  296. if ($explore_list_data[$key]['answer_info'])
  297. {
  298. $explore_list_data[$key]['answer_info']['user_info'] = $users_info[$last_answers[$data['post_id']]['uid']];
  299. }
  300. break;
  301. case 'article':
  302. $explore_list_data[$key] = $article_infos[$data['post_id']];
  303. break;
  304. case 'project':
  305. continue 2;
  306. $explore_list_data[$key] = $project_infos[$data['post_id']];
  307. break;
  308. }
  309. $explore_list_data[$key]['post_type'] = $data['post_type'];
  310. if (get_setting('category_enable') == 'Y')
  311. {
  312. $explore_list_data[$key]['category_info'] = $this->model('system')->get_category_info($data['category_id']);
  313. }
  314. $explore_list_data[$key]['topics'] = $topic_infos[$data['post_type']][$data['post_id']];
  315. $explore_list_data[$key]['user_info'] = $users_info[$data['uid']];
  316. }
  317. return $explore_list_data;
  318. }
  319. public function get_posts_list_by_topic_ids($post_type, $topic_type, $topic_ids, $category_id = null, $answer_count = null, $order_by = 'post_id DESC', $is_recommend = false, $page = 1, $per_page = 10)
  320. {
  321. if (!is_array($topic_ids))
  322. {
  323. return false;
  324. }
  325. array_walk_recursive($topic_ids, 'intval_string');
  326. $result_cache_key = 'posts_list_by_topic_ids_' . md5(implode(',', $topic_ids) . $answer_count . $category_id . $order_by . $is_recommend . $page . $per_page . $post_type . $topic_type);
  327. $found_rows_cache_key = 'posts_list_by_topic_ids_found_rows_' . md5(implode(',', $topic_ids) . $answer_count . $category_id . $is_recommend . $per_page . $post_type . $topic_type);
  328. $topic_relation_where[] = '`topic_id` IN(' . implode(',', $topic_ids) . ')';
  329. if ($topic_type)
  330. {
  331. $topic_relation_where[] = "`type` = '" . $this->quote($topic_type) . "'";
  332. }
  333. if ($topic_relation_query = $this->query_all("SELECT `item_id`, `type` FROM " . get_table('topic_relation') . " WHERE " . implode(' AND ', $topic_relation_where)))
  334. {
  335. foreach ($topic_relation_query AS $key => $val)
  336. {
  337. $post_ids[$val['type']][$val['item_id']] = $val['item_id'];
  338. }
  339. }
  340. if (!$post_ids)
  341. {
  342. return false;
  343. }
  344. foreach ($post_ids AS $key => $val)
  345. {
  346. $post_id_where[] = "(post_id IN (" . implode(',', $val) . ") AND post_type = '" . $this->quote($key) . "')";
  347. }
  348. if ($post_id_where)
  349. {
  350. $where[] = '(' . implode(' OR ', $post_id_where) . ')';
  351. }
  352. if (is_digits($answer_count))
  353. {
  354. if ($answer_count == 0)
  355. {
  356. $where[] = "answer_count = " . $answer_count;
  357. }
  358. else if ($answer_count > 0)
  359. {
  360. $where[] = "answer_count >= " . $answer_count;
  361. }
  362. }
  363. if ($is_recommend)
  364. {
  365. $where[] = 'is_recommend = 1';
  366. }
  367. if ($post_type)
  368. {
  369. $where[] = "post_type = '" . $this->quote($post_type) . "'";
  370. }
  371. if ($category_id)
  372. {
  373. $where[] = 'category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
  374. }
  375. if (!$result = AWS_APP::cache()->get($result_cache_key))
  376. {
  377. if ($result = $this->fetch_page('posts_index', implode(' AND ', $where), $order_by, $page, $per_page))
  378. {
  379. AWS_APP::cache()->set($result_cache_key, $result, get_setting('cache_level_high'));
  380. }
  381. }
  382. if (!$found_rows = AWS_APP::cache()->get($found_rows_cache_key))
  383. {
  384. if ($found_rows = $this->found_rows())
  385. {
  386. AWS_APP::cache()->set($found_rows_cache_key, $found_rows, get_setting('cache_level_high'));
  387. }
  388. }
  389. $this->posts_list_total = $found_rows;
  390. return $result;
  391. }
  392. public function get_recommend_posts_by_topic_ids($topic_ids)
  393. {
  394. if (!$topic_ids OR !is_array($topic_ids))
  395. {
  396. return false;
  397. }
  398. $related_topic_ids = array();
  399. foreach ($topic_ids AS $topic_id)
  400. {
  401. $related_topic_ids = array_merge($related_topic_ids, $this->model('topic')->get_related_topic_ids_by_id($topic_id));
  402. }
  403. if ($related_topic_ids)
  404. {
  405. $recommend_posts = $this->model('posts')->get_posts_list(null, 1, 10, null, $related_topic_ids, null, null, 30, true);
  406. }
  407. return $recommend_posts;
  408. }
  409. }