Source of file SortableMenuExtension.php
Size: 5,786 Bytes - Last Modified: 2021-12-23T10:21:02+00:00
/var/www/docs.ssmods.com/process/src/src/SortableMenuExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 | <?php namespace Symbiote\SortableMenu; use Page; use Symbiote\SortableMenu\SortableMenuExtensionException; use SilverStripe\ORM\FieldType\DBBoolean; use SilverStripe\ORM\DataList; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\CheckboxField; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataObject; use SilverStripe\CMS\Model\SiteTree; use Symbiote\Multisites\Model\Site; class SortableMenuExtension extends DataExtension { private static $menus = array(); protected static $_cached_cache_max_lastedited = array(); protected static $inConfigCall = false; public function __construct() { parent::__construct(); } public static function get_extra_config($class, $extension, $args) { if (self::$inConfigCall) { throw new SortableMenuException(__FUNCTION__.': Recursion error.'); } // NOTE(Jake): If 'get_extra_config' is really aiming to be deprecated // post 3.2+, then this logic can moved to a CompositeDBField // and the setup method can migrate to that. $menus = static::get_sortable_menu_configuration(); $dbFields = array(); foreach ($menus as $fieldName => $extraInfo) { $dbFields[$fieldName] = DBBoolean::class; $dbFields[$extraInfo['Sort']] = 'Int'; } $result = array( 'db' => $dbFields, ); return $result; } /** * @return DataList */ public function SortableMenu($fieldName) { return $this->SortableMenuUncached($fieldName); } /** * @return string */ public function SortableMenuCacheKey($cacheKey) { if (!$cacheKey) { throw new SortableMenuException(__FUNCTION__.': Cannot have empty $cacheKey parameter.'); } $baseClass = $this->getOwnerBaseclass(); if (!isset(self::$_cached_cache_max_lastedited[$baseClass])) { // Reuse the max('LastEdited') / count() values for the class if // already queried. (Can shave off upto ~0.0010s per sortable menu) $list = DataList::create($baseClass); self::$_cached_cache_max_lastedited[$baseClass] = implode('_', array( $list->max('LastEdited'), (int)$list->count(), )); } // Add SiteID to cache key so each Site has a seperate cache key if (class_exists(Site::class)) { $record = $this->owner; $siteID = 0; if ($record instanceof Site) { $siteID = $record->ID; } else { $siteID = $record->SiteID; } $cacheKey .= '_'.$siteID; } return $cacheKey.'_'.self::$_cached_cache_max_lastedited[$baseClass]; } /** * @return DataList */ public function SortableMenuUncached($fieldName) { $menus = $this->getSortableMenuConfiguration(); if (!isset($menus[$fieldName])) { throw new SortableMenuException(__FUNCTION__.': "'.$fieldName.'" hasn\'t been configured.'); } $extraInfo = $menus[$fieldName]; $class = $this->getOwnerBaseclass(); $list = DataList::create($class)->filter(array( $fieldName => 1 ))->sort($extraInfo['Sort']); // Filter by SiteID if (class_exists(Site::class)) { $record = $this->owner; $siteID = 0; if ($record instanceof Site) { $siteID = $record->ID; } else { $siteID = $record->SiteID; } if ($siteID) { $list = $list->filter('SiteID', $siteID); } } // Store fieldname directly on list to use as a cache parameter $list->SortableMenuFieldName = $fieldName; return $list; } public function updateSettingsFields($fields) { $menus = Config::inst()->get(__CLASS__, 'menus'); if (!$menus) { $menus = array(); } foreach (Config::inst()->get(__CLASS__, 'menus') as $fieldName => $extraInfo) { $fieldTitle = $fieldName; if (isset($extraInfo['Title'])) { $fieldTitle = 'Show in "'.$extraInfo['Title'].'"?'; } $fields->insertAfter(CheckboxField::create($fieldName, $fieldTitle), 'ShowInMenus'); } } public function getSortableMenuConfiguration() { return static::get_sortable_menu_configuration(); } public static function get_sortable_menu_configuration() { self::$inConfigCall = true; $menus = Config::inst()->get(__CLASS__, 'menus'); self::$inConfigCall = false; if (!$menus) { $menus = array(); } $result = array(); foreach ($menus as $fieldName => $extraInfo) { if (!isset($extraInfo['Sort'])) { $extraInfo['Sort'] = 'Sort'.$fieldName; } if (!isset($extraInfo['Title'])) { $extraInfo['Title'] = $fieldName; } $result[$fieldName] = $extraInfo; } return $result; } /** * @return string */ private function getOwnerBaseclass() { $owner = $this->getOwner(); if ($owner instanceof Page) { return Page::class; } // Fallback to support SiteTree for BC reasons. // ie. upgrading a SS 3.X wherein this extension was applied to `SiteTree` if ($owner instanceof SiteTree) { return SiteTree::class; } $class = get_class($owner); $class = DataObject::getSchema()->baseDataClass($class); return $class; } } |