GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e0ffd1...07b001 )
by Shea
12:14
created
src/BlockManager.php 2 patches
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -3,11 +3,9 @@
 block discarded – undo
3 3
 namespace SheaDawson\Blocks;
4 4
 
5 5
 use SilverStripe\ORM\ArrayLib;
6
-use SilverStripe\SiteConfig\SiteConfig;
7 6
 use SilverStripe\Core\Config\Config;
8 7
 use SilverStripe\Core\ClassInfo;
9 8
 use SilverStripe\Forms\FormField;
10
-use SilverStripe\View\SSViewer;
11 9
 use SilverStripe\View\ViewableData;
12 10
 
13 11
 /**
Please login to merge, or discard this patch.
Indentation   +162 added lines, -162 removed lines patch added patch discarded remove patch
@@ -17,187 +17,187 @@
 block discarded – undo
17 17
  */
18 18
 class BlockManager extends ViewableData
19 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
-	public function __construct()
35
-	{
36
-		parent::__construct();
37
-	}
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
-			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
-			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
-	public function getBlockClasses()
115
-	{
116
-		$classes = ArrayLib::valuekey(ClassInfo::subclassesFor("SheaDawson\Blocks\model\Block"));
117
-		array_shift($classes);
118
-		foreach ($classes as $k => $v) {
119
-			$classes[$k] = singleton($k)->singular_name();
120
-		}
121
-
122
-		$config = $this->config()->get('options');
123
-
124
-		if (isset($config['use_default_blocks']) && !$config['use_default_blocks']) {
125
-	        unset($classes['ContentBlock']);
126
-	    } else if (!$config['use_default_blocks']) {
127
-	        unset($classes['ContentBlock']);
128
-	    }
129
-
130
-		$disabledArr = Config::inst()->get("BlockManager", 'disabled_blocks') ? Config::inst()->get("BlockManager", 'disabled_blocks') : [];
131
-		if (isset($config['disabled_blocks'])) {
132
-		    $disabledArr = array_merge($disabledArr, $config['disabled_blocks']);
133
-		}
134
-		if (count($disabledArr)) {
135
-			foreach ($disabledArr as $k => $v) {
136
-				unset($classes[$v]);
137
-			}
138
-		}
139
-
140
-		return $classes;
141
-	}
142
-
143
-	/*
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
+    public function __construct()
35
+    {
36
+        parent::__construct();
37
+    }
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
+            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
+            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
+    public function getBlockClasses()
115
+    {
116
+        $classes = ArrayLib::valuekey(ClassInfo::subclassesFor("SheaDawson\Blocks\model\Block"));
117
+        array_shift($classes);
118
+        foreach ($classes as $k => $v) {
119
+            $classes[$k] = singleton($k)->singular_name();
120
+        }
121
+
122
+        $config = $this->config()->get('options');
123
+
124
+        if (isset($config['use_default_blocks']) && !$config['use_default_blocks']) {
125
+            unset($classes['ContentBlock']);
126
+        } else if (!$config['use_default_blocks']) {
127
+            unset($classes['ContentBlock']);
128
+        }
129
+
130
+        $disabledArr = Config::inst()->get("BlockManager", 'disabled_blocks') ? Config::inst()->get("BlockManager", 'disabled_blocks') : [];
131
+        if (isset($config['disabled_blocks'])) {
132
+            $disabledArr = array_merge($disabledArr, $config['disabled_blocks']);
133
+        }
134
+        if (count($disabledArr)) {
135
+            foreach ($disabledArr as $k => $v) {
136
+                unset($classes[$v]);
137
+            }
138
+        }
139
+
140
+        return $classes;
141
+    }
142
+
143
+    /*
144 144
 	 * Usage of BlockSets configurable from yaml
145 145
 	 */
146
-	public function getUseBlockSets()
147
-	{
148
-		$config = $this->config()->get('options');
146
+    public function getUseBlockSets()
147
+    {
148
+        $config = $this->config()->get('options');
149 149
 
150
-		return isset($config['use_blocksets']) ? $config['use_blocksets'] : true;
151
-	}
150
+        return isset($config['use_blocksets']) ? $config['use_blocksets'] : true;
151
+    }
152 152
 
153
-	/*
153
+    /*
154 154
 	 * Exclusion of blocks from page types defined in yaml
155 155
 	 */
156
-	public function getExcludeFromPageTypes()
157
-	{
158
-		$config = $this->config()->get('options');
156
+    public function getExcludeFromPageTypes()
157
+    {
158
+        $config = $this->config()->get('options');
159 159
 
160
-		return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : [];
161
-	}
160
+        return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : [];
161
+    }
162 162
 
