/models/actions.php

https://gitlab.com/Ltaimao/wecenter · PHP · 544 lines · 413 code · 99 blank · 32 comment · 70 complexity · 234f98bdc648a03a06119cc4717bb8e6 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 actions_class extends AWS_MODEL
  19. {
  20. public function home_activity($uid, $limit = 10)
  21. {
  22. // 我关注的话题
  23. if ($user_focus_topics_ids = $this->model('topic')->get_focus_topic_ids_by_uid($uid))
  24. {
  25. if ($user_focus_topics_questions_ids = $this->model('topic')->get_item_ids_by_topics_ids($user_focus_topics_ids, 'question', 1000))
  26. {
  27. if ($user_focus_topics_info = $this->model('question')->get_topic_info_by_question_ids($user_focus_topics_questions_ids))
  28. {
  29. foreach ($user_focus_topics_info AS $key => $user_focus_topics_info_by_question)
  30. {
  31. foreach ($user_focus_topics_info_by_question AS $_key => $_val)
  32. {
  33. if (!in_array($_val['topic_id'], $user_focus_topics_ids))
  34. {
  35. unset($user_focus_topics_info[$key][$_key]);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. $user_focus_topics_article_ids = $this->model('topic')->get_item_ids_by_topics_ids($user_focus_topics_ids, 'article', 1000);
  42. }
  43. // 我关注的问题
  44. /*if ($user_focus_questions_ids = $this->model('question')->get_focus_question_ids_by_uid($uid))
  45. {
  46. // 回复问题
  47. $where_in[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND associate_id IN (" . implode(',', $user_focus_questions_ids) . ") AND associate_action = " . ACTION_LOG::ANSWER_QUESTION . " AND uid <> " . $uid . ")";
  48. }*/
  49. // 我关注的话题
  50. if ($user_focus_topics_questions_ids)
  51. {
  52. // 回复问题, 新增问题, 赞同答案
  53. $where_in[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND associate_id IN (" . implode(',', $user_focus_topics_questions_ids) . ") AND associate_action IN (" . ACTION_LOG::ANSWER_QUESTION . ", " . ACTION_LOG::ADD_QUESTION . ", " . ACTION_LOG::ADD_AGREE . ") AND uid <> " . $uid . ")";
  54. }
  55. if ($user_focus_topics_article_ids)
  56. {
  57. // 发表文章, 文章评论
  58. $where_in[] = "(associate_id IN (" . implode(',', $user_focus_topics_article_ids) . ") AND associate_action IN (" . ACTION_LOG::ADD_ARTICLE . ", " . ACTION_LOG::ADD_COMMENT_ARTICLE . ") AND uid <> " . $uid . ")";
  59. }
  60. // 我关注的人
  61. if ($user_follow_uids = $this->model('follow')->get_user_friends_ids($uid))
  62. {
  63. // 添加问题, 回复问题, 添加文章
  64. $where_in[] = "(uid IN (" . implode(',', $user_follow_uids) . ") AND associate_action IN(" . ACTION_LOG::ADD_QUESTION . ',' . ACTION_LOG::ANSWER_QUESTION . ',' . ACTION_LOG::ADD_ARTICLE . ',' . ACTION_LOG::ADD_COMMENT_ARTICLE . '))';
  65. // 增加赞同, 文章评论
  66. $where_in[] = "(uid IN (" . implode(',', $user_follow_uids) . ") AND associate_action IN(" . ACTION_LOG::ADD_AGREE_ARTICLE .", " . ACTION_LOG::ADD_COMMENT_ARTICLE . ") AND uid <> " . $uid . ")";
  67. // 添加问题关注
  68. if ($user_focus_questions_ids)
  69. {
  70. $where_in[] = "(uid IN (" . implode(',', $user_follow_uids) . ") AND associate_action = " . ACTION_LOG::ADD_REQUESTION_FOCUS . " AND associate_id NOT IN (" . implode(',', $user_focus_questions_ids) . "))";
  71. }
  72. else
  73. {
  74. $where_in[] = "(uid IN (" . implode(',', $user_follow_uids) . ") AND associate_action = " . ACTION_LOG::ADD_REQUESTION_FOCUS . ")";
  75. }
  76. }
  77. else
  78. {
  79. $user_follow_uids = array();
  80. }
  81. // 添加问题, 添加文章
  82. $where_in[] = "(associate_action IN (" . ACTION_LOG::ADD_QUESTION . ", " . ACTION_LOG::ADD_ARTICLE . ") AND uid = " . $uid . ")";
  83. if ($questions_uninterested_ids = $this->model('question')->get_question_uninterested($uid))
  84. {
  85. $where = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND associate_id NOT IN (" . implode(',', $questions_uninterested_ids) . "))";
  86. }
  87. else
  88. {
  89. $where = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . ")";
  90. }
  91. if ($where_in AND $where)
  92. {
  93. $where .= ' AND (' . implode(' OR ', $where_in) . ')';
  94. }
  95. else if ($where_in)
  96. {
  97. $where = implode(' OR ', $where_in);
  98. }
  99. if (! $action_list = ACTION_LOG::get_actions_fresh_by_where($where, $limit))
  100. {
  101. return false;
  102. }
  103. foreach ($action_list as $key => $val)
  104. {
  105. if (in_array($val['associate_action'], array(
  106. ACTION_LOG::ADD_ARTICLE,
  107. ACTION_LOG::ADD_AGREE_ARTICLE,
  108. ACTION_LOG::ADD_COMMENT_ARTICLE
  109. )))
  110. {
  111. $action_list_article_ids[] = $val['associate_id'];
  112. if ($val['associate_action'] == ACTION_LOG::ADD_COMMENT_ARTICLE AND $val['associate_attached'])
  113. {
  114. $action_list_article_comment_ids[] = $val['associate_attached'];
  115. }
  116. }
  117. else if (in_array($val['associate_action'], array(
  118. ACTION_LOG::ADD_LIKE_PROJECT,
  119. ACTION_LOG::ADD_SUPPORT_PROJECT
  120. )))
  121. {
  122. $action_list_project_ids[] = $val['associate_id'];
  123. }
  124. else
  125. {
  126. $action_list_question_ids[] = $val['associate_id'];
  127. if (in_array($val['associate_action'], array(
  128. ACTION_LOG::ANSWER_QUESTION,
  129. ACTION_LOG::ADD_AGREE
  130. )) AND $val['associate_attached'])
  131. {
  132. $action_list_answer_ids[] = $val['associate_attached'];
  133. }
  134. }
  135. if (! $action_list_uids[$val['uid']])
  136. {
  137. $action_list_uids[$val['uid']] = $val['uid'];
  138. }
  139. }
  140. if ($action_list_question_ids)
  141. {
  142. $question_infos = $this->model('question')->get_question_info_by_ids($action_list_question_ids);
  143. foreach ($question_infos as $key => $val) {
  144. $question_infos[$key]['question_detail'] = strip_ubb($val['question_detail']);
  145. }
  146. }
  147. if ($action_list_answer_ids)
  148. {
  149. $answer_infos = $this->model('answer')->get_answers_by_ids($action_list_answer_ids);
  150. foreach ($answer_infos as $key => $val) {
  151. $answer_infos[$key]['answer_content'] = strip_ubb($val['answer_content']);
  152. }
  153. $answer_attachs = $this->model('publish')->get_attachs('answer', $action_list_answer_ids, 'min');
  154. }
  155. if ($action_list_project_ids)
  156. {
  157. $project_infos = $this->model('project')->get_project_info_by_ids($action_list_project_ids);
  158. foreach ($project_infos as $key => $val) {
  159. $project_infos[$key]['description'] = strip_ubb($val['description']);
  160. }
  161. }
  162. if ($action_list_uids)
  163. {
  164. $user_info_lists = $this->model('account')->get_user_info_by_uids($action_list_uids, true);
  165. }
  166. if ($action_list_article_ids)
  167. {
  168. $article_infos = $this->model('article')->get_article_info_by_ids($action_list_article_ids);
  169. foreach ($article_infos as $key => $val) {
  170. $article_infos[$key]['message'] = strip_ubb($val['message']);
  171. }
  172. }
  173. if ($action_list_article_comment_ids)
  174. {
  175. $article_comment = $this->model('article')->get_comments_by_ids($action_list_article_comment_ids);
  176. }
  177. // 重组信息
  178. foreach ($action_list as $key => $val)
  179. {
  180. $action_list[$key]['user_info'] = $user_info_lists[$val['uid']];
  181. if ($user_focus_topics_info[$val['associate_id']] AND !in_array($action_list[$key]['uid'], $user_follow_uids) AND $action_list[$key]['uid'] != $uid)
  182. {
  183. $topic_info = end($user_focus_topics_info[$val['associate_id']]);
  184. }
  185. else
  186. {
  187. unset($topic_info);
  188. }
  189. switch ($val['associate_action'])
  190. {
  191. case ACTION_LOG::ADD_ARTICLE:
  192. case ACTION_LOG::ADD_AGREE_ARTICLE:
  193. case ACTION_LOG::ADD_COMMENT_ARTICLE:
  194. $article_info = $article_infos[$val['associate_id']];
  195. $action_list[$key]['title'] = $article_info['title'];
  196. $action_list[$key]['link'] = get_js_url('/article/' . $article_info['id']);
  197. $action_list[$key]['article_info'] = $article_info;
  198. if ($val['associate_action'] == ACTION_LOG::ADD_COMMENT_ARTICLE)
  199. {
  200. $action_list[$key]['comment_info'] = $article_comment[$val['associate_attached']];
  201. }
  202. break;
  203. case ACTION_LOG::ADD_LIKE_PROJECT:
  204. case ACTION_LOG::ADD_SUPPORT_PROJECT:
  205. $project_info = $project_infos[$val['associate_id']];
  206. $action_list[$key]['title'] = $project_info['title'];
  207. $action_list[$key]['link'] = get_js_url('/project/' . $project_info['id']);
  208. $action_list[$key]['project_info'] = $project_info;
  209. break;
  210. default:
  211. $question_info = $question_infos[$val['associate_id']];
  212. $action_list[$key]['title'] = $question_info['question_content'];
  213. $action_list[$key]['link'] = get_js_url('/question/' . $question_info['question_id']);
  214. // 是否关注
  215. if ($user_focus_questions_ids)
  216. {
  217. if (in_array($question_info['question_id'], $user_focus_questions_ids))
  218. {
  219. $question_info['has_focus'] = TRUE;
  220. }
  221. }
  222. // 对于回复问题的
  223. if ($answer_infos[$val['associate_attached']] AND in_array($val['associate_action'], array(
  224. ACTION_LOG::ANSWER_QUESTION,
  225. ACTION_LOG::ADD_AGREE
  226. )))
  227. {
  228. $action_list[$key]['answer_info'] = $answer_infos[$val['associate_attached']];
  229. }
  230. $action_list[$key]['question_info'] = $question_info;
  231. // 处理回复
  232. if ($action_list[$key]['answer_info']['answer_id'])
  233. {
  234. if ($action_list[$key]['answer_info']['anonymous'] AND $val['associate_action'] == ACTION_LOG::ANSWER_QUESTION)
  235. {
  236. unset($action_list[$key]);
  237. continue;
  238. }
  239. $final_list_answer_ids[] = $action_list[$key]['answer_info']['answer_id'];
  240. if ($action_list[$key]['answer_info']['has_attach'])
  241. {
  242. $action_list[$key]['answer_info']['attachs'] = $answer_attachs[$action_list[$key]['answer_info']['answer_id']];
  243. }
  244. }
  245. break;
  246. }
  247. if ($action_list[$key])
  248. {
  249. $action_list[$key]['last_action_str'] = ACTION_LOG::format_action_data($val['associate_action'], $val['uid'], $user_info_lists[$val['uid']]['user_name'], $question_info, $topic_info);
  250. }
  251. }
  252. if ($final_list_answer_ids)
  253. {
  254. $answer_agree_users = $this->model('answer')->get_vote_user_by_answer_ids($final_list_answer_ids);
  255. $answer_vote_status = $this->model('answer')->get_answer_vote_status($final_list_answer_ids, $uid);
  256. $answer_users_rated_thanks = $this->model('answer')->users_rated('thanks', $final_list_answer_ids, $uid);
  257. $answer_users_rated_uninterested = $this->model('answer')->users_rated('uninterested', $final_list_answer_ids, $uid);
  258. }
  259. foreach ($action_list as $key => $val)
  260. {
  261. if (isset($action_list[$key]['answer_info']['answer_id']))
  262. {
  263. $answer_id = $action_list[$key]['answer_info']['answer_id'];
  264. if (isset($answer_agree_users[$answer_id]))
  265. {
  266. $action_list[$key]['answer_info']['agree_users'] = $answer_agree_users[$answer_id];
  267. }
  268. if (isset($answer_vote_status[$answer_id]))
  269. {
  270. $action_list[$key]['answer_info']['agree_status'] = $answer_vote_status[$answer_id];
  271. }
  272. if (isset($answer_users_rated_thanks[$answer_id]))
  273. {
  274. $action_list[$key]['answer_info']['user_rated_thanks'] = $answer_users_rated_thanks[$answer_id];
  275. }
  276. if (isset($answer_users_rated_uninterested[$answer_id]))
  277. {
  278. $action_list[$key]['answer_info']['user_rated_uninterested'] = $answer_users_rated_uninterested[$answer_id];
  279. }
  280. }
  281. }
  282. return $action_list;
  283. }
  284. public function get_user_actions($uid, $limit = 10, $actions = false, $this_uid = 0)
  285. {
  286. $cache_key = 'user_actions_' . md5($uid . $limit . $actions . $this_uid);
  287. if ($user_actions = AWS_APP::cache()->get($cache_key))
  288. {
  289. return $user_actions;
  290. }
  291. $associate_action = ACTION_LOG::ADD_QUESTION;
  292. if (strstr($actions, ','))
  293. {
  294. $associate_action = explode(',', $actions);
  295. array_walk_recursive($associate_action, 'intval_string');
  296. $associate_action = implode(',', $associate_action);
  297. }
  298. else if ($actions)
  299. {
  300. $associate_action = intval($actions);
  301. }
  302. if (!$uid)
  303. {
  304. $where[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND associate_action IN(" . $this->quote($associate_action) . "))";
  305. }
  306. else
  307. {
  308. $where[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND uid = " . intval($uid) . " AND associate_action IN(" . $this->quote($associate_action) . "))";
  309. }
  310. if ($this_uid == $uid)
  311. {
  312. $show_anonymous = true;
  313. }
  314. $action_list = ACTION_LOG::get_action_by_where(implode($where, ' OR '), $limit, $show_anonymous);
  315. // 重组信息
  316. foreach ($action_list as $key => $val)
  317. {
  318. $uids[] = $val['uid'];
  319. switch ($val['associate_type'])
  320. {
  321. case ACTION_LOG::CATEGORY_QUESTION:
  322. if (in_array($val['associate_action'], array(
  323. ACTION_LOG::ADD_ARTICLE,
  324. ACTION_LOG::ADD_COMMENT_ARTICLE
  325. )))
  326. {
  327. $article_ids[] = $val['associate_id'];
  328. }
  329. else if (in_array($val['associate_action'], array(
  330. ACTION_LOG::ADD_LIKE_PROJECT,
  331. ACTION_LOG::ADD_SUPPORT_PROJECT
  332. )))
  333. {
  334. $action_list_project_ids[] = $val['associate_id'];
  335. }
  336. else
  337. {
  338. $question_ids[] = $val['associate_id'];
  339. }
  340. if (in_array($val['associate_action'], array(
  341. ACTION_LOG::ADD_TOPIC,
  342. ACTION_LOG::MOD_TOPIC,
  343. ACTION_LOG::MOD_TOPIC_DESCRI,
  344. ACTION_LOG::MOD_TOPIC_PIC,
  345. ACTION_LOG::DELETE_TOPIC,
  346. ACTION_LOG::ADD_TOPIC_FOCUS
  347. )) AND $val['associate_attached'])
  348. {
  349. $associate_topic_ids[] = $val['associate_attached'];
  350. }
  351. break;
  352. }
  353. }
  354. if ($uids)
  355. {
  356. $action_list_users = $this->model('account')->get_user_info_by_uids($uids, true);
  357. }
  358. if ($question_ids)
  359. {
  360. $action_questions_info = $this->model('question')->get_question_info_by_ids($question_ids);
  361. }
  362. if ($associate_topic_ids)
  363. {
  364. $associate_topics = $this->model('topic')->get_topics_by_ids($associate_topic_ids);
  365. }
  366. if ($article_ids)
  367. {
  368. $action_articles_info = $this->model('article')->get_article_info_by_ids($article_ids);
  369. }
  370. if ($action_list_project_ids)
  371. {
  372. $project_infos = $this->model('project')->get_project_info_by_ids($action_list_project_ids);
  373. }
  374. foreach ($action_list as $key => $val)
  375. {
  376. $action_list[$key]['user_info'] = $action_list_users[$val['uid']];
  377. switch ($val['associate_type'])
  378. {
  379. case ACTION_LOG::CATEGORY_QUESTION:
  380. switch ($val['associate_action'])
  381. {
  382. case ACTION_LOG::ADD_ARTICLE:
  383. case ACTION_LOG::ADD_COMMENT_ARTICLE:
  384. $article_info = $action_articles_info[$val['associate_id']];
  385. $action_list[$key]['title'] = $article_info['title'];
  386. $action_list[$key]['link'] = get_js_url('/article/' . $article_info['id']);
  387. $action_list[$key]['article_info'] = $article_info;
  388. $action_list[$key]['last_action_str'] = ACTION_LOG::format_action_data($val['associate_action'], $val['uid'], $action_list_users[$val['uid']]['user_name']);
  389. break;
  390. case ACTION_LOG::ADD_LIKE_PROJECT:
  391. case ACTION_LOG::ADD_SUPPORT_PROJECT:
  392. $project_info = $project_infos[$val['associate_id']];
  393. $action_list[$key]['title'] = $project_info['title'];
  394. $action_list[$key]['link'] = get_js_url('/project/' . $project_info['id']);
  395. $action_list[$key]['project_info'] = $project_info;
  396. $action_list[$key]['last_action_str'] = ACTION_LOG::format_action_data($val['associate_action'], $val['uid'], $action_list_users[$val['uid']]['user_name']);
  397. break;
  398. default:
  399. $question_info = $action_questions_info[$val['associate_id']];
  400. $action_list[$key]['title'] = $question_info['question_content'];
  401. $action_list[$key]['link'] = get_js_url('/question/' . $question_info['question_id']);
  402. if (in_array($val['associate_action'], array(
  403. ACTION_LOG::ADD_TOPIC,
  404. ACTION_LOG::MOD_TOPIC,
  405. ACTION_LOG::MOD_TOPIC_DESCRI,
  406. ACTION_LOG::MOD_TOPIC_PIC,
  407. ACTION_LOG::DELETE_TOPIC,
  408. ACTION_LOG::ADD_TOPIC_FOCUS
  409. )) AND $val['associate_attached'])
  410. {
  411. $topic_info = $associate_topics[$val['associate_attached']];
  412. }
  413. else
  414. {
  415. unset($topic_info);
  416. }
  417. if (in_array($val['associate_action'], array(
  418. ACTION_LOG::ADD_QUESTION
  419. )) AND $question_info['has_attach'])
  420. {
  421. $question_info['attachs'] = $question_attachs[$question_info['question_id']];
  422. }
  423. if ($val['uid'])
  424. {
  425. $action_list[$key]['last_action_str'] = ACTION_LOG::format_action_data($val['associate_action'], $val['uid'], $action_list_users[$val['uid']]['user_name'], $question_info, $topic_info);
  426. }
  427. if (in_array($val['associate_action'], array(
  428. ACTION_LOG::ANSWER_QUESTION
  429. )) AND $question_info['answer_count'])
  430. {
  431. if ($answer_list = $this->model('answer')->get_answer_by_id($val['associate_attached']))
  432. {
  433. $action_list[$key]['answer_info'] = $answer_list;
  434. }
  435. }
  436. $action_list[$key]['question_info'] = $question_info;
  437. break;
  438. }
  439. break;
  440. }
  441. }
  442. AWS_APP::cache()->set($cache_key, $action_list, get_setting('cache_level_normal'));
  443. return $action_list;
  444. }
  445. }