Completed
Push — master ( cd834b...e038e5 )
by Leo
03:10
created

BlocksAdmin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 7
c 6
b 0
f 0
lcom 1
cbo 5
dl 0
loc 50
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditForm() 0 22 1
B getAvaiableTypes() 0 19 6
1
<?php
2
/**
3
 * @file BlocksAdmin.php
4
 *
5
 * Left-hand-side tab : Admin Blocks
6
 * */
7
class BlocksAdmin extends ModelAdmin {
8
	private static $managed_models = array('Block');
9
	private static $url_segment = 'blocks';
10
	private static $menu_title = 'Blocks';
11
	private static $menu_priority = 10;
12
	private static $menu_icon = 'silverstripe-block/images/icon-block.png';
13
		
14
	public function getEditForm($id = null, $fields = null) {
15
		$form = parent::getEditForm($id, $fields);
16
		
17
		$grid = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
18
		
19
		
20
		$grid->getConfig()
21
			->removeComponentsByType('GridFieldPaginator')
22
			->removeComponentsByType('GridFieldAddNewButton')
23
			->removeComponentsByType('GridFieldPrintButton')
24
			->removeComponentsByType('GridFieldExportButton')
25
			->addComponents(
26
				new GridFieldPaginatorWithShowAll(30),
27
				$multiClass = new MultiClassSelector(),
28
				$sortable = new GridFieldOrderableRows('SortOrder')
29
			);
30
			
31
		$subBlocks = self::getAvaiableTypes();
32
		$multiClass->setClasses($subBlocks);
33
		$grid->setTitle('All Blcoks');
34
		return $form;
35
	}
36
	
37
	public static function getAvaiableTypes() {
38
		$subBlocks = ClassInfo::subclassesFor('Block');
39
		if (is_null($subBlocks)) {
40
			$subBlocks = array('Block');
41
		}else{
42
			$disabledTypes = Config::inst()->get('Block','DisabledBlocks');
43
			Debugger::inspect($disabledTypes);
44
			if (!empty($disabledTypes)) {
45
				foreach ($disabledTypes as $disabledType) {
46
					unset($subBlocks[$disabledType]);
47
				}
48
			}
49
			foreach ($subBlocks as $key => &$value) {
50
				$value = empty($key::$singular_name) ? ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $value)))) : $key::$singular_name;
51
			}
52
		}
53
		
54
		return $subBlocks;
55
	}
56
}
57