163
-	/*
163
+    /*
164 164
 	 * getWhiteListedPageTypes optionally configured by the developer
165 165
 	 */
166
-	public function getWhiteListedPageTypes()
167
-	{
168
-		$config = $this->config()->get('options');
169
-		return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : [];
170
-	}
166
+    public function getWhiteListedPageTypes()
167
+    {
168
+        $config = $this->config()->get('options');
169
+        return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : [];
170
+    }
171 171
 
172
-	/*
172
+    /*
173 173
 	 * getBlackListedPageTypes optionally configured by the developer
174 174
 	 * Includes blacklisted page types defined in the old exclude_from_page_types array
175 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
-	/*
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 185
 	 * Usage of extra css classes configurable from yaml
186 186
 	 */
187
-	public function getUseExtraCSSClasses()
188
-	{
189
-		$config = $this->config()->get('options');
187
+    public function getUseExtraCSSClasses()
188
+    {
189
+        $config = $this->config()->get('options');
190 190
 
191
-		return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false;
192
-	}
191
+        return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false;
192
+    }
193 193
 
194
-	/*
194
+    /*
195 195
 	 * Prefix for the default CSSClasses
196 196
 	 */
197
-	public function getPrefixDefaultCSSClasses()
198
-	{
199
-		$config = $this->config()->get('options');
197
+    public function getPrefixDefaultCSSClasses()
198
+    {
199
+        $config = $this->config()->get('options');
200 200
 
201
-		return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false;
202
-	}
201
+        return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false;
202
+    }
203 203
 }
Please login to merge, or discard this patch.
src/extensions/BlocksSiteTreeExtension.php 2 patches
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -3,7 +3,6 @@  discard block
 block discarded – undo
3 3
 namespace SheaDawson\Blocks\Extensions;
4 4
 
5 5
 use SheaDawson\Blocks\Model\Block;
6
-use SheaDawson\Blocks\Model\Blockset;
7 6
 use SheaDawson\Blocks\BlockManager;
8 7
 use SheaDawson\Blocks\Forms\GridFieldConfigBlockManager;
9 8
 use SilverStripe\CMS\Model\SiteTreeExtension;
@@ -18,7 +17,6 @@  discard block
 block discarded – undo
18 17
 use SilverStripe\ORM\ArrayList;
19 18
 use SilverStripe\Security\Permission;
20 19
 use SilverStripe\Control\Controller;
21
-use SilverStripeAustralia\GridFieldExtensions\GridFieldOrderableRows;
22 20
 
23 21
 /**
24 22
  * BlocksSiteTreeExtension.
Please login to merge, or discard this patch.
Indentation   +317 added lines, -317 removed lines patch added patch discarded remove patch
@@ -27,321 +27,321 @@
 block discarded – undo
27 27
  */
28 28
 class BlocksSiteTreeExtension extends SiteTreeExtension
