Source of file SidebarMenuWidget.php
Size: 3,666 Bytes - Last Modified: 2021-12-24T06:49:12+00:00
/var/www/docs.ssmods.com/process/src/code/SidebarMenuWidget.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 | <?php class SidebarMenuWidget extends Widget { private static $db = array( "MenuRoot" => "Enum(array('Root', 'Selected Page', 'Current Page', 'Root of Current Page'))", "ShowAll" => "Boolean" ); private static $has_one = array( "RootPage" => "SiteTree" ); private $currentRootPage = null; private $currentMenuItems = null; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->add($MenuRootField = DropdownField::Create( 'MenuRoot', 'Menu Root', singleton('SidebarMenuWidget')->dbObject('MenuRoot')->enumValues())); $MenuRootField->setDescription("Select from where the menu starts.<br/> <strong>Root</strong>: Show the entire page tree.<br/> <strong>Selected Page</strong>: Show the children of a specific page.<br/> <strong>Current Page</strong>: Show the children of the page currently being viewed.<br/> <strong>Root of Current Page</strong>: Show children of the current pages top most page.<br/>"); $fields->add($ShowAllField = CheckboxField::Create('ShowAll', 'Show All')); $ShowAllField->setDescription("Show all pages, even those who are marked as not to show in menus"); $fields->add($RootPageField = TreeDropdownField::Create("RootPageID", "Root Page", "SiteTree") ->setDescription("If <strong>MenuRoot</strong> is set to "Selected Page" set which page to start the menu from")); $RootPageField->displayIf("MenuRoot")->isEqualTo("Selected Page"); $fields->changeFieldOrder(array("WidgetName", "WidgetLabel", "Enabled", "MenuRoot", "RootPageID", "ShowAll")); return $fields; } /** * @var string */ private static $title = "Menu Widget"; /** * @var string */ private static $cmsTitle = "Menu Widget"; /** * @var string */ private static $description = ""; public function Title() { return $this->WidgetLabel; } public function Menu() { if (!$this->currentMenuItems) { if ($this->MenuRoot == 'Root') { if ($this->ShowAll) { $result = SiteTree::get()->filter(array("ParentID" => 0)); foreach ($result as $page) { if ($page->canView()) { $visible[] = $page; } } $this->currentMenuItems = new ArrayList($visible); } else { $this->currentMenuItems = Controller::curr()->Menu(1); } } else { $rootPage = $this->RootPage(); if ($rootPage) { $this->currentMenuItems = $this->ShowAll ? $rootPage->AllChildren() : $rootPage->Children(); } } } return $this->currentMenuItems; } public function RootPage() { if (!$this->currentRootPage) { $this->currentRootPage = Director::get_current_page(); if ($this->MenuRoot == 'Selected Page' && parent::RootPage()) { $this->currentRootPage = parent::RootPage(); } elseif ($this->MenuRoot == 'Root of Current Page' || $this->MenuRoot == 'Root') { while ($this->currentRootPage->exists() && ((int) $this->currentRootPage->ParentID) !== 0) { $this->currentRootPage = $this->currentRootPage->Parent(); } } } return $this->currentRootPage; } } |