Completed
Push — master ( 3f1f28...ffeb82 )
by Leo
02:33
created

Block::getCMSFields()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 68
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 68
rs 8.7864
cc 4
eloc 47
nc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class Block extends DataObject {
4
	protected static $db = array (
5
		'SortOrder'			=>	'Int',
6
		'Title'				=>	'Varchar(64)',
7
		'TitleWrapper'		=>	'Enum("h2,h3,h4,h5,h6")',
8
		'hideTitle'			=>	'Boolean',
9
		'showBlockbyClass'	=>	'Boolean',
10
		'Description'		=>	'Varchar(128)',
11
		'MemberVisibility'	=>	'Varchar(255)',
12
		'shownInClass'		=>	'Text'
13
	);
14
	
15
	protected static $many_many = array (
16
		'Pages'				=>	'Page'
17
	);
18
		
19
	protected static $create_table_options = array(
20
		'MySQLDatabase'		=> 'ENGINE=MyISAM'
21
    );
22
	
23
	protected static $extensions = array (
24
		'StandardPermissions'
25
	);
26
	
27
	protected static $summary_fields = array(
28
		'BlockType',
29
		'Title', 
30
		'Description',
31
		'shownOn',
32
		'VisibleTo'
33
	);
34
	
35
	protected static $field_labels = array(
36
		'BlockType'			=>	'Block type',
37
		'shownOn'			=>	'is shown on',
38
		'VisibleTo'			=>	'Visible to'
39
	);
40
	
41
	public function VisibleTo() {
42
		if (strlen(trim($this->MemberVisibility)) > 0) {
43
			$lists = 'Group: ' . str_replace(',','<br />Group: ', $this->MemberVisibility);
44
		}else{
45
			$lists = '<em>&lt;All&gt;</em>';
46
		}
47
		
48
		return new LiteralField('VisibleTo',$lists);
49
	}
50
	
51
	public function BlockType() {
52
		return $this->singular_name();
53
	}
54
	
55
	public function shownOn() {
56
		if ($this->showBlockbyClass) {
57
			if (strlen(trim($this->shownInClass)) > 0) {
58
				$lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
59
			}else{
60
				$lists = '<em>&lt;not assigned&gt;</em>';
61
			}
62
		}else{
63
			if ($this->Pages()->count() > 0) {
64
				$lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
65
			}else{
66
				$lists = '<em>&lt;not assigned&gt;</em>';
67
			}
68
		}
69
		return new LiteralField('shownOn',$lists);
70
	}
71
	
72
	public function getCMSFields() {
73
		$fields = parent::getCMSFields();
74
		$fields->removeFieldFromTab('Root', 'Pages');
75
		$fields->removeFieldsFromTab('Root.Main', array(
76
			'SortOrder',
77
			'showBlockbyClass',
78
			'shownInClass',
79
			'MemberVisibility'
80
		));
81
		
82
		$memberGroups = Group::get();
83
		$sourcemap = $memberGroups->map('Code', 'Title');
84
		$source = array(
85
			'anonymous'		=>	'Anonymous visitors'
86
		);
87
		foreach ($sourcemap as $mapping => $key) {
88
			$source[$mapping] = $key;
89
		}
90
		
91
		$memberVisibility = new CheckboxSetField(
92
			$name = "MemberVisibility",
93
			$title = "Show block for specific groups",
94
			$source
95
		);
96
		
97
		$memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
98
		
99
		$availabelClasses = $this->availableClasses();
100
		$inClass = new CheckboxSetField(
101
			$name = "shownInClass",
102
			$title = "Show block for specific content types",
103
			$availabelClasses
104
		);
105
		
106
		$filterSelector = OptionsetField::create(
107
			'showBlockbyClass',
108
			'Choose filter set',
109
			array(
110
				'0'			=>	'by page',
111
				'1'			=>	'by page/data type'
112
			)
113
		)->setDescription('<p><br /><strong>by page</strong>: block will be displayed in the selected page(s)<br /><strong>by page/data type</strong>: block will be displayed on the pages created with the particular page/data type. e.g. is <strong>"InternalPage"</strong> is picked, the block will be displayed, and will ONLY be displayed on all <strong>Internal Pages</strong></p>');
114
		
115
		$availablePages = Page::get()->exclude('ClassName', array(
116
			'ErrorPage',
117
			'RedirectorPage',
118
			'VirtualPage'
119
		));
120
		$pageSelector = new CheckboxSetField(
121
			$name = "Pages",
122
			$title = "Show on Page(s)",
123
			$availablePages
124
		);
125
		
126
		
127
		if ($this->canConfigPageAndType(Member::currentUser())) {
1 ignored issue
show
Bug introduced by
The method canConfigPageAndType() does not exist on Block. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128
			$fields->addFieldsToTab('Root.VisibilitySettings', array(
129
				$filterSelector,
130
				$pageSelector,
131
				$inClass
132
			));
133
		}
134
		
135
		if ($this->canConfigMemberVisibility(Member::currentUser())) {
1 ignored issue
show
Bug introduced by
The method canConfigMemberVisibility() does not exist on Block. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
			$fields->addFieldToTab('Root.VisibilitySettings', $memberVisibility);
137
		}
138
		return $fields;
139
	}
140
	
141
	private function availableClasses() {
142
		$Classes = array_diff(
143
			ClassInfo::subclassesFor('Page'),
144
			ClassInfo::subclassesFor('RedirectorPage'),
145
			ClassInfo::subclassesFor('VirtualPage')
146
		);
147
		return $Classes;
148
	}
149
	
150
	public function forTemplate() {
151
		if ($this->canDisplayMemberCheck()) {
152
			return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
153
		}
154
		
155
		return false;
156
	}
157
	
158
	private function canDisplayMemberCheck() {
159
		$rawVisibility = $this->MemberVisibility;
160
		
161
		if (empty($rawVisibility)) {
162
			return true;
163
		}
164
		
165
		$visibility = explode(',', $rawVisibility);
166
		$member = Member::currentUser();
167
		
168
		if (!$member && in_array('anonymous', $visibility)) {
169
			return true;
170
		}
171
		
172
		if ($member) {
173
			$memberGroups = $member->Groups()->column('Code');
174
			foreach ($memberGroups as $memberGroup) {
175
				if (in_array($memberGroup, $visibility)) {
176
					return true;
177
				}
178
			}
179
		}
180
		
181
		return false;
182
	}
183
	
184
	public function frontendEditable() {
185
		$member = Member::currentUser();		
186
		return $this->canEdit($member);
187
	}
188
	
189
	public function Type2Class() {
190
		return strtolower(str_replace(' ', '-', $this->singular_name()));
191
	}
192
}