Completed
Push — master ( 093a3a...44d378 )
by Leo
02:17
created

Block::canDisplayMemberCheck()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 6.7272
cc 7
eloc 14
nc 5
nop 0
1
<?php
2
3
class Block extends DataObject {
4
	protected static $db = array (
5
		'SortOrder'			=>	'Int',
6
		'Title'				=>	'Varchar(64)',
7
		'hideTitle'			=>	'Boolean',
8
		'showBlockbyClass'	=>	'Boolean',
9
		'Description'		=>	'Varchar(128)',
10
		'MemberVisibility'	=>	'Varchar(255)',
11
		'shownInClass'		=>	'Text'
12
	);
13
	
14
	protected static $many_many = array (
15
		'Pages'				=>	'Page'
16
	);
17
		
18
	protected static $create_table_options = array(
19
		'MySQLDatabase'		=> 'ENGINE=MyISAM'
20
    );
21
	
22
	protected static $extensions = array (
23
		'StandardPermissions'
24
	);
25
	
26
	protected static $summary_fields = array(
27
		'BlockType',
28
		'Title', 
29
		'Description',
30
		'shownOn'
31
	);
32
	
33
	protected static $field_labels = array(
34
		'shownOn'				=>	'is shown on'
35
	);
36
	
37
	public function BlockType() {
38
		return $this->singular_name();
39
	}
40
	
41
	public function shownOn() {
42
		$lists = '';
0 ignored issues
show
Unused Code introduced by
$lists is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
		if ($this->showBlockbyClass) {
44
			if (strlen(trim($this->shownInClass)) > 0) {
45
				$lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
46
			}else{
47
				$lists = '<em>&lt;not assigned&gt;</em>';
48
			}
49
		}else{
50
			$lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
51
		}
52
		return new LiteralField('shownOn',$lists);
53
	}
54
	
55
	
56
	public function getCMSFields() {
57
		$fields = parent::getCMSFields();
58
		$fields->removeFieldFromTab('Root', 'Pages');
59
		$fields->removeFieldsFromTab('Root.Main', array(
60
			'SortOrder',
61
			'showBlockbyClass',
62
			'shownInClass',
63
			'MemberVisibility'
64
		));
65
		
66
		$memberGroups = Group::get();
67
		$sourcemap = $memberGroups->map('Code', 'Title');
68
		$source = array(
69
			'anonymous'		=>	'Anonymous visitors'
70
		);
71
		foreach ($sourcemap as $mapping => $key) {
72
			$source[$mapping] = $key;
73
		}
74
		
75
		$memberVisibility = new CheckboxSetField(
76
			$name = "MemberVisibility",
77
			$title = "Show block for specific groups",
78
			$source
79
		);
80
		
81
		$memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
82
		
83
		$availabelClasses = $this->availableClasses();
84
		$inClass = new CheckboxSetField(
85
			$name = "shownInClass",
86
			$title = "Show block for specific content types",
87
			$availabelClasses
88
		);
89
		
90
		$filterSelector = OptionsetField::create(
91
			'showBlockbyClass',
92
			'Choose filter set',
93
			array(
94
				'0'			=>	'by page',
95
				'1'			=>	'by page/data type'
96
			)
97
		)->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>');
98
		
99
		$availablePages = Page::get()->exclude('ClassName', array(
100
			'ErrorPage',
101
			'RedirectorPage',
102
			'VirtualPage'
103
		));
104
		$pageSelector = new CheckboxSetField(
105
			$name = "Pages",
106
			$title = "Show on Page(s)",
107
			$availablePages
108
		);
109
		
110
		$fields->addFieldsToTab('Root.VisibilitySettings', array(
111
			$filterSelector,
112
			$pageSelector,
113
			$inClass,
114
			$memberVisibility
115
		));
116
		return $fields;
117
	}
118
	
119
	private function availableClasses() {
120
		/*$Classes = array_diff(
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
			ClassInfo::subclassesFor('DataObject'), 
122
			ClassInfo::subclassesFor('Block'), 
123
			ClassInfo::subclassesFor('File'),
124
			ClassInfo::subclassesFor('LoginAttempt'),
125
			ClassInfo::subclassesFor('Group'),
126
			ClassInfo::subclassesFor('Member'),
127
			ClassInfo::subclassesFor('MemberPassword'),
128
			ClassInfo::subclassesFor('Permission'),
129
			ClassInfo::subclassesFor('PermissionRole'),
130
			ClassInfo::subclassesFor('PermissionRoleCode'),
131
			ClassInfo::subclassesFor('LoginAttempt'),
132
			ClassInfo::subclassesFor('ErrorPage'),
133
			ClassInfo::subclassesFor('RedirectorPage'),
134
			ClassInfo::subclassesFor('VirtualPage'),
135
			ClassInfo::subclassesFor('LeftAndMainTest_Object'),
136
			ClassInfo::subclassesFor('ModelAdminTest_Contact'),
137
			ClassInfo::subclassesFor('ModelAdminTest_Player'),
138
			ClassInfo::subclassesFor('SiteConfig')
139
		);
140
		
141
		unset($Classes['DataObject']);
142
		unset($Classes['SiteTree']);*/
143
		
144
		$Classes = array_diff(
145
			ClassInfo::subclassesFor('Page'),
146
			ClassInfo::subclassesFor('ErrorPage'),
147
			ClassInfo::subclassesFor('RedirectorPage'),
148
			ClassInfo::subclassesFor('VirtualPage')
149
		);
150
		
151
		
152
		return $Classes;
153
	}
154
	
155
	public function forTemplate() {
156
		if (!empty(trim($this->shownInClass))) {
157 View Code Duplication
			if ($this->canDisplayMemberCheck() && $this->canDisplayURLCheck() && $this->canDisplayClassCheck()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
				return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
159
			}
160 View Code Duplication
		}else{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
			if ($this->canDisplayMemberCheck() && $this->canDisplayURLCheck()) {
162
				return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
163
			}
164
		}
165
		
166
		return false;
167
	}
168
	
169
	private function canDisplayClassCheck() {
170
		$ClassName = Controller::curr()->ClassName;
171
		$Classes = explode(',', $this->shownInClass);
172
		
173
		if (in_array($ClassName, $Classes)) {
174
			
175
			return true;
176
		}
177
		return false;
178
	}
179
	
180
	private function canDisplayMemberCheck() {
181
		$rawVisibility = $this->MemberVisibility;
182
		
183
		if (empty($rawVisibility)) {
184
			return true;
185
		}
186
		
187
		$visibility = explode(',', $rawVisibility);
188
		$member = Member::currentUser();
189
		
190
		if (!$member && in_array('anonymous', $visibility)) {
191
			return true;
192
		}
193
		
194
		if ($member) {
195
			$memberGroups = $member->Groups()->column('Code');
196
			foreach ($memberGroups as $memberGroup) {
197
				if (in_array($memberGroup, $visibility)) {
198
					return true;
199
				}
200
			}
201
		}
202
		
203
		return false;
204
	}
205
	
206
	public function frontendEditable() {
207
		return true;
208
	}
209
	
210
	public function curLink() {
211
		$URI = $_SERVER['REQUEST_URI'];
212
		return rtrim(ltrim($URI, '/'), '/');
213
	}
214
}