/** * Standard modular run function for ajax-tree hooks. Generates XML for a tree list, which is interpreted by Javascript and expanded on-demand (via new calls). * * @param ?ID_TEXT The ID to do under (NULL: root) * @param array Options being passed through * @param ?ID_TEXT The ID to select by default (NULL: none) * @return string XML in the special category,entry format */ function run($id, $options, $default = NULL) { require_code('ocf_forums'); require_code('ocf_forums2'); $tree = ocf_get_topic_tree(is_null($id) ? NULL : intval($id), NULL, NULL, is_null($id) ? 0 : 1); if (!has_actual_page_access(NULL, 'forumview')) { $tree = array(); } $out = ''; foreach ($tree as $t) { $_id = $t['id']; if ($id === strval($_id)) { foreach ($t['entries'] as $eid => $etitle) { $out .= '<entry id="' . xmlentities(strval($eid)) . '" title="' . xmlentities($etitle) . '" selectable="true"></entry>'; } continue; } $title = $t['title']; $has_children = $t['child_count'] != 0 || $t['child_entry_count'] != 0; $out .= '<category id="' . xmlentities(strval($_id)) . '" title="' . xmlentities($title) . '" has_children="' . ($has_children ? 'true' : 'false') . '" selectable="false"></category>'; } // Mark parent cats for pre-expansion if (!is_null($default) && $default != '') { $cat = $GLOBALS['FORUM_DB']->query_value_null_ok('f_topics', 't_forum_id', array('id' => intval($default))); while (!is_null($cat)) { $out .= '<expand>' . strval($cat) . '</expand>'; $cat = $GLOBALS['FORUM_DB']->query_value_null_ok('f_forums', 'f_parent_forum', array('id' => $cat)); } } return '<result>' . $out . '</result>'; }
/** * Get a list of maps containing all the topics, and path information, under the specified forum - and those beneath it, recursively. * * @param ?AUTO_LINK The forum being at the root of our recursion (NULL: true root forum) * @param ?string The tree up to this point in the recursion (NULL: blank, as we are starting the recursion) * @param ?ID_TEXT The forum name of the $forum_id we are currently going through (NULL: look it up). This is here for efficiency reasons, as finding children IDs to recurse to also reveals the childs title * @param ?integer The number of recursive levels to search (NULL: all) * @return array A list of maps for all forums. Each map entry containins the fields 'id' (forum ID) and 'tree' (tree path to the forum, including the forums own title), and more. */ function ocf_get_topic_tree($forum_id = NULL, $tree = NULL, $title = NULL, $levels = NULL) { if (is_null($forum_id)) { $forum_id = db_get_first_id(); } if (is_null($tree)) { $tree = ''; } if (!has_category_access(get_member(), 'forums', strval($forum_id))) { return array(); } // Put our title onto our tree if (is_null($title)) { $title = $GLOBALS['FORUM_DB']->query_value('f_forums', 'f_name', array('id' => $forum_id)); } $tree .= $title; // We'll be putting all children in this entire tree into a single list $children = array(); $children[0] = array(); $children[0]['id'] = $forum_id; $children[0]['title'] = $title; $children[0]['tree'] = $tree; // Children of this category $rows = $GLOBALS['FORUM_DB']->query_select('f_forums', array('id', 'f_name'), array('f_parent_forum' => $forum_id), 'ORDER BY f_category_id,f_position', 200); if (count($rows) == 200) { $rows = array(); } // Too many, this method will suck $tmap = array('t_forum_id' => $forum_id); if (!has_specific_permission(get_member(), 'see_unvalidated')) { $tmap['t_validated'] = 1; } $children[0]['entries'] = collapse_2d_complexity('id', 't_cache_first_title', $GLOBALS['FORUM_DB']->query_select('f_topics', array('id', 't_cache_first_title'), $tmap, 'ORDER BY t_cache_first_time DESC', 12)); $children[0]['child_entry_count'] = count($children[0]['entries']); if ($levels === 0) { $children[0]['entries'] = array(); } $children[0]['child_count'] = count($rows); $tree .= ' > '; if ($levels !== 0) { foreach ($rows as $child) { $child_id = $child['id']; $child_title = $child['f_name']; $child_tree = $tree; $child_children = ocf_get_topic_tree($child_id, $child_tree, $child_title, is_null($levels) ? NULL : max(0, $levels - 1)); $children = array_merge($children, $child_children); } } return $children; }