Completed
Push — master ( 766fcd...e234d5 )
by Leo
02:05
created
code/Block.php 1 patch
Indentation   +242 added lines, -242 removed lines patch added patch discarded remove patch
@@ -1,185 +1,185 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 class Block extends DataObject {
3
-	private static $db = array (
4
-		'SortOrder'			=>	'Int',
5
-		'Title'				=>	'Varchar(64)',
6
-		'TitleWrapper'		=>	'Enum("h2,h3,h4,h5,h6")',
7
-		'hideTitle'			=>	'Boolean',
8
-		'showBlockbyClass'	=>	'Boolean',
9
-		'Description'		=>	'Varchar(128)',
10
-		'MemberVisibility'	=>	'Varchar(255)',
11
-		'shownInClass'		=>	'Text',
12
-		'addMarginTop'		=>	'Boolean',
13
-		'addMarginBottom'	=>	'Boolean',
14
-		'addPaddingTop'		=>	'Boolean',
15
-		'addPaddingBottom'	=>	'Boolean'
16
-	);
17
-
18
-	private static $many_many = array (
19
-		'Pages'				=>	'Page'
20
-	);
21
-
22
-	private static $default_sort = array(
23
-		'SortOrder'			=>	'ASC',
24
-		'ID'					=>	'DESC'
25
-	);
26
-
27
-	private static $create_table_options = array(
28
-		'MySQLDatabase'		=> 'ENGINE=MyISAM'
3
+    private static $db = array (
4
+        'SortOrder'			=>	'Int',
5
+        'Title'				=>	'Varchar(64)',
6
+        'TitleWrapper'		=>	'Enum("h2,h3,h4,h5,h6")',
7
+        'hideTitle'			=>	'Boolean',
8
+        'showBlockbyClass'	=>	'Boolean',
9
+        'Description'		=>	'Varchar(128)',
10
+        'MemberVisibility'	=>	'Varchar(255)',
11
+        'shownInClass'		=>	'Text',
12
+        'addMarginTop'		=>	'Boolean',
13
+        'addMarginBottom'	=>	'Boolean',
14
+        'addPaddingTop'		=>	'Boolean',
15
+        'addPaddingBottom'	=>	'Boolean'
29 16
     );
30 17
 
31
-	private static $extensions = array (
32
-		'StandardPermissions'
33
-	);
34
-
35
-	private static $summary_fields = array(
36
-		'BlockType',
37
-		'Title',
38
-		'Description',
39
-		'shownOn',
40
-		'VisibleTo',
41
-		'Published'
42
-	);
43
-
44
-	private static $field_labels = array(
45
-		'BlockType'			=>	'Block type',
46
-		'shownOn'			=>	'is shown on',
47
-		'VisibleTo'			=>	'Visible to'
48
-	);
49
-
50
-	public function VisibleTo() {
51
-		if (strlen(trim($this->MemberVisibility)) > 0) {
52
-			$lists = 'Group: ' . str_replace(',','<br />Group: ', $this->MemberVisibility);
53
-		}else{
54
-			$lists = '<em>&lt;All&gt;</em>';
55
-		}
56
-
57
-		return new LiteralField('VisibleTo',$lists);
58
-	}
59
-
60
-	public function BlockType() {
61
-		return $this->singular_name();
62
-	}
63
-
64
-	public function shownOn() {
65
-		if ($this->showBlockbyClass) {
66
-			if (strlen(trim($this->shownInClass)) > 0) {
67
-				$lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
68
-			}else{
69
-				$lists = '<em>&lt;not assigned&gt;</em>';
70
-			}
71
-		}else{
72
-			if ($this->Pages()->count() > 0) {
73
-				$lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
74
-			}else{
75
-				$lists = '<em>&lt;not assigned&gt;</em>';
76
-			}
77
-		}
78
-		return new LiteralField('shownOn',$lists);
79
-	}
80
-
81
-	public function getCMSFields() {
82
-		$fields = parent::getCMSFields();
83
-		$fields->removeFieldFromTab('Root', 'Pages');
84
-		$fields->removeFieldsFromTab('Root.Main', array(
85
-			'SortOrder',
86
-			'showBlockbyClass',
87
-			'shownInClass',
88
-			'MemberVisibility'
89
-		));
90
-
91
-		$fields->addFieldToTab('Root.Main', LiteralField::create('Status', 'Published: ' . $this->Published()), 'Title');
92
-
93
-		$memberGroups = Group::get();
94
-		$sourcemap = $memberGroups->map('Code', 'Title');
95
-		$source = array(
96
-			'anonymous'		=>	'Anonymous visitors'
97
-		);
98
-		foreach ($sourcemap as $mapping => $key) {
99
-			$source[$mapping] = $key;
100
-		}
101
-
102
-		$memberVisibility = new CheckboxSetField(
103
-			$name = "MemberVisibility",
104
-			$title = "Show block for specific groups",
105
-			$source
106
-		);
107
-
108
-		$memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
109
-
110
-		$availabelClasses = $this->availableClasses();
111
-		$inClass = new CheckboxSetField(
112
-			$name = "shownInClass",
113
-			$title = "Show block for specific content types",
114
-			$availabelClasses
115
-		);
116
-
117
-		$filterSelector = OptionsetField::create(
118
-			'showBlockbyClass',
119
-			'Choose filter set',
120
-			array(
121
-				'0'			=>	'by page',
122
-				'1'			=>	'by page/data type'
123
-			)
124
-		)->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>');
125
-
126
-		$availablePages = Page::get()->exclude('ClassName', array(
127
-			'ErrorPage',
128
-			'RedirectorPage',
129
-			'VirtualPage'
130
-		));
131
-		$pageSelector = new CheckboxSetField(
132
-			$name = "Pages",
133
-			$title = "Show on Page(s)",
134
-			$availablePages->map('ID','Title')
135
-		);
136
-
137
-
138
-		if ($this->canConfigPageAndType(Member::currentUser())) {
139
-			$fields->addFieldsToTab('Root.VisibilitySettings', array(
140
-				$filterSelector,
141
-				$pageSelector,
142
-				$inClass
143
-			));
144
-		}
145
-
146
-		if ($this->canConfigMemberVisibility(Member::currentUser())) {
147
-			$fields->addFieldToTab('Root.VisibilitySettings', $memberVisibility);
148
-		}
149
-
150
-		if (!$fields->fieldByName('Options')) {
151
-			$fields->insertBefore($right = RightSidebar::create('Options'), 'Root');
152
-	    }
153
-
154
-	    $fields->addFieldsToTab('Options', array(
155
-			CheckboxField::create('addMarginTop', 'add "margin-top" class to block wrapper'),
156
-			CheckboxField::create('addMarginBottom', 'add "margin-bottom" class to block wrapper'),
157
-            		CheckboxField::create('addPaddingTop', 'add "padding-top" class to block wrapper'),
158
-			CheckboxField::create('addPaddingBottom', 'add "padding-bottom" class to block wrapper')
159
-	    ));
160
-
161
-		return $fields;
162
-	}
163
-
164
-	public function doPublish() {
165
-		$this->writeToStage('Live');
166
-	}
167
-
168
-	public function onBeforeWrite() {
169
-		parent::onBeforeWrite();
170
-		if (empty($this->byPass)) {
171
-			$this->readmode = Versioned::get_reading_mode();
172
-			Versioned::set_reading_mode('Stage.Stage');
173
-		}
174
-	}
175
-
176
-	public function onAfterWrite() {
177
-		parent::onAfterWrite();
178
-		if (isset($this->readmode)) {
179
-			Versioned::set_reading_mode('Stage.' . $this->readmode);
180
-		}
181
-
182
-		/*if ($this->isPublished()) {
18
+    private static $many_many = array (
19
+        'Pages'				=>	'Page'
20
+    );
21
+
22
+    private static $default_sort = array(
23
+        'SortOrder'			=>	'ASC',
24
+        'ID'					=>	'DESC'
25
+    );
26
+
27
+    private static $create_table_options = array(
28
+        'MySQLDatabase'		=> 'ENGINE=MyISAM'
29
+    );
30
+
31
+    private static $extensions = array (
32
+        'StandardPermissions'
33
+    );
34
+
35
+    private static $summary_fields = array(
36
+        'BlockType',
37
+        'Title',
38
+        'Description',
39
+        'shownOn',
40
+        'VisibleTo',
41
+        'Published'
42
+    );
43
+
44
+    private static $field_labels = array(
45
+        'BlockType'			=>	'Block type',
46
+        'shownOn'			=>	'is shown on',
47
+        'VisibleTo'			=>	'Visible to'
48
+    );
49
+
50
+    public function VisibleTo() {
51
+        if (strlen(trim($this->MemberVisibility)) > 0) {
52
+            $lists = 'Group: ' . str_replace(',','<br />Group: ', $this->MemberVisibility);
53
+        }else{
54
+            $lists = '<em>&lt;All&gt;</em>';
55
+        }
56
+
57
+        return new LiteralField('VisibleTo',$lists);
58
+    }
59
+
60
+    public function BlockType() {
61
+        return $this->singular_name();
62
+    }
63
+
64
+    public function shownOn() {
65
+        if ($this->showBlockbyClass) {
66
+            if (strlen(trim($this->shownInClass)) > 0) {
67
+                $lists = 'Type: ' . str_replace(',','<br />Type: ', $this->shownInClass);
68
+            }else{
69
+                $lists = '<em>&lt;not assigned&gt;</em>';
70
+            }
71
+        }else{
72
+            if ($this->Pages()->count() > 0) {
73
+                $lists = 'Page: ' . implode('<br />Page: ', $this->Pages()->column('Title'));
74
+            }else{
75
+                $lists = '<em>&lt;not assigned&gt;</em>';
76
+            }
77
+        }
78
+        return new LiteralField('shownOn',$lists);
79
+    }
80
+
81
+    public function getCMSFields() {
82
+        $fields = parent::getCMSFields();
83
+        $fields->removeFieldFromTab('Root', 'Pages');
84
+        $fields->removeFieldsFromTab('Root.Main', array(
85
+            'SortOrder',
86
+            'showBlockbyClass',
87
+            'shownInClass',
88
+            'MemberVisibility'
89
+        ));
90
+
91
+        $fields->addFieldToTab('Root.Main', LiteralField::create('Status', 'Published: ' . $this->Published()), 'Title');
92
+
93
+        $memberGroups = Group::get();
94
+        $sourcemap = $memberGroups->map('Code', 'Title');
95
+        $source = array(
96
+            'anonymous'		=>	'Anonymous visitors'
97
+        );
98
+        foreach ($sourcemap as $mapping => $key) {
99
+            $source[$mapping] = $key;
100
+        }
101
+
102
+        $memberVisibility = new CheckboxSetField(
103
+            $name = "MemberVisibility",
104
+            $title = "Show block for specific groups",
105
+            $source
106
+        );
107
+
108
+        $memberVisibility->setDescription('Show this block only for the selected group(s). If you select no groups, the block will be visible to all members.');
109
+
110
+        $availabelClasses = $this->availableClasses();
111
+        $inClass = new CheckboxSetField(
112
+            $name = "shownInClass",
113
+            $title = "Show block for specific content types",
114
+            $availabelClasses
115
+        );
116
+
117
+        $filterSelector = OptionsetField::create(
118
+            'showBlockbyClass',
119
+            'Choose filter set',
120
+            array(
121
+                '0'			=>	'by page',
122
+                '1'			=>	'by page/data type'
123
+            )
124
+        )->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>');
125
+
126
+        $availablePages = Page::get()->exclude('ClassName', array(
127
+            'ErrorPage',
128
+            'RedirectorPage',
129
+            'VirtualPage'
130
+        ));
131
+        $pageSelector = new CheckboxSetField(
132
+            $name = "Pages",
133
+            $title = "Show on Page(s)",
134
+            $availablePages->map('ID','Title')
135
+        );
136
+
137
+
138
+        if ($this->canConfigPageAndType(Member::currentUser())) {
139
+            $fields->addFieldsToTab('Root.VisibilitySettings', array(
140
+                $filterSelector,
141
+                $pageSelector,
142
+                $inClass
143
+            ));
144
+        }
145
+
146
+        if ($this->canConfigMemberVisibility(Member::currentUser())) {
147
+            $fields->addFieldToTab('Root.VisibilitySettings', $memberVisibility);
148
+        }
149
+
150
+        if (!$fields->fieldByName('Options')) {
151
+            $fields->insertBefore($right = RightSidebar::create('Options'), 'Root');
152
+        }
153
+
154
+        $fields->addFieldsToTab('Options', array(
155
+            CheckboxField::create('addMarginTop', 'add "margin-top" class to block wrapper'),
156
+            CheckboxField::create('addMarginBottom', 'add "margin-bottom" class to block wrapper'),
157
+                    CheckboxField::create('addPaddingTop', 'add "padding-top" class to block wrapper'),
158
+            CheckboxField::create('addPaddingBottom', 'add "padding-bottom" class to block wrapper')
159
+        ));
160
+
161
+        return $fields;
162
+    }
163
+
164
+    public function doPublish() {
165
+        $this->writeToStage('Live');
166
+    }
167
+
168
+    public function onBeforeWrite() {
169
+        parent::onBeforeWrite();
170
+        if (empty($this->byPass)) {
171
+            $this->readmode = Versioned::get_reading_mode();
172
+            Versioned::set_reading_mode('Stage.Stage');
173
+        }
174
+    }
175
+
176
+    public function onAfterWrite() {
177
+        parent::onAfterWrite();
178
+        if (isset($this->readmode)) {
179
+            Versioned::set_reading_mode('Stage.' . $this->readmode);
180
+        }
181
+
182
+        /*if ($this->isPublished()) {
183 183
 			$live = Versioned::get_by_stage('Block', 'Live')->byID($this->ID);
184 184
 			$stage = Versioned::get_by_stage('Block', 'Stage')->byID($this->ID);
185 185
 			if ($live->SortOrder != $stage->SortOrder) {
@@ -188,74 +188,74 @@  discard block
 block discarded – undo
188 188
 				$this->doPublish();
189 189
 			}
190 190
 		}*/
191
-	}
192
-
193
-	public function availableClasses() {
194
-		$Classes = array_diff(
195
-			ClassInfo::subclassesFor('Page'),
196
-			ClassInfo::subclassesFor('RedirectorPage'),
197
-			ClassInfo::subclassesFor('VirtualPage')
198
-		);
199
-		return $Classes;
200
-	}
201
-
202
-	public function forTemplate() {
203
-		if ($this->canDisplayMemberCheck()) {
204
-			return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
205
-		}
206
-
207
-		return false;
208
-	}
209
-
210
-	public function canDisplayMemberCheck() {
211
-		$rawVisibility = $this->MemberVisibility;
212
-
213
-		if (empty($rawVisibility)) {
214
-			return true;
215
-		}
216
-
217
-		$visibility = explode(',', $rawVisibility);
218
-		$member = Member::currentUser();
219
-
220
-		if (!$member && in_array('anonymous', $visibility)) {
221
-			return true;
222
-		}
223
-
224
-		if ($member) {
225
-			$memberGroups = $member->Groups()->column('Code');
226
-			foreach ($memberGroups as $memberGroup) {
227
-				if (in_array($memberGroup, $visibility)) {
228
-					return true;
229
-				}
230
-			}
231
-		}
232
-
233
-		return false;
234
-	}
235
-
236
-	public function frontendEditable() {
237
-		$member = Member::currentUser();
238
-		return $this->canEdit($member) && Config::inst()->get('Block', 'FrontendEditable');
239
-	}
240
-
241
-	public function Type2Class() {
242
-		return strtolower(str_replace(' ', '-', $this->singular_name()));
243
-	}
244
-
245
-	/*public function doPublish() {
191
+    }
192
+
193
+    public function availableClasses() {
194
+        $Classes = array_diff(
195
+            ClassInfo::subclassesFor('Page'),
196
+            ClassInfo::subclassesFor('RedirectorPage'),
197
+            ClassInfo::subclassesFor('VirtualPage')
198
+        );
199
+        return $Classes;
200
+    }
201
+
202
+    public function forTemplate() {
203
+        if ($this->canDisplayMemberCheck()) {
204
+            return $this->renderWith(array($this->getClassName(), 'BaseBlock'));
205
+        }
206
+
207
+        return false;
208
+    }
209
+
210
+    public function canDisplayMemberCheck() {
211
+        $rawVisibility = $this->MemberVisibility;
212
+
213
+        if (empty($rawVisibility)) {
214
+            return true;
215
+        }
216
+
217
+        $visibility = explode(',', $rawVisibility);
218
+        $member = Member::currentUser();
219
+
220
+        if (!$member && in_array('anonymous', $visibility)) {
221
+            return true;
222
+        }
223
+
224
+        if ($member) {
225
+            $memberGroups = $member->Groups()->column('Code');
226
+            foreach ($memberGroups as $memberGroup) {
227
+                if (in_array($memberGroup, $visibility)) {
228
+                    return true;
229
+                }
230
+            }
231
+        }
232
+
233
+        return false;
234
+    }
235
+
236
+    public function frontendEditable() {
237
+        $member = Member::currentUser();
238
+        return $this->canEdit($member) && Config::inst()->get('Block', 'FrontendEditable');
239
+    }
240
+
241
+    public function Type2Class() {
242
+        return strtolower(str_replace(' ', '-', $this->singular_name()));
243
+    }
244
+
245
+    /*public function doPublish() {
246 246
 		$this->writeToStage('Live');
247 247
 	}*/
248 248
 
249
-	public function Published() {
250
-		return $this->isPublished() ? 'Yes' : 'No';
251
-	}
249
+    public function Published() {
250
+        return $this->isPublished() ? 'Yes' : 'No';
251
+    }
252 252
 
253
-	public function isPublished() {
254
-		if (!empty(Versioned::get_by_stage('Block', 'Live')->byID($this->ID))) {
255
-			return true;
256
-		}
253
+    public function isPublished() {
254
+        if (!empty(Versioned::get_by_stage('Block', 'Live')->byID($this->ID))) {
255
+            return true;
256
+        }
257 257
 
258
-		return false;
259
-	}
258
+        return false;
259
+    }
260 260
 
261 261
 }
Please login to merge, or discard this patch.