Source of file ChildPageListBlock.php
Size: 3,319 Bytes - Last Modified: 2021-12-23T10:25:20+00:00
/var/www/docs.ssmods.com/process/src/code/dataobjects/ChildPageListBlock.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | <?php /** * A Block which allows listing of child pages */ class ChildPageListBlock extends Block { private static $db = array( 'AbsoluteSource' => 'Boolean', 'PageTypes' => 'MultiValueField', 'ExcludeItems' => 'MultiValueField', 'EnforceShowInMenu' => 'Boolean' ); private static $has_one = array( 'Source' => 'SiteTree' ); public function getCMSFields(){ $fields = parent::getCMSFields(); $fields->removeByName('ExcludeItems'); $fields->removeByName('SourceID'); $pageClasses = SiteTree::page_type_classes(); $pageTypes = array(); foreach ($pageClasses as $class){ $pageTypes[$class] = singleton($class)->i18n_singular_name(); } $fields->addFieldsToTab( 'Root.Main', array( CheckboxField::create( 'EnforceShowInMenu', _t('ChildPageListBlock.EXCLUDEITEMSNOTINMENUS', "Exclude items which don't show in menus") ), MultiValueListField::create( 'PageTypes', _t('ChildPageListBock.ONLYINCLUDETYPES','Only include these page types'), $pageTypes )->setRightTitle('Leave blank to include all types'), OptionsetField::create( 'AbsoluteSource', _t('ChildPageListBock.SOURCEOPTION', 'Source option'), array( 0 => _t('ChildPageListBock.SOURCECURRENTPAGE', 'Current page being viewed'), 1 => _t('ChildPageListBock.SOURCESPECIFICPAGE', 'A specific page') ) ), TreeDropdownField::create( 'SourceID', _t('ChildPageListBock.SOURCEPAGE','Source page'), 'SiteTree' )->hideIf("AbsoluteSource")->isEqualTo(0)->end(), ) ); if ($this->AbsoluteSource){ $kids = $this->Source()->AllChildren(); if ($kids && $kids->Count()){ $fields->addFieldToTab( 'Root.Main', MultiValueListField::create( 'ExcludeItems', _t('ChildPageListBock.EXCLUDECHILDREN', 'Exclude these children'), $kids->map('ID', 'Title' ))->hideIf("AbsoluteSource")->isEqualTo(0)->end() ); } } return $fields; } public function onBeforeWrite(){ parent::onBeforeWrite(); if ($this->ExcludeItems){ if ($this->SourceID == 0){ $this->ExcludeItems = null; } else { $exclude = $this->ExcludeItems->getValues(); $ids = $exclude ? array_values($exclude) : null; $excludes = SiteTree::get() ->filter(array( 'ID' => $ids, 'ParentID' => (int)$this->SourceID )); $valids = array_intersect(($ids ? $ids : array()), $excludes->column('ID')); $this->ExcludeItems->setValue($valids); } } } public function getItems(){ $viewables = ArrayList::create(); $source = $this->AbsoluteSource ? $this->Source() : Controller::curr()->data(); $items = $this->EnforceShowInMenu ? $source->Children() : $source->AllChildren(); if (!$items || !$items->Count()){ return $viewables; } if ($this->PageTypes){ $types = $this->obj('PageTypes')->getValues(); if (count($types)){ $items = $items->filter(array('ClassName' => array_values($types))); } } if ($this->ExcludeItems && $this->AbsoluteSource && $items->Count()){ $excludes = $this->obj('ExcludeItems')->getValues(); if (count($excludes)){ $items = $items->exclude(array('ID' => array_values($excludes))); } } foreach ($items as $item){ if ($item->canView()){ $viewables->push($item); } } return $viewables; } } |