|
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 = ''; |
|
|
|
|
|
|
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><not assigned></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
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.