Source of file Section.php
Size: 9,674 Bytes - Last Modified: 2021-12-23T10:10:42+00:00
/var/www/docs.ssmods.com/process/src/code/models/Section.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 | <?php /** * Creates a base object to be extended upon. This section is not usable by itself. * * @package silverstripe * @subpackage sections */ class Section extends DataObject implements PermissionProvider { private static $title = "Section"; private static $description = ""; /** * Singular name for CMS * @var string */ private static $singular_name = 'Section'; /** * Plural name for CMS * @var string */ private static $plural_name = 'Sections'; /** * Database fields * @var array */ private static $db = array( 'UniqueConfigTitle' => 'Varchar(100)', // store the original AdminTitle determined by config here for filtering 'AdminTitle' => 'Varchar(100)', 'MenuTitle' => 'Varchar(30)', 'Public' => 'Boolean', 'Style' => 'Text', "ShowInMenus" => "Boolean" ); private static $defaults = array( "ShowInMenus" => 1, "Public" => 1 ); /** * Belongs_many_many relationship * @var array */ private static $belongs_many_many = array( 'Pages' => 'Page' ); private static $base_class = 'section'; /** * @var SectionController */ protected $controller; /** * CMS Fields * @return FieldList */ public function getCMSFields() { $fields = $this->scaffoldFormFields( array( // Don't allow has_many/many_many relationship editing before the record is first saved 'includeRelations' => ($this->ID > 0), 'tabbed' => true, 'ajaxSafe' => true ) ); $fields->removeByName('MenuTitle'); $fields->addFieldsToTab( 'Root.Main', array( HiddenField::create('UniqueConfigTitle'), TextField::create( 'AdminTitle', 'Admin title' ) ->setDescription('This field is for adminisration use only and will not display on the site.'), CheckboxField::create( 'ShowInMenus', 'Show in menus', 0 ), DisplayLogicWrapper::create( TextField::create( 'MenuTitle', 'Navigation label' ) ) ->displayIf("ShowInMenus")->isChecked()->end() ) ); $fields->addFieldsToTab( 'Root.Settings', array( CheckboxField::create( 'Public', 'Public', 1 ) ->setDescription('Is this section publicly accessible?.'), DropdownField::create( 'Style', 'Select a style', $this->ConfigStyles ) ->setEmptyString('Default') ) ); $this->extend('updateCMSFields', $fields); return $fields; } /** * Summary Fields * @return array */ public static $summary_fields = array( 'AdminTitle' => 'Title', 'Type' => 'Type' ); /** * Searchable Fields * @return array */ public static $searchable_fields = array( 'AdminTitle', 'MenuTitle' ); /** * Permissions * @return array */ public function providePermissions() { return array( "VIEW_SECTIONS" => array( 'name' => 'View any sections', 'help' => 'Allow users to view any sections on a page', 'category' => 'Sections', 'sort' => 88 ), "CREATE_SECTIONS" => array( 'name' => 'Create sections', 'help' => 'Allow users to create new sections on pages', 'category' => 'Sections', 'sort' => 90 ), "EDIT_SECTIONS" => array( 'name' => 'Edit sections', 'help' => 'Allow users to edit existing sections', 'category' => 'Sections', 'sort' => 92 ), "DELETE_SECTIONS" => array( 'name' => 'Delete sections', 'help' => 'Allow users to delete sections from pages', 'category' => 'Sections', 'sort' => 94 ), "LINK_SECTIONS" => array( 'name' => 'Link existing sections', 'help' => 'Allow users to link existing sections to a page', 'category' => 'Sections', 'sort' => 96 ), "REORDER_SECTIONS" => array( 'name' => 'Change order of sections', 'help' => 'Allow users to change the order of existing sections on a page', 'category' => 'Sections', 'sort' => 100 ) ); } /** * Viewing Permissions * @return boolean */ public function canView($member = null) { return Permission::check('VIEW_SECTIONS', 'any', $member); } /** * Editing Permissions * @return boolean */ public function canEdit($member = null) { return Permission::check('EDIT_SECTIONS', 'any', $member); } /** * Deleting Permissions * @return boolean */ public function canDelete($member = null) { if ($this->UniqueConfigTitle){ return false; } return Permission::check('DELETE_SECTIONS', 'any', $member); } /** * Creating Permissions * @return boolean */ public function canCreate($member = null) { return Permission::check('CREATE_SECTIONS', 'any', $member); } public function getConfigStyles(){ $config_styles = Config::inst()->get(get_called_class(), 'styles'); if ($config_styles) { foreach ($config_styles as $value) { $styles[$value] = preg_replace('/([a-z]+)([A-Z0-9])/', '$1 $2', $value); } return $styles; } return array(); } public static function Type($ClassName = NULL){ if (!$ClassName) { $ClassName = get_called_class(); } $niceTitle = Config::inst()->get($ClassName,'title'); if ($niceTitle) { return $niceTitle; } return trim(preg_replace('/([A-Z])/', ' $1', str_ireplace('Section', '', $ClassName))); } /** * Applies anchor to section in template. * * @return string $classes */ public function Anchor(){ if ($this->MenuTitle && $this->ShowInMenus) { return strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-',$this->MenuTitle), '-')); } return false; } /** * Applies anchor to section in template. * * @return string $classes */ public function AnchorAttr(){ if ($this->Anchor()) { return ' id="'.$this->Anchor().'"'; } return false; } /** * Applies classes to section in template. * * @return string $classes */ public function Classes(){ $classes = array($this->config()->get('base_class')); if ($this->Style) { $classes[] = strtolower($this->Style).'-'.strtolower(preg_replace('/([a-z]+)([A-Z0-9])/', '$1-$2', get_called_class())); }else{ $classes[] = strtolower(preg_replace('/([a-z]+)([A-Z0-9])/', '$1-$2', get_called_class())); } return implode(' ',$classes); } /** * Applies classes to section in template. * * @return string $classes */ public function ClassAttr(){ return 'class="'.$this->Classes().'"'; } public function Render(){ $page = Director::get_current_page(); $styleType = ($this->Style ? '_'.$this->Style : ''); $pageType = ($page->ClassName ? '_'.$page->ClassName : ''); $sectionType = get_called_class(); return array( $sectionType.$pageType.$styleType, $sectionType.$styleType, $sectionType.$pageType, $sectionType, 'DefaultSection' ); } /** * Access current page scope from section templates with $CurrentPage * * @return Controller */ public function getCurrentPage() { return Controller::curr(); } public function Layout() { $member = Member::currentUser(); $access = Permission::checkMember($member, 'CMS_ACCESS'); if($this->Public || $access){ return $this->renderWith($this->Render()); } } public function getController(){ if ($this->controller) { return $this->controller; } foreach (array_reverse(ClassInfo::ancestry($this->class)) as $sectionClass) { $controllerClass = "{$sectionClass}_Controller"; if (class_exists($controllerClass)) { break; } } if (!class_exists($controllerClass)) { throw new Exception("Could not find controller class for $this->classname"); } $this->controller = Injector::inst()->create($controllerClass, $this); return $this->controller; } public function GridFieldRowClasses(){ if ($this->UniqueConfigTitle){ return array('preset'); } return array(); } } |