Completed
Push — master ( 25a516...fbacac )
by Ingo
10s
created
code/model/SiteConfig.php 1 patch
Indentation   +424 added lines, -424 removed lines patch added patch discarded remove patch
@@ -22,428 +22,428 @@
 block discarded – undo
22 22
  */
23 23
 class SiteConfig extends DataObject implements PermissionProvider, TemplateGlobalProvider
24 24
 {
25
-    private static $db = array(
26
-        "Title" => "Varchar(255)",
27
-        "Tagline" => "Varchar(255)",
28
-        "Theme" => "Varchar(255)",
29
-        "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
30
-        "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
31
-        "CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
32
-    );
33
-
34
-    private static $many_many = array(
35
-        "ViewerGroups" => "Group",
36
-        "EditorGroups" => "Group",
37
-        "CreateTopLevelGroups" => "Group"
38
-    );
39
-
40
-    private static $defaults = array(
41
-        "CanViewType" => "Anyone",
42
-        "CanEditType" => "LoggedInUsers",
43
-        "CanCreateTopLevelType" => "LoggedInUsers",
44
-    );
45
-
46
-    /**
47
-     * @config
48
-     *
49
-     * @var array
50
-     */
51
-    private static $disabled_themes = array();
52
-
53
-    /**
54
-     * Default permission to check for 'LoggedInUsers' to create or edit pages
55
-     *
56
-     * @var array
57
-     * @config
58
-     */
59
-    private static $required_permission = array('CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain');
60
-
61
-
62
-    public function populateDefaults()
63
-    {
64
-        $this->Title = _t('SiteConfig.SITENAMEDEFAULT', "Your Site Name");
65
-        $this->Tagline = _t('SiteConfig.TAGLINEDEFAULT', "your tagline here");
66
-
67
-        // Allow these defaults to be overridden
68
-        parent::populateDefaults();
69
-    }
70
-
71
-    /**
72
-     * Get the fields that are sent to the CMS.
73
-     *
74
-     * In your extensions: updateCMSFields($fields).
75
-     *
76
-     * @return FieldList
77
-     */
78
-    public function getCMSFields()
79
-    {
80
-        $groupsMap = array();
81
-
82
-        foreach (Group::get() as $group) {
83
-            // Listboxfield values are escaped, use ASCII char instead of »
84
-            $groupsMap[$group->ID] = $group->getBreadcrumbs(' > ');
85
-        }
86
-        asort($groupsMap);
87
-
88
-        $fields = new FieldList(
89
-            new TabSet("Root",
90
-                $tabMain = new Tab('Main',
91
-                    $titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
92
-                    $taglineField = new TextField("Tagline", _t('SiteConfig.SITETAGLINE', "Site Tagline/Slogan")),
93
-                    $themeDropdownField = new DropdownField("Theme", _t('SiteConfig.THEME', 'Theme'), $this->getAvailableThemes())
94
-                ),
95
-                $tabAccess = new Tab('Access',
96
-                    $viewersOptionsField = new OptionsetField("CanViewType", _t('SiteConfig.VIEWHEADER', "Who can view pages on this site?")),
97
-                    $viewerGroupsField = ListboxField::create("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups"))
98
-                        ->setSource($groupsMap)
99
-                        ->setAttribute(
100
-                            'data-placeholder',
101
-                            _t('SiteTree.GroupPlaceholder', 'Click to select group')
102
-                        ),
103
-                    $editorsOptionsField = new OptionsetField("CanEditType", _t('SiteConfig.EDITHEADER', "Who can edit pages on this site?")),
104
-                    $editorGroupsField = ListboxField::create("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups"))
105
-                        ->setSource($groupsMap)
106
-                        ->setAttribute(
107
-                            'data-placeholder',
108
-                            _t('SiteTree.GroupPlaceholder', 'Click to select group')
109
-                        ),
110
-                    $topLevelCreatorsOptionsField = new OptionsetField("CanCreateTopLevelType", _t('SiteConfig.TOPLEVELCREATE', "Who can create pages in the root of the site?")),
111
-                    $topLevelCreatorsGroupsField = ListboxField::create("CreateTopLevelGroups", _t('SiteTree.TOPLEVELCREATORGROUPS', "Top level creators"))
112
-                        ->setSource($groupsMap)
113
-                        ->setAttribute(
114
-                            'data-placeholder',
115
-                            _t('SiteTree.GroupPlaceholder', 'Click to select group')
116
-                        )
117
-                )
118
-            ),
119
-            new HiddenField('ID')
120
-        );
121
-
122
-        $themeDropdownField->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)'));
123
-
124
-        $viewersOptionsSource = array();
125
-        $viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
126
-        $viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
127
-        $viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
128
-        $viewersOptionsField->setSource($viewersOptionsSource);
129
-
130
-        $editorsOptionsSource = array();
131
-        $editorsOptionsSource["LoggedInUsers"] = _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS");
132
-        $editorsOptionsSource["OnlyTheseUsers"] = _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)");
133
-        $editorsOptionsField->setSource($editorsOptionsSource);
134
-
135
-        $topLevelCreatorsOptionsField->setSource($editorsOptionsSource);
136
-
137
-        if (!Permission::check('EDIT_SITECONFIG')) {
138
-            $fields->makeFieldReadonly($viewersOptionsField);
139
-            $fields->makeFieldReadonly($viewerGroupsField);
140
-            $fields->makeFieldReadonly($editorsOptionsField);
141
-            $fields->makeFieldReadonly($editorGroupsField);
142
-            $fields->makeFieldReadonly($topLevelCreatorsOptionsField);
143
-            $fields->makeFieldReadonly($topLevelCreatorsGroupsField);
144
-            $fields->makeFieldReadonly($taglineField);
145
-            $fields->makeFieldReadonly($titleField);
146
-        }
147
-
148
-        if (file_exists(BASE_PATH . '/install.php')) {
149
-            $fields->addFieldToTab("Root.Main", new LiteralField("InstallWarningHeader",
150
-                "<p class=\"message warning\">" . _t("SiteTree.REMOVE_INSTALL_WARNING",
151
-                "Warning: You should remove install.php from this SilverStripe install for security reasons.")
152
-                . "</p>"), "Title");
153
-        }
154
-
155
-        $tabMain->setTitle(_t('SiteConfig.TABMAIN', "Main"));
156
-        $tabAccess->setTitle(_t('SiteConfig.TABACCESS', "Access"));
157
-        $this->extend('updateCMSFields', $fields);
158
-
159
-        return $fields;
160
-    }
161
-
162
-    /**
163
-     * Get all available themes that haven't been marked as disabled.
164
-     *
165
-     * @param string $baseDir Optional alternative theme base directory for testing
166
-     *
167
-     * @return array of theme directory names
168
-     */
169
-    public function getAvailableThemes($baseDir = null)
170
-    {
171
-        $themes = SSViewer::get_themes($baseDir);
172
-        $disabled = (array)$this->config()->disabled_themes;
173
-
174
-        foreach ($disabled as $theme) {
175
-            if (isset($themes[$theme])) {
176
-                unset($themes[$theme]);
177
-            }
178
-        }
179
-
180
-        return $themes;
181
-    }
182
-
183
-    /**
184
-     * Get the actions that are sent to the CMS.
185
-     *
186
-     * In your extensions: updateEditFormActions($actions)
187
-     *
188
-     * @return FieldList
189
-     */
190
-    public function getCMSActions()
191
-    {
192
-        if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) {
193
-            $actions = new FieldList(
194
-                FormAction::create('save_siteconfig', _t('CMSMain.SAVE', 'Save'))
195
-                    ->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
196
-            );
197
-        } else {
198
-            $actions = new FieldList();
199
-        }
200
-
201
-        $this->extend('updateCMSActions', $actions);
202
-
203
-        return $actions;
204
-    }
205
-
206
-    /**
207
-     * @return string
208
-     */
209
-    public function CMSEditLink()
210
-    {
211
-        return singleton('CMSSettingsController')->Link();
212
-    }
213
-
214
-    /**
215
-     * Get the current sites SiteConfig, and creates a new one through
216
-     * {@link make_site_config()} if none is found.
217
-     *
218
-     * @return SiteConfig
219
-     */
220
-    public static function current_site_config()
221
-    {
222
-        if ($siteConfig = DataObject::get_one('SiteConfig')) {
223
-            return $siteConfig;
224
-        }
225
-
226
-        return self::make_site_config();
227
-    }
228
-
229
-    /**
230
-     * Setup a default SiteConfig record if none exists.
231
-     */
232
-    public function requireDefaultRecords()
233
-    {
234
-        parent::requireDefaultRecords();
235
-
236
-        $config = DataObject::get_one('SiteConfig');
237
-
238
-        if (!$config) {
239
-            self::make_site_config();
240
-
241
-            DB::alteration_message("Added default site config", "created");
242
-        }
243
-    }
244
-
245
-    /**
246
-     * Create SiteConfig with defaults from language file.
247
-     *
248
-     * @return SiteConfig
249
-     */
250
-    public static function make_site_config()
251
-    {
252
-        $config = SiteConfig::create();
253
-        $config->write();
254
-
255
-        return $config;
256
-    }
257
-
258
-    /**
259
-     * Can a user view this SiteConfig instance?
260
-     *
261
-     * @param Member $member
262
-     * @return boolean
263
-     */
264
-    public function canView($member = null)
265
-    {
266
-        if (!$member) {
267
-            $member = Member::currentUserID();
268
-        }
269
-        if ($member && is_numeric($member)) {
270
-            $member = DataObject::get_by_id('Member', $member);
271
-        }
272
-
273
-        $extended = $this->extendedCan('canView', $member);
274
-        if ($extended !== null) {
275
-            return $extended;
276
-        }
277
-
278
-        // Assuming all that can edit this object can also view it
279
-        return $this->canEdit($member);
280
-    }
281
-
282
-    /**
283
-     * Can a user view pages on this site? This method is only
284
-     * called if a page is set to Inherit, but there is nothing
285
-     * to inherit from.
286
-     *
287
-     * @param Member $member
288
-     * @return boolean
289
-     */
290
-    public function canViewPages($member = null)
291
-    {
292
-        if (!$member) {
293
-            $member = Member::currentUserID();
294
-        }
295
-        if ($member && is_numeric($member)) {
296
-            $member = DataObject::get_by_id('Member', $member);
297
-        }
298
-
299
-        if ($member && Permission::checkMember($member, "ADMIN")) {
300
-            return true;
301
-        }
302
-
303
-        $extended = $this->extendedCan('canViewPages', $member);
304
-        if ($extended !== null) {
305
-            return $extended;
306
-        }
307
-
308
-        if (!$this->CanViewType || $this->CanViewType == 'Anyone') {
309
-            return true;
310
-        }
311
-
312
-        // check for any logged-in users
313
-        if ($this->CanViewType === 'LoggedInUsers' && $member) {
314
-            return true;
315
-        }
316
-
317
-        // check for specific groups
318
-        if ($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) {
319
-            return true;
320
-        }
321
-
322
-        return false;
323
-    }
324
-
325
-    /**
326
-     * Can a user edit pages on this site? This method is only
327
-     * called if a page is set to Inherit, but there is nothing
328
-     * to inherit from, or on new records without a parent.
329
-     *
330
-     * @param Member $member
331
-     * @return boolean
332
-     */
333
-    public function canEditPages($member = null)
334
-    {
335
-        if (!$member) {
336
-            $member = Member::currentUserID();
337
-        }
338
-        if ($member && is_numeric($member)) {
339
-            $member = DataObject::get_by_id('Member', $member);
340
-        }
341
-
342
-        if ($member && Permission::checkMember($member, "ADMIN")) {
343
-            return true;
344
-        }
345
-
346
-        $extended = $this->extendedCan('canEditPages', $member);
347
-        if ($extended !== null) {
348
-            return $extended;
349
-        }
350
-
351
-        // check for any logged-in users with CMS access
352
-        if ($this->CanEditType === 'LoggedInUsers'
353
-            && Permission::checkMember($member, $this->config()->required_permission)
354
-        ) {
355
-            return true;
356
-        }
357
-
358
-            // check for specific groups
359
-        if ($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) {
360
-            return true;
361
-        }
362
-
363
-        return false;
364
-    }
365
-
366
-    public function canEdit($member = null)
367
-    {
368
-        if (!$member) {
369
-            $member = Member::currentUserID();
370
-        }
371
-        if ($member && is_numeric($member)) {
372
-            $member = DataObject::get_by_id('Member', $member);
373
-        }
374
-
375
-        $extended = $this->extendedCan('canEdit', $member);
376
-        if ($extended !== null) {
377
-            return $extended;
378
-        }
379
-
380
-        return Permission::checkMember($member, "EDIT_SITECONFIG");
381
-    }
382
-
383
-    /**
384
-     * @return array
385
-     */
386
-    public function providePermissions()
387
-    {
388
-        return array(
389
-            'EDIT_SITECONFIG' => array(
390
-                'name' => _t('SiteConfig.EDIT_PERMISSION', 'Manage site configuration'),
391
-                'category' => _t('Permissions.PERMISSIONS_CATEGORY', 'Roles and access permissions'),
392
-                'help' => _t('SiteConfig.EDIT_PERMISSION_HELP', 'Ability to edit global access settings/top-level page permissions.'),
393
-                'sort' => 400
394
-            )
395
-        );
396
-    }
397
-
398
-    /**
399
-     * Can a user create pages in the root of this site?
400
-     *
401
-     * @param Member $member
402
-     * @return boolean
403
-     */
404
-    public function canCreateTopLevel($member = null)
405
-    {
406
-        if (!$member) {
407
-            $member = Member::currentUserID();
408
-        }
409
-        if ($member && is_numeric($member)) {
410
-            $member = DataObject::get_by_id('Member', $member);
411
-        }
412
-
413
-        if ($member && Permission::checkMember($member, "ADMIN")) {
414
-            return true;
415
-        }
416
-
417
-        $extended = $this->extendedCan('canCreateTopLevel', $member);
418
-        if ($extended !== null) {
419
-            return $extended;
420
-        }
421
-
422
-        // check for any logged-in users with CMS permission
423
-        if ($this->CanCreateTopLevelType === 'LoggedInUsers'
424
-            && Permission::checkMember($member, $this->config()->required_permission)
425
-        ) {
426
-            return true;
427
-        }
428
-
429
-        // check for specific groups
430
-        if ($this->CanCreateTopLevelType === 'OnlyTheseUsers'
431
-            && $member
432
-            && $member->inGroups($this->CreateTopLevelGroups())
433
-        ) {
434
-            return true;
435
-        }
436
-
437
-        return false;
438
-    }
439
-
440
-    /**
441
-     * Add $SiteConfig to all SSViewers
442
-     */
443
-    public static function get_template_global_variables()
444
-    {
445
-        return array(
446
-            'SiteConfig' => 'current_site_config',
447
-        );
448
-    }
25
+	private static $db = array(
26
+		"Title" => "Varchar(255)",
27
+		"Tagline" => "Varchar(255)",
28
+		"Theme" => "Varchar(255)",
29
+		"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
30
+		"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
31
+		"CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
32
+	);
33
+
34
+	private static $many_many = array(
35
+		"ViewerGroups" => "Group",
36
+		"EditorGroups" => "Group",
37
+		"CreateTopLevelGroups" => "Group"
38
+	);
39
+
40
+	private static $defaults = array(
41
+		"CanViewType" => "Anyone",
42
+		"CanEditType" => "LoggedInUsers",
43
+		"CanCreateTopLevelType" => "LoggedInUsers",
44
+	);
45
+
46
+	/**
47
+	 * @config
48
+	 *
49
+	 * @var array
50
+	 */
51
+	private static $disabled_themes = array();
52
+
53
+	/**
54
+	 * Default permission to check for 'LoggedInUsers' to create or edit pages
55
+	 *
56
+	 * @var array
57
+	 * @config
58
+	 */
59
+	private static $required_permission = array('CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain');
60
+
61
+
62
+	public function populateDefaults()
63
+	{
64
+		$this->Title = _t('SiteConfig.SITENAMEDEFAULT', "Your Site Name");
65
+		$this->Tagline = _t('SiteConfig.TAGLINEDEFAULT', "your tagline here");
66
+
67
+		// Allow these defaults to be overridden
68
+		parent::populateDefaults();
69
+	}
70
+
71
+	/**
72
+	 * Get the fields that are sent to the CMS.
73
+	 *
74
+	 * In your extensions: updateCMSFields($fields).
75
+	 *
76
+	 * @return FieldList
77
+	 */
78
+	public function getCMSFields()
79
+	{
80
+		$groupsMap = array();
81
+
82
+		foreach (Group::get() as $group) {
83
+			// Listboxfield values are escaped, use ASCII char instead of &raquo;
84
+			$groupsMap[$group->ID] = $group->getBreadcrumbs(' > ');
85
+		}
86
+		asort($groupsMap);
87
+
88
+		$fields = new FieldList(
89
+			new TabSet("Root",
90
+				$tabMain = new Tab('Main',
91
+					$titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
92
+					$taglineField = new TextField("Tagline", _t('SiteConfig.SITETAGLINE', "Site Tagline/Slogan")),
93
+					$themeDropdownField = new DropdownField("Theme", _t('SiteConfig.THEME', 'Theme'), $this->getAvailableThemes())
94
+				),
95
+				$tabAccess = new Tab('Access',
96
+					$viewersOptionsField = new OptionsetField("CanViewType", _t('SiteConfig.VIEWHEADER', "Who can view pages on this site?")),
97
+					$viewerGroupsField = ListboxField::create("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups"))
98
+						->setSource($groupsMap)
99
+						->setAttribute(
100
+							'data-placeholder',
101
+							_t('SiteTree.GroupPlaceholder', 'Click to select group')
102
+						),
103
+					$editorsOptionsField = new OptionsetField("CanEditType", _t('SiteConfig.EDITHEADER', "Who can edit pages on this site?")),
104
+					$editorGroupsField = ListboxField::create("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups"))
105
+						->setSource($groupsMap)
106
+						->setAttribute(
107
+							'data-placeholder',
108
+							_t('SiteTree.GroupPlaceholder', 'Click to select group')
109
+						),
110
+					$topLevelCreatorsOptionsField = new OptionsetField("CanCreateTopLevelType", _t('SiteConfig.TOPLEVELCREATE', "Who can create pages in the root of the site?")),
111
+					$topLevelCreatorsGroupsField = ListboxField::create("CreateTopLevelGroups", _t('SiteTree.TOPLEVELCREATORGROUPS', "Top level creators"))
112
+						->setSource($groupsMap)
113
+						->setAttribute(
114
+							'data-placeholder',
115
+							_t('SiteTree.GroupPlaceholder', 'Click to select group')
116
+						)
117
+				)
118
+			),
119
+			new HiddenField('ID')
120
+		);
121
+
122
+		$themeDropdownField->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)'));
123
+
124
+		$viewersOptionsSource = array();
125
+		$viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
126
+		$viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
127
+		$viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
128
+		$viewersOptionsField->setSource($viewersOptionsSource);
129
+
130
+		$editorsOptionsSource = array();
131
+		$editorsOptionsSource["LoggedInUsers"] = _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS");
132
+		$editorsOptionsSource["OnlyTheseUsers"] = _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)");
133
+		$editorsOptionsField->setSource($editorsOptionsSource);
134
+
135
+		$topLevelCreatorsOptionsField->setSource($editorsOptionsSource);
136
+
137
+		if (!Permission::check('EDIT_SITECONFIG')) {
138
+			$fields->makeFieldReadonly($viewersOptionsField);
139
+			$fields->makeFieldReadonly($viewerGroupsField);
140
+			$fields->makeFieldReadonly($editorsOptionsField);
141
+			$fields->makeFieldReadonly($editorGroupsField);
142
+			$fields->makeFieldReadonly($topLevelCreatorsOptionsField);
143
+			$fields->makeFieldReadonly($topLevelCreatorsGroupsField);
144
+			$fields->makeFieldReadonly($taglineField);
145
+			$fields->makeFieldReadonly($titleField);
146
+		}
147
+
148
+		if (file_exists(BASE_PATH . '/install.php')) {
149
+			$fields->addFieldToTab("Root.Main", new LiteralField("InstallWarningHeader",
150
+				"<p class=\"message warning\">" . _t("SiteTree.REMOVE_INSTALL_WARNING",
151
+				"Warning: You should remove install.php from this SilverStripe install for security reasons.")
152
+				. "</p>"), "Title");
153
+		}
154
+
155
+		$tabMain->setTitle(_t('SiteConfig.TABMAIN', "Main"));
156
+		$tabAccess->setTitle(_t('SiteConfig.TABACCESS', "Access"));
157
+		$this->extend('updateCMSFields', $fields);
158
+
159
+		return $fields;
160
+	}
161
+
162
+	/**
163
+	 * Get all available themes that haven't been marked as disabled.
164
+	 *
165
+	 * @param string $baseDir Optional alternative theme base directory for testing
166
+	 *
167
+	 * @return array of theme directory names
168
+	 */
169
+	public function getAvailableThemes($baseDir = null)
170
+	{
171
+		$themes = SSViewer::get_themes($baseDir);
172
+		$disabled = (array)$this->config()->disabled_themes;
173
+
174
+		foreach ($disabled as $theme) {
175
+			if (isset($themes[$theme])) {
176
+				unset($themes[$theme]);
177
+			}
178
+		}
179
+
180
+		return $themes;
181
+	}
182
+
183
+	/**
184
+	 * Get the actions that are sent to the CMS.
185
+	 *
186
+	 * In your extensions: updateEditFormActions($actions)
187
+	 *
188
+	 * @return FieldList
189
+	 */
190
+	public function getCMSActions()
191
+	{
192
+		if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) {
193
+			$actions = new FieldList(
194
+				FormAction::create('save_siteconfig', _t('CMSMain.SAVE', 'Save'))
195
+					->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
196
+			);
197
+		} else {
198
+			$actions = new FieldList();
199
+		}
200
+
201
+		$this->extend('updateCMSActions', $actions);
202
+
203
+		return $actions;
204
+	}
205
+
206
+	/**
207
+	 * @return string
208
+	 */
209
+	public function CMSEditLink()
210
+	{
211
+		return singleton('CMSSettingsController')->Link();
212
+	}
213
+
214
+	/**
215
+	 * Get the current sites SiteConfig, and creates a new one through
216
+	 * {@link make_site_config()} if none is found.
217
+	 *
218
+	 * @return SiteConfig
219
+	 */
220
+	public static function current_site_config()
221
+	{
222
+		if ($siteConfig = DataObject::get_one('SiteConfig')) {
223
+			return $siteConfig;
224
+		}
225
+
226
+		return self::make_site_config();
227
+	}
228
+
229
+	/**
230
+	 * Setup a default SiteConfig record if none exists.
231
+	 */
232
+	public function requireDefaultRecords()
233
+	{
234
+		parent::requireDefaultRecords();
235
+
236
+		$config = DataObject::get_one('SiteConfig');
237
+
238
+		if (!$config) {
239
+			self::make_site_config();
240
+
241
+			DB::alteration_message("Added default site config", "created");
242
+		}
243
+	}
244
+
245
+	/**
246
+	 * Create SiteConfig with defaults from language file.
247
+	 *
248
+	 * @return SiteConfig
249
+	 */
250
+	public static function make_site_config()
251
+	{
252
+		$config = SiteConfig::create();
253
+		$config->write();
254
+
255
+		return $config;
256
+	}
257
+
258
+	/**
259
+	 * Can a user view this SiteConfig instance?
260
+	 *
261
+	 * @param Member $member
262
+	 * @return boolean
263
+	 */
264
+	public function canView($member = null)
265
+	{
266
+		if (!$member) {
267
+			$member = Member::currentUserID();
268
+		}
269
+		if ($member && is_numeric($member)) {
270
+			$member = DataObject::get_by_id('Member', $member);
271
+		}
272
+
273
+		$extended = $this->extendedCan('canView', $member);
274
+		if ($extended !== null) {
275
+			return $extended;
276
+		}
277
+
278
+		// Assuming all that can edit this object can also view it
279
+		return $this->canEdit($member);
280
+	}
281
+
282
+	/**
283
+	 * Can a user view pages on this site? This method is only
284
+	 * called if a page is set to Inherit, but there is nothing
285
+	 * to inherit from.
286
+	 *
287
+	 * @param Member $member
288
+	 * @return boolean
289
+	 */
290
+	public function canViewPages($member = null)
291
+	{
292
+		if (!$member) {
293
+			$member = Member::currentUserID();
294
+		}
295
+		if ($member && is_numeric($member)) {
296
+			$member = DataObject::get_by_id('Member', $member);
297
+		}
298
+
299
+		if ($member && Permission::checkMember($member, "ADMIN")) {
300
+			return true;
301
+		}
302
+
303
+		$extended = $this->extendedCan('canViewPages', $member);
304
+		if ($extended !== null) {
305
+			return $extended;
306
+		}
307
+
308
+		if (!$this->CanViewType || $this->CanViewType == 'Anyone') {
309
+			return true;
310
+		}
311
+
312
+		// check for any logged-in users
313
+		if ($this->CanViewType === 'LoggedInUsers' && $member) {
314
+			return true;
315
+		}
316
+
317
+		// check for specific groups
318
+		if ($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) {
319
+			return true;
320
+		}
321
+
322
+		return false;
323
+	}
324
+
325
+	/**
326
+	 * Can a user edit pages on this site? This method is only
327
+	 * called if a page is set to Inherit, but there is nothing
328
+	 * to inherit from, or on new records without a parent.
329
+	 *
330
+	 * @param Member $member
331
+	 * @return boolean
332
+	 */
333
+	public function canEditPages($member = null)
334
+	{
335
+		if (!$member) {
336
+			$member = Member::currentUserID();
337
+		}
338
+		if ($member && is_numeric($member)) {
339
+			$member = DataObject::get_by_id('Member', $member);
340
+		}
341
+
342
+		if ($member && Permission::checkMember($member, "ADMIN")) {
343
+			return true;
344
+		}
345
+
346
+		$extended = $this->extendedCan('canEditPages', $member);
347
+		if ($extended !== null) {
348
+			return $extended;
349
+		}
350
+
351
+		// check for any logged-in users with CMS access
352
+		if ($this->CanEditType === 'LoggedInUsers'
353
+			&& Permission::checkMember($member, $this->config()->required_permission)
354
+		) {
355
+			return true;
356
+		}
357
+
358
+			// check for specific groups
359
+		if ($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) {
360
+			return true;
361
+		}
362
+
363
+		return false;
364
+	}
365
+
366
+	public function canEdit($member = null)
367
+	{
368
+		if (!$member) {
369
+			$member = Member::currentUserID();
370
+		}
371
+		if ($member && is_numeric($member)) {
372
+			$member = DataObject::get_by_id('Member', $member);
373
+		}
374
+
375
+		$extended = $this->extendedCan('canEdit', $member);
376
+		if ($extended !== null) {
377
+			return $extended;
378
+		}
379
+
380
+		return Permission::checkMember($member, "EDIT_SITECONFIG");
381
+	}
382
+
383
+	/**
384
+	 * @return array
385
+	 */
386
+	public function providePermissions()
387
+	{
388
+		return array(
389
+			'EDIT_SITECONFIG' => array(
390
+				'name' => _t('SiteConfig.EDIT_PERMISSION', 'Manage site configuration'),
391
+				'category' => _t('Permissions.PERMISSIONS_CATEGORY', 'Roles and access permissions'),
392
+				'help' => _t('SiteConfig.EDIT_PERMISSION_HELP', 'Ability to edit global access settings/top-level page permissions.'),
393
+				'sort' => 400
394
+			)
395
+		);
396
+	}
397
+
398
+	/**
399
+	 * Can a user create pages in the root of this site?
400
+	 *
401
+	 * @param Member $member
402
+	 * @return boolean
403
+	 */
404
+	public function canCreateTopLevel($member = null)
405
+	{
406
+		if (!$member) {
407
+			$member = Member::currentUserID();
408
+		}
409
+		if ($member && is_numeric($member)) {
410
+			$member = DataObject::get_by_id('Member', $member);
411
+		}
412
+
413
+		if ($member && Permission::checkMember($member, "ADMIN")) {
414
+			return true;
415
+		}
416
+
417
+		$extended = $this->extendedCan('canCreateTopLevel', $member);
418
+		if ($extended !== null) {
419
+			return $extended;
420
+		}
421
+
422
+		// check for any logged-in users with CMS permission
423
+		if ($this->CanCreateTopLevelType === 'LoggedInUsers'
424
+			&& Permission::checkMember($member, $this->config()->required_permission)
425
+		) {
426
+			return true;
427
+		}
428
+
429
+		// check for specific groups
430
+		if ($this->CanCreateTopLevelType === 'OnlyTheseUsers'
431
+			&& $member
432
+			&& $member->inGroups($this->CreateTopLevelGroups())
433
+		) {
434
+			return true;
435
+		}
436
+
437
+		return false;
438
+	}
439
+
440
+	/**
441
+	 * Add $SiteConfig to all SSViewers
442
+	 */
443
+	public static function get_template_global_variables()
444
+	{
445
+		return array(
446
+			'SiteConfig' => 'current_site_config',
447
+		);
448
+	}
449 449
 }
Please login to merge, or discard this patch.