-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_comments.module
729 lines (637 loc) · 27.5 KB
/
ajax_comments.module
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
<?php
return ;
/**
* @file
* AJAX comments module file.
*/
/**
* Implements hook_menu().
*/
function ajax_comments_menu() {
$items['ajax_comments/reply/%node'] = array(
'page callback' => 'ajax_comments_reply',
'page arguments' => array(2),
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
$items['ajax_comments/reply/%node/%'] = array(
'page callback' => 'ajax_comments_reply',
'page arguments' => array(2,3),
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
$items['ajax_comments/edit/%comment'] = array(
'page callback' => 'ajax_comments_edit',
'page arguments' => array(2),
'access callback' => 'comment_access',
'access arguments' => array('edit', 2),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
$items['ajax_comments/delete/%'] = array(
'page callback' => 'ajax_comments_delete',
'page arguments' => array(2),
'access arguments' => array('administer comments'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
/*
$items['admin/config/content/ajax_comments'] = array(
'title' => 'AJAX comments',
'description' => 'AJAXifies comments on site.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ajax_comments_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'ajax_comments.admin.inc',
);
*/
return $items;
}
/**
* Implements hook_views_api().
*/
function ajax_comments_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'ajax_comments'),
);
}
/**
* Implements hook_views_data().
*/
function ajax_comments_views_data() {
$data['node']['list_comments'] = array(
'title' => t('List of comments'),
'help' => t("Display the node's list of comments."),
'field' => array(
'handler' => 'ajax_comments_handler_field_list_comments',
),
);
//@TODO: remove
$data['node']['ajax_comment'] = array(
'title' => t('AJAX Add Comment'),
'help' => t('Adds an inline AJAX comment form.'),
'field' => array(
'handler' => 'ajax_comments_handler_field_ajax_add_comment',
),
);
return $data;
}
/**
* Implements hook_preprocess_node().
*
* AJAXify "Add new comment" link when there is no default form.
*/
function ajax_comments_preprocess_node(&$variables) {
$node = $variables['node'];
$view_mode = $variables['view_mode'];
if (!ajax_comments_node_type_active($node->type, $view_mode)) {
return;
}
if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW) {
return;
}
if (empty($node->comment) || $node->comment == COMMENT_NODE_CLOSED) {
return;
}
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'drupal.form');
drupal_add_js(drupal_get_path('module', 'ajax_comments') .'/ajax_comments.js', 'file');
$variables['content']['links']['comment']['#links']['comment-add']['attributes']['class'] = 'use-ajax';
$variables['content']['links']['comment']['#links']['comment-add']['attributes']['id'] = 'reply-0';
$variables['content']['links']['comment']['#links']['comment-add']['href'] = 'ajax_comments/reply/' . $node->nid;
$variables['content']['links']['comment']['#links']['comment-add']['fragment'] = NULL;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ajax_comments_form_comment_form_alter(&$form, &$form_state, $form_id) {
// Check to see if this node type uses ajax comments.
if (!ajax_comments_node_type_active($form['#node']->type, 'any')) {
return;
}
// Disable output original node/comment in preview
if (isset($form['comment_preview'])) {
$form['notify_text'] = array(
'#markup' => variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('type' => 'preview', 'comment' => $form_state['comment'])) : '',
'#weight' => "-100",
);
if (isset($form['comment_output_below'])) {
unset($form['comment_output_below']);
}
}
if (empty($form_state['storage']['ajax_comments_form_id'])) {
$cid = $pid = empty($form_state['comment']->cid) ? 0 : $form_state['comment']->cid;
$pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
$id = 'ajax-comment-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid;
$form_state['storage']['ajax_comments_form_id'] = $id;
} else {
$id = $form_state['storage']['ajax_comments_form_id'];
}
// not sure if we still need this
$form['#attributes']['id'] = drupal_html_id($id);
$form['#attributes']['class'][] = $id;
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_comments_submit_js',
'wrapper' => $id,
'method' => 'replace',
'effect' => 'fade',
);
// Set unique id (need for Views with enabled AJAX)
if (empty($form['actions']['submit']['#id'])) {
$form['actions']['submit']['#id'] = drupal_html_id('edit-' . $id);
}
$form['actions']['preview']['#ajax'] = array(
'callback' => 'ajax_comments_preview_js',
'wrapper' => $id,
'method' => 'replace',
'effect' => 'fade',
);
// Set unique id (need for Views with enabled AJAX)
if (empty($form['actions']['preview']['#id'])) {
$form['actions']['preview']['#id'] = drupal_html_id('preview-' . $id);
}
// If this a reply to comment offer a 'cancel' button
if (isset($form_state['comment']->pid)) {
$form['actions']['cancel'] = array (
'#type' => 'button',
'#value' => t('Cancel'),
'#access' => true,
'#weight' => 21,
'#limit_validation_errors' => array(),
);
$form['actions']['cancel']['#ajax'] = array(
'wrapper' => $id,
'method' => 'replace',
'effect' => 'fade',
);
if (empty($form_state['comment']->cid)) {
$form['actions']['cancel']['#ajax']['callback'] = 'ajax_comments_cancel_js';
} else {
$form['actions']['cancel']['#ajax']['callback'] = 'ajax_comments_edit_cancel_js';
}
// Set unique id (need for Views with enabled AJAX)
if (empty($form['actions']['cancel']['#id'])) {
$form['actions']['cancel']['#id'] = drupal_html_id('cancel-' . $id);
}
}
$form['#attached'] = array(
'js' => array(drupal_get_path('module', 'ajax_comments') . '/ajax_comments.js'),
);
$form['actions'] = ajax_pre_render_element($form['actions']);
// @todo: test without this hack, and remove
// HACK, stop ctools from modifying us in node_comment_form.inc
//$form_state['ctools comment alter'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ajax_comments_form_comment_confirm_delete_alter(&$form, &$form_state, $form_id) {
if (ajax_comments_node_type_active(substr($form['#comment']->node_type, strlen('comment_node_')), 'any')) {
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_comments_delete_js',
'wrapper' => $form['#id'],
'method' => 'replace',
);
$form['actions']['cancel']['#attributes']['onclick'][] = 'jQuery(\'#' . $form['#id'] . '\').siblings().show().end().remove(); return false;';
}
}
/**
* Previews the comment.
*/
function ajax_comments_preview_js($form, &$form_state) {
// Return the actual form if it contains errors.
if (form_get_errors()) {
return $form;
}
$pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
$cid = empty($form_state['comment']->cid) ? 0 : $form_state['comment']->cid;
$commands[] = ajax_command_replace('.ajax-comment-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid, drupal_render($form));
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.ajax-comment-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Cancel edit the comment.
*/
function ajax_comments_cancel_js($form, &$form_state) {
$cid = empty($form_state['comment']->cid) ? 0 : $form_state['comment']->cid;
$pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
$commands[] = ajax_command_remove('.ajax-comment-reply-form-' . $form['#node']->nid . '-' . $pid . '-' . $cid);
if (variable_get('comment_form_location_' . $form['#node']->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW) {
if (!variable_get('ajax_comments_disable_scroll', 0)) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.comment-wrapper-' . $pid);
}
}
$commands[] = ajax_command_invoke('a#reply-' . $pid, 'show');
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Re-grabs comment after editing is cancelled.
*/
function ajax_comments_edit_cancel_js($form, &$form_state) {
$comment = comment_load($form_state['comment']->cid);
$comment_build = comment_view($comment, $form['#node']);
$comment_output = drupal_render($comment_build);
$commands[] = ajax_command_replace('.ajax-comment-reply-form-' . $comment->nid . '-' . $comment->pid . '-' . $comment->cid, $comment_output);
if (!variable_get('ajax_comments_disable_scroll', 0)) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.comment-wrapper-' . $form_state['comment']->cid);
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Builds the comment.
*/
function ajax_comments_submit_js($form, &$form_state) {
// Return the actual form if it contains errors.
if (form_get_errors()) {
return $form;
}
// This is to remove the "Your comment has been posted" status message that
// will appear upon refresh. This seems dirty but it means we don't have to
// rewrite the whole comment_form_submit(). Please chime in if you think this
// is dumb.
ajax_comments_remove_status();
$cid = empty($form['cid']['#value']) ? 0 : $form['cid']['#value'];
$pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
if (cache_get('ajax_comments_reply_form-' . $form_state['comment']->nid . '-' . $pid)) {
// This form cached. Remove it from cache.
cache_clear_all('ajax_comments_reply_form-' . $form_state['comment']->nid . '-' . $pid, 'cache');
}
$comment = comment_load($form_state['comment']->cid);
$node = $form['#node'];
$notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('comment' => $comment)) : '';
$comment_build = comment_view($comment, $node);
/**
* comment_goodnes module compatibility:
* 1 - Older first
* 2 - Newer first
*/
$sort = variable_get('comment_default_sorting_' . $node->type, 1);
// remove messages
if (variable_get('ajax_comments_notify')) {
$commands[] = ajax_command_remove('.messages.warning');
$commands[] = ajax_command_remove('.messages.status');
}
// Don't display as a preview as this is being submitted.
unset($comment_build['comment_body']['#object']->in_preview);
unset($form_state['comment_preview']);
$comment_output = drupal_render($comment_build);
// Are we editing a comment.
if (isset($form['cid']['#value'])) {
//$commands[] = ajax_command_replace('.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, $comment_output);
$commands[] = array('command' => 'ajaxCommentsReplace', 'selector' => '.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'html' => $comment_output);
}
// Or are we replying to another comment.
elseif (!empty($form_state['values']['pid'])) {
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
if (!empty($mode)) {
// Threaded. Append comment to parent wrapper.
//$commands[] = ajax_command_replace('.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, $comment_output);
$commands[] = array('command' => 'ajaxCommentsReplace', 'selector' => '.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'html' => $comment_output);
} else {
// Flat. Check sort by comment_goodness.
if ($sort == 1) {
// Older first. Append comment to last wrapper.
$commands[] = ajax_command_invoke('.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');
//$commands[] = ajax_command_after('div.comment-wrapper-nid-' . $comment->nid . ' > div.ajax-comment-wrapper:last', $comment_output);
$commands[] = array('command' => 'ajaxCommentsAfter', 'selector' => 'div.comment-wrapper-nid-' . $comment->nid . ' > div.ajax-comment-wrapper:last', 'html' => $comment_output);
} else {
// Newer first. Append comment to top.
$commands[] = ajax_command_invoke('.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');
//$commands[] = ajax_command_prepend('div.comment-wrapper-nid-' . $comment->nid, $comment_output);
$commands[] = array('command' => 'ajaxCommentsPrepend', 'selector' => 'div.comment-wrapper-nid-' . $comment->nid, 'html' => $comment_output);
}
}
if (!variable_get('ajax_comments_disable_scroll', 0) && empty($mode)) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.comment-wrapper-' . $comment->cid);
}
}
// Or is this a brand new comment?
else {
// Check sort by comment_goodness.
if ($sort == 1) {
// Older first. Append comment to last wrapper.
//$commands[] = ajax_command_after('div.comment-wrapper-nid-' . $comment->nid . ' > div.ajax-comment-wrapper:last', $comment_output);
$commands[] = array('command' => 'ajaxCommentsAfter', 'selector' => 'div.comment-wrapper-nid-' . $comment->nid . ' > div.ajax-comment-wrapper:last', 'html' => $comment_output);
} else {
// Newer first. Append comment to top.
//$commands[] = ajax_command_prepend('div.comment-wrapper-nid-' . $comment->nid, $comment_output);
$commands[] = array('command' => 'ajaxCommentsBefore', 'selector' => 'div.comment-wrapper-nid-' . $comment->nid . '> div.ajax-comment-wrapper:first', 'html' => $comment_output);
}
// If we have a default form, update it with a new one.
if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW) {
$new_form_state = array();
$new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
// Don't pull from cache.
$new_form_state['input'] = array();
$new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
$commands[] = ajax_command_replace('.ajax-comment-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid, drupal_render($new_form_build));
if (!variable_get('ajax_comments_disable_scroll', 0) && $sort == 2) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => 'div.comment-wrapper-nid-' . $comment->nid);
}
}
// Otherwise, delete it.
else {
$commands[] = ajax_command_invoke('.ajax-comment-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');
}
}
// Show notify
if (!empty($notify_text)) {
//$commands[] = ajax_command_before('.comment-wrapper-' . $comment->cid, $notify_text);
$commands[] = array('command' => 'ajaxCommentsBefore', 'selector' => '.comment-wrapper-' . $comment->cid, 'html' => $notify_text);
}
$commands[] = ajax_command_invoke('a#reply-' . $pid, 'show');
$output = array('#type' => 'ajax', '#commands' => $commands);
return $output;
}
/**
* Removes the comment.
*/
function ajax_comments_delete_js($form, &$form_state) {
$comment = $form['#comment'];
ajax_comments_remove_status();
$notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('type' => 'delete', 'comment' => $comment)) : '';
if ($notify_text) {
$commands[] = ajax_command_remove('.messages.status');
$commands[] = ajax_command_replace('.comment-wrapper-' . $comment->cid, $notify_text);
$commands[] = ajax_command_remove('a#comment-' . $comment->cid);
}
else {
$commands[] = ajax_command_remove('.comment-wrapper-' . $comment->cid);
$commands[] = ajax_command_remove('a#comment-' . $comment->cid);
}
// Remove all replies to deleted comment from page
if (!empty($form_state['storage']['cids'])) {
foreach ($form_state['storage']['cids'] as $cid) {
$commands[] = ajax_command_remove('.comment-wrapper-' . $cid);
$commands[] = ajax_command_remove('a#comment-' . $cid);
}
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implements hook_comment_view().
*/
function ajax_comments_comment_view($comment, $view_mode, $langcode) {
// Check
if (ajax_comments_node_type_active(substr($comment->node_type, strlen('comment_node_')), $view_mode)) {
// Reply.
if (isset($comment->content['links']['comment']['#links']['comment-reply'])) {
$comment->content['links']['comment']['#links']['comment-reply']['attributes']['class'] = array('use-ajax', 'ajax-comment-reply', 'reply-' . $comment->cid);
$comment->content['links']['comment']['#links']['comment-reply']['attributes']['id'] = array('reply-' . $comment->cid);
$comment->content['links']['comment']['#links']['comment-reply']['href'] = 'ajax_comments/reply/' . $comment->nid . '/' . $comment->cid;
}
// Edit.
if (isset($comment->content['links']['comment']['#links']['comment-edit'])) {
$comment->content['links']['comment']['#links']['comment-edit']['attributes']['class'] = array('use-ajax', 'ajax-comment-edit');
$comment->content['links']['comment']['#links']['comment-edit']['href'] = 'ajax_comments/edit/' . $comment->cid;
}
// Delete.
if (isset($comment->content['links']['comment']['#links']['comment-delete'])) {
$comment->content['links']['comment']['#links']['comment-delete']['attributes']['class'] = array('use-ajax');
$comment->content['links']['comment']['#links']['comment-delete']['href'] = 'ajax_comments/delete/' . $comment->cid;
}
}
}
/**
* Implements template_preprocess_comment()
* Wrap comments and their replies in a #comment-wrapper-(cid) div
*/
function ajax_comments_preprocess_comment(&$variables) {
$element = $variables['elements'];
$comment = $element['#comment'];
if (ajax_comments_node_type_active(substr($comment->node_type, strlen('comment_node_')), $element['#view_mode'])) {
$variables['classes_array'][] = 'ajax-comment-wrapper';
$variables['classes_array'][] = 'comment-wrapper-' . $comment->cid;
}
}
/*
* Implements template_preprocess_comment_wrapper()
*/
function ajax_comments_preprocess_comment_wrapper(&$variables) {
$variables['classes_array'][] = 'comment-wrapper-nid-' . $variables['node']->nid;
}
/**
* Callback for clicking "reply".
* Note: $pid is an optional parameter. This functionality is utilized by the
* "Add new comment" link on pages where there is no default comment form
* (comment_form_location is COMMENT_FORM_SEPARATE_PAGE)
*/
function ajax_comments_reply($node, $pid = 0, $flag = 'node') {
if (!user_access('post comments')) {
return MENU_ACCESS_DENIED;
}
// If there is a pid this is a reply to a comment.
if ($pid == "NULL") $pid = NULL;
if (!empty($pid)) {
if (!user_access('access comments')) {
return MENU_ACCESS_DENIED;
}
// Make sure the comment is valid and published.
if (!($comments = comment_load_multiple(array($pid), array('status' => COMMENT_PUBLISHED)))) {
return MENU_NOT_FOUND;
}
$comment = $comments[$pid];
// Make sure the comment belongs to this node.
if ($comment->nid != $node->nid) {
return MENU_NOT_FOUND;
}
}
if ($cache = cache_get('ajax_comments_reply_form-' . $node->nid . '-' . $pid)) {
// Get form from cache
$form_build = $cache->data;
} else {
// Build form and Save to cache*/
$form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid, 'pid' => $pid));
cache_set('ajax_comments_reply_form-' . $node->nid . '-' . $pid, $form_build, 'cache', time() + 360);
}
$form = trim(drupal_render($form_build));
/**
* comment_goodnes module compatibility:
* 1 - Older first
* 2 - Newer first
*/
$sort = variable_get('comment_default_sorting_' . $node->type, 1);
// Add the new form.
if (!empty($pid)) {
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
if (empty($mode)) {
$commands[] = ajax_command_after('.comment-wrapper-' . $pid, $form);
} else {
// Add div with class "indented" if they are not exist
$commands[] = array('command' => 'ajaxCommentsAddDummyDivAfter', 'selector' => '.comment-wrapper-' . $pid, 'class' => 'indented');
// Check sort by comment_goodness.
if ($sort == 1) {
// Newer first.
$commands[] = ajax_command_append('.comment-wrapper-' . $pid . ' + .indented', $form);
} else {
// Older first.
$commands[] = ajax_command_prepend('.comment-wrapper-' . $pid . ' + .indented', $form);
}
}
} else {
// Check sort by comment_goodness.
if ($sort == 1) {
// Older first. Append comment to last wrapper.
$commands[] = ajax_command_after('div.comment-wrapper-nid-' . $node->nid . ' > div.ajax-comment-wrapper:last', $form);
} else {
// Newer first. Append comment to top.
$commands[] = ajax_command_before('div.comment-wrapper-nid-' . $node->nid . '> div.ajax-comment-wrapper:first', $form);
}
}
$commands[] = ajax_command_invoke('a#reply-' . $pid, 'hide');
if (!variable_get('ajax_comments_disable_scroll', 0)) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.ajax-comment-reply-form-' . $node->nid . '-' . $pid . '-0');
$commands[] = ajax_command_invoke('.ajax-comment-reply-form-' . $node->nid . '-' . $pid . '-0 .form-textarea' , 'focus');
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Callback for clicking "edit".
*/
function ajax_comments_edit($comment) {
$node = node_load($comment->nid);
// Build form.
$form_build = drupal_get_form("comment_node_{$node->type}_form", $comment);
$form = drupal_render($form_build);
// Remove anchor
$commands[] = ajax_command_remove('a#comment-' . $comment->cid);
// Replace comment with form.
$commands[] = ajax_command_replace('.comment-wrapper-' . $comment->cid, $form);
if (!variable_get('ajax_comments_disable_scroll', 0)) {
$commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.ajax-comment-reply-form-' . $comment->nid . '-' . $comment->pid . '-' . $comment->cid);
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Callback for clicking "delete".
*/
function ajax_comments_delete($cid) {
if (!($comment = comment_load($cid))) {
return MENU_NOT_FOUND;
}
// Need to include comment module admin file for delete form.
$form_state = array();
$form_state['build_info']['args'] = array($comment);
// Load this using form_load_include so it's cached properly and works in the
// ajax callback.
form_load_include($form_state, 'inc', 'comment', 'comment.admin');
// Get child comments (replies on this comment)
$form_state['storage']['cids'] = array();
$query = db_select('comment', 'c');
$query->addField('c', 'cid');
$query->condition('c.nid', $comment->nid)->condition('c.thread', substr($comment->thread, 0, -1) . '.%', 'LIKE')->addTag('node_access');
if (!user_access('administer comments')) {
$query->condition('c.status', COMMENT_PUBLISHED);
}
$query->orderBy('c.cid', 'ASC');
$cids = $query->execute()->fetchCol();
// Save child comments ids if they are exist
if (!empty($cids)) {
$form_state['storage']['cids'] = $cids;
}
$form_build = drupal_build_form('comment_confirm_delete', $form_state);
$form = drupal_render($form_build);
// Hide contents.
$commands[] = ajax_command_invoke('.comment-wrapper-' . $cid . ' >*', 'hide');
// Put form inside main comment wrapper.
$commands[] = ajax_command_prepend('.comment-wrapper-' . $cid, $form);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Removes "Your comment has been posted." or "Your comment has been queued.."
* from the status message.
*/
function ajax_comments_remove_status() {
$deleted = t('The comment and all its replies have been deleted.');
$published = t('Your comment has been posted.');
$not_published = t('Your comment has been queued for review by site administrators and will be published after approval.');
foreach ($_SESSION['messages']['status'] as $key => $value) {
if ($value == $published || $value == $not_published || $value = $deleted) {
unset($_SESSION['messages']['status'][$key]);
}
}
if (empty($_SESSION['messages']['status'])) {
unset($_SESSION['messages']['status']);
if (empty($_SESSION['messages'])) {
unset($_SESSION['messages']);
}
}
}
/**
* Returns TRUE if this node uses ajax comments or if no nodes are selected.
*/
function ajax_comments_node_type_active($node_type, $view_mode = '') {
$types = array_filter(variable_get('ajax_comments_node_types', array()));
$view_modes = variable_get('ajax_comments_view_modes', array());
// return TRUE if nothing select
if (empty($types) && empty($view_modes[$view_mode])) {
return TRUE;
}
// return TRUE if node type and view mode match
if (!empty($types[$node_type]) && !empty($view_modes[$view_mode])) {
return TRUE;
}
// return TRUE if node type match but mode is empty (hack for altered forms)
if (!empty($types[$node_type]) && empty($view_modes[$view_mode])) {
return TRUE;
}
}
/**
* Implements hook_theme().
*/
function ajax_comments_theme($existing, $type, $theme, $path) {
return array(
'ajax_comments_notify_text' => array(
'variables' => array('type' => NULL, 'comment' => NULL),
),
);
}
/**
* Returns text to notify user their comment has been added.
*/
function theme_ajax_comments_notify_text($vars = array()) {
$text = t('Your comment has been posted');
$status = 'status';
// If the comment is unapproved, alter the message
if (isset($vars['comment']) && !$vars['comment']->status) {
$text = t('Your comment has been queued for review by site administrators and will be published after approval.');
$status = 'warning';
}
if ($vars['type'] == 'delete') {
$text = t('Your comment has been deleted');
}
elseif ($vars['type'] == 'preview') {
$text = t('This is the preview for your comment. You must click SAVE or your comment will be lost.');
$status = 'warning';
}
drupal_set_message($text, $status);
return theme('status_messages');
}
/**
* Implements hook_process_node().
* We use prorcess instead preprocess just because views overwrite $variables['content']['comments'] when display comments is enabled
*/
function ajax_comments_process_node(&$variables) {
if (isset($variables['content']['comments']) && $variables['node']->comment == COMMENT_NODE_OPEN && ajax_comments_node_type_active($variables['node']->type, $variables['elements']['#view_mode'])) {
/**
* A little HACK for always run comment-wrapper.tpl.php and comment.tpl.php also when we have no comments.
* This is give us always have a right div layout on the page and
* dummy comment give us right point to place first comment on the page.
*/
if (empty($variables['content']['comments'])) {
$variables['content']['comments']['#node'] = $variables['node'];
$variables['content']['comments']['#theme'] = 'comment_wrapper__node_' . $variables['node']->type;
}
$variables['content']['comments']['comments']['dummyComment'] = array(
'#prefix' => '<div class="ajax-comment-wrapper ajax-comment-dummy-comment" style="display:none">',
'#type' => 'item',
'#markup' => '',
'#suffix' => '</div>',
);
}
}