Completed
Branch master (f11977)
by Leo
02:15
created

Block::availableClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
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
		'BlockType'			=>	'Block type',
35
		'shownOn'			=>	'is shown on'
36
	);
37
	
38
	public function BlockType() {
39
		return $this->singular_name();
40
	}
41
	
42
	public function shownOn() {
43
		$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...
44
		if ($this->showBlockbyClass) {
45
			if (strlen(trim($this->shownInClass)) > 0) {
46
				$lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
47
			}else{
48
				$lists = '<em>&lt;not assigned&gt;</em>';
49
			}
50
		}else{
51
			$lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
52
		}
53
		return new LiteralField('shownOn',$lists);
54
	}
55
	
56
	
57
	public function getCMSFields() {
58
		$fields = parent::getCMSFields();
59
		$fields->removeFieldFromTab('Root', 'Pages');
60
		$fields->removeFieldsFromTab('Root.Main', array(
61
			'SortOrder',
62
			'showBlockbyClass',
63
			'shownInClass',
64
			'MemberVisibility'
65
		));
66
		
67
		$memberGroups = Group::get();
68
		$sourcemap = $memberGroups->map('Code', 'Title');
69
		$source = array(
70
			'anonymous'		=>	'Anonymous visitors'
71
		);
72
		foreach ($sourcemap as $mapping => $key) {
73
			$source[$mapping] = $key;
74
		}
75
		
76
		$memberVisibility = new CheckboxSetField(
77
			$name = "MemberVisibility",
78
			$title = "Show block for specific groups",
79
			$source
80
		);
81
		
82
		$memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
83
		
84
		$availabelClasses = $this->availableClasses();
85
		$inClass = new CheckboxSetField(
86
			$name = "shownInClass",
87
			$title = "Show block for specific content types",
88
			$availabelClasses
89
		);
90
		
91
		$filterSelector = OptionsetField::create(
92
			'showBlockbyClass',
93
			'Choose filter set',
94
			array(
95
				'0'			=>	'by page',
96
				'1'			=>	'by page/data type'
97
			)
98
		)->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>');
99
		
100
		$availablePages = Page::get()->exclude('ClassName', array(
101
			'ErrorPage',
102
			'RedirectorPage',
103
			'VirtualPage'
104
		));
105
		$pageSelector = new CheckboxSetField(
106
			$name = "Pages",
107
			$title = "Show on Page(s)",
108
			$availablePages
109
		);
110
		
111
		$fields->addFieldsToTab('Root.VisibilitySettings', array(
112
			$filterSelector,
113
			$pageSelector,
114
			$inClass,
115
			$memberVisibility
116
		));
117
		return $fields;
118
	}
119
	
120
	private function availableClasses() {
121
		$Classes = array_diff(
122
			ClassInfo::subclassesFor('Page'),
123
			ClassInfo::subclassesFor('ErrorPage'),
124
			ClassInfo::subclassesFor('RedirectorPage'),
125
			ClassInfo::subclassesFor('VirtualPage')
126
		);
127
		return $Classes;
128
	}
129
	
130
	public function forTemplate() {
131
		if ($this->canDisplayMemberCheck()) {
132
			return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
133
		}
134
		
135
		return false;
136
	}
137
	
138
	private function canDisplayMemberCheck() {
139
		$rawVisibility = $this->MemberVisibility;
140
		
141
		if (empty($rawVisibility)) {
142
			return true;
143
		}
144
		
145
		$visibility = explode(',', $rawVisibility);
146
		$member = Member::currentUser();
147
		
148
		if (!$member && in_array('anonymous', $visibility)) {
149
			return true;
150
		}
151
		
152
		if ($member) {
153
			$memberGroups = $member->Groups()->column('Code');
154
			foreach ($memberGroups as $memberGroup) {
155
				if (in_array($memberGroup, $visibility)) {
156
					return true;
157
				}
158
			}
159
		}
160
		
161
		return false;
162
	}
163
	
164
	public function frontendEditable() {
165
		$member = Member::currentUser();		
166
		return $this->canEdit($member);
167
	}
168
}