| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SheaDawson\Blocks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\ORM\ArrayLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\SiteConfig\SiteConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\FormField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\View\SSViewer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\View\ViewableData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * BlockManager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Shea Dawson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class BlockManager extends ViewableData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * Use default ContentBlock class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	private static $use_default_blocks = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Show a block area preview button in CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	private static $block_area_preview = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 6 |  | 	public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 6 |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * Gets an array of all areas defined for blocks. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @param bool   $keyAsValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @return array $areas | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	public function getAreas($keyAsValue = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$areas = $this->config()->get('areas'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$areas = $keyAsValue ? ArrayLib::valuekey(array_keys($areas)) : $areas; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		if (count($areas)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			foreach ($areas as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				$areas[$k] = $keyAsValue ? FormField::name_to_label($k) : $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		return $areas; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * Gets an array of all areas defined that are compatible with pages of type $class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param string $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @return array $areas | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	public function getAreasForPageType($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$areas = $this->getAreas(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		if (!$areas) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		foreach ($areas as $area => $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			if (!is_array($config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 80 |  | View Code Duplication | 			if (isset($config['except'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$except = $config['except']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				if (is_array($except) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 					? in_array($class, $except) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 					: $except == $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 					unset($areas[$area]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 					continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication | 			if (isset($config['only'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 				$only = $config['only']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				if (is_array($only) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 					? !in_array($class, $only) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 					: $only != $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 					unset($areas[$area]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 					continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		if (count($areas)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			foreach ($areas as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				$areas[$k] = _t('Block.BlockAreaName.'.$k, FormField::name_to_label($k)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			return $areas; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			return $areas; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  | 	public function getBlockClasses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  | 		$classes = ArrayLib::valuekey(ClassInfo::subclassesFor("SheaDawson\Blocks\model\Block")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  | 		array_shift($classes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  | 		foreach ($classes as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  | 			$classes[$k] = singleton($k)->singular_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  | 		if (isset($config['use_default_blocks']) && !$config['use_default_blocks']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	        unset($classes['ContentBlock']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  | 	    } else if (!$config['use_default_blocks']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  | 	        unset($classes['ContentBlock']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	    } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  | 		$disabledArr = Config::inst()->get("BlockManager", 'disabled_blocks') ? Config::inst()->get("BlockManager", 'disabled_blocks') : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  | 		if (isset($config['disabled_blocks'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		    $disabledArr = array_merge($disabledArr, $config['disabled_blocks']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  | 		if (count($disabledArr)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			foreach ($disabledArr as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 				unset($classes[$v]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  | 		return $classes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * Usage of BlockSets configurable from yaml | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 	public function getUseBlockSets() | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  | 		$config = $this->config()->get('options'); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  | 		return isset($config['use_blocksets']) ? $config['use_blocksets'] : true; | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * Exclusion of blocks from page types defined in yaml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	public function getExcludeFromPageTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * getWhiteListedPageTypes optionally configured by the developer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	public function getWhiteListedPageTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * getBlackListedPageTypes optionally configured by the developer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * Includes blacklisted page types defined in the old exclude_from_page_types array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	public function getBlackListedPageTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		$legacy = isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$current = isset($config['pagetype_blacklist']) ? $config['pagetype_blacklist'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		return array_merge($legacy, $current); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	 * Usage of extra css classes configurable from yaml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 1 |  | 	public function getUseExtraCSSClasses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 1 |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 1 |  | 		return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	 * Prefix for the default CSSClasses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	public function getPrefixDefaultCSSClasses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 		$config = $this->config()->get('options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  | 
            
                        
This check marks private properties in classes that are never used. Those properties can be removed.