function show_menu2($aMenu = 0, $aStart = SM2_ROOT, $aMaxLevel = -1999, $aOptions = SM2_TRIM, $aItemOpen = false, $aItemClose = false, $aMenuOpen = false, $aMenuClose = false, $aTopItemOpen = false, $aTopMenuOpen = false) { global $wb; // extract the flags and set $aOptions to an array $flags = 0; if (is_int($aOptions)) { $flags = $aOptions; $aOptions = array(); } else { if (isset($aOptions['flags'])) { $flags = $aOptions['flags']; } else { $flags = SM2_TRIM; $aOptions = array(); @error_logs('show_menu2 error: $aOptions is invalid. No flags supplied!'); } } // ensure we have our group 1 flag, we don't check for the "exactly 1" part, but // we do ensure that they provide at least one. if (0 == ($flags & _SM2_GROUP_1)) { @error_logs('show_menu2 error: $aOptions is invalid. No flags from group 1 supplied!'); $flags |= SM2_TRIM; // default to TRIM } // search page results don't have any of the page data loaded by WB, so we load it // ourselves using the referrer ID as the current page $CURR_PAGE_ID = defined('REFERRER_ID') ? REFERRER_ID : PAGE_ID; if (count($wb->page) == 0 && defined('REFERRER_ID') && REFERRER_ID > 0) { global $database; $sql = 'SELECT * FROM `' . CAT_TABLE_PREFIX . 'pages` WHERE `page_id` = ' . REFERRER_ID . ''; $result = $database->query($sql); if ($result->numRows() == 1) { $wb->page = $result->fetchRow(); } unset($result); } // fix up the menu number to default to the menu number // of the current page if no menu has been supplied if ($aMenu == 0) { $aMenu = $wb->page['menu'] == '' ? 1 : $wb->page['menu']; } // Set some of the $wb->page[] settings to defaults if not set $pageLevel = $wb->page['level'] == '' ? 0 : $wb->page['level']; $pageParent = $wb->page['parent'] == '' ? 0 : $wb->page['parent']; // adjust the start level and start page ID as necessary to // handle the special values that can be passed in as $aStart $aStartLevel = 0; if ($aStart < SM2_ROOT) { // SM2_CURR+N if ($aStart == SM2_CURR) { $aStartLevel = $pageLevel; $aStart = $pageParent; } else { $aStartLevel = $pageLevel + $aStart - SM2_CURR; $aStart = $CURR_PAGE_ID; } } elseif ($aStart < 0) { // SM2_ROOT+N $aStartLevel = $aStart - SM2_ROOT; $aStart = 0; } // we get the menu data once and store it in a global variable. This allows // multiple calls to show_menu2 in a single page with only a single call to // the database. If this variable exists, then we have already retrieved all // of the information and processed it, so we don't need to do it again. if (($flags & SM2_NOCACHE) != 0 || !array_key_exists('show_menu2_data', $GLOBALS) || !array_key_exists($aMenu, $GLOBALS['show_menu2_data'])) { global $database; // create an array of all parents of the current page. As the page_trail // doesn't include the theoretical root element 0, we add it ourselves. $rgCurrParents = explode(",", '0,' . $wb->page['page_trail']); array_pop($rgCurrParents); // remove the current page $rgParent = array(); // if the caller wants all menus gathered together (e.g. for a sitemap) // then we don't limit our SQL query $menuLimitSql = ' AND `menu`=' . $aMenu; if ($aMenu == SM2_ALLMENU) { $menuLimitSql = ''; } // we only load the description and keywords if we have been told to, // this cuts the memory load for pages that don't use them. Note that if // we haven't been told to load these fields the *FIRST TIME* show_menu2 // is called (i.e. where the database is loaded) then the info won't // exist anyhow. if ($flags & SM2_ALLINFO) { $fields = '*'; } else { $fields = '`parent`,`page_id`,`menu_title`,`page_title`,`link`,`target`,'; $fields .= '`level`,`visibility`,`viewing_groups`,`viewing_users`'; } // we request all matching rows from the database for the menu that we // are about to create it is cheaper for us to get everything we need // from the database once and create the menu from memory then make // multiple calls to the database. $sql = 'SELECT ' . $fields . ' FROM `' . CAT_TABLE_PREFIX . 'pages` '; $sql .= 'WHERE ' . $wb->extra_where_sql . ' ' . $menuLimitSql . ' '; $sql .= 'ORDER BY `level` ASC, `position` ASC'; $sql = str_replace('hidden', 'IGNOREME', $sql); // we want the hidden pages $oRowset = $database->query($sql); if (is_object($oRowset) && $oRowset->numRows() > 0) { // create an in memory array of the database data based on the item's parent. // The array stores all elements in the correct display order. while ($page = $oRowset->fetchRow()) { // ignore all pages that the current user is not permitted to view // 1. hidden pages aren't shown unless they are on the current page if ($page['visibility'] == 'hidden') { $page['sm2_hide'] = true; } else { if (!CAT_Helper_Page::isActive($page['page_id']) && $page['link'] != $wb->default_link && !INTRO_PAGE) { continue; } else { if (!CAT_Helper_Page::isVisible($page['page_id']) && $page['visibility'] != 'registered') { continue; } else { if (!CAT_Helper_Page::isVisible($page['page_id']) && $page['visibility'] == 'registered') { continue; } } } } $idx = $page['parent']; // mark our current page as being on the current path if ($page['page_id'] == $CURR_PAGE_ID) { $page['sm2_is_curr'] = true; $page['sm2_on_curr_path'] = true; if ($flags & SM2_SHOWHIDDEN) { // show hidden pages if active and SHOWHIDDEN flag supplied unset($page['sm2_hide']); } } // mark parents of the current page as such if (in_array($page['page_id'], $rgCurrParents)) { $page['sm2_is_parent'] = true; $page['sm2_on_curr_path'] = true; if ($flags & SM2_SHOWHIDDEN) { // show hidden pages if active and SHOWHIDDEN flag supplied unset($page['sm2_hide']); // don't hide a parent page } } // add the entry to the array if (!isset($page['sm2_hide'])) { // ensure that we have an array entry in the table to add this to if (!array_key_exists($idx, $rgParent)) { $rgParent[$idx] = array(); } $rgParent[$idx][] = $page; } } } unset($oRowset); // mark all elements that are siblings of any element on the current path foreach ($rgCurrParents as $x) { if (array_key_exists($x, $rgParent)) { foreach (array_keys($rgParent[$x]) as $y) { $mark =& $rgParent[$x][$y]; $mark['sm2_path_sibling'] = true; unset($mark); } } } // mark all elements that have children and are siblings of the current page $parentId = $pageParent; foreach (array_keys($rgParent) as $x) { $childSet =& $rgParent[$x]; foreach (array_keys($childSet) as $y) { $mark =& $childSet[$y]; if (array_key_exists($mark['page_id'], $rgParent)) { $mark['sm2_has_child'] = true; } if ($mark['parent'] == $parentId && $mark['page_id'] != $CURR_PAGE_ID) { $mark['sm2_is_sibling'] = true; } unset($mark); } unset($childSet); } // mark all children of the current page. We don't do this when // $CURR_PAGE_ID is 0, as 0 is the parent of everything. // $CURR_PAGE_ID == 0 occurs on special pages like search results // when no referrer is available.s if ($CURR_PAGE_ID != 0) { sm2_mark_children($rgParent, $CURR_PAGE_ID, 1); } // store the complete processed menu data as a global. We don't // need to read this from the database anymore regardless of how // many menus are displayed on the same page. if (!array_key_exists('show_menu2_data', $GLOBALS)) { $GLOBALS['show_menu2_data'] = array(); } $GLOBALS['show_menu2_data'][$aMenu] =& $rgParent; unset($rgParent); } // adjust $aMaxLevel to the level number of the final level that // will be displayed. That is, we display all levels <= aMaxLevel. if ($aMaxLevel == SM2_ALL) { $aMaxLevel = 1000; } elseif ($aMaxLevel < 0) { // SM2_CURR+N $aMaxLevel += $pageLevel - SM2_CURR; } elseif ($aMaxLevel >= SM2_MAX) { // SM2_MAX+N $aMaxLevel += $aStartLevel - SM2_MAX; if ($aMaxLevel > $pageLevel) { $aMaxLevel = $pageLevel; } } else { // SM2_START+N $aMaxLevel += $aStartLevel - SM2_START; } // generate the menu $retval = false; if (array_key_exists($aStart, $GLOBALS['show_menu2_data'][$aMenu])) { $formatter = $aItemOpen; if (!is_object($aItemOpen)) { static $sm2formatter; if (!isset($sm2formatter)) { $sm2formatter = new SM2_Formatter(); } $formatter = $sm2formatter; $formatter->set($flags, $aItemOpen, $aItemClose, $aMenuOpen, $aMenuClose, $aTopItemOpen, $aTopMenuOpen); } // adjust the level until we show everything and ignore the SM2_TRIM flag. // Usually this will be less than the start level to disable it. $showAllLevel = $aStartLevel - 1; if (isset($aOptions['notrim'])) { $showAllLevel = $aStartLevel + $aOptions['notrim']; } // display the menu $formatter->initialize(); sm2_recurse($GLOBALS['show_menu2_data'][$aMenu], $aStart, $aStartLevel, $showAllLevel, $aMaxLevel, $flags, $formatter); $formatter->finalize(); // if we are returning something, get the data if (($flags & SM2_BUFFER) != 0) { $retval = $formatter->getOutput(); } } // clear the data if we aren't caching it if (($flags & SM2_NOCACHE) != 0) { unset($GLOBALS['show_menu2_data'][$aMenu]); } return $retval; }
public function show_page($page) { return CAT_Helper_Page::isVisible($page['page_id']); }