/includes/functions_thanks.php

https://github.com/Vexilurz/phpbb_forum · PHP · 487 lines · 424 code · 37 blank · 26 comment · 49 complexity · 4c50eff762588e65fe0c3e6190a5eada MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id: functions_thanks.php,v 123 2009-06-11 10:02:51 Палыч$
  6. * @copyright (c) 2008 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. $user->add_lang('mods/thanks_mod');
  18. // Output thanks list
  19. function get_thanks($post_id)
  20. {
  21. global $thankers;
  22. $return = '';
  23. $user_list = array();
  24. foreach($thankers as $key => $value)
  25. {
  26. if ($thankers[$key]['post_id'] == $post_id)
  27. {
  28. $user_list[$thankers[$key]['username_clean']] = array(
  29. 'username' => $thankers[$key]['username'],
  30. 'user_id' => $thankers[$key]['user_id'],
  31. 'user_colour' => $thankers[$key]['user_colour'],
  32. );
  33. }
  34. }
  35. ksort($user_list);
  36. $comma = '';
  37. foreach($user_list as $key => $value)
  38. {
  39. $return .= $comma;
  40. $return .= get_username_string('full', $value['user_id'], $value['username'], $value['user_colour']);
  41. $comma = ', ';
  42. }
  43. $return = ($return == '') ? false : $return;
  44. return $return;
  45. }
  46. //get thanks number
  47. function get_thanks_number($post_id)
  48. {
  49. global $thankers;
  50. $i = 0;
  51. foreach($thankers as $key => $value)
  52. {
  53. if ($thankers[$key]['post_id'] == $post_id)
  54. {
  55. $i++;
  56. }
  57. }
  58. return $i;
  59. }
  60. // add a user to the thanks list
  61. function insert_thanks($post_id, $user_id)
  62. {
  63. global $db, $user, $thanked, $phpbb_root_path, $phpEx, $forum_id;
  64. $to_id = request_var('to_id', 0);
  65. $sql_array = array(
  66. 'SELECT' => 'p.post_id, p.poster_id',
  67. 'FROM' => array (POSTS_TABLE => 'p'),
  68. 'WHERE' => "p.post_id = $post_id");
  69. $sql = $db->sql_build_query('SELECT', $sql_array);
  70. $result = $db->sql_query($sql);
  71. $row = $db->sql_fetchrow($result);
  72. if ($user->data['user_type'] != USER_IGNORE && !empty($to_id))
  73. {
  74. if ($row['poster_id'] != $user_id && $row['poster_id'] == $to_id && !$thanked)
  75. {
  76. $sql = 'INSERT INTO ' . THANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  77. 'user_id' => $user_id,
  78. 'post_id' => $post_id,
  79. 'poster_id' => $to_id
  80. ));
  81. $db->sql_query($sql);
  82. $lang_act = 'GIVE';
  83. send_thanks_pm($user_id, $to_id, $send_pm = true, $post_id, $lang_act);
  84. meta_refresh (1, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id"));
  85. trigger_error($user->lang['THANKS_INFO_'.$lang_act] . '<br /><br /><a href="'.append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id").'">'.$user->lang['RETURN_POST'].'</a>');
  86. }
  87. else
  88. {
  89. trigger_error($user->lang['INCORRECT_THANKS'] . '<br /><br /><a href="'.append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id").'">'.$user->lang['RETURN_POST'].'</a>');
  90. }
  91. }
  92. return;
  93. }
  94. // remove a user's thanks
  95. function delete_thanks($post_id, $user_id)
  96. {
  97. global $db, $user, $phpbb_root_path, $phpEx, $forum_id;
  98. $to_id = request_var('to_id', 0);
  99. // confirm
  100. $s_hidden_fields = build_hidden_fields(array(
  101. 'to_id' => $to_id,
  102. 'rthanks' => $post_id,
  103. 'p' => $post_id,
  104. )
  105. );
  106. if (confirm_box(true))
  107. {
  108. if ($user->data['user_type'] != USER_IGNORE && !empty($to_id))
  109. {
  110. $sql = "DELETE FROM " . THANKS_TABLE . "
  111. WHERE post_id = $post_id AND user_id = " . $user->data['user_id'];
  112. $db->sql_query($sql);
  113. $result = $db->sql_affectedrows($sql);
  114. if ($result != 0)
  115. {
  116. $lang_act = 'REMOVE';
  117. send_thanks_pm($user_id, $to_id, $send_pm = true, $post_id, $lang_act);
  118. meta_refresh (1, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id"));
  119. trigger_error($user->lang['THANKS_INFO_'.$lang_act].'<br /><br /><a href="'.append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id").'">'.$user->lang['RETURN_POST'].'</a>');
  120. }
  121. else
  122. {
  123. trigger_error($user->lang['INCORRECT_THANKS'] . '<br /><br /><a href="'.append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id") .'">'.$user->lang['RETURN_POST'].'</a>');
  124. }
  125. }
  126. }
  127. else
  128. {
  129. confirm_box(false, 'REMOVE_THANKS', $s_hidden_fields);
  130. meta_refresh (0,append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;p=$post_id#p$post_id"));
  131. }
  132. return;
  133. }
  134. // display the text/image saying either to add or remove thanks
  135. function get_thanks_text($post_id)
  136. {
  137. global $db, $user, $postrow;
  138. if (already_thanked($post_id, $user->data['user_id']))
  139. {
  140. $postrow = array_merge($postrow, array(
  141. 'THANK_ALT' => $user->lang['REMOVE_THANKS'],
  142. 'THANKS_IMG' => 'removethanks-icon',
  143. ));
  144. return;
  145. }
  146. $postrow = array_merge($postrow, array(
  147. 'THANK_ALT' => $user->lang['THANK_POST'],
  148. 'THANKS_IMG' => 'thanks-icon',
  149. ));
  150. return;
  151. }
  152. // change the variable sent via the link to avoid odd errors
  153. function get_thanks_link($post_id)
  154. {
  155. global $db, $user;
  156. if (already_thanked($post_id, $user->data['user_id']))
  157. {
  158. return 'rthanks';
  159. }
  160. return 'thanks';
  161. }
  162. // check if the user has already thanked that post
  163. function already_thanked($post_id, $user_id)
  164. {
  165. global $db, $thankers;
  166. $thanked = false;
  167. foreach((array)$thankers as $key => $value)
  168. {
  169. if ($thankers[$key]['post_id'] == $post_id && $thankers[$key]['user_id'] == $user_id)
  170. {
  171. $thanked = true;
  172. }
  173. }
  174. return $thanked;
  175. }
  176. // gets the number of users that have thanked the poster
  177. function get_user_count($poster_id, $receive)
  178. {
  179. global $thankss, $thankeds;
  180. if ($receive)
  181. {
  182. $count = count(array_keys ($thankeds, $poster_id));
  183. return $count;
  184. }
  185. else
  186. {
  187. $count = count(array_keys ($thankss, $poster_id));
  188. return $count;
  189. }
  190. }
  191. // stuff goes here to avoid over-editing memberlist.php
  192. function output_thanks_memberlist($user_id)
  193. {
  194. global $db, $user, $row, $phpEx, $template, $phpbb_root_path, $config;
  195. $thankers_member = array();
  196. $thankered_member = array();
  197. $thanks = '';
  198. $thanked = '';
  199. $poster_receive_count = 0;
  200. $poster_give_count = 0;
  201. $poster_limit = $config['thanks_number'];
  202. $sql_array = array(
  203. 'SELECT' => 't.*, u.username, u.user_colour',
  204. 'FROM' => array(THANKS_TABLE => 't', USERS_TABLE => 'u'),
  205. );
  206. $sql_array['WHERE'] = "t.poster_id = $user_id AND ";
  207. $sql_array['WHERE'] .= "u.user_id = t.user_id";
  208. $sql = $db->sql_build_query('SELECT', $sql_array);
  209. $result = $db->sql_query($sql);
  210. while ($row = $db->sql_fetchrow($result))
  211. {
  212. $thankers_member[$poster_receive_count] = array(
  213. 'user_id' => $row['user_id'],
  214. 'poster_id' => $row['poster_id'],
  215. 'post_id' => $row['post_id'],
  216. 'username' => $row['username'],
  217. 'user_colour' => $row['user_colour'],
  218. );
  219. $poster_receive_count++;
  220. }
  221. $user_list = array();
  222. $post_list = array ();
  223. $i=0;
  224. foreach($thankers_member as $key => $value)
  225. {
  226. if ($thankers_member[$key]['poster_id'] == $user_id)
  227. {
  228. $i++;
  229. $user_list[$i] = array(
  230. 'username' => $thankers_member[$key]['username'],
  231. 'user_id' => $thankers_member[$key]['user_id'],
  232. 'user_colour' => $thankers_member[$key]['user_colour'],
  233. 'post_id' => $thankers_member[$key]['post_id'],
  234. );
  235. }
  236. }
  237. unset ($value);
  238. ksort($user_list);
  239. $i = 0;
  240. foreach($user_list as $value)
  241. {
  242. if ($i > 0 and $i <= $poster_limit)
  243. {
  244. $thanked .= ', ';
  245. }
  246. $i++;
  247. if ($i <= $poster_limit)
  248. {
  249. $thanked .= get_username_string('full', $value['user_id'], $value['username'], $value['user_colour']) . ' &#8594; <a href="'. append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $value['post_id']. '#p' . $value['post_id']) . '">' . $user->lang['FOR_MESSAGE'] . '</a>';
  250. }
  251. }
  252. unset ($value);
  253. $sql_array = array(
  254. 'SELECT' => 't.*, u.username, u.user_colour',
  255. 'FROM' => array(THANKS_TABLE => 't', USERS_TABLE => 'u'),
  256. );
  257. $sql_array['WHERE'] = "t.user_id = $user_id AND ";
  258. $sql_array['WHERE'] .= "u.user_id = t.poster_id";
  259. $sql = $db->sql_build_query('SELECT', $sql_array);
  260. $result = $db->sql_query($sql);
  261. while ($row = $db->sql_fetchrow($result))
  262. {
  263. $thankered_member[$poster_give_count] = array(
  264. 'user_id' => $row['user_id'],
  265. 'poster_id' => $row['poster_id'],
  266. 'post_id' => $row['post_id'],
  267. 'username' => $row['username'],
  268. 'user_colour' => $row['user_colour'],
  269. );
  270. $poster_give_count++;
  271. }
  272. $db->sql_freeresult($result);
  273. $i=0;
  274. foreach($thankered_member as $key => $value)
  275. {
  276. if ($thankered_member[$key]['user_id'] == $user_id)
  277. {
  278. $i++;
  279. $post_list[$i] = array(
  280. 'postername' => $thankered_member[$key]['username'],
  281. 'poster_id' => $thankered_member[$key]['poster_id'],
  282. 'poster_colour' => $thankered_member[$key]['user_colour'],
  283. 'post_id' => $thankered_member[$key]['post_id'],
  284. );
  285. }
  286. }
  287. unset ($value);
  288. ksort($user_list);
  289. $i = 0;
  290. foreach($post_list as $value)
  291. {
  292. if ($i > 0 and $i <= $poster_limit)
  293. {
  294. $thanks .= ', ';
  295. }
  296. $i++;
  297. if ($i <= $poster_limit)
  298. {
  299. $thanks .= get_username_string('full', $value['poster_id'], $value['postername'], $value['poster_colour']) . ' &#8592; <a href="'. append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $value['post_id']. '#p' . $value['post_id']) . '">' . $user->lang['FOR_MESSAGE'] . '</a>';
  300. }
  301. }
  302. unset ($value);
  303. $template->assign_vars(array(
  304. 'POSTER_RECEIVE_COUNT' => $poster_receive_count,
  305. 'THANKS' => $thanks,
  306. 'POSTER_GIVE_COUNT' => $poster_give_count,
  307. 'THANKED' => $thanked,
  308. 'THANKS_PROFILELIST_VIEW' => $config['thanks_profilelist_view'],
  309. ));
  310. }
  311. // stuff goes here to avoid over-editing viewtopic.php
  312. function output_thanks($user_id)
  313. {
  314. global $db, $user, $poster_id, $postrow, $row, $phpEx, $topic_data, $phpbb_root_path, $config, $forum_id;
  315. if (!empty($postrow))
  316. {
  317. // $forum_id = (isset($forum_id)) ? $forum_id : 0;
  318. get_thanks_text($row['post_id']);
  319. $postrow = array_merge($postrow, array(
  320. 'THANKS' => get_thanks($row['post_id']),
  321. 'THANKS_LINK' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$row['post_id']}" . (($topic_data['topic_type'] == POST_GLOBAL) ? "&amp;f=$forum_id" : '') . "&amp;" . get_thanks_link($row['post_id']) . "={$row['post_id']}&amp;to_id=$poster_id"),
  322. 'THANK_TEXT' => $user->lang['THANK_TEXT_1'],
  323. 'THANK_TEXT_2' => (get_thanks_number($row['post_id']) != 1) ? sprintf($user->lang['THANK_TEXT_2pl'], get_thanks_number($row['post_id'])) : $user->lang['THANK_TEXT_2'],
  324. 'THANKS_FROM' => $user->lang['THANK_FROM'],
  325. 'POSTER_RECEIVE_COUNT' => get_user_count($poster_id, true),
  326. 'POSTER_GIVE_COUNT' => get_user_count($poster_id, false),
  327. 'S_IS_OWN_POST' => ($user->data['user_id'] == $poster_id) ? true : false,
  328. 'S_POST_ANONYMOUS' => ($poster_id == ANONYMOUS) ? true : false,
  329. 'THANK_IMG' => (already_thanked($row['post_id'], $user->data['user_id'])) ? $user->img('removethanks', $user->lang['REMOVE_THANKS']. get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : $user->img('thankposts', $user->lang['THANK_POST']. get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])),
  330. 'THANKS_POSTLIST_VIEW' => $config['thanks_postlist_view'],
  331. 'S_ALREADY_THANKED' => already_thanked($row['post_id'], $user->data['user_id']),
  332. 'S_REMOVE_THANKS' => $config['remove_thanks'],
  333. ));
  334. }
  335. }
  336. //refresh counts if post delete
  337. function delete_post_thanks($post_id)
  338. {
  339. global $db;
  340. $sql = 'DELETE
  341. FROM ' . THANKS_TABLE . "
  342. WHERE post_id = $post_id";
  343. $db->sql_query($sql);
  344. }
  345. //send pm
  346. function send_thanks_pm($user_id, $to_id, $send_pm = true, $post_id = 0, $lang_act)
  347. {
  348. global $phpEx, $phpbb_root_path, $config, $row, $forum_id, $user, $user_cache;
  349. if (!$user_cache[$to_id]['allow_thanks_pm'])
  350. {
  351. return;
  352. }
  353. include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
  354. include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
  355. $user->data['user_lang'] = (file_exists($phpbb_root_path . 'language/' . $user->data['user_lang'] . "/mods/thanks_mod.$phpEx")) ? $user->data['user_lang'] : $config['default_lang'];
  356. include($phpbb_root_path . 'language/' . basename($user->data['user_lang']) . "/mods/thanks_mod.$phpEx");
  357. $massage = '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $post_id .'#p' . $post_id) .'">'. $user->lang['THANKS_PM_MES_'. $lang_act] .'</a>';
  358. $message_parser = new parse_message();
  359. $message_parser->message = sprintf($massage);
  360. $message_parser->parse(true, true, true, false, false, true, true);
  361. $pm_data = array(
  362. 'from_user_id' => $user->data['user_id'],
  363. 'from_user_ip' => $user->ip,
  364. 'from_username' => $user->data['username'],
  365. 'enable_sig' => false,
  366. 'enable_bbcode' => true,
  367. 'enable_smilies' => true,
  368. 'enable_urls' => false,
  369. 'icon_id' => 0,
  370. 'bbcode_bitfield' => $message_parser->bbcode_bitfield,
  371. 'bbcode_uid' => $message_parser->bbcode_uid,
  372. 'message' => $message_parser->message,
  373. 'address_list' => array('u' => array($to_id => 'to')),
  374. );
  375. submit_pm('post', $user->lang['THANKS_PM_SUBJECT_'.$lang_act], $pm_data, false);
  376. return;
  377. }
  378. // create an array of all thanks info
  379. function array_all_thanks()
  380. {
  381. global $db, $post_list, $thankers, $thankss, $thankeds;
  382. $thankers = array();
  383. $thankss = array();
  384. $thankeds = array();
  385. $sql_array = array(
  386. 'SELECT' => 't.*, u.username, u.username_clean, u.user_colour',
  387. 'FROM' => array(THANKS_TABLE => 't', USERS_TABLE => 'u'),
  388. );
  389. $sql_array['WHERE'] = "u.user_id = t.user_id AND (";
  390. $sql_array['WHERE'] .= "t.post_id = $post_list[0]";
  391. for ($i = 1, $end = sizeof($post_list); $i < $end; ++$i)
  392. {
  393. $sql_array['WHERE'] .= " OR t.post_id = $post_list[$i]";
  394. }
  395. $sql_array['WHERE'] .= ')';
  396. $sql = $db->sql_build_query('SELECT', $sql_array);
  397. $result = $db->sql_query($sql);
  398. $j = 0;
  399. while ($row = $db->sql_fetchrow($result))
  400. {
  401. $thankers[$j] = array(
  402. 'user_id' => $row['user_id'],
  403. 'poster_id' => $row['poster_id'],
  404. 'post_id' => $row['post_id'],
  405. 'username' => $row['username'],
  406. 'username_clean' => $row['username_clean'],
  407. 'user_colour' => $row['user_colour'],
  408. );
  409. $j++;
  410. }
  411. $db->sql_freeresult($result);
  412. $sql_array = array(
  413. 'SELECT' => 't.poster_id, t.post_id, t.user_id',
  414. 'FROM' => array(THANKS_TABLE => 't', POSTS_TABLE => 'p'),
  415. );
  416. $sql_array['WHERE'] = "t.poster_id = p.poster_id AND (";
  417. $sql_array['WHERE'] .= "p.post_id = $post_list[0]";
  418. for ($i = 1, $end = sizeof($post_list); $i < $end; ++$i)
  419. {
  420. $sql_array['WHERE'] .= " OR p.post_id = $post_list[$i]";
  421. }
  422. $sql_array['WHERE'] .= ')';
  423. $sql = $db->sql_build_query('SELECT_DISTINCT', $sql_array);
  424. $result = $db->sql_query($sql);
  425. while ($row = $db->sql_fetchrow($result))
  426. {
  427. $thankeds[] = $row['poster_id'];
  428. }
  429. $db->sql_freeresult($result);
  430. $sql_array = array(
  431. 'SELECT' => 't.user_id, t.post_id, t.poster_id',
  432. 'FROM' => array(THANKS_TABLE => 't', POSTS_TABLE => 'p'),
  433. );
  434. $sql_array['WHERE'] = "p.poster_id = t.user_id AND (";
  435. $sql_array['WHERE'] .= "p.post_id = $post_list[0]";
  436. for ($i = 1, $end = sizeof($post_list); $i < $end; ++$i)
  437. {
  438. $sql_array['WHERE'] .= " OR p.post_id = $post_list[$i]";
  439. }
  440. $sql_array['WHERE'] .= ')';
  441. $sql = $db->sql_build_query('SELECT_DISTINCT', $sql_array);
  442. $result = $db->sql_query($sql);
  443. while ($row = $db->sql_fetchrow($result))
  444. {
  445. $thankss[] = $row['user_id'];
  446. }
  447. $db->sql_freeresult($result);
  448. return;
  449. }
  450. ?>