/sources/subs/Boards.subs.php
PHP | 1027 lines | 726 code | 117 blank | 184 comment | 88 complexity | d9a72fa02c1e7e78a929c2383197d1b5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
- <?php
- /**
- * @name ElkArte Forum
- * @copyright ElkArte Forum contributors
- * @license BSD http://opensource.org/licenses/BSD-3-Clause
- *
- * This software is a derived product, based on:
- *
- * Simple Machines Forum (SMF)
- * copyright: 2011 Simple Machines (http://www.simplemachines.org)
- * license: BSD, See included LICENSE.TXT for terms and conditions.
- *
- * @version 1.0 Alpha
- *
- * This file is mainly concerned with minor tasks relating to boards, such as
- * marking them read, collapsing categories, or quick moderation.
- *
- */
- if (!defined('ELKARTE'))
- die('No access...');
- /**
- * Mark a board or multiple boards read.
- *
- * @param array $boards
- * @param bool $unread
- */
- function markBoardsRead($boards, $unread = false)
- {
- global $user_info, $modSettings, $smcFunc;
- // Force $boards to be an array.
- if (!is_array($boards))
- $boards = array($boards);
- else
- $boards = array_unique($boards);
- // No boards, nothing to mark as read.
- if (empty($boards))
- return;
- // Allow the user to mark a board as unread.
- if ($unread)
- {
- // Clear out all the places where this lovely info is stored.
- // @todo Maybe not log_mark_read?
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_mark_read
- WHERE id_board IN ({array_int:board_list})
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'board_list' => $boards,
- )
- );
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_boards
- WHERE id_board IN ({array_int:board_list})
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'board_list' => $boards,
- )
- );
- }
- // Otherwise mark the board as read.
- else
- {
- $markRead = array();
- foreach ($boards as $board)
- $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $board);
- // Update log_mark_read and log_boards.
- $smcFunc['db_insert']('replace',
- '{db_prefix}log_mark_read',
- array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
- $markRead,
- array('id_board', 'id_member')
- );
- $smcFunc['db_insert']('replace',
- '{db_prefix}log_boards',
- array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
- $markRead,
- array('id_board', 'id_member')
- );
- }
- // Get rid of useless log_topics data, because log_mark_read is better for it - even if marking unread - I think so...
- // @todo look at this...
- // The call to markBoardsRead() in Display() used to be simply
- // marking log_boards (the previous query only)
- $result = $smcFunc['db_query']('', '
- SELECT MIN(id_topic)
- FROM {db_prefix}log_topics
- WHERE id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- )
- );
- list ($lowest_topic) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- if (empty($lowest_topic))
- return;
- // @todo SLOW This query seems to eat it sometimes.
- $result = $smcFunc['db_query']('', '
- SELECT lt.id_topic
- FROM {db_prefix}log_topics AS lt
- INNER JOIN {db_prefix}topics AS t /*!40000 USE INDEX (PRIMARY) */ ON (t.id_topic = lt.id_topic
- AND t.id_board IN ({array_int:board_list}))
- WHERE lt.id_member = {int:current_member}
- AND lt.id_topic >= {int:lowest_topic}
- AND lt.disregarded != 1',
- array(
- 'current_member' => $user_info['id'],
- 'board_list' => $boards,
- 'lowest_topic' => $lowest_topic,
- )
- );
- $topics = array();
- while ($row = $smcFunc['db_fetch_assoc']($result))
- $topics[] = $row['id_topic'];
- $smcFunc['db_free_result']($result);
- if (!empty($topics))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_topics
- WHERE id_member = {int:current_member}
- AND id_topic IN ({array_int:topic_list})',
- array(
- 'current_member' => $user_info['id'],
- 'topic_list' => $topics,
- )
- );
- }
- /**
- * Get the id_member associated with the specified message.
- * @param int $messageID
- * @return int the member id
- */
- function getMsgMemberID($messageID)
- {
- global $smcFunc;
- // Find the topic and make sure the member still exists.
- $result = $smcFunc['db_query']('', '
- SELECT IFNULL(mem.id_member, 0)
- FROM {db_prefix}messages AS m
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
- WHERE m.id_msg = {int:selected_message}
- LIMIT 1',
- array(
- 'selected_message' => (int) $messageID,
- )
- );
- if ($smcFunc['db_num_rows']($result) > 0)
- list ($memberID) = $smcFunc['db_fetch_row']($result);
- // The message doesn't even exist.
- else
- $memberID = 0;
- $smcFunc['db_free_result']($result);
- return (int) $memberID;
- }
- /**
- * Modify the settings and position of a board.
- * Used by ManageBoards.php to change the settings of a board.
- *
- * @param int $board_id
- * @param array &$boardOptions
- */
- function modifyBoard($board_id, &$boardOptions)
- {
- global $cat_tree, $boards, $boardList, $modSettings, $smcFunc;
- // Get some basic information about all boards and categories.
- getBoardTree();
- // Make sure given boards and categories exist.
- if (!isset($boards[$board_id]) || (isset($boardOptions['target_board']) && !isset($boards[$boardOptions['target_board']])) || (isset($boardOptions['target_category']) && !isset($cat_tree[$boardOptions['target_category']])))
- fatal_lang_error('no_board');
- $id = $board_id;
- call_integration_hook('integrate_pre_modify_board', array($id, $boardOptions));
- // All things that will be updated in the database will be in $boardUpdates.
- $boardUpdates = array();
- $boardUpdateParameters = array();
- // In case the board has to be moved
- if (isset($boardOptions['move_to']))
- {
- // Move the board to the top of a given category.
- if ($boardOptions['move_to'] == 'top')
- {
- $id_cat = $boardOptions['target_category'];
- $child_level = 0;
- $id_parent = 0;
- $after = $cat_tree[$id_cat]['last_board_order'];
- }
- // Move the board to the bottom of a given category.
- elseif ($boardOptions['move_to'] == 'bottom')
- {
- $id_cat = $boardOptions['target_category'];
- $child_level = 0;
- $id_parent = 0;
- $after = 0;
- foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy)
- $after = max($after, $boards[$id_board]['order']);
- }
- // Make the board a child of a given board.
- elseif ($boardOptions['move_to'] == 'child')
- {
- $id_cat = $boards[$boardOptions['target_board']]['category'];
- $child_level = $boards[$boardOptions['target_board']]['level'] + 1;
- $id_parent = $boardOptions['target_board'];
- // People can be creative, in many ways...
- if (isChildOf($id_parent, $board_id))
- fatal_lang_error('mboards_parent_own_child_error', false);
- elseif ($id_parent == $board_id)
- fatal_lang_error('mboards_board_own_child_error', false);
- $after = $boards[$boardOptions['target_board']]['order'];
- // Check if there are already children and (if so) get the max board order.
- if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child']))
- foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy)
- $after = max($after, $boards[$childBoard_id]['order']);
- }
- // Place a board before or after another board, on the same child level.
- elseif (in_array($boardOptions['move_to'], array('before', 'after')))
- {
- $id_cat = $boards[$boardOptions['target_board']]['category'];
- $child_level = $boards[$boardOptions['target_board']]['level'];
- $id_parent = $boards[$boardOptions['target_board']]['parent'];
- $after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0);
- }
- // Oops...?
- else
- trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR);
- // Get a list of children of this board.
- $childList = array();
- recursiveBoards($childList, $boards[$board_id]['tree']);
- // See if there are changes that affect children.
- $childUpdates = array();
- $levelDiff = $child_level - $boards[$board_id]['level'];
- if ($levelDiff != 0)
- $childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
- if ($id_cat != $boards[$board_id]['category'])
- $childUpdates[] = 'id_cat = {int:category}';
- // Fix the children of this board.
- if (!empty($childList) && !empty($childUpdates))
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET ' . implode(',
- ', $childUpdates) . '
- WHERE id_board IN ({array_int:board_list})',
- array(
- 'board_list' => $childList,
- 'category' => $id_cat,
- 'level_diff' => $levelDiff,
- )
- );
- // Make some room for this spot.
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET board_order = board_order + {int:new_order}
- WHERE board_order > {int:insert_after}
- AND id_board != {int:selected_board}',
- array(
- 'insert_after' => $after,
- 'selected_board' => $board_id,
- 'new_order' => 1 + count($childList),
- )
- );
- $boardUpdates[] = 'id_cat = {int:id_cat}';
- $boardUpdates[] = 'id_parent = {int:id_parent}';
- $boardUpdates[] = 'child_level = {int:child_level}';
- $boardUpdates[] = 'board_order = {int:board_order}';
- $boardUpdateParameters += array(
- 'id_cat' => $id_cat,
- 'id_parent' => $id_parent,
- 'child_level' => $child_level,
- 'board_order' => $after + 1,
- );
- }
- // This setting is a little twisted in the database...
- if (isset($boardOptions['posts_count']))
- {
- $boardUpdates[] = 'count_posts = {int:count_posts}';
- $boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
- }
- // Set the theme for this board.
- if (isset($boardOptions['board_theme']))
- {
- $boardUpdates[] = 'id_theme = {int:id_theme}';
- $boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
- }
- // Should the board theme override the user preferred theme?
- if (isset($boardOptions['override_theme']))
- {
- $boardUpdates[] = 'override_theme = {int:override_theme}';
- $boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
- }
- // Who's allowed to access this board.
- if (isset($boardOptions['access_groups']))
- {
- $boardUpdates[] = 'member_groups = {string:member_groups}';
- $boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
- }
- // And who isn't.
- if (isset($boardOptions['deny_groups']))
- {
- $boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
- $boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
- }
- if (isset($boardOptions['board_name']))
- {
- $boardUpdates[] = 'name = {string:board_name}';
- $boardUpdateParameters['board_name'] = $boardOptions['board_name'];
- }
- if (isset($boardOptions['board_description']))
- {
- $boardUpdates[] = 'description = {string:board_description}';
- $boardUpdateParameters['board_description'] = $boardOptions['board_description'];
- }
- if (isset($boardOptions['profile']))
- {
- $boardUpdates[] = 'id_profile = {int:profile}';
- $boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
- }
- if (isset($boardOptions['redirect']))
- {
- $boardUpdates[] = 'redirect = {string:redirect}';
- $boardUpdateParameters['redirect'] = $boardOptions['redirect'];
- }
- if (isset($boardOptions['num_posts']))
- {
- $boardUpdates[] = 'num_posts = {int:num_posts}';
- $boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
- }
- $id = $board_id;
- call_integration_hook('integrate_modify_board', array($id, $boardUpdates, $boardUpdateParameters));
- // Do the updates (if any).
- if (!empty($boardUpdates))
- $request = $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET
- ' . implode(',
- ', $boardUpdates) . '
- WHERE id_board = {int:selected_board}',
- array_merge($boardUpdateParameters, array(
- 'selected_board' => $board_id,
- ))
- );
- // Set moderators of this board.
- if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']))
- {
- // Reset current moderators for this board - if there are any!
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}moderators
- WHERE id_board = {int:board_list}',
- array(
- 'board_list' => $board_id,
- )
- );
- // Validate and get the IDs of the new moderators.
- if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
- {
- // Divvy out the usernames, remove extra space.
- $moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('"' => '"'));
- preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
- $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
- for ($k = 0, $n = count($moderators); $k < $n; $k++)
- {
- $moderators[$k] = trim($moderators[$k]);
- if (strlen($moderators[$k]) == 0)
- unset($moderators[$k]);
- }
- // Find all the id_member's for the member_name's in the list.
- if (empty($boardOptions['moderators']))
- $boardOptions['moderators'] = array();
- if (!empty($moderators))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}members
- WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
- LIMIT ' . count($moderators),
- array(
- 'moderator_list' => $moderators,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $boardOptions['moderators'][] = $row['id_member'];
- $smcFunc['db_free_result']($request);
- }
- }
- // Add the moderators to the board.
- if (!empty($boardOptions['moderators']))
- {
- $inserts = array();
- foreach ($boardOptions['moderators'] as $moderator)
- $inserts[] = array($board_id, $moderator);
- $smcFunc['db_insert']('insert',
- '{db_prefix}moderators',
- array('id_board' => 'int', 'id_member' => 'int'),
- $inserts,
- array('id_board', 'id_member')
- );
- }
- // Note that caches can now be wrong!
- updateSettings(array('settings_updated' => time()));
- }
- if (isset($boardOptions['move_to']))
- reorderBoards();
- clean_cache('data');
- if (empty($boardOptions['dont_log']))
- logAction('edit_board', array('board' => $board_id), 'admin');
- }
- /**
- * Create a new board and set its properties and position.
- * Allows (almost) the same options as the modifyBoard() function.
- * With the option inherit_permissions set, the parent board permissions
- * will be inherited.
- *
- * @param array $boardOptions
- * @return int The new board id
- */
- function createBoard($boardOptions)
- {
- global $boards, $modSettings, $smcFunc;
- // Trigger an error if one of the required values is not set.
- if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
- trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR);
- if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
- trigger_error('createBoard(): Target board is not set', E_USER_ERROR);
- // Set every optional value to its default value.
- $boardOptions += array(
- 'posts_count' => true,
- 'override_theme' => false,
- 'board_theme' => 0,
- 'access_groups' => array(),
- 'board_description' => '',
- 'profile' => 1,
- 'moderators' => '',
- 'inherit_permissions' => true,
- 'dont_log' => true,
- );
- $board_columns = array(
- 'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
- 'member_groups' => 'string', 'redirect' => 'string',
- );
- $board_parameters = array(
- $boardOptions['target_category'], $boardOptions['board_name'] , '', 0,
- '-1,0', '',
- );
- call_integration_hook('integrate_create_board', array($boardOptions, $board_columns, $board_parameters));
- // Insert a board, the settings are dealt with later.
- $smcFunc['db_insert']('',
- '{db_prefix}boards',
- $board_columns,
- $board_parameters,
- array('id_board')
- );
- $board_id = $smcFunc['db_insert_id']('{db_prefix}boards', 'id_board');
- if (empty($board_id))
- return 0;
- // Change the board according to the given specifications.
- modifyBoard($board_id, $boardOptions);
- // Do we want the parent permissions to be inherited?
- if ($boardOptions['inherit_permissions'])
- {
- getBoardTree();
- if (!empty($boards[$board_id]['parent']))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_profile
- FROM {db_prefix}boards
- WHERE id_board = {int:board_parent}
- LIMIT 1',
- array(
- 'board_parent' => (int) $boards[$board_id]['parent'],
- )
- );
- list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET id_profile = {int:new_profile}
- WHERE id_board = {int:current_board}',
- array(
- 'new_profile' => $boardOptions['profile'],
- 'current_board' => $board_id,
- )
- );
- }
- }
- // Clean the data cache.
- clean_cache('data');
- // Created it.
- logAction('add_board', array('board' => $board_id), 'admin');
- // Here you are, a new board, ready to be spammed.
- return $board_id;
- }
- /**
- * Remove one or more boards.
- * Allows to move the children of the board before deleting it
- * if moveChildrenTo is set to null, the child boards will be deleted.
- * Deletes:
- * - all topics that are on the given boards;
- * - all information that's associated with the given boards;
- * updates the statistics to reflect the new situation.
- *
- * @param array $boards_to_remove
- * @param array $moveChildrenTo = null
- */
- function deleteBoards($boards_to_remove, $moveChildrenTo = null)
- {
- global $boards, $smcFunc;
- // No boards to delete? Return!
- if (empty($boards_to_remove))
- return;
- getBoardTree();
- call_integration_hook('integrate_delete_board', array($boards_to_remove, $moveChildrenTo));
- // If $moveChildrenTo is set to null, include the children in the removal.
- if ($moveChildrenTo === null)
- {
- // Get a list of the child boards that will also be removed.
- $child_boards_to_remove = array();
- foreach ($boards_to_remove as $board_to_remove)
- recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
- // Merge the children with their parents.
- if (!empty($child_boards_to_remove))
- $boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
- }
- // Move the children to a safe home.
- else
- {
- foreach ($boards_to_remove as $id_board)
- {
- // @todo Separate category?
- if ($moveChildrenTo === 0)
- fixChildren($id_board, 0, 0);
- else
- fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
- }
- }
- // Delete ALL topics in the selected boards (done first so topics can't be marooned.)
- $request = $smcFunc['db_query']('', '
- SELECT id_topic
- FROM {db_prefix}topics
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- $topics = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $topics[] = $row['id_topic'];
- $smcFunc['db_free_result']($request);
- require_once(SUBSDIR . '/Topic.subs.php');
- removeTopics($topics, false);
- // Delete the board's logs.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_mark_read
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_boards
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_notify
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- // Delete this board's moderators.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}moderators
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- // Delete any extra events in the calendar.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}calendar
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- // Delete any message icons that only appear on these boards.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}message_icons
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- // Delete the boards.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}boards
- WHERE id_board IN ({array_int:boards_to_remove})',
- array(
- 'boards_to_remove' => $boards_to_remove,
- )
- );
- // Latest message/topic might not be there anymore.
- updateStats('message');
- updateStats('topic');
- updateSettings(array(
- 'calendar_updated' => time(),
- ));
- // Plus reset the cache to stop people getting odd results.
- updateSettings(array('settings_updated' => time()));
- // Clean the cache as well.
- clean_cache('data');
- // Let's do some serious logging.
- foreach ($boards_to_remove as $id_board)
- logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
- reorderBoards();
- }
- /**
- * Put all boards in the right order and sorts the records of the boards table.
- * Used by modifyBoard(), deleteBoards(), modifyCategory(), and deleteCategories() functions
- */
- function reorderBoards()
- {
- global $cat_tree, $boardList, $boards, $smcFunc;
- getBoardTree();
- // Set the board order for each category.
- $board_order = 0;
- foreach ($cat_tree as $catID => $dummy)
- {
- foreach ($boardList[$catID] as $boardID)
- if ($boards[$boardID]['order'] != ++$board_order)
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET board_order = {int:new_order}
- WHERE id_board = {int:selected_board}',
- array(
- 'new_order' => $board_order,
- 'selected_board' => $boardID,
- )
- );
- }
- // Sort the records of the boards table on the board_order value.
- $smcFunc['db_query']('alter_table_boards', '
- ALTER TABLE {db_prefix}boards
- ORDER BY board_order',
- array(
- 'db_error_skip' => true,
- )
- );
- }
- /**
- * Fixes the children of a board by setting their child_levels to new values.
- * Used when a board is deleted or moved, to affect its children.
- *
- * @param int $parent
- * @param int $newLevel
- * @param int $newParent
- */
- function fixChildren($parent, $newLevel, $newParent)
- {
- global $smcFunc;
- // Grab all children of $parent...
- $result = $smcFunc['db_query']('', '
- SELECT id_board
- FROM {db_prefix}boards
- WHERE id_parent = {int:parent_board}',
- array(
- 'parent_board' => $parent,
- )
- );
- $children = array();
- while ($row = $smcFunc['db_fetch_assoc']($result))
- $children[] = $row['id_board'];
- $smcFunc['db_free_result']($result);
- // ...and set it to a new parent and child_level.
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
- WHERE id_parent = {int:parent_board}',
- array(
- 'new_parent' => $newParent,
- 'new_child_level' => $newLevel,
- 'parent_board' => $parent,
- )
- );
- // Recursively fix the children of the children.
- foreach ($children as $child)
- fixChildren($child, $newLevel + 1, $child);
- }
- /**
- * Load a lot of useful information regarding the boards and categories.
- * The information retrieved is stored in globals:
- * $boards properties of each board.
- * $boardList a list of boards grouped by category ID.
- * $cat_tree properties of each category.
- */
- function getBoardTree()
- {
- global $cat_tree, $boards, $boardList, $txt, $modSettings, $smcFunc;
- // Getting all the board and category information you'd ever wanted.
- $request = $smcFunc['db_query']('', '
- SELECT
- IFNULL(b.id_board, 0) AS id_board, b.id_parent, b.name AS board_name, b.description, b.child_level,
- b.board_order, b.count_posts, b.member_groups, b.id_theme, b.override_theme, b.id_profile, b.redirect,
- b.num_posts, b.num_topics, b.deny_member_groups, c.id_cat, c.name AS cat_name, c.cat_order, c.can_collapse
- FROM {db_prefix}categories AS c
- LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)
- ORDER BY c.cat_order, b.child_level, b.board_order',
- array(
- )
- );
- $cat_tree = array();
- $boards = array();
- $last_board_order = 0;
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if (!isset($cat_tree[$row['id_cat']]))
- {
- $cat_tree[$row['id_cat']] = array(
- 'node' => array(
- 'id' => $row['id_cat'],
- 'name' => $row['cat_name'],
- 'order' => $row['cat_order'],
- 'can_collapse' => $row['can_collapse']
- ),
- 'is_first' => empty($cat_tree),
- 'last_board_order' => $last_board_order,
- 'children' => array()
- );
- $prevBoard = 0;
- $curLevel = 0;
- }
- if (!empty($row['id_board']))
- {
- if ($row['child_level'] != $curLevel)
- $prevBoard = 0;
- $boards[$row['id_board']] = array(
- 'id' => $row['id_board'],
- 'category' => $row['id_cat'],
- 'parent' => $row['id_parent'],
- 'level' => $row['child_level'],
- 'order' => $row['board_order'],
- 'name' => $row['board_name'],
- 'member_groups' => explode(',', $row['member_groups']),
- 'deny_groups' => explode(',', $row['deny_member_groups']),
- 'description' => $row['description'],
- 'count_posts' => empty($row['count_posts']),
- 'posts' => $row['num_posts'],
- 'topics' => $row['num_topics'],
- 'theme' => $row['id_theme'],
- 'override_theme' => $row['override_theme'],
- 'profile' => $row['id_profile'],
- 'redirect' => $row['redirect'],
- 'prev_board' => $prevBoard
- );
- $prevBoard = $row['id_board'];
- $last_board_order = $row['board_order'];
- if (empty($row['child_level']))
- {
- $cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
- 'node' => &$boards[$row['id_board']],
- 'is_first' => empty($cat_tree[$row['id_cat']]['children']),
- 'children' => array()
- );
- $boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
- }
- else
- {
- // Parent doesn't exist!
- if (!isset($boards[$row['id_parent']]['tree']))
- fatal_lang_error('no_valid_parent', false, array($row['board_name']));
- // Wrong childlevel...we can silently fix this...
- if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}boards
- SET child_level = {int:new_child_level}
- WHERE id_board = {int:selected_board}',
- array(
- 'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
- 'selected_board' => $row['id_board'],
- )
- );
- $boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
- 'node' => &$boards[$row['id_board']],
- 'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
- 'children' => array()
- );
- $boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
- }
- }
- }
- $smcFunc['db_free_result']($request);
- // Get a list of all the boards in each category (using recursion).
- $boardList = array();
- foreach ($cat_tree as $catID => $node)
- {
- $boardList[$catID] = array();
- recursiveBoards($boardList[$catID], $node);
- }
- }
- /**
- * Recursively get a list of boards.
- * Used by getBoardTree
- *
- * @param array &$_boardList
- * @param array &$_tree
- */
- function recursiveBoards(&$_boardList, &$_tree)
- {
- if (empty($_tree['children']))
- return;
- foreach ($_tree['children'] as $id => $node)
- {
- $_boardList[] = $id;
- recursiveBoards($_boardList, $node);
- }
- }
- /**
- * Returns whether the child board id is actually a child of the parent (recursive).
- * @param int $child
- * @param int $parent
- * @return boolean
- */
- function isChildOf($child, $parent)
- {
- global $boards;
- if (empty($boards[$child]['parent']))
- return false;
- if ($boards[$child]['parent'] == $parent)
- return true;
- return isChildOf($boards[$child]['parent'], $parent);
- }
- /**
- * Returns whether this member has notification turned on for the specified board.
- *
- * @param int $id_member
- * @param int $id_board
- * @return bool
- */
- function hasBoardNotification($id_member, $id_board)
- {
- global $smcFunc, $user_info, $board;
- // Find out if they have notification set for this board already.
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}log_notify
- WHERE id_member = {int:current_member}
- AND id_board = {int:current_board}
- LIMIT 1',
- array(
- 'current_board' => $board,
- 'current_member' => $user_info['id'],
- )
- );
- $hasNotification = $smcFunc['db_num_rows']($request) != 0;
- $smcFunc['db_free_result']($request);
- return hasNotification;
- }
- /**
- * Set board notification on or off for the given member.
- *
- * @param int $id_member
- * @param int $id_board
- * @param bool $on = false
- */
- function setBoardNotification($id_member, $id_board, $on = false)
- {
- global $smcFunc;
- if ($on)
- {
- // Turn notification on. (note this just blows smoke if it's already on.)
- $smcFunc['db_insert']('ignore',
- '{db_prefix}log_notify',
- array('id_member' => 'int', 'id_board' => 'int'),
- array($id_member, $id_board),
- array('id_member', 'id_board')
- );
- }
- else
- {
- // Turn notification off for this board.
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}log_notify
- WHERE id_member = {int:current_member}
- AND id_board = {int:current_board}',
- array(
- 'current_board' => $id_board,
- 'current_member' => $id_member,
- )
- );
- }
- }
- /**
- * Returns all the boards accessible to the current user.
- */
- function accessibleBoards()
- {
- global $smcFunc;
- // Find all the boards this user can see.
- $result = $smcFunc['db_query']('', '
- SELECT b.id_board
- FROM {db_prefix}boards AS b
- WHERE {query_see_board}',
- array(
- )
- );
- $boards = array();
- while ($row = $smcFunc['db_fetch_assoc']($result))
- $boards[] = $row['id_board'];
- $smcFunc['db_free_result']($result);
- return $boards;
- }