false, 'mode' => 'exclude', 'properties' => array('title' => 'menuTitle', 'url' => 'url'), 'excludeList' => array(array(array('isRoot' => 'y'))), ); protected static $_currentPage = CURRENT_PAGE_FINAL; public static function isEnabled() { if (null === Qs_Navigation::$_isEnabled) { Qs_Navigation::$_isEnabled = Qs_Navigation::_getConfig('enabled'); } return Qs_Navigation::$_isEnabled; } public static function clean() { $nav = Qs_Navigation::$_navigation; Qs_Navigation::$_navigation = null; return $nav; } /** * Allows to show the breadcrumb navigation on the page * * @return bool Old Value */ public static function enable() { $oldValue = Qs_Navigation::$_isEnabled; Qs_Navigation::$_isEnabled = true; return $oldValue; } /** * Disallows to show the breadcrumb navigation on the page * * @return bool Old Value */ public static function disable() { $oldValue = Qs_Navigation::$_isEnabled; Qs_Navigation::$_isEnabled = false; return $oldValue; } /** * @param string $fullAlias */ public static function setCurrentPage($fullAlias) { self::$_currentPage = $fullAlias; } /** * Set navigation that will be displayed on the page, * Optional param $enable allows/disallows to show navigation on the page * * @param array $nav * @param bool $enable * @return bool * @throws Qs_Exception */ public static function set($nav, $enable = null) { if (!empty($nav) && is_array($nav)) { $nav = (is_array(reset($nav))) ? $nav : array($nav); $oldNav = Qs_Navigation::$_navigation; Qs_Navigation::$_navigation = $nav; if (null !== $enable) { if (is_bool($enable)) { Qs_Navigation::$_isEnabled = $enable; } else { throw new Qs_Exception('Invalid $enable parameter given in Qs_Navigation'); } } return $oldNav; } else { throw new Qs_Exception('Invalid $nav parameter given in Qs_Navigation'); } } public static function add(array $nav) { if (empty(Qs_Navigation::$_navigation)) { Qs_Navigation::$_navigation = []; } Qs_Navigation::$_navigation[] = $nav; } public static function unshift(array $nav) { if (empty(Qs_Navigation::$_navigation)) { Qs_Navigation::$_navigation = []; } array_unshift(Qs_Navigation::$_navigation, $nav); } /** * Returns breadcrumbs for current page (including config rules, changes made by * function Qs_Navigation::set(), Qs_Navigation::append()). * Parameter "$properties" sets page properties that will be returned (default: 'title'=>'menuTitle', 'url'=>'url'). * * @param array $properties * @return array * @throws Qs_Exception */ public static function get($properties = null) { if (null !== $properties && !is_array($properties)) { throw new Qs_Exception('Invalid $properties parameter given in Qs_Navigation'); } if (null !== Qs_Navigation::$_navigation) { $nav = Qs_Navigation::$_navigation; } else { $nav = Qs_Navigation::build(); Qs_Navigation::$_navigation = $nav; } if (null !== $properties && null !== $nav) { foreach ($nav as $key => $page) { $nav[$key] = Qs_Navigation::_processPage($page, $properties); } } return $nav; } /** * Appends/Prepends $breadcrumbs to the $target (if given), or Qs_Navigation::get(). * Returns navigation. * Function does not affect the output of navigation on the page. Use Qs_Navigation::append() instead. * * @param array $breadcrumbs * @param string $mode * @param array $target * @return array * @throws Qs_Exception */ public static function getAppend($breadcrumbs, $mode = 'append', $target = null) { if (!is_array($breadcrumbs)) { throw new Qs_Exception('Invalid $breadcrumbs parameter in Qs_Navigation'); } if (null == $target) { $target = Qs_Navigation::get(); if (null === $target) { return null; } } elseif (!is_array($target)) { throw new Qs_Exception('Invalid $target parameter given in Qs_Navigation'); } $breadcrumbs = (is_array(reset($breadcrumbs))) ? $breadcrumbs : array($breadcrumbs); if ('append' == $mode) { foreach ($breadcrumbs as $item) { array_push($target, $item); } } else { $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs as $item) { array_unshift($target, $item); } } return $target; } /** * Appends/Prepends $breadcrumbs to the default navigation and retuns it * * @param array $breadcrumbs * @param string $mode * @return array */ public static function append($breadcrumbs, $mode = 'append') { Qs_Navigation::$_navigation = Qs_Navigation::getAppend($breadcrumbs, $mode); return Qs_Navigation::$_navigation; } /** * Returns breadcrumb navigation based on SiteMap and Config rules * * @return array * @throws Qs_Exception */ public static function build() { if (null !== Qs_Navigation::$_buildNavigation) { return Qs_Navigation::$_buildNavigation; } $config = Qs_Navigation::_getConfig(); $mode = $config['mode']; if (!isset($config['mode']) || !method_exists('Qs_Navigation', '_check' . ucfirst($mode) . 'Rules') || !isset($config['properties']) || !is_array($config['properties']) || (isset($config['prepend']) && !is_array($config['prepend'])) || (isset($config['append']) && !is_array($config['append'])) || !isset($config[$mode.'List']) ) { throw new Qs_Exception('Invalid config given in Qs_Navigation::getNavigation()'); } $navigation = null; if (call_user_func(array('Qs_Navigation', '_check' . ucfirst($mode).'Rules'), $config[$mode . 'List'])) { $navigation = Qs_Navigation::_getDefault($config['properties']); if (isset($config['prepend'])) { $navigation = Qs_Navigation::getAppend($config['prepend'], 'prepend', $navigation); } if (isset($config['append'])) { $navigation = Qs_Navigation::getAppend($config['append'], 'append', $navigation); } } Qs_Navigation::$_buildNavigation = (empty($navigation)) ? null : $navigation; return Qs_Navigation::$_buildNavigation; } protected static function _isBackEndArea() { return (0 === strpos(self::$_currentPage, 'admin/')); } /** * Returns default breadcrumbs navigation based on SiteMap (not including config rules) * * @param array $properties * @return array */ protected static function _getDefault($properties = array()) { if (null !== Qs_Navigation::$_defaultNavigation && (empty($properties) || ($properties == Qs_Navigation::_getConfig('properties')))) { return Qs_Navigation::$_defaultNavigation; } $nav = $tree = array(); $subUrl = self::$_currentPage; $properties = (empty($properties)) ? Qs_Navigation::_getConfig('properties') : $properties; $parts = explode('/', trim($subUrl, '/')); if (Qs_Navigation::_isBackEndArea()) { $tree = Qs_SiteMap::getBackendSiteMapFull(); } else { $tree = Qs_SiteMap::getFromDb(); $firstPage = reset($tree); if ('y' != $firstPage['isRoot'] && !in_array(reset($parts), array('', $firstPage['alias'])) && null !== ($sub = Qs_Navigation::_processPage($firstPage, $properties)) ) { $nav[] = $sub; } } foreach ($parts as $alias) { if (array_key_exists($alias, $tree)) { $nav[] = Qs_Navigation::_processPage($tree[$alias], $properties); if (isset($tree[$alias]['sub'])) { $tree = $tree[$alias]['sub']; } else { break; } } else { break; } } if (empty($properties) || ($properties == Qs_Navigation::_getConfig('properties'))) { Qs_Navigation::$_defaultNavigation = (empty($nav)) ? null : $nav; } return (empty($nav)) ? null : $nav; } protected static function _checkIncludeRules($includeList) { $page = Qs_SiteMap::getPage(CURRENT_PAGE); if (!is_array($page) || empty($page)) { return false; } return self::checkRule($page, $includeList); } protected static function _checkExcludeRules($excludeList) { $page = Qs_SiteMap::getPage(CURRENT_PAGE); if (!is_array($page) || empty($page)) { return true; } return !self::checkRule($page, $excludeList); } public static function checkRule($page, $rules) { $rules = (is_array($rules)) ? $rules : array($rules); foreach ($rules as $rule) { if (is_int($rule)) { if ($rule == $page['id']) { return true; } } elseif (is_string($rule)) { if ($rule == $page['fullAlias']) { return true; } } elseif (is_array($rule)) { if (true == Qs_Navigation::_checkPageQuery($page, $rule)) { return true; } } else { throw new Qs_Exception('Invalid $excludeList parameter given in Qs_Navigation'); } } return false; } public static function _processPage($page, $properties) { if (!isset($page) || !is_array($page)) { return null; } $part = array(); foreach ($properties as $name => $prop) { $name = (is_string($name)) ? $name : $prop; if (isset($page[$prop])) { $part[$name] = $page[$prop]; } } // якщо елемент редірект на сторінку на якій ми знаходимось, то не виводимо лінк для цього елемента if ($page['redirect'] == 'y' && $page['redirectType'] == 'page') { $doc = Zend_Registry::get('doc'); if ($page['redirectPageId'] == $doc->getOption('id')) { $part['url'] = ''; } } return $part; } protected static function _getConfig($field = null) { if (null === Qs_Navigation::$_config) { $config = Qs_Config::get(basename(__FILE__, '.php'), Qs_Config::QS_TYPE)->toArray(); if (empty($config)) { throw new Qs_Exception('config is not found'); } $configPath = (Qs_Navigation::_isBackEndArea()) ? 'admin' : NULL; Qs_Navigation::$_config = Qs_Array::get($config, $configPath, Qs_Navigation::$_defaultConfig); } return (null === $field) ? Qs_Navigation::$_config : Qs_Array::get(Qs_Navigation::$_config, $field); } protected static function _checkPageQuery($page, $rule) { if (!$rule) { return true; } $_page = array_shift($rule) or $_page = array(); $_item = array_shift($rule) or $_item = array(); $_config = array_shift($rule) or $_config = array(); $result = true; if ($_page) { $diff = array_diff_assoc($_page, $page); if ($diff) { return false; } } if ($_item) { if (!isset($page['items'])) { $page['items'] = array(); } foreach ($page['items'] as &$item) { $diff = array_diff_assoc($_item, $item); if (!$diff) { if ($_config) { $config = Qs_Array::get($item, 'config', array()); $diff = array_diff_assoc($_config, $config); if (!$diff) { return true; } } } } } else { return true; } return false; } }