29 29
 {
30
-	private static $db = [
31
-		'InheritBlockSets' => 'Boolean',
32
-	];
33
-
34
-	private static $many_many = [
35
-		"Blocks" => Block::class,
36
-		"DisabledBlocks" => Block::class,
37
-	];
38
-
39
-	public static $many_many_extraFields = [
40
-		'Blocks' => [
41
-			'Sort' => 'Int',
42
-			'BlockArea' => 'Varchar',
43
-		],
44
-	];
45
-
46
-	private static $defaults = [
47
-		'InheritBlockSets' => 1,
48
-	];
49
-
50
-	private static $dependencies = [
51
-		'blockManager' => '%$blockManager',
52
-	];
53
-
54
-	public $blockManager;
55
-
56
-
57
-	/**
58
-	 * Check if the Blocks CMSFields should be displayed for this Page
59
-	 *
60
-	 * @return boolean
61
-	 **/
62
-	public function showBlocksFields()
63
-	{
64
-		$whiteList = $this->blockManager->getWhiteListedPageTypes();
65
-		$blackList = $this->blockManager->getBlackListedPageTypes();
66
-
67
-		if (in_array($this->owner->ClassName, $blackList)) {
68
-			return false;
69
-		}
70
-
71
-		if (count($whiteList) && !in_array($this->owner->ClassName, $whiteList)) {
72
-			return false;
73
-		}
74
-
75
-		if (!Permission::check('BLOCK_EDIT')) {
76
-			return false;
77
-		}
78
-
79
-		return true;
80
-	}
81
-
82
-	/**
83
-	 * Block manager for Pages.
84
-	 * */
85
-	public function updateCMSFields(FieldList $fields)
86
-	{
87
-		if ($fields->fieldByName('Root.Blocks') || !$this->showBlocksFields()) {
88
-			return;
89
-		}
90
-
91
-		$areas = $this->blockManager->getAreasForPageType($this->owner->ClassName);
92
-
93
-		if ($areas && count($areas)) {
94
-			$fields->addFieldToTab('Root', new Tab('Blocks', _t('Block.PLURALNAME', 'Blocks')));
95
-			if (BlockManager::config()->get('block_area_preview')) {
96
-				$fields->addFieldToTab('Root.Blocks',
97
-						LiteralField::create('PreviewLink', $this->areasPreviewButton()));
98
-			}
99
-
100
-			// Blocks related directly to this Page
101
-			$gridConfig = GridFieldConfigBlockManager::create(true, true, true, true)
102
-				->addExisting($this->owner->class)
103
-				//->addBulkEditing()
104
-				// ->addComponent(new GridFieldOrderableRows()) // Comment until below TODO is complete.
105
-				;
106
-
107
-
108
-			// TODO it seems this sort is not being applied...
109
-			$gridSource = $this->owner->Blocks();
110
-				// ->sort(array(
111
-				// 	"FIELD(SiteTree_Blocks.BlockArea, '" . implode("','", array_keys($areas)) . "')" => '',
112
-				// 	'SiteTree_Blocks.Sort' => 'ASC',
113
-				// 	'Name' => 'ASC'
114
-				// ));
115
-
116
-			$fields->addFieldToTab('Root.Blocks', GridField::create('Blocks', _t('Block.PLURALNAME', 'Blocks'), $gridSource, $gridConfig));
117
-
118
-
119
-			// Blocks inherited from BlockSets
120
-			if ($this->blockManager->getUseBlockSets()) {
121
-				$inheritedBlocks = $this->getBlocksFromAppliedBlockSets(null, true);
122
-
123
-				if ($inheritedBlocks->count()) {
124
-					$activeInherited = $this->getBlocksFromAppliedBlockSets(null, false);
125
-
126
-					if ($activeInherited->count()) {
127
-						$fields->addFieldsToTab('Root.Blocks', [
128
-							GridField::create('InheritedBlockList', _t('BlocksSiteTreeExtension.BlocksInheritedFromBlockSets', 'Blocks Inherited from Block Sets'), $activeInherited,
129
-								GridFieldConfigBlockManager::create(false, false, false)),
130
-							LiteralField::create('InheritedBlockListTip', "<p class='message'>"._t('BlocksSiteTreeExtension.InheritedBlocksEditLink', 'Tip: Inherited blocks can be edited in the {link_start}Block Admin area{link_end}', '', ['link_start' => '<a href="admin/block-admin">', 'link_end' => '</a>']).'<p>'),
131
-						]);
132
-					}
133
-
134
-					$fields->addFieldToTab('Root.Blocks',
135
-							ListboxField::create('DisabledBlocks', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'),
136
-									$inheritedBlocks->map('ID', 'Title'), null, null, true)
137
-									->setDescription(_t('BlocksSiteTreeExtension.DisableInheritedBlocksDescription', 'Select any inherited blocks that you would not like displayed on this page.'))
138
-					);
139
-				} else {
140
-					$fields->addFieldToTab('Root.Blocks',
141
-							ReadonlyField::create('DisabledBlocksReadOnly', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'),
142
-									_t('BlocksSiteTreeExtension.NoInheritedBlocksToDisable','This page has no inherited blocks to disable.')));
143
-				}
144
-
145
-				$fields->addFieldToTab('Root.Blocks',
146
-					CheckboxField::create('InheritBlockSets', _t('BlocksSiteTreeExtension.InheritBlocksFromBlockSets', 'Inherit Blocks from Block Sets')));
147
-			}
148
-		}
149
-	}
150
-
151
-	/**
152
-	 * Called from templates to get rendered blocks for the given area.
153
-	 *
154
-	 * @param string $area
155
-	 * @param int    $limit Limit the items to this number, or null for no limit
156
-	 */
157
-	public function BlockArea($area, $limit = null)
158
-	{
159
-		if ($this->owner->ID <= 0) {
160
-			return;
161
-		} // blocks break on fake pages ie Security/login
162
-
163
-		$list = $this->getBlockList($area);
164
-
165
-		foreach ($list as $block) {
166
-			if (!$block->canView()) {
167
-				$list->remove($block);
168
-			}
169
-		}
170
-
171
-		if ($limit !== null) {
172
-			$list = $list->limit($limit);
173
-		}
174
-
175
-		$data = [];
176
-		$data['HasBlockArea'] = ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) || $list->Count() > 0;
177
-		$data['BlockArea'] = $list;
178
-		$data['AreaID'] = $area;
179
-
180
-		$data = $this->owner->customise($data);
181
-
182
-		$template = ['BlockArea_'.$area];
183
-
184
-		if (SSViewer::hasTemplate($template)) {
185
-			return $data->renderWith($template);
186
-		} else {
187
-			return $data->renderWith('BlockArea');
188
-		}
189
-	}
190
-
191
-	public function HasBlockArea($area)
192
-	{
193
-		if ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) {
194
-			return true;
195
-		}
196
-
197
-		$list = $this->getBlockList($area);
198
-
199
-		foreach ($list as $block) {
200
-			if (!$block->canView()) {
201
-				$list->remove($block);
202
-			}
203
-		}
204
-
205
-		return $list->Count() > 0;
206
-	}
207
-
208
-	/**
209
-	 * Get a merged list of all blocks on this page and ones inherited from BlockSets.
210
-	 *
211
-	 * @param string|null $area            filter by block area
212
-	 * @param bool        $includeDisabled Include blocks that have been explicitly excluded from this page
213
-	 *                                     i.e. blocks from block sets added to the "disable inherited blocks" list
214
-	 *
215
-	 * @return ArrayList
216
-	 * */
217
-	public function getBlockList($area = null, $includeDisabled = false)
218
-	{
219
-		$includeSets = $this->blockManager->getUseBlockSets() && $this->owner->InheritBlockSets;
220
-		$blocks = ArrayList::create();
221
-
222
-		// get blocks directly linked to this page
223
-		$nativeBlocks = $this->owner->Blocks()->sort('Sort');
224
-		if ($area) {
225
-			$nativeBlocks = $nativeBlocks->filter('BlockArea', $area);
226
-		}
227
-
228
-		if ($nativeBlocks->count()) {
229
-			foreach ($nativeBlocks as $block) {
230
-				$blocks->add($block);
231
-			}
232
-		}
233
-
234
-		// get blocks from BlockSets
235
-		if ($includeSets) {
236
-			$blocksFromSets = $this->getBlocksFromAppliedBlockSets($area, $includeDisabled);
237
-			if ($blocksFromSets->count()) {
238
-				// merge set sources
239
-				foreach ($blocksFromSets as $block) {
240
-					if (!$blocks->find('ID', $block->ID)) {
241
-						if ($block->AboveOrBelow == 'Above') {
242
-							$blocks->unshift($block);
243
-						} else {
244
-							$blocks->push($block);
245
-						}
246
-					}
247
-				}
248
-			}
249
-		}
250
-
251
-		return $blocks;
252
-	}
253
-
254
-	/**
255
-	 * Get Any BlockSets that apply to this page.
256
-	 *
257
-	 * @return ArrayList
258
-	 * */
259
-	public function getAppliedSets()
260
-	{
261
-		$list = ArrayList::create();
262
-		if (!$this->owner->InheritBlockSets) {
263
-			return $list;
264
-		}
265
-
266
-		$sets = BlockSet::get()->where("(PageTypesValue IS NULL) OR (PageTypesValue LIKE '%:\"{$this->owner->ClassName}%')");
267
-		$ancestors = $this->owner->getAncestors()->column('ID');
268
-
269
-		foreach ($sets as $set) {
270
-			$restrictedToParerentIDs = $set->PageParents()->column('ID');
271
-			if (count($restrictedToParerentIDs)) {
272
-				// check whether the set should include selected parent, in which case check whether
273
-				// it was in the restricted parents list. If it's not, or if include parentpage
274
-				// wasn't selected, we check the ancestors of this page.
275
-				if ($set->IncludePageParent && in_array($this->owner->ID, $restrictedToParerentIDs)) {
276
-					$list->add($set);
277
-				} else {
278
-					if (count($ancestors)) {
279
-						foreach ($ancestors as $ancestor) {
280
-							if (in_array($ancestor, $restrictedToParerentIDs)) {
281
-								$list->add($set);
282
-								continue;
283
-							}
284
-						}
285
-					}
286
-				}
287
-			} else {
288
-				$list->add($set);
289
-			}
290
-		}
291
-
292
-		return $list;
293
-	}
294
-
295
-	/**
296
-	 * Get all Blocks from BlockSets that apply to this page.
297
-	 *
298
-	 * @return ArrayList
299
-	 * */
300
-	public function getBlocksFromAppliedBlockSets($area = null, $includeDisabled = false)
301
-	{
302
-		$blocks = ArrayList::create();
303
-		$sets = $this->getAppliedSets();
304
-
305
-		if (!$sets->count()) {
306
-			return $blocks;
307
-		}
308
-
309
-		foreach ($sets as $set) {
310
-			$setBlocks = $set->Blocks()->sort('Sort DESC');
311
-
312
-			if (!$includeDisabled) {
313
-				$setBlocks = $setBlocks->exclude('ID', $this->owner->DisabledBlocks()->column('ID'));
314
-			}
315
-
316
-			if ($area) {
317
-				$setBlocks = $setBlocks->filter('BlockArea', $area);
318
-			}
319
-
320
-			$blocks->merge($setBlocks);
321
-		}
322
-
323
-		$blocks->removeDuplicates();
324
-
325
-		return $blocks;
326
-	}
327
-
328
-	/**
329
-	 * Get's the link for a block area preview button.
330
-	 *
331
-	 * @return string
332
-	 * */
333
-	public function areasPreviewLink()
334
-	{
335
-		return Controller::join_links($this->owner->Link(), '?block_preview=1');
336
-	}
337
-
338
-	/**
339
-	 * Get's html for a block area preview button.
340
-	 *
341
-	 * @return string
342
-	 * */
343
-	public function areasPreviewButton()
344
-	{
345
-		return "<a class='btn btn-primary' style='font-style:normal;' href='".$this->areasPreviewLink()."' target='_blank'>"._t('BlocksSiteTreeExtension.PreviewBlockAreasLink', 'Preview Block Areas for this page').'</a>';
346
-	}
30
+    private static $db = [
31
+        'InheritBlockSets' => 'Boolean',
32
+    ];
33
+
34
+    private static $many_many = [
35
+        "Blocks" => Block::class,
36
+        "DisabledBlocks" => Block::class,
37
+    ];
38
+
39
+    public static $many_many_extraFields = [
40
+        'Blocks' => [
41
+            'Sort' => 'Int',
42
+            'BlockArea' => 'Varchar',
43
+        ],
44
+    ];
45
+
46
+    private static $defaults = [
47
+        'InheritBlockSets' => 1,
48
+    ];
49
+
50
+    private static $dependencies = [
51
+        'blockManager' => '%$blockManager',
52
+    ];
53
+
54
+    public $blockManager;
55
+
56
+
57
+    /**
58
+     * Check if the Blocks CMSFields should be displayed for this Page
59
+     *
60
+     * @return boolean
61
+     **/
62
+    public function showBlocksFields()
63
+    {
64
+        $whiteList = $this->blockManager->getWhiteListedPageTypes();
65
+        $blackList = $this->blockManager->getBlackListedPageTypes();
66
+
67
+        if (in_array($this->owner->ClassName, $blackList)) {
68
+            return false;
69
+        }
70
+
71
+        if (count($whiteList) && !in_array($this->owner->ClassName, $whiteList)) {
72
+            return false;
73
+        }
74
+
75
+        if (!Permission::check('BLOCK_EDIT')) {
76
+            return false;
77
+        }
78
+
79
+        return true;
80
+    }
81
+
82
+    /**
83
+     * Block manager for Pages.
84
+     * */
85
+    public function updateCMSFields(FieldList $fields)
86
+    {
87
+        if ($fields->fieldByName('Root.Blocks') || !$this->showBlocksFields()) {
88
+            return;
89
+        }
90
+
91
+        $areas = $this->blockManager->getAreasForPageType($this->owner->ClassName);
92
+
93
+        if ($areas && count($areas)) {
94
+            $fields->addFieldToTab('Root', new Tab('Blocks', _t('Block.PLURALNAME', 'Blocks')));
95
+            if (BlockManager::config()->get('block_area_preview')) {
96
+                $fields->addFieldToTab('Root.Blocks',
97
+                        LiteralField::create('PreviewLink', $this->areasPreviewButton()));
98
+            }
99
+
100
+            // Blocks related directly to this Page
101
+            $gridConfig = GridFieldConfigBlockManager::create(true, true, true, true)
102
+                ->addExisting($this->owner->class)
103
+                //->addBulkEditing()
104
+                // ->addComponent(new GridFieldOrderableRows()) // Comment until below TODO is complete.
105
+                ;
106
+
107
+
108
+            // TODO it seems this sort is not being applied...
109
+            $gridSource = $this->owner->Blocks();
110
+                // ->sort(array(
111
+                // 	"FIELD(SiteTree_Blocks.BlockArea, '" . implode("','", array_keys($areas)) . "')" => '',
112
+                // 	'SiteTree_Blocks.Sort' => 'ASC',
113
+                // 	'Name' => 'ASC'
114
+                // ));
115
+
116
+            $fields->addFieldToTab('Root.Blocks', GridField::create('Blocks', _t('Block.PLURALNAME', 'Blocks'), $gridSource, $gridConfig));
117
+
118
+
119
+            // Blocks inherited from BlockSets
120
+            if ($this->blockManager->getUseBlockSets()) {
121
+                $inheritedBlocks = $this->getBlocksFromAppliedBlockSets(null, true);
122
+
123
+                if ($inheritedBlocks->count()) {
124
+                    $activeInherited = $this->getBlocksFromAppliedBlockSets(null, false);
125
+
126
+                    if ($activeInherited->count()) {
127
+                        $fields->addFieldsToTab('Root.Blocks', [
128
+                            GridField::create('InheritedBlockList', _t('BlocksSiteTreeExtension.BlocksInheritedFromBlockSets', 'Blocks Inherited from Block Sets'), $activeInherited,
129
+                                GridFieldConfigBlockManager::create(false, false, false)),
130
+                            LiteralField::create('InheritedBlockListTip', "<p class='message'>"._t('BlocksSiteTreeExtension.InheritedBlocksEditLink', 'Tip: Inherited blocks can be edited in the {link_start}Block Admin area{link_end}', '', ['link_start' => '<a href="admin/block-admin">', 'link_end' => '</a>']).'<p>'),
131
+                        ]);
132
+                    }
133
+
134
+                    $fields->addFieldToTab('Root.Blocks',
135
+                            ListboxField::create('DisabledBlocks', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'),
136
+                                    $inheritedBlocks->map('ID', 'Title'), null, null, true)
137
+                                    ->setDescription(_t('BlocksSiteTreeExtension.DisableInheritedBlocksDescription', 'Select any inherited blocks that you would not like displayed on this page.'))
138
+                    );
139
+                } else {
140
+                    $fields->addFieldToTab('Root.Blocks',
141
+                            ReadonlyField::create('DisabledBlocksReadOnly', _t('BlocksSiteTreeExtension.DisableInheritedBlocks', 'Disable Inherited Blocks'),
142
+                                    _t('BlocksSiteTreeExtension.NoInheritedBlocksToDisable','This page has no inherited blocks to disable.')));
143
+                }
144
+
145
+                $fields->addFieldToTab('Root.Blocks',
146
+                    CheckboxField::create('InheritBlockSets', _t('BlocksSiteTreeExtension.InheritBlocksFromBlockSets', 'Inherit Blocks from Block Sets')));
147
+            }
148
+        }
149
+    }
150
+
151
+    /**
152
+     * Called from templates to get rendered blocks for the given area.
153
+     *
154
+     * @param string $area
155
+     * @param int    $limit Limit the items to this number, or null for no limit
156
+     */
157
+    public function BlockArea($area, $limit = null)
158
+    {
159
+        if ($this->owner->ID <= 0) {
160
+            return;
161
+        } // blocks break on fake pages ie Security/login
162
+
163
+        $list = $this->getBlockList($area);
164
+
165
+        foreach ($list as $block) {
166
+            if (!$block->canView()) {
167
+                $list->remove($block);
168
+            }
169
+        }
170
+
171
+        if ($limit !== null) {
172
+            $list = $list->limit($limit);
173
+        }
174
+
175
+        $data = [];
176
+        $data['HasBlockArea'] = ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) || $list->Count() > 0;
177
+        $data['BlockArea'] = $list;
178
+        $data['AreaID'] = $area;
179
+
180
+        $data = $this->owner->customise($data);
181
+
182
+        $template = ['BlockArea_'.$area];
183
+
184
+        if (SSViewer::hasTemplate($template)) {
185
+            return $data->renderWith($template);
186
+        } else {
187
+            return $data->renderWith('BlockArea');
188
+        }
189
+    }
190
+
191
+    public function HasBlockArea($area)
192
+    {
193
+        if ($this->owner->canEdit() && isset($_REQUEST['block_preview']) && $_REQUEST['block_preview']) {
194
+            return true;
195
+        }
196
+
197
+        $list = $this->getBlockList($area);
198
+
199
+        foreach ($list as $block) {
200
+            if (!$block->canView()) {
201
+                $list->remove($block);
202
+            }
203
+        }
204
+
205
+        return $list->Count() > 0;
206
+    }
207
+
208
+    /**
209
+     * Get a merged list of all blocks on this page and ones inherited from BlockSets.
210
+     *
211
+     * @param string|null $area            filter by block area
212
+     * @param bool        $includeDisabled Include blocks that have been explicitly excluded from this page
213
+     *                                     i.e. blocks from block sets added to the "disable inherited blocks" list
214
+     *
215
+     * @return ArrayList
216
+     * */
217
+    public function getBlockList($area = null, $includeDisabled = false)
218
+    {
219
+        $includeSets = $this->blockManager->getUseBlockSets() && $this->owner->InheritBlockSets;
220
+        $blocks = ArrayList::create();
221
+
222
+        // get blocks directly linked to this page
223
+        $nativeBlocks = $this->owner->Blocks()->sort('Sort');
224
+        if ($area) {
225
+            $nativeBlocks = $nativeBlocks->filter('BlockArea', $area);
226
+        }
227
+
228
+        if ($nativeBlocks->count()) {
229
+            foreach ($nativeBlocks as $block) {
230
+                $blocks->add($block);
231
+            }
232
+        }
233
+
234
+        // get blocks from BlockSets
235
+        if ($includeSets) {
236
+            $blocksFromSets = $this->getBlocksFromAppliedBlockSets($area, $includeDisabled);
237
+            if ($blocksFromSets->count()) {
238
+                // merge set sources
239
+                foreach ($blocksFromSets as $block) {
240
+                    if (!$blocks->find('ID', $block->ID)) {
241
+                        if ($block->AboveOrBelow == 'Above') {
242
+                            $blocks->unshift($block);
243
+                        } else {
244
+                            $blocks->push($block);
245
+                        }
246
+                    }
247
+                }
248
+            }
249
+        }
250
+
251
+        return $blocks;
252
+    }
253
+
254
+    /**
255
+     * Get Any BlockSets that apply to this page.
256
+     *
257
+     * @return ArrayList
258
+     * */
259
+    public function getAppliedSets()
260
+    {
261
+        $list = ArrayList::create();
262
+        if (!$this->owner->InheritBlockSets) {
263
+            return $list;
264
+        }
265
+
266
+        $sets = BlockSet::get()->where("(PageTypesValue IS NULL) OR (PageTypesValue LIKE '%:\"{$this->owner->ClassName}%')");
267
+        $ancestors = $this->owner->getAncestors()->column('ID');
268
+
269
+        foreach ($sets as $set) {
270
+            $restrictedToParerentIDs = $set->PageParents()->column('ID');
271
+            if (count($restrictedToParerentIDs)) {
272
+                // check whether the set should include selected parent, in which case check whether
273
+                // it was in the restricted parents list. If it's not, or if include parentpage
274
+                // wasn't selected, we check the ancestors of this page.
275
+                if ($set->IncludePageParent && in_array($this->owner->ID, $restrictedToParerentIDs)) {
276
+                    $list->add($set);
277
+                } else {
278
+                    if (count($ancestors)) {
279
+                        foreach ($ancestors as $ancestor) {
280
+                            if (in_array($ancestor, $restrictedToParerentIDs)) {
281
+                                $list->add($set);
282
+                                continue;
283
+                            }
284
+                        }
285
+                    }
286
+                }
287
+            } else {
288
+                $list->add($set);
289
+            }
290
+        }
291
+
292
+        return $list;
293
+    }
294
+
295
+    /**
296
+     * Get all Blocks from BlockSets that apply to this page.
297
+     *
298
+     * @return ArrayList
299
+     * */
300
+    public function getBlocksFromAppliedBlockSets($area = null, $includeDisabled = false)
301
+    {
302
+        $blocks = ArrayList::create();
303
+        $sets = $this->getAppliedSets();
304
+
305
+        if (!$sets->count()) {
306
+            return $blocks;
307
+        }
308
+
309
+        foreach ($sets as $set) {
310
+            $setBlocks = $set->Blocks()->sort('Sort DESC');
311
+
312
+            if (!$includeDisabled) {
313
+                $setBlocks = $setBlocks->exclude('ID', $this->owner->DisabledBlocks()->column('ID'));
314
+            }
315
+
316
+            if ($area) {
317
+                $setBlocks = $setBlocks->filter('BlockArea', $area);
318
+            }
319
+
320
+            $blocks->merge($setBlocks);
321
+        }
322
+
323
+        $blocks->removeDuplicates();
324
+
325
+        return $blocks;
326
+    }
327
+
328
+    /**
329
+     * Get's the link for a block area preview button.
330
+     *
331
+     * @return string
332
+     * */
333
+    public function areasPreviewLink()
334
+    {
335
+        return Controller::join_links($this->owner->Link(), '?block_preview=1');
336
+    }
337
+
338
+    /**
339
+     * Get's html for a block area preview button.
340
+     *
341
+     * @return string
342
+     * */
343
+    public function areasPreviewButton()
344
+    {
345
+        return "<a class='btn btn-primary' style='font-style:normal;' href='".$this->areasPreviewLink()."' target='_blank'>"._t('BlocksSiteTreeExtension.PreviewBlockAreasLink', 'Preview Block Areas for this page').'</a>';
346
+    }
347 347
 }
