forked from backdrop-contrib/services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment_resource.inc
418 lines (385 loc) · 13.6 KB
/
comment_resource.inc
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
function _comment_resource_definition() {
if (!module_exists('comment')) {
return array();
}
return array(
'comment' => array(
'operations' => array(
'create' => array(
'help' => 'Create a comment',
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'callback' => '_comment_resource_create',
'access callback' => '_comment_resource_access',
'access arguments' => array('create'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'comment',
'type' => 'array',
'description' => 'The comment object',
'source' => 'data',
'optional' => FALSE,
),
),
),
'retrieve' => array(
'help' => 'Retrieve a comment',
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'callback' => '_comment_resource_retrieve',
'access callback' => '_comment_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'cid',
'type' => 'int',
'description' => 'The cid of the comment to retrieve.',
'source' => array('path' => '0'),
'optional' => FALSE,
),
),
),
'update' => array(
'help' => 'Update a comment',
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'callback' => '_comment_resource_update',
'access callback' => '_comment_resource_access',
'access arguments' => array('edit'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'cid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'int',
'description' => 'The unique identifier for this comment.',
),
array(
'name' => 'data',
'type' => 'array',
'description' => 'The comment object with updated information',
'source' => 'data',
'optional' => FALSE,
),
),
),
'delete' => array(
'help' => 'Delete a comment',
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'callback' => '_comment_resource_delete',
'access callback' => '_comment_resource_access',
'access arguments' => array('edit'),
'access arguments append' => TRUE,
'args' => array(
array(
'name' => 'cid',
'type' => 'int',
'description' => 'The id of the comment to delete',
'source' => array('path' => '0'),
'optional' => FALSE,
),
),
),
'index' => array(
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'callback' => '_comment_resource_index',
'args' => array(
array(
'name' => 'page',
'optional' => TRUE,
'type' => 'int',
'description' => 'The zero-based index of the page to get, defaults to 0.',
'default value' => 0,
'source' => array('param' => 'page'),
),
array(
'name' => 'fields',
'optional' => TRUE,
'type' => 'string',
'description' => 'The fields to get.',
'default value' => '*',
'source' => array('param' => 'fields'),
),
array(
'name' => 'parameters',
'optional' => TRUE,
'type' => 'array',
'description' => 'Parameters',
'default value' => array(),
'source' => array('param' => 'parameters'),
),
array(
'name' => 'pagesize',
'optional' => TRUE,
'type' => 'int',
'description' => 'Number of records to get per page.',
'default value' => config_get('services.settings', 'services_comment_index_page_size'),
'source' => array('param' => 'pagesize'),
),
array(
'name' => 'options',
'optional' => TRUE,
'type' => 'array',
'description' => 'Additional query options.',
'default value' => array(
'orderby' => array(
'created' => 'DESC'
)
),
'source' => array('param' => 'options'),
),
),
'access callback' => '_comment_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
),
),
'actions' => array(
'countAll' => array(
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'help' => t('Return number of comments on a given node.'),
'access callback' => '_comment_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
'callback' => '_comment_resource_count_all',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => t('The node id to count all comments.'),
'source' => array('data' => 'nid'),
'optional' => FALSE,
),
),
),
'countNew' => array(
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
'help' => t('Returns number of new comments on a given node since a given timestamp.'),
'access callback' => '_comment_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
'callback' => '_comment_resource_count_new',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => t('The node id to load comments for.'),
'source' => array('data' => 'nid'),
'optional' => FALSE,
),
array(
'name' => 'since',
'type' => 'int',
'optional' => TRUE,
'description' => t('Timestamp to count from (defaults to time of last user acces to node).'),
'source' => array('data' => 'since'),
'optional' => TRUE,
'default value' => 0,
),
),
),
),
),
);
}
/**
* Adds a new comment to a node and returns the cid.
*
* @param $comment
* An object as would be returned from comment_load().
* @return
* Unique identifier for the comment (cid) or errors if there was a problem.
*/
function _comment_resource_create($comment) {
// Adds backwards compatability with regression fixed in #1083242
$comment = _services_arg_value($comment, 'comment');
if (empty($comment['nid'])) {
return services_error(t('A nid must be provided'));
}
$form_state['values'] = $comment;
$form_state['values']['op'] = config_get('services.settings', 'services_comment_save_button_resource_create');
$comment_empty = array(
'nid' => $comment['nid'],
'cid' => NULL,
);
// If a pid is provide use it
if (!empty($comment['pid'])) {
$comment_empty['pid'] = $comment['pid'];
}
$comment_empty = entity_create('comment', $comment_empty);
$form_state['programmed_bypass_access_check'] = FALSE;
$ret = backdrop_form_submit('comment_form', $form_state, $comment_empty);
// Error if needed.
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
}
$comment = $form_state['comment'];
return array(
'cid' => $comment->cid,
'uri' => services_resource_uri(array('comment', $comment->cid)),
);
}
/**
* Returns a specified comment
*
* @param $cid
* Unique identifier for the specified comment
* @return
* The comment object
*/
function _comment_resource_retrieve($cid) {
$comment = comment_load($cid);
return $comment ?
services_field_permissions_clean('view', 'comment', $comment) :
services_error(t('Comment @cid not found', array('@cid' => $cid)), 404);
}
/**
* Updates a comment and returns the cid.
*
* @param $cid
* Unique identifier for this comment.
* @param $comment
* An object as would be returned from comment_load().
* @return
* Unique identifier for the comment (cid) or FALSE if there was a problem.
*/
function _comment_resource_update($cid, $comment) {
// Adds backwards compatability with regression fixed in #1083242
$comment = _services_arg_value($comment, 'data');
$comment['cid'] = $cid;
$old_comment = comment_load($cid);
if (empty($old_comment)) {
return services_error(t('Comment @cid not found', array('@cid' => $cid)), 404);
}
// Setup form_state.
$form_state = array();
$form_state['values'] = $comment;
$form_state['values']['op'] = config_get('services.settings', 'services_comment_save_button_resource_update');
$form_state['comment'] = $old_comment;
$form_state['programmed_bypass_access_check'] = FALSE;
backdrop_form_submit('comment_form', $form_state, $old_comment);
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
}
return $cid;
}
/**
* Delete a comment.
*
* @param $cid
* Unique identifier of the comment to delete.
* @return
* True.
*/
function _comment_resource_delete($cid) {
// Load in the required includes for comment_delete.
module_load_include('inc', 'comment', 'comment.admin');
// The following is from comment_confirm_delete_submit in comment.admin.inc
$comment = comment_load($cid);
if (empty($comment)) {
return services_error(t('There is no comment found with id @cid', array('@cid' => $cid)), 404);
}
// Delete comment and its replies.
comment_delete($cid);
// Clear the cache so an anonymous user sees that his comment was deleted.
cache_clear_all();
return TRUE;
}
/**
* Return an array of optionally paged cids based on a set of criteria.
*
* An example request might look like
*
* http://domain/endpoint/comment?fields=cid,nid¶meters[nid]=7¶meters[uid]=2
*
* This would return an array of objects with only cid and nid defined, where
* nid = 7 and uid = 1.
*
* @param $page
* Page number of results to return (in pages of 20).
* @param $fields
* The fields you want returned.
* @param $parameters
* An array containing fields and values used to build a sql WHERE clause
* indicating items to retrieve.
* @param $page_size
* Integer number of items to be returned.
* @param $options
* Additional query options.
* @return
* An array of comment objects.
*
* @see _node_resource_index() for more notes
**/
function _comment_resource_index($page, $fields, $parameters, $page_size, $options = array()) {
$comment_select = db_select('comment', 't');
services_resource_build_index_query($comment_select, $page, $fields, $parameters, $page_size, 'comment', $options);
if (!user_access('administer comments')) {
$comment_select->condition('status', COMMENT_PUBLISHED);
}
$results = services_resource_execute_index_query($comment_select);
return services_resource_build_index_list($results, 'comment', 'cid');
}
/**
* Returns the number of comments on a given node id.
*
* @param $nid
* Unique identifier for the specified node.
* @return
* Number of comments that node has.
*/
function _comment_resource_count_all($nid) {
$node = node_load($nid);
return $node->comment_count;
}
/**
* Returns the number of new comments on a given node id since timestamp.
*
* @param $nid
* Unique identifier for the specified node.
* @param $since
* Timestamp to indicate what nodes are new. Defaults to time of last user acces to node.
* @return
* Number of comments that node has.
*/
function _comment_resource_count_new($nid, $since) {
return comment_num_new($nid, $since);
}
/**
* Access check callback for comment controllers.
*/
function _comment_resource_access($op = 'view', $args = array()) {
// Adds backwards compatability with regression fixed in #1083242
if (isset($args[0])) {
$args[0] = _services_access_value($args[0], array('comment', 'data'));
}
if ($op == 'create') {
$comment = (object)$args[0];
}
else {
$comment = comment_load($args[0]);
}
if(isset($comment->nid)) {
$node = node_load($comment->nid);
if($op == 'create' && !$node->nid) {
return services_error(t('Node nid: @nid does not exist.', array('@nid' => $comment->nid)), 406);
}
}
if (user_access('administer comments')) {
return TRUE;
}
switch ($op) {
case 'view':
// Check if the user has access to comments
return user_access('access comments');
case 'delete':
return user_access('administer comments');
case 'edit':
return comment_access('edit', $comment);
case 'create':
// Check if the user may post comments, node has comments enabled
// and that the user has access to the node.
return user_access('post comments') && ($node->comment == COMMENT_NODE_OPEN);
}
}