Completed
Push — master ( abeeee...712df8 )
by Leo
02:34
created

BlockinPage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 11 3
A dockedBlocks() 0 14 3
B gridBuilder() 0 23 4
1
<?php
2
class BlockinPage extends Extension {
3
	protected static $belongs_many_many = array (
4
		'Blocks'			=>	'Block'
5
	);
6
	
7
	public function updateCMSFields( FieldList $fields ) {
8
		$ancestry = ClassInfo::ancestry($this->owner->ClassName);
9
		if (!in_array('RedirectorPage', $ancestry) && !in_array('VirtualPage', $ancestry)) {
10
			$blocks = $this->owner->Blocks();
11
			$blocks_grid = $this->gridBuilder('Blocks', $blocks, '', true,'GridFieldConfig_RelationEditor');
12
			$docked_grid = $this->gridBuilder('DockedBlocks', $this->dockedBlocks(), '');
13
			
14
			$fields->addFieldToTab('Root.MyBlocks', $blocks_grid);
15
			$fields->addFieldToTab('Root.DockedBlocks', $docked_grid);
16
		}
17
	}
18
	
19
	private function gridBuilder($name, $source, $label = '', $canAdd = false, $gridHeaderType = 'GridFieldConfig_RecordEditor') {
20
		/*
21
		GridFieldConfig_Base
22
		GridFieldConfig_RecordViewer
23
		GridFieldConfig_RecordEditor
24
		GridFieldConfig_RelationEditor
25
		*/
26
		if ($label == '') { $label = $name; }
27
		$grid = new GridField($name, $label, $source);
28
		$config = $gridHeaderType::create();
29
		$config->removeComponentsByType('GridFieldAddNewButton');
30
		if ( $canAdd ) {
31
			$config->addComponents(
32
				$multiClass = new GridFieldAddNewMultiClass(),
33
				$sortable = new GridFieldOrderableRows('SortOrder')
34
			);
35
			$subBlocks = ClassInfo::subclassesFor('Block') || array();
36
			unset($subBlocks['Block']);
37
			$multiClass->setClasses($subBlocks);
0 ignored issues
show
Documentation introduced by
$subBlocks is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
		}
39
		$grid->setConfig($config);
40
		return $grid;
41
	}
42
		
43
	private function dockedBlocks() {
44
		$blocks = Block::get();
45
		$IDs = array();
46
		$ClassName = $this->owner->ClassName;
47
		$Classes = $blocks->map('ID', 'shownInClass');
48
		foreach ($Classes as $BlockID => $Class) {
49
			$listedClasses = explode(',', $Class);
50
			if (in_array($ClassName, $listedClasses)) {
51
				$IDs[] = $BlockID;
52
			}
53
		}
54
		$blocks = Block::get()->filter('ID', $IDs)->sort('SortOrder','ASC');
55
		return $blocks;
56
	}
57
}