1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Extension to {@link DataObject} to enable tracking comments. |
5
|
|
|
* |
6
|
|
|
* @package comments |
7
|
|
|
*/ |
8
|
|
|
class CommentsExtension extends DataExtension |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Default configuration values |
12
|
|
|
* |
13
|
|
|
* enabled: Allows commenting to be disabled even if the extension is present |
14
|
|
|
* enabled_cms: Allows commenting to be enabled or disabled via the CMS |
15
|
|
|
* require_login: Boolean, whether a user needs to login (required for required_permission) |
16
|
|
|
* require_login_cms: Allows require_login to be set via the CMS |
17
|
|
|
* required_permission: Permission (or array of permissions) required to comment |
18
|
|
|
* include_js: Enhance operation by ajax behaviour on moderation links (required for use_preview) |
19
|
|
|
* use_gravatar: Set to true to show gravatar icons |
20
|
|
|
* gravatar_default: Theme for 'not found' gravatar {@see http://gravatar.com/site/implement/images} |
21
|
|
|
* gravatar_rating: Gravatar rating (same as the standard default) |
22
|
|
|
* show_comments_when_disabled: Show older comments when commenting has been disabled. |
23
|
|
|
* order_comments_by: Default sort order. |
24
|
|
|
* order_replies_by: Sort order for replies. |
25
|
|
|
* comments_holder_id: ID for the comments holder |
26
|
|
|
* comment_permalink_prefix: ID prefix for each comment |
27
|
|
|
* require_moderation: Require moderation for all comments |
28
|
|
|
* require_moderation_cms: Ignore other comment moderation config settings and set via CMS |
29
|
|
|
* frontend_moderation: Display unmoderated comments in the frontend, if the user can moderate them. |
30
|
|
|
* frontend_spam: Display spam comments in the frontend, if the user can moderate them. |
31
|
|
|
* html_allowed: Allow for sanitized HTML in comments |
32
|
|
|
* use_preview: Preview formatted comment (when allowing HTML) |
33
|
|
|
* nested_comments: Enable nested comments |
34
|
|
|
* nested_depth: Max depth of nested comments in levels (where root is 1 depth) 0 means no limit. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
* |
38
|
|
|
* @config |
39
|
|
|
*/ |
40
|
|
|
private static $comments = array( |
|
|
|
|
41
|
|
|
'enabled' => true, |
42
|
|
|
'enabled_cms' => false, |
43
|
|
|
'require_login' => false, |
44
|
|
|
'require_login_cms' => false, |
45
|
|
|
'required_permission' => false, |
46
|
|
|
'include_js' => true, |
47
|
|
|
'use_gravatar' => false, |
48
|
|
|
'gravatar_size' => 80, |
49
|
|
|
'gravatar_default' => 'identicon', |
50
|
|
|
'gravatar_rating' => 'g', |
51
|
|
|
'show_comments_when_disabled' => false, |
52
|
|
|
'order_comments_by' => '"Created" DESC', |
53
|
|
|
'order_replies_by' => false, |
54
|
|
|
'comments_per_page' => 10, |
55
|
|
|
'comments_holder_id' => 'comments-holder', |
56
|
|
|
'comment_permalink_prefix' => 'comment-', |
57
|
|
|
'require_moderation' => false, |
58
|
|
|
'require_moderation_nonmembers' => false, |
59
|
|
|
'require_moderation_cms' => false, |
60
|
|
|
'frontend_moderation' => false, |
61
|
|
|
'frontend_spam' => false, |
62
|
|
|
'html_allowed' => false, |
63
|
|
|
'html_allowed_elements' => array('a', 'img', 'i', 'b'), |
64
|
|
|
'use_preview' => false, |
65
|
|
|
'nested_comments' => false, |
66
|
|
|
'nested_depth' => 2, |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private static $db = array( |
|
|
|
|
73
|
|
|
'ProvideComments' => 'Boolean', |
74
|
|
|
'ModerationRequired' => 'Enum(\'None,Required,NonMembersOnly\',\'None\')', |
75
|
|
|
'CommentsRequireLogin' => 'Boolean', |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* CMS configurable options should default to the config values, but respect |
80
|
|
|
* default values specified by the object |
81
|
|
|
*/ |
82
|
|
|
public function populateDefaults() |
83
|
|
|
{ |
84
|
|
|
$defaults = $this->owner->config()->defaults; |
85
|
|
|
|
86
|
|
|
// Set if comments should be enabled by default |
87
|
|
View Code Duplication |
if (isset($defaults['ProvideComments'])) { |
|
|
|
|
88
|
|
|
$this->owner->ProvideComments = $defaults['ProvideComments']; |
89
|
|
|
} else { |
90
|
|
|
$this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// If moderation options should be configurable via the CMS then |
94
|
|
|
if (isset($defaults['ModerationRequired'])) { |
95
|
|
|
$this->owner->ModerationRequired = $defaults['ModerationRequired']; |
96
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation')) { |
97
|
|
|
$this->owner->ModerationRequired = 'Required'; |
98
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation_nonmembers')) { |
99
|
|
|
$this->owner->ModerationRequired = 'NonMembersOnly'; |
100
|
|
|
} else { |
101
|
|
|
$this->owner->ModerationRequired = 'None'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Set login required |
105
|
|
View Code Duplication |
if (isset($defaults['CommentsRequireLogin'])) { |
|
|
|
|
106
|
|
|
$this->owner->CommentsRequireLogin = $defaults['CommentsRequireLogin']; |
107
|
|
|
} else { |
108
|
|
|
$this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* If this extension is applied to a {@link SiteTree} record then |
115
|
|
|
* append a Provide Comments checkbox to allow authors to trigger |
116
|
|
|
* whether or not to display comments |
117
|
|
|
* |
118
|
|
|
* @todo Allow customization of other {@link Commenting} configuration |
119
|
|
|
* |
120
|
|
|
* @param FieldList $fields |
121
|
|
|
*/ |
122
|
|
|
public function updateSettingsFields(FieldList $fields) |
123
|
|
|
{ |
124
|
|
|
$options = FieldGroup::create()->setTitle(_t('CommentsExtension.COMMENTOPTIONS', 'Comments')); |
125
|
|
|
|
126
|
|
|
// Check if enabled setting should be cms configurable |
127
|
|
|
if ($this->owner->getCommentsOption('enabled_cms')) { |
128
|
|
|
$options->push(new CheckboxField('ProvideComments', _t('Comment.ALLOWCOMMENTS', 'Allow Comments'))); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Check if we should require users to login to comment |
132
|
|
|
if ($this->owner->getCommentsOption('require_login_cms')) { |
133
|
|
|
$options->push( |
134
|
|
|
new CheckboxField( |
135
|
|
|
'CommentsRequireLogin', |
136
|
|
|
_t('Comments.COMMENTSREQUIRELOGIN', 'Require login to comment') |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($options->FieldList()->count()) { |
142
|
|
|
if ($fields->hasTabSet()) { |
143
|
|
|
$fields->addFieldsToTab('Root.Settings', $options); |
|
|
|
|
144
|
|
|
} else { |
145
|
|
|
$fields->push($options); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Check if moderation should be enabled via cms configurable |
150
|
|
|
if ($this->owner->getCommentsOption('require_moderation_cms')) { |
151
|
|
|
$moderationField = new DropdownField('ModerationRequired', 'Comment Moderation', array( |
152
|
|
|
'None' => _t('CommentsExtension.MODERATIONREQUIRED_NONE', 'No moderation required'), |
153
|
|
|
'Required' => _t('CommentsExtension.MODERATIONREQUIRED_REQUIRED', 'Moderate all comments'), |
154
|
|
|
'NonMembersOnly' => _t( |
155
|
|
|
'CommentsExtension.MODERATIONREQUIRED_NONMEMBERSONLY', |
156
|
|
|
'Only moderate non-members' |
157
|
|
|
), |
158
|
|
|
)); |
159
|
|
|
if ($fields->hasTabSet()) { |
160
|
|
|
$fields->addFieldsToTab('Root.Settings', $moderationField); |
|
|
|
|
161
|
|
|
} else { |
162
|
|
|
$fields->push($moderationField); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get comment moderation rules for this parent |
169
|
|
|
* |
170
|
|
|
* None: No moderation required |
171
|
|
|
* Required: All comments |
172
|
|
|
* NonMembersOnly: Only anonymous users |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getModerationRequired() |
177
|
|
|
{ |
178
|
|
|
if ($this->owner->getCommentsOption('require_moderation_cms')) { |
179
|
|
|
return $this->owner->getField('ModerationRequired'); |
180
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation')) { |
181
|
|
|
return 'Required'; |
182
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation_nonmembers')) { |
183
|
|
|
return 'NonMembersOnly'; |
184
|
|
|
} else { |
185
|
|
|
return 'None'; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Determine if users must be logged in to post comments |
191
|
|
|
* |
192
|
|
|
* @return boolean |
193
|
|
|
*/ |
194
|
|
|
public function getCommentsRequireLogin() |
195
|
|
|
{ |
196
|
|
|
if ($this->owner->getCommentsOption('require_login_cms')) { |
197
|
|
|
return (bool) $this->owner->getField('CommentsRequireLogin'); |
198
|
|
|
} else { |
199
|
|
|
return (bool) $this->owner->getCommentsOption('require_login'); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the RelationList of all comments against this object. Can be used as a data source |
205
|
|
|
* for a gridfield with write access. |
206
|
|
|
* |
207
|
|
|
* @return CommentList |
208
|
|
|
*/ |
209
|
|
|
public function AllComments() |
210
|
|
|
{ |
211
|
|
|
$order = $this->owner->getCommentsOption('order_comments_by'); |
212
|
|
|
$comments = CommentList::create($this->ownerBaseClass) |
213
|
|
|
->forForeignID($this->owner->ID) |
214
|
|
|
->sort($order); |
215
|
|
|
$this->owner->extend('updateAllComments', $comments); |
216
|
|
|
return $comments; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns all comments against this object, with with spam and unmoderated items excluded, for use in the frontend |
221
|
|
|
* |
222
|
|
|
* @return CommentList |
223
|
|
|
*/ |
224
|
|
|
public function AllVisibleComments() |
225
|
|
|
{ |
226
|
|
|
$list = $this->AllComments(); |
227
|
|
|
|
228
|
|
|
// Filter spam comments for non-administrators if configured |
229
|
|
|
$showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments(); |
230
|
|
|
if (!$showSpam) { |
231
|
|
|
$list = $list->filter('IsSpam', 0); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Filter un-moderated comments for non-administrators if moderation is enabled |
235
|
|
|
$showUnmoderated = ($this->owner->ModerationRequired === 'None') |
236
|
|
|
|| ($this->owner->getCommentsOption('frontend_moderation') && $this->owner->canModerateComments()); |
237
|
|
|
if (!$showUnmoderated) { |
238
|
|
|
$list = $list->filter('Moderated', 1); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->owner->extend('updateAllVisibleComments', $list); |
242
|
|
|
return $list; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns the root level comments, with spam and unmoderated items excluded, for use in the frontend |
247
|
|
|
* |
248
|
|
|
* @return CommentList |
249
|
|
|
*/ |
250
|
|
|
public function Comments() |
251
|
|
|
{ |
252
|
|
|
$list = $this->AllVisibleComments(); |
253
|
|
|
|
254
|
|
|
// If nesting comments, only show root level |
255
|
|
|
if ($this->owner->getCommentsOption('nested_comments')) { |
256
|
|
|
$list = $list->filter('ParentCommentID', 0); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$this->owner->extend('updateComments', $list); |
260
|
|
|
return $list; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns a paged list of the root level comments, with spam and unmoderated items excluded, |
265
|
|
|
* for use in the frontend |
266
|
|
|
* |
267
|
|
|
* @return PaginatedList |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function PagedComments() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$list = $this->Comments(); |
272
|
|
|
|
273
|
|
|
// Add pagination |
274
|
|
|
$list = new PaginatedList($list, Controller::curr()->getRequest()); |
|
|
|
|
275
|
|
|
$list->setPaginationGetVar('commentsstart' . $this->owner->ID); |
276
|
|
|
$list->setPageLength($this->owner->getCommentsOption('comments_per_page')); |
277
|
|
|
|
278
|
|
|
$this->owner->extend('updatePagedComments', $list); |
279
|
|
|
return $list; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Check if comments are configured for this page even if they are currently disabled. |
284
|
|
|
* Do not include the comments on pages which don't have id's such as security pages |
285
|
|
|
* |
286
|
|
|
* @deprecated since version 2.0 |
287
|
|
|
* |
288
|
|
|
* @return boolean |
289
|
|
|
*/ |
290
|
|
|
public function getCommentsConfigured() |
291
|
|
|
{ |
292
|
|
|
Deprecation::notice('2.0', 'getCommentsConfigured is deprecated. Use getCommentsEnabled instead'); |
293
|
|
|
return true; // by virtue of all classes with this extension being 'configured' |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Determine if comments are enabled for this instance |
298
|
|
|
* |
299
|
|
|
* @return boolean |
300
|
|
|
*/ |
301
|
|
|
public function getCommentsEnabled() |
302
|
|
|
{ |
303
|
|
|
// Don't display comments form for pseudo-pages (such as the login form) |
304
|
|
|
if (!$this->owner->exists()) { |
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// Determine which flag should be used to determine if this is enabled |
309
|
|
|
if ($this->owner->getCommentsOption('enabled_cms')) { |
310
|
|
|
return $this->owner->ProvideComments; |
311
|
|
|
} else { |
312
|
|
|
return $this->owner->getCommentsOption('enabled'); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Get the HTML ID for the comment holder in the template |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function getCommentHolderID() |
322
|
|
|
{ |
323
|
|
|
return $this->owner->getCommentsOption('comments_holder_id'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @deprecated since version 2.0 |
328
|
|
|
*/ |
329
|
|
|
public function getPostingRequiresPermission() |
330
|
|
|
{ |
331
|
|
|
Deprecation::notice('2.0', 'Use getPostingRequiredPermission instead'); |
332
|
|
|
return $this->getPostingRequiredPermission(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Permission codes required in order to post (or empty if none required) |
337
|
|
|
* |
338
|
|
|
* @return string|array Permission or list of permissions, if required |
339
|
|
|
*/ |
340
|
|
|
public function getPostingRequiredPermission() |
341
|
|
|
{ |
342
|
|
|
return $this->owner->getCommentsOption('required_permission'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function canPost() |
346
|
|
|
{ |
347
|
|
|
Deprecation::notice('2.0', 'Use canPostComment instead'); |
348
|
|
|
return $this->canPostComment(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Determine if a user can post comments on this item |
353
|
|
|
* |
354
|
|
|
* @param Member $member Member to check |
|
|
|
|
355
|
|
|
* |
356
|
|
|
* @return boolean |
357
|
|
|
*/ |
358
|
|
|
public function canPostComment($member = null) |
359
|
|
|
{ |
360
|
|
|
// Deny if not enabled for this object |
361
|
|
|
if (!$this->owner->CommentsEnabled) { |
362
|
|
|
return false; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
// Check if member is required |
366
|
|
|
$requireLogin = $this->owner->CommentsRequireLogin; |
367
|
|
|
if (!$requireLogin) { |
368
|
|
|
return true; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// Check member is logged in |
372
|
|
|
$member = $member ?: Member::currentUser(); |
373
|
|
|
if (!$member) { |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// If member required check permissions |
378
|
|
|
$requiredPermission = $this->owner->PostingRequiredPermission; |
379
|
|
|
if ($requiredPermission && !Permission::checkMember($member, $requiredPermission)) { |
|
|
|
|
380
|
|
|
return false; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return true; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Determine if this member can moderate comments in the CMS |
388
|
|
|
* |
389
|
|
|
* @param Member $member |
|
|
|
|
390
|
|
|
* |
391
|
|
|
* @return boolean |
392
|
|
|
*/ |
393
|
|
|
public function canModerateComments($member = null) |
394
|
|
|
{ |
395
|
|
|
// Deny if not enabled for this object |
396
|
|
|
if (!$this->owner->CommentsEnabled) { |
397
|
|
|
return false; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// Fallback to can-edit |
401
|
|
|
return $this->owner->canEdit($member); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
public function getRssLink() |
405
|
|
|
{ |
406
|
|
|
Deprecation::notice('2.0', 'Use getCommentRSSLink instead'); |
407
|
|
|
return $this->getCommentRSSLink(); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Gets the RSS link to all comments |
412
|
|
|
* |
413
|
|
|
* @return string |
414
|
|
|
*/ |
415
|
|
|
public function getCommentRSSLink() |
416
|
|
|
{ |
417
|
|
|
return Controller::join_links(Director::baseURL(), 'CommentingController/rss'); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
public function getRssLinkPage() |
421
|
|
|
{ |
422
|
|
|
Deprecation::notice('2.0', 'Use getCommentRSSLinkPage instead'); |
423
|
|
|
return $this->getCommentRSSLinkPage(); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get the RSS link to all comments on this page |
428
|
|
|
* |
429
|
|
|
* @return string |
430
|
|
|
*/ |
431
|
|
|
public function getCommentRSSLinkPage() |
432
|
|
|
{ |
433
|
|
|
return Controller::join_links( |
434
|
|
|
$this->getCommentRSSLink(), $this->ownerBaseClass, $this->owner->ID |
435
|
|
|
); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Comments interface for the front end. Includes the CommentAddForm and the composition |
440
|
|
|
* of the comments display. |
441
|
|
|
* |
442
|
|
|
* To customize the html see templates/CommentInterface.ss or extend this function with |
443
|
|
|
* your own extension. |
444
|
|
|
* |
445
|
|
|
* @todo Cleanup the passing of all this configuration based functionality |
446
|
|
|
* |
447
|
|
|
* @see docs/en/Extending |
448
|
|
|
*/ |
449
|
|
|
public function CommentsForm() |
|
|
|
|
450
|
|
|
{ |
451
|
|
|
// Check if enabled |
452
|
|
|
$enabled = $this->getCommentsEnabled(); |
453
|
|
|
if ($enabled && $this->owner->getCommentsOption('include_js')) { |
454
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
455
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); |
456
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/lib/jquery.form.js'); |
457
|
|
|
Requirements::javascript(COMMENTS_THIRDPARTY . '/jquery-validate/jquery.validate.min.js'); |
458
|
|
|
Requirements::add_i18n_javascript('comments/javascript/lang'); |
459
|
|
|
Requirements::javascript('comments/javascript/CommentsInterface.js'); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
$controller = CommentingController::create(); |
463
|
|
|
$controller->setOwnerRecord($this->owner); |
464
|
|
|
$controller->setBaseClass($this->ownerBaseClass); |
465
|
|
|
$controller->setOwnerController(Controller::curr()); |
466
|
|
|
|
467
|
|
|
$moderatedSubmitted = Session::get('CommentsModerated'); |
468
|
|
|
Session::clear('CommentsModerated'); |
469
|
|
|
|
470
|
|
|
$form = ($enabled) ? $controller->CommentsForm() : false; |
471
|
|
|
|
472
|
|
|
// a little bit all over the show but to ensure a slightly easier upgrade for users |
473
|
|
|
// return back the same variables as previously done in comments |
474
|
|
|
return $this |
475
|
|
|
->owner |
476
|
|
|
->customise(array( |
477
|
|
|
'AddCommentForm' => $form, |
478
|
|
|
'ModeratedSubmitted' => $moderatedSubmitted, |
479
|
|
|
)) |
480
|
|
|
->renderWith('CommentsInterface'); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Returns whether this extension instance is attached to a {@link SiteTree} object |
485
|
|
|
* |
486
|
|
|
* @return bool |
487
|
|
|
*/ |
488
|
|
|
public function attachedToSiteTree() |
489
|
|
|
{ |
490
|
|
|
$class = $this->ownerBaseClass; |
491
|
|
|
|
492
|
|
|
return (is_subclass_of($class, 'SiteTree')) || ($class == 'SiteTree'); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* @deprecated 1.0 Please use {@link CommentsExtension->CommentsForm()} |
497
|
|
|
*/ |
498
|
|
|
public function PageComments() |
|
|
|
|
499
|
|
|
{ |
500
|
|
|
// This method is very commonly used, don't throw a warning just yet |
501
|
|
|
Deprecation::notice('1.0', '$PageComments is deprecated. Please use $CommentsForm'); |
502
|
|
|
return $this->CommentsForm(); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Get the commenting option for this object |
507
|
|
|
* |
508
|
|
|
* This can be overridden in any instance or extension to customise the option available |
509
|
|
|
* |
510
|
|
|
* @param string $key |
511
|
|
|
* |
512
|
|
|
* @return mixed Result if the setting is available, or null otherwise |
513
|
|
|
*/ |
514
|
|
|
public function getCommentsOption($key) |
515
|
|
|
{ |
516
|
|
|
$settings = $this->owner // In case singleton is called on the extension directly |
517
|
|
|
? $this->owner->config()->comments |
518
|
|
|
: Config::inst()->get(__CLASS__, 'comments'); |
519
|
|
|
$value = null; |
520
|
|
|
if (isset($settings[$key])) { |
521
|
|
|
$value = $settings[$key]; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
// To allow other extensions to customise this option |
525
|
|
|
if ($this->owner) { |
526
|
|
|
$this->owner->extend('updateCommentsOption', $key, $value); |
527
|
|
|
} |
528
|
|
|
return $value; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Add moderation functions to the current fieldlist |
533
|
|
|
* |
534
|
|
|
* @param FieldList $fields |
535
|
|
|
*/ |
536
|
|
|
protected function updateModerationFields(FieldList $fields) |
537
|
|
|
{ |
538
|
|
|
Requirements::css(COMMENTS_DIR . '/css/cms.css'); |
539
|
|
|
|
540
|
|
|
$newComments = $this->owner->AllComments()->filter('Moderated', 0); |
541
|
|
|
|
542
|
|
|
$newGrid = new CommentsGridField( |
543
|
|
|
'NewComments', |
544
|
|
|
_t('CommentsAdmin.NewComments', 'New'), |
545
|
|
|
$newComments, |
546
|
|
|
CommentsGridFieldConfig::create() |
547
|
|
|
); |
548
|
|
|
|
549
|
|
|
$approvedComments = $this->owner->AllComments()->filter('Moderated', 1)->filter('IsSpam', 0); |
550
|
|
|
|
551
|
|
|
$approvedGrid = new CommentsGridField( |
552
|
|
|
'ApprovedComments', |
553
|
|
|
_t('CommentsAdmin.Comments', 'Approved'), |
554
|
|
|
$approvedComments, |
555
|
|
|
CommentsGridFieldConfig::create() |
556
|
|
|
); |
557
|
|
|
|
558
|
|
|
$spamComments = $this->owner->AllComments()->filter('Moderated', 1)->filter('IsSpam', 1); |
559
|
|
|
|
560
|
|
|
$spamGrid = new CommentsGridField( |
561
|
|
|
'SpamComments', |
562
|
|
|
_t('CommentsAdmin.SpamComments', 'Spam'), |
563
|
|
|
$spamComments, |
564
|
|
|
CommentsGridFieldConfig::create() |
565
|
|
|
); |
566
|
|
|
|
567
|
|
|
$newCount = '(' . count($newComments) . ')'; |
568
|
|
|
$approvedCount = '(' . count($approvedComments) . ')'; |
569
|
|
|
$spamCount = '(' . count($spamComments) . ')'; |
570
|
|
|
|
571
|
|
|
if ($fields->hasTabSet()) { |
572
|
|
|
$tabs = new TabSet( |
573
|
|
|
'Comments', |
574
|
|
|
new Tab('CommentsNewCommentsTab', _t('CommentAdmin.NewComments', 'New') . ' ' . $newCount, |
575
|
|
|
$newGrid |
576
|
|
|
), |
577
|
|
|
new Tab('CommentsCommentsTab', _t('CommentAdmin.Comments', 'Approved') . ' ' . $approvedCount, |
578
|
|
|
$approvedGrid |
579
|
|
|
), |
580
|
|
|
new Tab('CommentsSpamCommentsTab', _t('CommentAdmin.SpamComments', 'Spam') . ' ' . $spamCount, |
581
|
|
|
$spamGrid |
582
|
|
|
) |
583
|
|
|
); |
584
|
|
|
$fields->addFieldToTab('Root', $tabs); |
585
|
|
|
} else { |
586
|
|
|
$fields->push($newGrid); |
587
|
|
|
$fields->push($approvedGrid); |
588
|
|
|
$fields->push($spamGrid); |
589
|
|
|
} |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
public function updateCMSFields(FieldList $fields) |
593
|
|
|
{ |
594
|
|
|
// Disable moderation if not permitted |
595
|
|
|
if ($this->owner->canModerateComments()) { |
596
|
|
|
$this->updateModerationFields($fields); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
// If this isn't a page we should merge the settings into the CMS fields |
600
|
|
|
if (!$this->attachedToSiteTree()) { |
601
|
|
|
$this->updateSettingsFields($fields); |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.