| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SheaDawson\Blocks\model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SheaDawson\Blocks\forms\GridFieldConfigBlockManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\ORM\ArrayLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\CMS\Model\SiteTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Security\PermissionProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Security\Permission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Security\Group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SilverStripe\Forms\TreeMultiselectField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SilverStripe\Forms\CheckboxField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * BlockSet. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Shea Dawson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class BlockSet extends DataObject implements PermissionProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private static $table_name = "BlockSet"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private static $db = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'Title' => 'Varchar(255)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'PageTypes' => 'MultiValueField', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'IncludePageParent' => 'Boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private static $many_many = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         "Blocks" => "SheaDawson\Blocks\model\Block", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         "PageParents" => "SilverStripe\CMS\Model\SiteTree", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private static $many_many_extraFields = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         'Blocks' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'Sort' => 'Int', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'BlockArea' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'AboveOrBelow' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private static $above_or_below_options = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         'Above' => 'Above Page Blocks', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         'Below' => 'Below Page Blocks', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $fields = parent::getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $fields->removeFieldFromTab('Root', 'PageParents'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $fields->addFieldToTab('Root.Main', HeaderField::create('SettingsHeading', _t('BlockSet.Settings', 'Settings')), 'Title'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $fields->addFieldToTab('Root.Main', MultiValueCheckboxField::create('PageTypes', _t('BlockSet.OnlyApplyToThesePageTypes', 'Only apply to these Page Types:'), $this->pageTypeOptions()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ->setDescription(_t('BlockSet.OnlyApplyToThesePageTypesDescription', 'Selected Page Types will inherit this Block Set automatically. Leave all unchecked to apply to all page types.'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $fields->addFieldToTab('Root.Main', TreeMultiselectField::create('PageParents', _t('BlockSet.OnlyApplyToChildrenOfThesePages', 'Only apply to children of these Pages:'), 'SiteTree')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $fields->addFieldToTab('Root.Main', CheckboxField::create('IncludePageParent', _t('BlockSet.ApplyBlockSetToSelectedPageParentsAsWellAsChildren','Apply block set to selected page parents as well as children'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (!$this->ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $fields->addFieldToTab('Root.Main', LiteralField::create('NotSaved', "<p class='message warning'>"._t('BlockSet.YouCanAddBlocksToThisSetOnceYouHaveSavedIt', 'You can add Blocks to this set once you have saved it for the first time').'</p>')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $fields->removeFieldFromTab('Root', 'Blocks'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $gridConfig = GridFieldConfigBlockManager::create(true, true, true, true, true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->addExisting() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->addComponent(new GridFieldOrderableRows()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $gridSource = $this->Blocks()->Sort('Sort'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $fields->addFieldToTab('Root.Main', HeaderField::create('BlocksHeading', _t('Block.PLURALNAME', 'Blocks'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $fields->addFieldToTab('Root.Main', GridField::create('Blocks', _t('Block.PLURALNAME', 'Blocks'), $gridSource, $gridConfig)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Returns a sorted array suitable for a dropdown with pagetypes and their translated name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     protected function pageTypeOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $pageTypes = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $classes = ArrayLib::valueKey(SiteTree::page_type_classes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         unset($classes['VirtualPage']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         unset($classes['ErrorPage']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         unset($classes['RedirectorPage']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         foreach ($classes as $pageTypeClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $pageTypes[$pageTypeClass] = singleton($pageTypeClass)->i18n_singular_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         asort($pageTypes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $pageTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Returns a list of pages this BlockSet features on. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return DataList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function Pages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $pages = SiteTree::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $types = $this->PageTypes->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (count($types)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $pages = $pages->filter('ClassName', $types); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $parents = $this->PageParents()->column('ID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (count($parents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $pages = $pages->filter('ParentID', $parents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $pages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |     public function canView($member = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function canEdit($member = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return Permission::check('ADMIN') || Permission::check('BLOCK_EDIT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function canDelete($member = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return Permission::check('ADMIN') || Permission::check('BLOCK_DELETE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function canCreate($member = null, $context = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         return Permission::check('ADMIN') || Permission::check('BLOCK_CREATE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function providePermissions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             'BLOCKSET_EDIT' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 'name' => _t('BlockSet.EditBlockSet','Edit a Block Set'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 'category' => _t('Block.PermissionCategory', 'Blocks'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             'BLOCKSET_DELETE' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 'name' => _t('BlockSet.DeleteBlockSet','Delete a Block Set'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 'category' => _t('Block.PermissionCategory', 'Blocks'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             'BLOCKSET_CREATE' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 'name' => _t('BlockSet.CreateBlockSet','Create a Block Set'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 'category' => _t('Block.PermissionCategory', 'Blocks'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                        
This check marks private properties in classes that are never used. Those properties can be removed.