Source of file DataObjectNode.php
Size: 3,698 Bytes - Last Modified: 2021-12-23T10:07:53+00:00
/var/www/docs.ssmods.com/process/src/code/model/DataObjectNode.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 | <?php /** * Subclass of ArrayData that has some site specific functionality * * @author marcus */ class DataObjectNode extends ArrayData { /** * @var SiteDataService */ protected $siteData; public function __construct($value, SiteDataService $siteData) { if (!isset($value['MenuTitle']) || strlen($value['MenuTitle']) == 0 && strlen($value['MenuTitle'])) { if (isset($value['Title'])) { $value['MenuTitle'] = $value['Title']; } } parent::__construct($value); $this->siteData = $siteData; } public function Children() { $kids = ArrayList::create(); if (isset($this->array['kids'])) { foreach ($this->array['kids'] as $id) { $kid = $this->siteData->getItem($id); if ($kid && $kid->ShowInMenus) { $kids->push($kid); } } } $kids = $kids->sort('Sort ASC'); return $kids; } public function getAncestors() { $ancestors = new ArrayList(); $object = $this; while($object = $object->getParent()) { $ancestors->push($object); } return $ancestors; } public function getParent() { return $this->ParentID ? $this->siteData->getItem($this->ParentID) : null; } /** * Returns true if this is the currently active page being used to handle this request. * * @return bool */ public function isCurrent() { return $this->ID ? $this->ID == Director::get_current_page()->ID : false; } /** * Check if this page is in the currently active section (e.g. it is either current or one of its children is * currently being viewed). * * @return bool */ public function isSection() { if ($this->isCurrent()) { return true; } $ancestors = $this->getAncestors(); if (Director::get_current_page() instanceof Page) { $node = Director::get_current_page()->asMenuItem(); if ($node) { $ancestors = $node->getAncestors(); return $ancestors && in_array($this->ID, $node->getAncestors()->column()); } } return false; } /** * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible * to external users. * * @return bool */ public function isOrphaned() { // Always false for root pages if(empty($this->ParentID)) return false; // Parent must exist and not be an orphan itself $parent = $this->getParent(); return !$parent || !$parent->ID || $parent->isOrphaned(); } /** * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. * * @return string */ public function LinkOrCurrent() { return $this->isCurrent() ? 'current' : 'link'; } /** * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. * * @return string */ public function LinkOrSection() { return $this->isSection() ? 'section' : 'link'; } /** * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page * but in the current section. * * @return string */ public function LinkingMode() { if($this->isCurrent()) { return 'current'; } elseif($this->isSection()) { return 'section'; } else { return 'link'; } } /** * Check if this page is in the given current section. * * @param string $sectionName Name of the section to check * @return bool True if we are in the given section */ public function InSection($sectionName) { $page = Director::get_current_page(); while($page) { if($sectionName == $page->URLSegment) return true; $page = $page->Parent; } return false; } } |