Please login to merge, or discard this patch.
src/model/BlockSet.php 2 patches
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -2,13 +2,11 @@
 block discarded – undo
2 2
 
3 3
 namespace SheaDawson\Blocks\Model;
4 4
 
5
-use SheaDawson\Blocks\Forms\GridFieldConfigBlockManager;
6 5
 use SilverStripe\ORM\DataObject;
7 6
 use SilverStripe\ORM\ArrayLib;
8 7
 use SilverStripe\CMS\Model\SiteTree;
9 8
 use SilverStripe\Security\PermissionProvider;
10 9
 use SilverStripe\Security\Permission;
11
-use SilverStripe\Security\Group;
12 10
 use SilverStripe\Forms\TreeMultiselectField;
13 11
 use SilverStripe\Forms\CheckboxField;
14 12
 use SilverStripe\Forms\HeaderField;
Please login to merge, or discard this patch.
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -89,8 +89,8 @@
 block discarded – undo
89 89
         $fields->removeFieldFromTab('Root', 'Blocks');
90 90
 
91 91
         /**
92
-        * @todo - change relation editor back to the custom block manager config and fix issues when 'creating' Blocks from a BlockSet.
93
-		*/
92
+         * @todo - change relation editor back to the custom block manager config and fix issues when 'creating' Blocks from a BlockSet.
93
+         */
94 94
         $gridConfig = GridFieldConfig_RelationEditor::create();
