-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopen_review.module
200 lines (165 loc) · 7.15 KB
/
open_review.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
<?php
// $Id$
/**
* @file
* A module to create comment functionality similar to CommentPress for WordPress.
* Allows comments to be made on a paragraph-by-paragraph basis.
*/
/**
* Implementation of hook_comment()
*
**/
function open_review_comment($a1, $op) {
switch ($op) {
case 'view':
//check whether the comment is associated with a paragraph
$para_id = db_result(db_query('SELECT p_id FROM {open_review} o WHERE o.cid = %d OR o.cid = %d', $a1->cid, $a1->pid));
if ($para_id) {
$wrappedComment = "<span class='open-review-comment' id='open-review-on-para-$para_id'>";
$wrappedComment .= "<span class='open-review-comment-open-on-para'>Open paragraph comments</span>";
$wrappedComment .= $a1->comment;
$wrappedComment .= "</span>";
$a1->comment = $wrappedComment;
}
break;
case 'insert':
$cid = $a1['cid'];
if ($a1['pid']) {
$para_id = db_result(db_query('SELECT p_id FROM {open_review} o WHERE o.cid = %d ', $a1['pid']));
}
else {
$para_id = $a1['open_review_para_id'];
}
$data = array('cid' => $cid, 'p_id' => $para_id );
drupal_write_record('open_review', $data );
break;
case 'delete':
db_query('DELETE FROM {open_review} WHERE cid = %d', $a1->cid);
break;
}
}
/**
* Implementation of hook_form_alter().
*/
function open_review_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'comment_form') {
$form['comment_filter']['open_review_para_id'] = array(
'#type' => 'hidden'
);
$form['#prefix'] = "<div id='open-review-comment-form'>";
$form['#suffix'] = "</div>";
}
elseif ($form_id = 'node_type_form' && isset($form['identity']['type'])) {
$form['comment']['open_review'] = array(
'#type' => 'radios',
'#title' => t('Open Review Comments'),
'#default_value' => variable_get('open_review_' . $form['#node_type']->type, 0),
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
'#description' => t('Use Open Review for this content type?')
);
$form['comment']['open_review_form_in_popup'] = array(
'#type' => 'radios',
'#title' => t('Open Review Comment Form in Popups?'),
'#default_value' => variable_get('open_review_form_in_popup_'. $form['#node_type']->type, 0),
'#options' => array(0 => t('No comment form'), 1 => t('Comment form under paragraph comments')),
'#description' => t('Show the comment form under paragraph comments?')
);
}
}
/**
* Implementation of hook_nodeapi()
*/
function open_review_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op=='view') {
if (variable_get('open_review_' . $node->type, 0)) {
//I'm bringing in my own copy of jQuery and jQuery UI because the jQuery Upgrade and jQuery UI
//modules have weak documentation and are too difficult to install
//jquery UI also breaks when switching themes, and theme-agnosticsim is a design goal
//of open_review: it's something that sets it apart from CommentPress
drupal_add_css(drupal_get_path('module', 'open_review') . '/js/jquery/themes/base/ui.dialog.css');
drupal_add_css(drupal_get_path('module', 'open_review') . '/js/jquery/themes/base/ui.resizable.css');
drupal_add_js(drupal_get_path('module', 'open_review') . '/js/jquery/jquery-1.3.2.min.js');
drupal_add_js(drupal_get_path('module', 'open_review') . '/js/jquery/jquery-ui-1.7.3.custom.min.js');
drupal_add_js(drupal_get_path('module', 'open_review') . '/js/open_review.js');
drupal_add_css(drupal_get_path('module', 'open_review') . '/css/open_review.css');
$text=$node->content['body']['#value'];
//I need to add an id to the <p> elements, so domify it to manipulate the <p>s
$dom = new DOMDocument();
$dom->loadHTML('<?xml encoding="UTF-8">' . $text);
$xpath = new DOMXPath($dom);
$ps = $dom->getElementsByTagName('p');
foreach ($ps as $p) {
$id = "p-" . sha1($p->textContent);
$p->setAttribute('id', $id);
$p->setAttribute('class', 'open-review-para');
$actionsEl = $dom->createElement('span');
$actionsEl->setAttribute('class', 'open-review-para-actions');
$commentEl = $dom->createElement('span');
$commentEl->setAttribute('class', 'open-review-para-comment');
$commentEl->setAttribute('title', 'Add A Comment');
$actionsEl->appendChild($commentEl);
$result = db_result(db_query('SELECT COUNT(*) FROM {open_review} WHERE p_id = "%s" ', $id ));
if ($result > 0 ) {
$viewEl = $dom->createElement('span', $result);
$viewEl->setAttribute('class', 'open-review-para-view');
$viewEl->setAttribute('title', 'View Comments');
$actionsEl->appendChild($viewEl);
}
$newCount = open_review_comment_num_new($node->nid, $id);
if ($newCount > 0) {
$newEl = $dom->createElement('span', $newCount);
$newEl->setAttribute('class', 'open-review-new');
$actionsEl->appendChild($newEl);
}
$p->insertBefore($actionsEl, $p->firstChild);
}
$node->content['body']['#value'] = $dom->saveHTML();
}
}
}
/**
* Implementation of hook_footer()
*/
function open_review_footer() {
$node = node_load(arg(1));
if (variable_get('open_review_' . $node->type, 0)) {
$output = "<div id='open-review-footer'>";
$output .= "<div id='open-review-comments-dialog'></div>";
$output .= "<div id='open-review-comment-form-dialog'></div>";
$output .= "</div>";
$output .= "<script type='text/javascript'>";
if (variable_get('open_review_form_in_popup_'. $node->type, 0)) {
$output .= "OpenReview.commentsInPopup = true;";
}
else {
$output .= "if (OpenReview) { OpenReview.commentsInPopup = false; }";
}
$output .= "</script>";
return $output;
}
}
/**
* Modified from comment_num_new. Retrieves the number of new comments on a paragraph
* @param int $nid
* @param string $paraId
* @param int $timestamp
* @return int the number of new comments
*/
function open_review_comment_num_new($nid, $paraId, $timestamp = 0) {
global $user;
if ($user->uid) {
// Retrieve the timestamp at which the current user last viewed the
// specified node.
if (!$timestamp) {
$timestamp = node_last_viewed($nid);
}
$timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
// Use the timestamp to retrieve the number of new comments.
$query = "SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid INNER JOIN {open_review} o ON c.cid = o.cid WHERE n.nid = %d AND timestamp > %d AND c.status = %d AND o.p_id = '%s' " ;
$result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid INNER JOIN {open_review} o ON c.cid = o.cid WHERE n.nid = %d AND timestamp > %d AND c.status = %d AND o.p_id = "%s" ', $nid, $timestamp, COMMENT_PUBLISHED, $paraId));
return $result;
}
else {
return 0;
}
}