PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/subs/Boards.subs.php

https://github.com/Arantor/Elkarte
PHP | 1027 lines | 726 code | 117 blank | 184 comment | 88 complexity | d9a72fa02c1e7e78a929c2383197d1b5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file is mainly concerned with minor tasks relating to boards, such as
  16. * marking them read, collapsing categories, or quick moderation.
  17. *
  18. */
  19. if (!defined('ELKARTE'))
  20. die('No access...');
  21. /**
  22. * Mark a board or multiple boards read.
  23. *
  24. * @param array $boards
  25. * @param bool $unread
  26. */
  27. function markBoardsRead($boards, $unread = false)
  28. {
  29. global $user_info, $modSettings, $smcFunc;
  30. // Force $boards to be an array.
  31. if (!is_array($boards))
  32. $boards = array($boards);
  33. else
  34. $boards = array_unique($boards);
  35. // No boards, nothing to mark as read.
  36. if (empty($boards))
  37. return;
  38. // Allow the user to mark a board as unread.
  39. if ($unread)
  40. {
  41. // Clear out all the places where this lovely info is stored.
  42. // @todo Maybe not log_mark_read?
  43. $smcFunc['db_query']('', '
  44. DELETE FROM {db_prefix}log_mark_read
  45. WHERE id_board IN ({array_int:board_list})
  46. AND id_member = {int:current_member}',
  47. array(
  48. 'current_member' => $user_info['id'],
  49. 'board_list' => $boards,
  50. )
  51. );
  52. $smcFunc['db_query']('', '
  53. DELETE FROM {db_prefix}log_boards
  54. WHERE id_board IN ({array_int:board_list})
  55. AND id_member = {int:current_member}',
  56. array(
  57. 'current_member' => $user_info['id'],
  58. 'board_list' => $boards,
  59. )
  60. );
  61. }
  62. // Otherwise mark the board as read.
  63. else
  64. {
  65. $markRead = array();
  66. foreach ($boards as $board)
  67. $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $board);
  68. // Update log_mark_read and log_boards.
  69. $smcFunc['db_insert']('replace',
  70. '{db_prefix}log_mark_read',
  71. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  72. $markRead,
  73. array('id_board', 'id_member')
  74. );
  75. $smcFunc['db_insert']('replace',
  76. '{db_prefix}log_boards',
  77. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  78. $markRead,
  79. array('id_board', 'id_member')
  80. );
  81. }
  82. // Get rid of useless log_topics data, because log_mark_read is better for it - even if marking unread - I think so...
  83. // @todo look at this...
  84. // The call to markBoardsRead() in Display() used to be simply
  85. // marking log_boards (the previous query only)
  86. $result = $smcFunc['db_query']('', '
  87. SELECT MIN(id_topic)
  88. FROM {db_prefix}log_topics
  89. WHERE id_member = {int:current_member}',
  90. array(
  91. 'current_member' => $user_info['id'],
  92. )
  93. );
  94. list ($lowest_topic) = $smcFunc['db_fetch_row']($result);
  95. $smcFunc['db_free_result']($result);
  96. if (empty($lowest_topic))
  97. return;
  98. // @todo SLOW This query seems to eat it sometimes.
  99. $result = $smcFunc['db_query']('', '
  100. SELECT lt.id_topic
  101. FROM {db_prefix}log_topics AS lt
  102. INNER JOIN {db_prefix}topics AS t /*!40000 USE INDEX (PRIMARY) */ ON (t.id_topic = lt.id_topic
  103. AND t.id_board IN ({array_int:board_list}))
  104. WHERE lt.id_member = {int:current_member}
  105. AND lt.id_topic >= {int:lowest_topic}
  106. AND lt.disregarded != 1',
  107. array(
  108. 'current_member' => $user_info['id'],
  109. 'board_list' => $boards,
  110. 'lowest_topic' => $lowest_topic,
  111. )
  112. );
  113. $topics = array();
  114. while ($row = $smcFunc['db_fetch_assoc']($result))
  115. $topics[] = $row['id_topic'];
  116. $smcFunc['db_free_result']($result);
  117. if (!empty($topics))
  118. $smcFunc['db_query']('', '
  119. DELETE FROM {db_prefix}log_topics
  120. WHERE id_member = {int:current_member}
  121. AND id_topic IN ({array_int:topic_list})',
  122. array(
  123. 'current_member' => $user_info['id'],
  124. 'topic_list' => $topics,
  125. )
  126. );
  127. }
  128. /**
  129. * Get the id_member associated with the specified message.
  130. * @param int $messageID
  131. * @return int the member id
  132. */
  133. function getMsgMemberID($messageID)
  134. {
  135. global $smcFunc;
  136. // Find the topic and make sure the member still exists.
  137. $result = $smcFunc['db_query']('', '
  138. SELECT IFNULL(mem.id_member, 0)
  139. FROM {db_prefix}messages AS m
  140. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  141. WHERE m.id_msg = {int:selected_message}
  142. LIMIT 1',
  143. array(
  144. 'selected_message' => (int) $messageID,
  145. )
  146. );
  147. if ($smcFunc['db_num_rows']($result) > 0)
  148. list ($memberID) = $smcFunc['db_fetch_row']($result);
  149. // The message doesn't even exist.
  150. else
  151. $memberID = 0;
  152. $smcFunc['db_free_result']($result);
  153. return (int) $memberID;
  154. }
  155. /**
  156. * Modify the settings and position of a board.
  157. * Used by ManageBoards.php to change the settings of a board.
  158. *
  159. * @param int $board_id
  160. * @param array &$boardOptions
  161. */
  162. function modifyBoard($board_id, &$boardOptions)
  163. {
  164. global $cat_tree, $boards, $boardList, $modSettings, $smcFunc;
  165. // Get some basic information about all boards and categories.
  166. getBoardTree();
  167. // Make sure given boards and categories exist.
  168. if (!isset($boards[$board_id]) || (isset($boardOptions['target_board']) && !isset($boards[$boardOptions['target_board']])) || (isset($boardOptions['target_category']) && !isset($cat_tree[$boardOptions['target_category']])))
  169. fatal_lang_error('no_board');
  170. $id = $board_id;
  171. call_integration_hook('integrate_pre_modify_board', array($id, $boardOptions));
  172. // All things that will be updated in the database will be in $boardUpdates.
  173. $boardUpdates = array();
  174. $boardUpdateParameters = array();
  175. // In case the board has to be moved
  176. if (isset($boardOptions['move_to']))
  177. {
  178. // Move the board to the top of a given category.
  179. if ($boardOptions['move_to'] == 'top')
  180. {
  181. $id_cat = $boardOptions['target_category'];
  182. $child_level = 0;
  183. $id_parent = 0;
  184. $after = $cat_tree[$id_cat]['last_board_order'];
  185. }
  186. // Move the board to the bottom of a given category.
  187. elseif ($boardOptions['move_to'] == 'bottom')
  188. {
  189. $id_cat = $boardOptions['target_category'];
  190. $child_level = 0;
  191. $id_parent = 0;
  192. $after = 0;
  193. foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy)
  194. $after = max($after, $boards[$id_board]['order']);
  195. }
  196. // Make the board a child of a given board.
  197. elseif ($boardOptions['move_to'] == 'child')
  198. {
  199. $id_cat = $boards[$boardOptions['target_board']]['category'];
  200. $child_level = $boards[$boardOptions['target_board']]['level'] + 1;
  201. $id_parent = $boardOptions['target_board'];
  202. // People can be creative, in many ways...
  203. if (isChildOf($id_parent, $board_id))
  204. fatal_lang_error('mboards_parent_own_child_error', false);
  205. elseif ($id_parent == $board_id)
  206. fatal_lang_error('mboards_board_own_child_error', false);
  207. $after = $boards[$boardOptions['target_board']]['order'];
  208. // Check if there are already children and (if so) get the max board order.
  209. if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child']))
  210. foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy)
  211. $after = max($after, $boards[$childBoard_id]['order']);
  212. }
  213. // Place a board before or after another board, on the same child level.
  214. elseif (in_array($boardOptions['move_to'], array('before', 'after')))
  215. {
  216. $id_cat = $boards[$boardOptions['target_board']]['category'];
  217. $child_level = $boards[$boardOptions['target_board']]['level'];
  218. $id_parent = $boards[$boardOptions['target_board']]['parent'];
  219. $after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0);
  220. }
  221. // Oops...?
  222. else
  223. trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR);
  224. // Get a list of children of this board.
  225. $childList = array();
  226. recursiveBoards($childList, $boards[$board_id]['tree']);
  227. // See if there are changes that affect children.
  228. $childUpdates = array();
  229. $levelDiff = $child_level - $boards[$board_id]['level'];
  230. if ($levelDiff != 0)
  231. $childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
  232. if ($id_cat != $boards[$board_id]['category'])
  233. $childUpdates[] = 'id_cat = {int:category}';
  234. // Fix the children of this board.
  235. if (!empty($childList) && !empty($childUpdates))
  236. $smcFunc['db_query']('', '
  237. UPDATE {db_prefix}boards
  238. SET ' . implode(',
  239. ', $childUpdates) . '
  240. WHERE id_board IN ({array_int:board_list})',
  241. array(
  242. 'board_list' => $childList,
  243. 'category' => $id_cat,
  244. 'level_diff' => $levelDiff,
  245. )
  246. );
  247. // Make some room for this spot.
  248. $smcFunc['db_query']('', '
  249. UPDATE {db_prefix}boards
  250. SET board_order = board_order + {int:new_order}
  251. WHERE board_order > {int:insert_after}
  252. AND id_board != {int:selected_board}',
  253. array(
  254. 'insert_after' => $after,
  255. 'selected_board' => $board_id,
  256. 'new_order' => 1 + count($childList),
  257. )
  258. );
  259. $boardUpdates[] = 'id_cat = {int:id_cat}';
  260. $boardUpdates[] = 'id_parent = {int:id_parent}';
  261. $boardUpdates[] = 'child_level = {int:child_level}';
  262. $boardUpdates[] = 'board_order = {int:board_order}';
  263. $boardUpdateParameters += array(
  264. 'id_cat' => $id_cat,
  265. 'id_parent' => $id_parent,
  266. 'child_level' => $child_level,
  267. 'board_order' => $after + 1,
  268. );
  269. }
  270. // This setting is a little twisted in the database...
  271. if (isset($boardOptions['posts_count']))
  272. {
  273. $boardUpdates[] = 'count_posts = {int:count_posts}';
  274. $boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
  275. }
  276. // Set the theme for this board.
  277. if (isset($boardOptions['board_theme']))
  278. {
  279. $boardUpdates[] = 'id_theme = {int:id_theme}';
  280. $boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
  281. }
  282. // Should the board theme override the user preferred theme?
  283. if (isset($boardOptions['override_theme']))
  284. {
  285. $boardUpdates[] = 'override_theme = {int:override_theme}';
  286. $boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
  287. }
  288. // Who's allowed to access this board.
  289. if (isset($boardOptions['access_groups']))
  290. {
  291. $boardUpdates[] = 'member_groups = {string:member_groups}';
  292. $boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
  293. }
  294. // And who isn't.
  295. if (isset($boardOptions['deny_groups']))
  296. {
  297. $boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
  298. $boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
  299. }
  300. if (isset($boardOptions['board_name']))
  301. {
  302. $boardUpdates[] = 'name = {string:board_name}';
  303. $boardUpdateParameters['board_name'] = $boardOptions['board_name'];
  304. }
  305. if (isset($boardOptions['board_description']))
  306. {
  307. $boardUpdates[] = 'description = {string:board_description}';
  308. $boardUpdateParameters['board_description'] = $boardOptions['board_description'];
  309. }
  310. if (isset($boardOptions['profile']))
  311. {
  312. $boardUpdates[] = 'id_profile = {int:profile}';
  313. $boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
  314. }
  315. if (isset($boardOptions['redirect']))
  316. {
  317. $boardUpdates[] = 'redirect = {string:redirect}';
  318. $boardUpdateParameters['redirect'] = $boardOptions['redirect'];
  319. }
  320. if (isset($boardOptions['num_posts']))
  321. {
  322. $boardUpdates[] = 'num_posts = {int:num_posts}';
  323. $boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
  324. }
  325. $id = $board_id;
  326. call_integration_hook('integrate_modify_board', array($id, $boardUpdates, $boardUpdateParameters));
  327. // Do the updates (if any).
  328. if (!empty($boardUpdates))
  329. $request = $smcFunc['db_query']('', '
  330. UPDATE {db_prefix}boards
  331. SET
  332. ' . implode(',
  333. ', $boardUpdates) . '
  334. WHERE id_board = {int:selected_board}',
  335. array_merge($boardUpdateParameters, array(
  336. 'selected_board' => $board_id,
  337. ))
  338. );
  339. // Set moderators of this board.
  340. if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']))
  341. {
  342. // Reset current moderators for this board - if there are any!
  343. $smcFunc['db_query']('', '
  344. DELETE FROM {db_prefix}moderators
  345. WHERE id_board = {int:board_list}',
  346. array(
  347. 'board_list' => $board_id,
  348. )
  349. );
  350. // Validate and get the IDs of the new moderators.
  351. if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
  352. {
  353. // Divvy out the usernames, remove extra space.
  354. $moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('&quot;' => '"'));
  355. preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
  356. $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
  357. for ($k = 0, $n = count($moderators); $k < $n; $k++)
  358. {
  359. $moderators[$k] = trim($moderators[$k]);
  360. if (strlen($moderators[$k]) == 0)
  361. unset($moderators[$k]);
  362. }
  363. // Find all the id_member's for the member_name's in the list.
  364. if (empty($boardOptions['moderators']))
  365. $boardOptions['moderators'] = array();
  366. if (!empty($moderators))
  367. {
  368. $request = $smcFunc['db_query']('', '
  369. SELECT id_member
  370. FROM {db_prefix}members
  371. WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
  372. LIMIT ' . count($moderators),
  373. array(
  374. 'moderator_list' => $moderators,
  375. )
  376. );
  377. while ($row = $smcFunc['db_fetch_assoc']($request))
  378. $boardOptions['moderators'][] = $row['id_member'];
  379. $smcFunc['db_free_result']($request);
  380. }
  381. }
  382. // Add the moderators to the board.
  383. if (!empty($boardOptions['moderators']))
  384. {
  385. $inserts = array();
  386. foreach ($boardOptions['moderators'] as $moderator)
  387. $inserts[] = array($board_id, $moderator);
  388. $smcFunc['db_insert']('insert',
  389. '{db_prefix}moderators',
  390. array('id_board' => 'int', 'id_member' => 'int'),
  391. $inserts,
  392. array('id_board', 'id_member')
  393. );
  394. }
  395. // Note that caches can now be wrong!
  396. updateSettings(array('settings_updated' => time()));
  397. }
  398. if (isset($boardOptions['move_to']))
  399. reorderBoards();
  400. clean_cache('data');
  401. if (empty($boardOptions['dont_log']))
  402. logAction('edit_board', array('board' => $board_id), 'admin');
  403. }
  404. /**
  405. * Create a new board and set its properties and position.
  406. * Allows (almost) the same options as the modifyBoard() function.
  407. * With the option inherit_permissions set, the parent board permissions
  408. * will be inherited.
  409. *
  410. * @param array $boardOptions
  411. * @return int The new board id
  412. */
  413. function createBoard($boardOptions)
  414. {
  415. global $boards, $modSettings, $smcFunc;
  416. // Trigger an error if one of the required values is not set.
  417. if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
  418. trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR);
  419. if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
  420. trigger_error('createBoard(): Target board is not set', E_USER_ERROR);
  421. // Set every optional value to its default value.
  422. $boardOptions += array(
  423. 'posts_count' => true,
  424. 'override_theme' => false,
  425. 'board_theme' => 0,
  426. 'access_groups' => array(),
  427. 'board_description' => '',
  428. 'profile' => 1,
  429. 'moderators' => '',
  430. 'inherit_permissions' => true,
  431. 'dont_log' => true,
  432. );
  433. $board_columns = array(
  434. 'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
  435. 'member_groups' => 'string', 'redirect' => 'string',
  436. );
  437. $board_parameters = array(
  438. $boardOptions['target_category'], $boardOptions['board_name'] , '', 0,
  439. '-1,0', '',
  440. );
  441. call_integration_hook('integrate_create_board', array($boardOptions, $board_columns, $board_parameters));
  442. // Insert a board, the settings are dealt with later.
  443. $smcFunc['db_insert']('',
  444. '{db_prefix}boards',
  445. $board_columns,
  446. $board_parameters,
  447. array('id_board')
  448. );
  449. $board_id = $smcFunc['db_insert_id']('{db_prefix}boards', 'id_board');
  450. if (empty($board_id))
  451. return 0;
  452. // Change the board according to the given specifications.
  453. modifyBoard($board_id, $boardOptions);
  454. // Do we want the parent permissions to be inherited?
  455. if ($boardOptions['inherit_permissions'])
  456. {
  457. getBoardTree();
  458. if (!empty($boards[$board_id]['parent']))
  459. {
  460. $request = $smcFunc['db_query']('', '
  461. SELECT id_profile
  462. FROM {db_prefix}boards
  463. WHERE id_board = {int:board_parent}
  464. LIMIT 1',
  465. array(
  466. 'board_parent' => (int) $boards[$board_id]['parent'],
  467. )
  468. );
  469. list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
  470. $smcFunc['db_free_result']($request);
  471. $smcFunc['db_query']('', '
  472. UPDATE {db_prefix}boards
  473. SET id_profile = {int:new_profile}
  474. WHERE id_board = {int:current_board}',
  475. array(
  476. 'new_profile' => $boardOptions['profile'],
  477. 'current_board' => $board_id,
  478. )
  479. );
  480. }
  481. }
  482. // Clean the data cache.
  483. clean_cache('data');
  484. // Created it.
  485. logAction('add_board', array('board' => $board_id), 'admin');
  486. // Here you are, a new board, ready to be spammed.
  487. return $board_id;
  488. }
  489. /**
  490. * Remove one or more boards.
  491. * Allows to move the children of the board before deleting it
  492. * if moveChildrenTo is set to null, the child boards will be deleted.
  493. * Deletes:
  494. * - all topics that are on the given boards;
  495. * - all information that's associated with the given boards;
  496. * updates the statistics to reflect the new situation.
  497. *
  498. * @param array $boards_to_remove
  499. * @param array $moveChildrenTo = null
  500. */
  501. function deleteBoards($boards_to_remove, $moveChildrenTo = null)
  502. {
  503. global $boards, $smcFunc;
  504. // No boards to delete? Return!
  505. if (empty($boards_to_remove))
  506. return;
  507. getBoardTree();
  508. call_integration_hook('integrate_delete_board', array($boards_to_remove, $moveChildrenTo));
  509. // If $moveChildrenTo is set to null, include the children in the removal.
  510. if ($moveChildrenTo === null)
  511. {
  512. // Get a list of the child boards that will also be removed.
  513. $child_boards_to_remove = array();
  514. foreach ($boards_to_remove as $board_to_remove)
  515. recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
  516. // Merge the children with their parents.
  517. if (!empty($child_boards_to_remove))
  518. $boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
  519. }
  520. // Move the children to a safe home.
  521. else
  522. {
  523. foreach ($boards_to_remove as $id_board)
  524. {
  525. // @todo Separate category?
  526. if ($moveChildrenTo === 0)
  527. fixChildren($id_board, 0, 0);
  528. else
  529. fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
  530. }
  531. }
  532. // Delete ALL topics in the selected boards (done first so topics can't be marooned.)
  533. $request = $smcFunc['db_query']('', '
  534. SELECT id_topic
  535. FROM {db_prefix}topics
  536. WHERE id_board IN ({array_int:boards_to_remove})',
  537. array(
  538. 'boards_to_remove' => $boards_to_remove,
  539. )
  540. );
  541. $topics = array();
  542. while ($row = $smcFunc['db_fetch_assoc']($request))
  543. $topics[] = $row['id_topic'];
  544. $smcFunc['db_free_result']($request);
  545. require_once(SUBSDIR . '/Topic.subs.php');
  546. removeTopics($topics, false);
  547. // Delete the board's logs.
  548. $smcFunc['db_query']('', '
  549. DELETE FROM {db_prefix}log_mark_read
  550. WHERE id_board IN ({array_int:boards_to_remove})',
  551. array(
  552. 'boards_to_remove' => $boards_to_remove,
  553. )
  554. );
  555. $smcFunc['db_query']('', '
  556. DELETE FROM {db_prefix}log_boards
  557. WHERE id_board IN ({array_int:boards_to_remove})',
  558. array(
  559. 'boards_to_remove' => $boards_to_remove,
  560. )
  561. );
  562. $smcFunc['db_query']('', '
  563. DELETE FROM {db_prefix}log_notify
  564. WHERE id_board IN ({array_int:boards_to_remove})',
  565. array(
  566. 'boards_to_remove' => $boards_to_remove,
  567. )
  568. );
  569. // Delete this board's moderators.
  570. $smcFunc['db_query']('', '
  571. DELETE FROM {db_prefix}moderators
  572. WHERE id_board IN ({array_int:boards_to_remove})',
  573. array(
  574. 'boards_to_remove' => $boards_to_remove,
  575. )
  576. );
  577. // Delete any extra events in the calendar.
  578. $smcFunc['db_query']('', '
  579. DELETE FROM {db_prefix}calendar
  580. WHERE id_board IN ({array_int:boards_to_remove})',
  581. array(
  582. 'boards_to_remove' => $boards_to_remove,
  583. )
  584. );
  585. // Delete any message icons that only appear on these boards.
  586. $smcFunc['db_query']('', '
  587. DELETE FROM {db_prefix}message_icons
  588. WHERE id_board IN ({array_int:boards_to_remove})',
  589. array(
  590. 'boards_to_remove' => $boards_to_remove,
  591. )
  592. );
  593. // Delete the boards.
  594. $smcFunc['db_query']('', '
  595. DELETE FROM {db_prefix}boards
  596. WHERE id_board IN ({array_int:boards_to_remove})',
  597. array(
  598. 'boards_to_remove' => $boards_to_remove,
  599. )
  600. );
  601. // Latest message/topic might not be there anymore.
  602. updateStats('message');
  603. updateStats('topic');
  604. updateSettings(array(
  605. 'calendar_updated' => time(),
  606. ));
  607. // Plus reset the cache to stop people getting odd results.
  608. updateSettings(array('settings_updated' => time()));
  609. // Clean the cache as well.
  610. clean_cache('data');
  611. // Let's do some serious logging.
  612. foreach ($boards_to_remove as $id_board)
  613. logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
  614. reorderBoards();
  615. }
  616. /**
  617. * Put all boards in the right order and sorts the records of the boards table.
  618. * Used by modifyBoard(), deleteBoards(), modifyCategory(), and deleteCategories() functions
  619. */
  620. function reorderBoards()
  621. {
  622. global $cat_tree, $boardList, $boards, $smcFunc;
  623. getBoardTree();
  624. // Set the board order for each category.
  625. $board_order = 0;
  626. foreach ($cat_tree as $catID => $dummy)
  627. {
  628. foreach ($boardList[$catID] as $boardID)
  629. if ($boards[$boardID]['order'] != ++$board_order)
  630. $smcFunc['db_query']('', '
  631. UPDATE {db_prefix}boards
  632. SET board_order = {int:new_order}
  633. WHERE id_board = {int:selected_board}',
  634. array(
  635. 'new_order' => $board_order,
  636. 'selected_board' => $boardID,
  637. )
  638. );
  639. }
  640. // Sort the records of the boards table on the board_order value.
  641. $smcFunc['db_query']('alter_table_boards', '
  642. ALTER TABLE {db_prefix}boards
  643. ORDER BY board_order',
  644. array(
  645. 'db_error_skip' => true,
  646. )
  647. );
  648. }
  649. /**
  650. * Fixes the children of a board by setting their child_levels to new values.
  651. * Used when a board is deleted or moved, to affect its children.
  652. *
  653. * @param int $parent
  654. * @param int $newLevel
  655. * @param int $newParent
  656. */
  657. function fixChildren($parent, $newLevel, $newParent)
  658. {
  659. global $smcFunc;
  660. // Grab all children of $parent...
  661. $result = $smcFunc['db_query']('', '
  662. SELECT id_board
  663. FROM {db_prefix}boards
  664. WHERE id_parent = {int:parent_board}',
  665. array(
  666. 'parent_board' => $parent,
  667. )
  668. );
  669. $children = array();
  670. while ($row = $smcFunc['db_fetch_assoc']($result))
  671. $children[] = $row['id_board'];
  672. $smcFunc['db_free_result']($result);
  673. // ...and set it to a new parent and child_level.
  674. $smcFunc['db_query']('', '
  675. UPDATE {db_prefix}boards
  676. SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
  677. WHERE id_parent = {int:parent_board}',
  678. array(
  679. 'new_parent' => $newParent,
  680. 'new_child_level' => $newLevel,
  681. 'parent_board' => $parent,
  682. )
  683. );
  684. // Recursively fix the children of the children.
  685. foreach ($children as $child)
  686. fixChildren($child, $newLevel + 1, $child);
  687. }
  688. /**
  689. * Load a lot of useful information regarding the boards and categories.
  690. * The information retrieved is stored in globals:
  691. * $boards properties of each board.
  692. * $boardList a list of boards grouped by category ID.
  693. * $cat_tree properties of each category.
  694. */
  695. function getBoardTree()
  696. {
  697. global $cat_tree, $boards, $boardList, $txt, $modSettings, $smcFunc;
  698. // Getting all the board and category information you'd ever wanted.
  699. $request = $smcFunc['db_query']('', '
  700. SELECT
  701. IFNULL(b.id_board, 0) AS id_board, b.id_parent, b.name AS board_name, b.description, b.child_level,
  702. b.board_order, b.count_posts, b.member_groups, b.id_theme, b.override_theme, b.id_profile, b.redirect,
  703. b.num_posts, b.num_topics, b.deny_member_groups, c.id_cat, c.name AS cat_name, c.cat_order, c.can_collapse
  704. FROM {db_prefix}categories AS c
  705. LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)
  706. ORDER BY c.cat_order, b.child_level, b.board_order',
  707. array(
  708. )
  709. );
  710. $cat_tree = array();
  711. $boards = array();
  712. $last_board_order = 0;
  713. while ($row = $smcFunc['db_fetch_assoc']($request))
  714. {
  715. if (!isset($cat_tree[$row['id_cat']]))
  716. {
  717. $cat_tree[$row['id_cat']] = array(
  718. 'node' => array(
  719. 'id' => $row['id_cat'],
  720. 'name' => $row['cat_name'],
  721. 'order' => $row['cat_order'],
  722. 'can_collapse' => $row['can_collapse']
  723. ),
  724. 'is_first' => empty($cat_tree),
  725. 'last_board_order' => $last_board_order,
  726. 'children' => array()
  727. );
  728. $prevBoard = 0;
  729. $curLevel = 0;
  730. }
  731. if (!empty($row['id_board']))
  732. {
  733. if ($row['child_level'] != $curLevel)
  734. $prevBoard = 0;
  735. $boards[$row['id_board']] = array(
  736. 'id' => $row['id_board'],
  737. 'category' => $row['id_cat'],
  738. 'parent' => $row['id_parent'],
  739. 'level' => $row['child_level'],
  740. 'order' => $row['board_order'],
  741. 'name' => $row['board_name'],
  742. 'member_groups' => explode(',', $row['member_groups']),
  743. 'deny_groups' => explode(',', $row['deny_member_groups']),
  744. 'description' => $row['description'],
  745. 'count_posts' => empty($row['count_posts']),
  746. 'posts' => $row['num_posts'],
  747. 'topics' => $row['num_topics'],
  748. 'theme' => $row['id_theme'],
  749. 'override_theme' => $row['override_theme'],
  750. 'profile' => $row['id_profile'],
  751. 'redirect' => $row['redirect'],
  752. 'prev_board' => $prevBoard
  753. );
  754. $prevBoard = $row['id_board'];
  755. $last_board_order = $row['board_order'];
  756. if (empty($row['child_level']))
  757. {
  758. $cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
  759. 'node' => &$boards[$row['id_board']],
  760. 'is_first' => empty($cat_tree[$row['id_cat']]['children']),
  761. 'children' => array()
  762. );
  763. $boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
  764. }
  765. else
  766. {
  767. // Parent doesn't exist!
  768. if (!isset($boards[$row['id_parent']]['tree']))
  769. fatal_lang_error('no_valid_parent', false, array($row['board_name']));
  770. // Wrong childlevel...we can silently fix this...
  771. if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
  772. $smcFunc['db_query']('', '
  773. UPDATE {db_prefix}boards
  774. SET child_level = {int:new_child_level}
  775. WHERE id_board = {int:selected_board}',
  776. array(
  777. 'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
  778. 'selected_board' => $row['id_board'],
  779. )
  780. );
  781. $boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
  782. 'node' => &$boards[$row['id_board']],
  783. 'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
  784. 'children' => array()
  785. );
  786. $boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
  787. }
  788. }
  789. }
  790. $smcFunc['db_free_result']($request);
  791. // Get a list of all the boards in each category (using recursion).
  792. $boardList = array();
  793. foreach ($cat_tree as $catID => $node)
  794. {
  795. $boardList[$catID] = array();
  796. recursiveBoards($boardList[$catID], $node);
  797. }
  798. }
  799. /**
  800. * Recursively get a list of boards.
  801. * Used by getBoardTree
  802. *
  803. * @param array &$_boardList
  804. * @param array &$_tree
  805. */
  806. function recursiveBoards(&$_boardList, &$_tree)
  807. {
  808. if (empty($_tree['children']))
  809. return;
  810. foreach ($_tree['children'] as $id => $node)
  811. {
  812. $_boardList[] = $id;
  813. recursiveBoards($_boardList, $node);
  814. }
  815. }
  816. /**
  817. * Returns whether the child board id is actually a child of the parent (recursive).
  818. * @param int $child
  819. * @param int $parent
  820. * @return boolean
  821. */
  822. function isChildOf($child, $parent)
  823. {
  824. global $boards;
  825. if (empty($boards[$child]['parent']))
  826. return false;
  827. if ($boards[$child]['parent'] == $parent)
  828. return true;
  829. return isChildOf($boards[$child]['parent'], $parent);
  830. }
  831. /**
  832. * Returns whether this member has notification turned on for the specified board.
  833. *
  834. * @param int $id_member
  835. * @param int $id_board
  836. * @return bool
  837. */
  838. function hasBoardNotification($id_member, $id_board)
  839. {
  840. global $smcFunc, $user_info, $board;
  841. // Find out if they have notification set for this board already.
  842. $request = $smcFunc['db_query']('', '
  843. SELECT id_member
  844. FROM {db_prefix}log_notify
  845. WHERE id_member = {int:current_member}
  846. AND id_board = {int:current_board}
  847. LIMIT 1',
  848. array(
  849. 'current_board' => $board,
  850. 'current_member' => $user_info['id'],
  851. )
  852. );
  853. $hasNotification = $smcFunc['db_num_rows']($request) != 0;
  854. $smcFunc['db_free_result']($request);
  855. return hasNotification;
  856. }
  857. /**
  858. * Set board notification on or off for the given member.
  859. *
  860. * @param int $id_member
  861. * @param int $id_board
  862. * @param bool $on = false
  863. */
  864. function setBoardNotification($id_member, $id_board, $on = false)
  865. {
  866. global $smcFunc;
  867. if ($on)
  868. {
  869. // Turn notification on. (note this just blows smoke if it's already on.)
  870. $smcFunc['db_insert']('ignore',
  871. '{db_prefix}log_notify',
  872. array('id_member' => 'int', 'id_board' => 'int'),
  873. array($id_member, $id_board),
  874. array('id_member', 'id_board')
  875. );
  876. }
  877. else
  878. {
  879. // Turn notification off for this board.
  880. $smcFunc['db_query']('', '
  881. DELETE FROM {db_prefix}log_notify
  882. WHERE id_member = {int:current_member}
  883. AND id_board = {int:current_board}',
  884. array(
  885. 'current_board' => $id_board,
  886. 'current_member' => $id_member,
  887. )
  888. );
  889. }
  890. }
  891. /**
  892. * Returns all the boards accessible to the current user.
  893. */
  894. function accessibleBoards()
  895. {
  896. global $smcFunc;
  897. // Find all the boards this user can see.
  898. $result = $smcFunc['db_query']('', '
  899. SELECT b.id_board
  900. FROM {db_prefix}boards AS b
  901. WHERE {query_see_board}',
  902. array(
  903. )
  904. );
  905. $boards = array();
  906. while ($row = $smcFunc['db_fetch_assoc']($result))
  907. $boards[] = $row['id_board'];
  908. $smcFunc['db_free_result']($result);
  909. return $boards;
  910. }