Source of file ContentBlock.php
Size: 7,015 Bytes - Last Modified: 2021-12-24T06:43:27+00:00
/var/www/docs.ssmods.com/process/src/src/Model/ContentBlock.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 | <?php namespace CyberDuck\BlockPage\Model; use Page; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Forms\GridField\GridFieldVersionedState; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\Tab; use SilverStripe\Forms\TabSet; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\Permission; use SilverStripe\Security\PermissionProvider; use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\VersionedGridFieldItemRequest; class ContentBlock extends DataObject implements PermissionProvider { private static $table_name = 'ContentBlock'; private static $db = []; private static $belongs_many_many = [ 'Pages' => Page::class, ]; private static $owned_by = [ 'Pages' ]; private static $extensions = [ Versioned::class ]; private static $versioned_gridfield_extensions = true; private static $singular_name = 'Content Block'; private static $plural_name = 'Content Blocks'; private static $summary_fields = [ 'Thumbnail' => '', 'ID' => 'ID', 'ClassName' => 'Content type', 'Title' => 'Title', 'Pages.Count' => 'Pages' ]; public function searchableFields() { return [ 'ClassName' => [ 'filter' => 'ExactMatchFilter', 'title' => 'Content Type', 'field' => DropdownField::create('ClassName')->setSource(ContentBlock::get()->map('ClassName', 'ClassName'))->setEmptyString('-- Content Type --') ] ]; } /** * @return DBField */ public function getThumbnail() { return DBField::create_field('HTMLText', sprintf('<img src="%s" height="20">', $this->getPreviewImagePath())); } /** * @return string */ protected function getPreviewImagePath() { $previewImagePath = $this->config()->get('preview'); $this->extend('updatePreviewImagePath', $previewImagePath); return $previewImagePath; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('Pages'); if($this->getAction() == 'new') { return $this->getCMSSelectionFields($fields); } else { $editor = GridFieldConfig_RelationEditor::create(); $grid = new GridField('Pages', 'Pages', $this->Pages(), $editor); $grid->getConfig() ->removeComponentsByType(GridFieldAddNewButton::class); $fields->addFieldToTab('Root.Pages', $grid); } return $fields; } public function getTemplateHolder() { return $this->renderWith(['Block/ContentBlock_holder']); } public function getTemplate() { if($this->ClassName != ContentBlock::class) { return $this->renderWith('Block/'.$this->ClassName); } } public function getAction() { $path = explode('/', Controller::curr()->getRequest()->getURL()); return array_pop($path); } public function getBlockType() { $className = $this->owner->ClassName; return $className::config()->get('title'); } private function getCMSSelectionFields(FieldList $fields) { $fields->removeByName('Root'); // fields used in the inital selection request $session = Controller::curr()->getRequest()->getSession(); $fields->push(HiddenField::create('BlockRelationID')->setValue($session->get('BlockRelationID'))); $fields->push(HiddenField::create('BlockRelationClass')->setValue($session->get('BlockRelationClass'))); // create the selection tab and options $fields->push(TabSet::create('Root', Tab::create('Main'))); $rules = (array) Config::inst()->get(ContentBlock::class, 'restrict'); if(array_key_exists($session->get('BlockRelationClass'), $rules)) { $classes = $rules[$session->get('BlockRelationClass')]; } else { $classes = (array) Config::inst()->get(ContentBlock::class, 'blocks'); } $options = []; foreach($classes as $class) { $options[$class] = DBField::create_field('HTMLText', Controller::curr() ->customise([ 'Preview' => singleton($class)->getPreviewImagePath(), 'Title' => $class::config()->get('title'), 'Description' => $class::config()->get('description') ]) ->renderWith('/Includes/ContentBlockOption') ); } $checked = key(array_slice($options, 0, 1, true)); $fields->addFieldToTab('Root.Main', OptionsetField::create('ContentBlock', false, $options, $checked)); $this->extend('updateCMSSelectionFields', $fields); return $fields; } public function providePermissions() { return [ 'VIEW_CONTENT_BLOCKS' => [ 'name' => 'View content blocks', 'help' => 'Allow viewing page content blocks', 'category' => 'Page - Content Blocks', 'sort' => 100 ], 'CREATE_CONTENT_BLOCKS' => [ 'name' => 'Create content blocks', 'help' => 'Allow creating page content blocks', 'category' => 'Page - Content Blocks', 'sort' => 100 ], 'EDIT_CONTENT_BLOCKS' => [ 'name' => 'Edit content blocks', 'help' => 'Allow editing page content blocks', 'category' => 'Page - Content Blocks', 'sort' => 100 ], 'DELETE_CONTENT_BLOCKS' => [ 'name' => 'Delete content blocks', 'help' => 'Allow deleting page content blocks', 'category' => 'Page - Content Blocks', 'sort' => 100 ] ]; } public function canView($member = null, $context = []) { return Permission::check('VIEW_CONTENT_BLOCKS', 'any', $member); } public function canCreate($member = null, $context = []) { return Permission::check('CREATE_CONTENT_BLOCKS', 'any', $member); } public function canEdit($member = null, $context = []) { return Permission::check('EDIT_CONTENT_BLOCKS', 'any', $member); } public function canDelete($member = null, $context = []) { return Permission::check('DELETE_CONTENT_BLOCKS', 'any', $member); } } |