/** * 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('catalogues'); require_lang('catalogues'); $catalogue_name = array_key_exists('catalogue_name', $options) ? $options['catalogue_name'] : NULL; $addable_filter = array_key_exists('addable_filter', $options) ? $options['addable_filter'] : false; $compound_list = array_key_exists('compound_list', $options) ? $options['compound_list'] : false; $stripped_id = $compound_list ? preg_replace('#,.*$#', '', $id) : $id; if (is_null($catalogue_name)) { $tree = array(); $catalogues = $GLOBALS['SITE_DB']->query_select('catalogues', array('c_name')); foreach ($catalogues as $catalogue) { $tree = array_merge($tree, get_catalogue_category_tree($catalogue['c_name'], is_null($id) ? NULL : intval($id), NULL, NULL, 1, $addable_filter, $compound_list)); } } else { $tree = get_catalogue_category_tree($catalogue_name, is_null($id) ? NULL : intval($id), NULL, NULL, 1, $addable_filter, $compound_list); } if (!has_actual_page_access(NULL, 'catalogues')) { $tree = array(); } $out = ''; foreach ($tree as $t) { if ($compound_list) { $_id = $t['compound_list']; } else { $_id = strval($t['id']); } if ($stripped_id === strval($t['id'])) { continue; } // Possible when we look under as a root $title = $t['title']; $has_children = $t['child_count'] != 0; $selectable = $addable_filter !== true || $t['addable']; $tag = 'category'; // category $out .= '<' . $tag . ' id="' . $_id . '" title="' . xmlentities($title) . '" has_children="' . ($has_children ? 'true' : 'false') . '" selectable="' . ($selectable ? 'true' : 'false') . '"></' . $tag . '>'; } // Mark parent cats for pre-expansion if (!is_null($default) && $default != '') { $cat = intval($default); while (!is_null($cat)) { $out .= '<expand>' . strval($cat) . '</expand>'; $cat = $GLOBALS['SITE_DB']->query_value_null_ok('catalogue_categories', 'cc_parent_id', array('id' => $cat)); } } $tag = 'result'; // result return '<' . $tag . '>' . $out . '</' . $tag . '>'; }
/** * 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('catalogues'); $name = substr($notification_code, strlen('catalogue_entry__')); if (is_null($id)) { $total = $GLOBALS['SITE_DB']->query_value_null_ok('catalogue_categories', 'COUNT(*)', array('c_name' => $name)); if ($total > 300) { return parent::create_category_tree($notification_code, $id); } // Too many, so just allow removing UI } $pagelinks = get_catalogue_category_tree($name, is_null($id) ? NULL : intval($id), NULL, NULL, 1); $filtered = array(); foreach ($pagelinks as $p) { if (strval($p['id']) !== $id) { $filtered[] = $p; } } return $filtered; }
/** * Get a list of maps containing all the subcategories, and path information, of the specified category - and those beneath it, recursively. * * @param ID_TEXT The catalogue name * @param ?AUTO_LINK The category being at the root of our recursion (NULL: true root category) * @param ?tempcode The tree up to this point in the recursion (NULL: blank, as we are starting the recursion) * @param ?string The category name of the $category_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) * @param boolean Whether to only show for what may be added to by the current member * @param boolean Whether to make the list elements store comma-separated child lists instead of IDs * @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), and 'count' (the number of entries in the category). */ function get_catalogue_category_tree($catalogue_name, $category_id, $tree = NULL, $title = NULL, $levels = NULL, $addable_filter = false, $use_compound_list = false) { if (!$use_compound_list) { if ($levels == -1) { return array(); } } if (!has_category_access(get_member(), 'catalogues_catalogue', $catalogue_name)) { return array(); } if (!is_null($category_id) && get_value('disable_cat_cat_perms') !== '1' && !has_category_access(get_member(), 'catalogues_category', strval($category_id))) { return array(); } if (is_null($tree)) { $tree = new ocp_tempcode(); } // Put our title onto our tree if (is_null($title)) { if (is_null($category_id)) { $_title = $GLOBALS['SITE_DB']->query_value_null_ok('catalogue_categories', 'cc_title', array('id' => $category_id)); } else { $_title = $GLOBALS['SITE_DB']->query_value('catalogue_categories', 'cc_title', array('id' => $category_id)); } $title = is_null($_title) ? do_lang('HOME') : get_translated_text($_title); } $tree->attach($title); // We'll be putting all children in this entire tree into a single list $children = array(); $is_tree = $GLOBALS['SITE_DB']->query_value_null_ok('catalogues', 'c_is_tree', array('c_name' => $catalogue_name)); if (is_null($is_tree)) { warn_exit(do_lang_tempcode('_MISSING_RESOURCE', 'catalogue:' . escape_html($catalogue_name))); } if (!is_null($category_id)) { $children[0]['id'] = $category_id; $children[0]['title'] = $title; $children[0]['tree'] = $tree; $children[0]['compound_list'] = strval($category_id) . ','; $children[0]['count'] = $GLOBALS['SITE_DB']->query_value('catalogue_entries', 'COUNT(*)', array('cc_id' => $category_id)); if ($addable_filter) { $children[0]['addable'] = has_submit_permission('mid', get_member(), get_ip_address(), 'cms_catalogues', array('catalogues_catalogue', $catalogue_name, 'catalogues_category', $category_id)); } } // Children of this category $tree2 = new ocp_tempcode(); $tree2->attach($tree); $tree2->attach(do_template('BREADCRUMB')); $rows = $GLOBALS['SITE_DB']->query_select('catalogue_categories', array('id', 'cc_title'), array('c_name' => $catalogue_name, 'cc_parent_id' => $category_id), 'ORDER BY id DESC', 300); foreach ($rows as $i => $child) { $rows[$i]['text_original'] = get_translated_text($child['cc_title']); } if (get_page_name() == 'cms_catalogues') { if (count($rows) == 300) { attach_message(do_lang_tempcode('TOO_MUCH_CHOOSE__RECENT_ONLY', escape_html(integer_format(300))), 'warn'); } } global $M_SORT_KEY; $M_SORT_KEY = 'text_original'; usort($rows, 'multi_sort'); $no_root = !array_key_exists(0, $children); if (!$no_root) { $children[0]['child_count'] = count($rows); } if ($levels !== 0 || $use_compound_list) { foreach ($rows as $child) { $child_id = $child['id']; $child_title = $child['text_original']; $child_tree = new ocp_tempcode(); $child_tree->attach($tree2); $child_children = get_catalogue_category_tree($catalogue_name, $child_id, $child_tree, $child_title, is_null($levels) ? NULL : $levels - 1, $addable_filter, $use_compound_list); if ($child_children != array()) { if ($use_compound_list) { list($child_children, $_compound_list) = $child_children; if (!$no_root) { $children[0]['compound_list'] .= $_compound_list; } } if ($levels !== 0) { $children = array_merge($children, $child_children); } } } } return $use_compound_list ? array($children, $no_root ? '' : $children[0]['compound_list']) : $children; }
/** * Standard modular page-link finder function (does not return the main entry-points that are not inside the tree). * * @param ?integer The number of tree levels to computer (NULL: no limit) * @param boolean Whether to not return stuff that does not support permissions (unless it is underneath something that does). * @param ?string Position to start at in the tree. Does not need to be respected. (NULL: from root) * @param boolean Whether to avoid returning categories. * @return ?array A tuple: 1) full tree structure [made up of (pagelink, permission-module, permissions-id, title, children, ?entry point for the children, ?children permission module, ?whether there are children) OR a list of maps from a get_* function] 2) permissions-page 3) optional base entry-point for the tree 4) optional permission-module 5) optional permissions-id (NULL: disabled). */ function get_page_links($max_depth = NULL, $require_permission_support = false, $start_at = NULL, $dont_care_about_categories = false) { $permission_page = 'cms_catalogues'; require_code('catalogues'); require_lang('catalogues'); $category_id = NULL; if (!is_null($start_at)) { $matches = array(); if (preg_match('#[^:]*:catalogues:type=index:id=(.*)#', $start_at, $matches) != 0) { $category_id = NULL; $adjusted_max_depth = is_null($max_depth) ? NULL : (is_null($category_id) ? $max_depth : $max_depth); $children = $dont_care_about_categories ? array() : get_catalogue_category_tree($matches[1], $category_id, NULL, NULL, $adjusted_max_depth, false); return array($children, $permission_page, '_SELF:_SELF:type=category:id=!', 'catalogues_category'); } elseif (preg_match('#[^:]*:catalogues:type=category:id=(.*)#', $start_at, $matches) != 0) { $category_id = $matches[1] == '' ? NULL : intval($matches[1]); $adjusted_max_depth = is_null($max_depth) ? NULL : (is_null($category_id) ? $max_depth : $max_depth); $catalogue_name = $GLOBALS['SITE_DB']->query_value('catalogue_categories', 'c_name', array('id' => $category_id)); $children = $dont_care_about_categories ? array() : get_catalogue_category_tree($catalogue_name, $category_id, NULL, NULL, $adjusted_max_depth, false); return array($children, $permission_page, '_SELF:_SELF:type=category:id=!', 'catalogues_category'); } } $children = array(); if ($dont_care_about_categories) { $rows = array(); } else { $query = 'SELECT c.c_title,c.c_name,t.text_original FROM ' . get_table_prefix() . 'catalogues c LEFT JOIN ' . $GLOBALS['SITE_DB']->get_table_prefix() . 'translate t ON ' . db_string_equal_to('language', user_lang()) . ' AND c.c_title=t.id'; // if (db_has_subqueries($GLOBALS['SITE_DB']->connection_read)) Actually we want empty ones in site trees // $query.=' WHERE EXISTS (SELECT * FROM '.get_table_prefix().'catalogue_entries e WHERE e.c_name=c.c_name)'; $rows = $GLOBALS['SITE_DB']->query($query); } foreach ($rows as $row) { if (substr($row['c_name'], 0, 1) == '_') { continue; } if (is_null($row['text_original'])) { $row['text_original'] = get_translated_text($row['c_title']); } $kids = array(); if (!is_null($max_depth) || $max_depth > 1) { $adjusted_max_depth = is_null($max_depth) ? NULL : (is_null($category_id) ? $max_depth - 2 : $max_depth - 1); $kids = get_catalogue_category_tree($row['c_name'], $category_id, NULL, NULL, $adjusted_max_depth, false); } $children[] = array('_SELF:_SELF:type=index:id=' . $row['c_name'], 'catalogues_catalogue', $row['c_name'], $row['text_original'], $kids, '_SELF:_SELF:type=category:id=!', 'catalogues_category', true); if (!$require_permission_support) { $children[] = array('_SELF:_SELF:type=atoz:catalogue_name=' . $row['c_name'], 'catalogues_catalogue', $row['c_name'], do_lang('DEFAULT__CATALOGUE_CATEGORY_ATOZ', $row['text_original'])); } } return array($children, $permission_page, '_SELF:_SELF:type=misc:id=!'); }