95 95
         $gridConfig->addComponent(new GridFieldOrderableRows('Sort'));
96 96
         $gridConfig->addComponent(new GridFieldDeleteAction());
Please login to merge, or discard this patch.
src/model/Block.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
 class Block extends DataObject implements PermissionProvider
35 35
 {
36 36
 
37
-	private static $table_name = 'Block';
37
+    private static $table_name = 'Block';
38 38
 
39 39
     /**
40 40
      * @var array
Please login to merge, or discard this patch.
src/model/ContentBlock.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 class ContentBlock extends Block
6 6
 {
7
-	private static $table_name = "ContentBlock";
7
+    private static $table_name = "ContentBlock";
8 8
 
9 9
     /**
10 10
      * If the singular name is set in a private static $singular_name, it cannot be changed using the translation files
@@ -30,13 +30,13 @@  discard block
 block discarded – undo
30 30
         'Content' => 'HTMLText',
31 31
     ];
32 32
 
33
-	public function fieldLabels($includeRelations = true)
34
-	{
35
-		return array_merge(
36
-			parent::fieldLabels($includeRelations),
37
-			[
38
-				'Content' => _t('Block.Content', 'Content'),
39
-			]
40
-		);
41
-	}
33
+    public function fieldLabels($includeRelations = true)
34
+    {
35
+        return array_merge(
36
+            parent::fieldLabels($includeRelations),
37
+            [
38
+                'Content' => _t('Block.Content', 'Content'),
39
+            ]
40
+        );
41
+    }
42 42
 }
Please login to merge, or discard this patch.
src/extensions/BlockSiteConfigExtension.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -13,15 +13,15 @@
 block discarded – undo
13 13
  */
14 14
 class BlockSiteConfigExtension extends DataExtension
15 15
 {
16
-	private static $many_many = [
17
-		"Blocks" => Block::class,
18
-	];
16
+    private static $many_many = [
17
+        "Blocks" => Block::class,
18
+    ];
19 19
 
20
-	/**
21
-	 *
22
-	 **/
23
-	public function updateCMSFields(FieldList $fields)
24
-	{
25
-		$fields->removeByName('Blocks');
26
-	}
20
+    /**
21
+     *
22
+     **/
23
+    public function updateCMSFields(FieldList $fields)
24
+    {
25
+        $fields->removeByName('Blocks');
26
+    }
27 27
 }
Please login to merge, or discard this patch.