From ef9a147d31b4e55c8657d664916f6bc00846d7e1 Mon Sep 17 00:00:00 2001 From: alekseybobkov Date: Thu, 2 Oct 2014 15:55:55 -0700 Subject: [PATCH] The CMS pages are now supported by the Static Pages menus --- modules/cms/ServiceProvider.php | 18 ++++++++ modules/cms/classes/Page.php | 74 +++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/modules/cms/ServiceProvider.php b/modules/cms/ServiceProvider.php index 24f8dd16e..a59e25ff8 100644 --- a/modules/cms/ServiceProvider.php +++ b/modules/cms/ServiceProvider.php @@ -1,6 +1,7 @@ 'CMS Page ' + ]; + }); + + Event::listen('pages.menuitem.getTypeInfo', function($type) { + if ($type == 'cms-page') + return CmsPage::getMenuTypeInfo($type); + }); + + Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) { + if ($type == 'cms-page') + return CmsPage::resolveMenuItem($item, $url, $theme); + }); } } diff --git a/modules/cms/classes/Page.php b/modules/cms/classes/Page.php index 14856bc55..50e767432 100644 --- a/modules/cms/classes/Page.php +++ b/modules/cms/classes/Page.php @@ -90,6 +90,7 @@ class Page extends CmsCompoundObject /** * Helper that makes a URL for a page in the active theme. + * @param mixed $page Specifies the Cms Page file name. * @return string */ public static function url($page, $params = [], $absolute = true) @@ -105,4 +106,77 @@ class Page extends CmsCompoundObject return $controller->pageUrl($page, $params, true, $absolute); } + + /** + * Handler for the pages.menuitem.getTypeInfo event. + * Returns a menu item type information. The type information is returned as array + * with the following elements: + * - references - a list of the item type reference options. The options are returned in the + * ["key"] => "title" format for options that don't have sub-options, and in the format + * ["key"] => ["title"=>"Option title", "items"=>[...]] for options that have sub-options. Optional, + * required only if the menu item type requires references. + * - nesting - Boolean value indicating whether the item type supports nested items. Optional, + * false if omitted. + * - dynamicItems - Boolean value indicating whether the item type could generate new menu items. + * Optional, false if omitted. + * - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires a CMS page reference to + * resolve the item URL. + * @param string $type Specifies the menu item type + * @return array Returns an array + */ + public static function getMenuTypeInfo($type) + { + $result = []; + + if ($type == 'cms-page') { + $theme = Theme::getActiveTheme(); + $pages = self::listInTheme($theme, true); + + foreach ($pages as $page) + $references[$page->getBaseFileName()] = $page->title; + + $result = [ + 'references' => $references, + 'nesting' => false, + 'dynamicItems' => false + ]; + } + + return $result; + } + + /** + * Handler for the pages.menuitem.resolveItem event. + * Returns information about a menu item. The result is an array + * with the following keys: + * - url - the menu item URL. Not required for menu item types that return all available records. + * The URL should be returned relative to the website root and include the subdirectory, if any. + * Use the URL::to() helper to generate the URLs. + * - isActive - determines whether the menu item is active. Not required for menu item types that + * return all available records. + * - items - an array of arrays with the same keys (url, isActive, items) + the title key. + * The items array should be added only if the $item's $nesting property value is TRUE. + * @param \RainLab\Pages\Classes\MenuItem $item Specifies the menu item. + * @param \Cms\Classes\Theme $theme Specifies the current theme. + * @param string $url Specifies the current page URL, normalized, in lower case + * The URL is specified relative to the website root, it includes the subdirectory name, if any. + * @return mixed Returns an array. Returns null if the item cannot be resolved. + */ + public static function resolveMenuItem($item, $url, $theme) + { + $result = null; + + if ($item->type == 'cms-page') { + if (!$item->reference) + return; + + $pageUrl = self::url($item->reference); + + $result = []; + $result['url'] = $pageUrl; + $result['isActive'] = $pageUrl == $url; + } + + return $result; + } }