Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Last active April 17, 2016 04:35
Show Gist options
  • Save joshuaadickerson/09f8b34904d2f4927ccd to your computer and use it in GitHub Desktop.
Save joshuaadickerson/09f8b34904d2f4927ccd to your computer and use it in GitHub Desktop.
<?php
function getBoardTree()
{
global $cat_tree, $boards, $boardList;
$db = database();
// Getting all the board and category information you'd ever wanted.
$request = $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;
$cattree_callback = false;
call_integration_hook('catTree-callback', array(&$cattree_callback));
$catTree_callback ? !is_callable($catTree_callback) ? false : $catTree_callback;
$boardtree_callback = false;
call_integration_hook('boardTree-callback', array(&$boardtree_callback));
$boardtree_callback ? !is_callable($boardtree_callback) ? false : $boardtree_callback;
while ($row = $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()
);
if ($catTree_callback !== false)
{
$catTree_callback($row, &$cat_tree);
}
$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']))
Errors::instance()->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)
$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']];
}
if ($boardTree_callback !== false)
{
$boardTree_callback($row, &$boards);
}
}
}
$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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment