/** * Standard function to create the standardised category tree * * @param ID_TEXT Notification code * @param ?ID_TEXT The ID of where we're looking under (NULL: N/A) * @return array Tree structure */ function create_category_tree($notification_code, $id) { require_code('cedi'); if (is_null($id)) { $total = $GLOBALS['SITE_DB']->query_value_null_ok('seedy_pages', 'COUNT(*)'); if ($total > 300) { return parent::create_category_tree($notification_code, $id); } // Too many, so just allow removing UI } static $cedi_seen = array(); $pagelinks = get_cedi_page_tree($cedi_seen, is_null($id) ? NULL : intval($id), NULL, NULL, false, false, is_null($id) ? 0 : 1); $filtered = array(); foreach ($pagelinks as $p) { if (strval($p['id']) !== $id) { $filtered[] = $p; } } return $filtered; }
/** * Get a list of maps containing all the subpages, and path information, of the specified page - and those beneath it, recursively. * * @param array A list of pages we've already seen (we don't repeat them in multiple list positions) * @param ?AUTO_LINK The page being at the root of our recursion (NULL: true root page) * @param ?string The tree up to this point in the recursion (NULL: blank, as we are starting the recursion) * @param ?ID_TEXT The name of the $page_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 boolean Whether to collect post counts with our tree information * @param boolean Whether to make a compound list (a pair of a comma-separated list of children, and the child array) * @param ?integer The number of recursive levels to search (NULL: all) * @return array A list of maps for all subcategories. Each map entry containins the fields 'id' (category ID) and 'tree' (tree path to the category, including the categories own title). There is also an additional 'downloadcount' entry if stats were requested */ function get_cedi_page_tree(&$cedi_seen, $page_id = NULL, $tree = NULL, $title = NULL, $do_stats = true, $use_compound_list = false, $levels = NULL) { if (!$use_compound_list) { if ($levels == -1) { return array(); } } if (is_null($page_id)) { $page_id = db_get_first_id(); } $cedi_seen[] = $page_id; if (is_null($tree)) { $tree = ''; } // Put our title onto our tree if (is_null($title)) { $title = get_translated_text($GLOBALS['SITE_DB']->query_value('seedy_pages', 'title', array('id' => $page_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'] = $page_id; $children[0]['title'] = $title; $children[0]['tree'] = $tree; $children[0]['compound_list'] = strval($page_id) . ','; if ($do_stats) { $children[0]['filecount'] = $GLOBALS['SITE_DB']->query_value('seedy_posts', 'COUNT(*)', array('page_id' => $page_id)); } // Children of this category $rows = $GLOBALS['SITE_DB']->query_select('seedy_children', array('*'), array('parent_id' => $page_id), 'ORDER BY title', 300); $children[0]['child_count'] = count($rows); $tree .= ' > '; if ($levels !== 0 || $use_compound_list) { foreach ($rows as $child) { if (!in_array($child['child_id'], $cedi_seen)) { if (!has_category_access(get_member(), 'seedy_page', strval($child['child_id']))) { continue; } if (is_null($child['title'])) { $temp_rows = $GLOBALS['SITE_DB']->query_select('seedy_pages', array('title'), array('id' => $child['child_id']), '', 1); $child['title'] = get_translated_text($temp_rows[0]['title']); $GLOBALS['SITE_DB']->query_update('seedy_children', array('title' => $child['title']), array('parent_id' => $page_id, 'child_id' => $child['child_id'])); } $child_id = $child['child_id']; $child_title = $child['title']; $child_tree = $tree; $child_children = get_cedi_page_tree($cedi_seen, $child_id, $child_tree, $child_title, $do_stats, $use_compound_list, is_null($levels) ? NULL : $levels - 1); if ($use_compound_list) { list($child_children, $_compound_list) = $child_children; $children[0]['compound_list'] .= $_compound_list; } if ($levels !== 0) { $children = array_merge($children, $child_children); } } } } return $use_compound_list ? array($children, $children[0]['compound_list']) : $children; }
/** * 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) { unset($options); require_code('cedi'); require_lang('cedi'); $cedi_seen = array(); $tree = get_cedi_page_tree($cedi_seen, is_null($id) ? NULL : intval($id), NULL, NULL, true, false, is_null($id) ? 0 : 1); $stripped_id = $id; $out = ''; if (!has_actual_page_access(NULL, 'cedi')) { $tree = array(); } foreach ($tree as $t) { $_id = strval($t['id']); if ($stripped_id === $_id) { continue; } // Possible when we look under as a root $title = $t['title']; $has_children = $t['child_count'] != 0; $selectable = true; $tag = 'category'; // category $out .= '<' . $tag . ' id="' . $_id . '" title="' . xmlentities($title) . '" has_children="' . ($has_children ? 'true' : 'false') . '" selectable="' . ($selectable ? 'true' : 'false') . '"></' . $tag . '>'; } if (is_null($id)) { if (!db_has_subqueries($GLOBALS['SITE_DB']->connection_read)) { $where = ''; $cedi_seen = array(); get_cedi_page_tree($cedi_seen, is_null($id) ? NULL : intval($id)); // To build up $cedi_seen foreach ($cedi_seen as $seen) { if ($where != '') { $where .= ' AND '; } $where .= 'p.id<>' . strval((int) $seen); } $orphans = $GLOBALS['SITE_DB']->query('SELECT p.id,text_original,p.title FROM ' . get_table_prefix() . 'seedy_pages p LEFT JOIN ' . get_table_prefix() . 'translate t ON ' . db_string_equal_to('language', user_lang()) . ' AND t.id=p.title WHERE ' . $where . ' ORDER BY add_date DESC', 50); } else { $orphans = $GLOBALS['SITE_DB']->query('SELECT p.id,text_original,p.title FROM ' . get_table_prefix() . 'seedy_pages p LEFT JOIN ' . get_table_prefix() . 'translate t ON ' . db_string_equal_to('language', user_lang()) . ' AND t.id=p.title WHERE NOT EXISTS(SELECT * FROM ' . get_table_prefix() . 'seedy_children WHERE child_id=p.id) ORDER BY add_date DESC', 50); if (count($orphans) < 50) { global $M_SORT_KEY; $M_SORT_KEY = 'text_original'; usort($orphans, 'multi_sort'); } } foreach ($orphans as $orphan) { if (!has_category_access(get_member(), 'seedy_page', strval($orphan['id']))) { continue; } if ($orphan['id'] == db_get_first_id()) { continue; } if ($GLOBALS['RECORD_LANG_STRINGS_CONTENT'] || is_null($orphan['text_original'])) { $orphan['text_original'] = get_translated_text($orphan['title']); } $_id = strval($orphan['id']); $title = $orphan['text_original']; $has_children = $GLOBALS['SITE_DB']->query_value('seedy_children', 'COUNT(*)', array('parent_id' => $orphan['id'])) != 0; $selectable = true; $tag = 'category'; // category $out .= '<' . $tag . ' id="' . $_id . '" title="' . xmlentities($title) . '" has_children="' . ($has_children ? 'true' : 'false') . '" selectable="' . ($selectable ? 'true' : 'false') . '"></' . $tag . '>'; } } $tag = 'result'; // result return '<' . $tag . '>' . $out . '</' . $tag . '>'; }