-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommentingPlugin.php
326 lines (285 loc) · 10.6 KB
/
CommentingPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
define('COMMENTING_PLUGIN_DIR', PLUGIN_DIR . '/Commenting');
class CommentingPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'public_items_show',
'public_collections_show',
'public_head',
'admin_head',
'config_form',
'config',
'define_acl',
'after_delete_record',
'upgrade',
'initialize'
);
protected $_filters = array(
'admin_navigation_main',
'search_record_types',
'api_resources',
'api_extend_items',
'api_extend_collections'
);
/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
public function setUp()
{
if (plugin_is_active('SimplePages')) {
$this->_filters[] = 'api_extend_simple_pages';
}
if (plugin_is_active('ExhibitBuilder')) {
$this->_filters[] = 'api_extend_exhibit_pages';
}
parent::setUp();
}
public function hookInstall()
{
$db = $this->_db;
// we think the 3rd party Comment plugin uses the same name,
// and it doesn't delete its table on uninstall, causing some
// collision problems. So, clobber it if it's here
$sql = "
DROP TABLE IF EXISTS `$db->Comment`;
";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->Comment` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`record_id` int(10) unsigned NOT NULL,
`record_type` tinytext COLLATE utf8_unicode_ci NOT NULL,
`path` tinytext COLLATE utf8_unicode_ci NOT NULL,
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`author_email` tinytext COLLATE utf8_unicode_ci,
`author_url` tinytext COLLATE utf8_unicode_ci,
`author_name` tinytext COLLATE utf8_unicode_ci,
`ip` tinytext COLLATE utf8_unicode_ci,
`user_agent` tinytext COLLATE utf8_unicode_ci,
`user_id` int(11) DEFAULT NULL,
`parent_comment_id` int(11) DEFAULT NULL,
`approved` tinyint(1) NOT NULL DEFAULT '0',
`flagged` tinyint(1) NOT NULL DEFAULT '0',
`is_spam` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `record_id` (`record_id`,`user_id`,`parent_comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
";
$db->query($sql);
set_option('commenting_comment_roles', serialize(array()));
set_option('commenting_moderate_roles', serialize(array()));
set_option('commenting_reqapp_comment_roles', serialize(array()));
set_option('commenting_view_roles', serialize(array()));
}
public function hookUpgrade($args)
{
$db = $this->_db;
$old = $args['old_version'];
$new = $args['new_version'];
if (version_compare($old, '1.0', '<')) {
if (!get_option('commenting_comment_roles')) {
$commentRoles = array('super');
set_option('commenting_comment_roles', serialize($commentRoles));
}
if (!get_option('commenting_moderate_roles')) {
$moderateRoles = array('super');
set_option('commenting_moderate_roles', serialize($moderateRoles));
}
if (!get_option('commenting_noapp_comment_roles')) {
set_option('commenting_noapp_comment_roles', serialize(array()));
}
if (!get_option('commenting_view_roles')) {
set_option('commenting_view_roles', serialize(array()));
}
}
if (version_compare($old, '2.0', '<')) {
$sql = "ALTER TABLE `$db->Comment` ADD `flagged` BOOLEAN NOT NULL DEFAULT '0' AFTER `approved` ";
$db->query($sql);
}
if (version_compare($old, '2.1', '<')) {
delete_option('commenting_noapp_comment_roles');
set_option('commenting_reqapp_comment_roles', serialize(array()));
$sql = "ALTER TABLE `$db->Comment` CHANGE `flagged` `flagged` TINYINT( 1 ) NOT NULL DEFAULT '0'";
$db->query($sql);
}
}
public function hookUninstall()
{
$db = get_db();
$sql = "DROP TABLE IF EXISTS `$db->Comment`";
$db->query($sql);
}
public function hookPublicHead()
{
queue_css_file('commenting');
queue_js_file('commenting');
queue_js_file('tinymce.min', 'javascripts/vendor/tinymce');
}
public function hookAdminHead()
{
queue_css_file('commenting');
}
public function hookAfterDeleteRecord($args)
{
$record = $args['record'];
$type = get_class($record);
$comments = get_db()->getTable('Comment')->findBy(array('record_type' => $type, 'record_id' => $record->id));
foreach ($comments as $comment) {
$comment->delete();
}
}
public static function showComments($record)
{
$view = get_view();
echo '<div id="comments-container">';
echo '<div id="comment-main-container">';
if ((get_option('commenting_allow_public') == 1)
|| (get_option('commenting_allow_public_view') == 1)
|| is_allowed('Commenting_Comment', 'show')
) {
$options = array('threaded' => get_option('commenting_threaded'), 'approved' => true);
$comments = $view->getComments($record, $options);
echo $view->partial('comments.php', array('comments' => $comments, 'threaded' => $options['threaded']));
}
echo "</div>";
if ((get_option('commenting_allow_public') == 1)
|| is_allowed('Commenting_Comment', 'add')
) {
echo $view->getCommentForm($record);
}
echo "</div>";
}
public function hookPublicItemsShow($args)
{
self::showComments($args['item']);
}
public function hookPublicCollectionsShow($args)
{
self::showComments($args['collection']);
}
public function hookConfig($args)
{
$post = $args['post'];
foreach ($post as $key => $value) {
if (($key == 'commenting_comment_roles')
|| ($key == 'commenting_moderate_roles')
|| ($key == 'commenting_view_roles')
|| ($key == 'commenting_reqapp_comment_roles')
) {
$value = serialize($value);
} elseif ($key == 'new_comment_notification_recipients') {
// sanitize list of emails
$emails = array_map('trim', (array) explode("\n", $value));
$emails = array_filter($emails, function($val) {
return filter_var($val, FILTER_VALIDATE_EMAIL);
});
$value = implode("\n", $emails);
}
set_option($key, $value);
}
}
public function hookConfigForm()
{
include COMMENTING_PLUGIN_DIR . '/config_form.php';
}
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$acl->addResource('Commenting_Comment');
$commentRoles = unserialize(get_option('commenting_comment_roles'));
$moderateRoles = unserialize(get_option('commenting_moderate_roles'));
$viewRoles = unserialize(get_option('commenting_view_roles'));
$acl->allow(null, 'Commenting_Comment', array('flag'));
if($viewRoles !== false) {
foreach($viewRoles as $role) {
//check that all the roles exist, in case a plugin-added role has been removed (e.g. GuestUser)
if($acl->hasRole($role)) {
$acl->allow($role, 'Commenting_Comment', 'show');
}
}
foreach($commentRoles as $role) {
if($acl->hasRole($role)) {
$acl->allow($role, 'Commenting_Comment', 'add');
}
}
foreach($moderateRoles as $role) {
if($acl->hasRole($role)) {
$acl->allow($role, 'Commenting_Comment', array(
'update-approved',
'update-spam',
'update-flagged',
'batch-delete',
'browse',
'delete'
));
}
}
if(get_option('commenting_allow_public')) {
$acl->allow(null, 'Commenting_Comment', array('show', 'add'));
}
}
}
public function filterAdminNavigationMain($tabs)
{
if(is_allowed('Commenting_Comment', 'update-approved') ) {
$tabs[] = array('uri' => url('commenting/comment/browse'), 'label' => __('Comments'));
}
return $tabs;
}
public function filterSearchRecordTypes($types)
{
$types['Comment'] = __('Comments');
return $types;
}
public function filterApiResources($apiResources)
{
$apiResources['comments'] = array(
'record_type' => 'Comment',
'actions' => array('get', 'index'),
'index_params' => array('record_type', 'record_id')
);
return $apiResources;
}
public function filterApiExtendItems($extend, $args)
{
return $this->_filterApiExtendRecords($extend, $args);
}
public function filterApiExtendCollections($extend, $args)
{
return $this->_filterApiExtendRecords($extend, $args);
}
public function filterApiExtendSimplePages($extend, $args)
{
return $this->_filterApiExtendRecords($extend, $args);
}
public function filterApiExtendExhibitPages($extend, $args)
{
return $this->_filterApiExtendRecords($extend, $args);
}
private function _filterApiExtendRecords($extend, $args)
{
$record = $args['record'];
$recordClass = get_class($record);
$extend['comments'] = array(
'count' => $this->_countComments($record),
'resource' => 'comments',
'url' => Omeka_Record_Api_AbstractRecordAdapter::getResourceUrl("/comments?record_type=$recordClass&record_id={$record->id}"),
);
return $extend;
}
private function _countComments($record)
{
$params = array(
'record_type' => get_class($record),
'record_id' => $record->id
);
return get_db()->getTable('Comment')->count($params);
}
}