| 1 |
<?php
|
| 2 |
// $Id: node_annotate.module,v 1.5.2.1 2008/02/12 16:42:25 tc1415 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_node_info
|
| 6 |
*/
|
| 7 |
function node_annotate_node_info() {
|
| 8 |
return array(
|
| 9 |
'node_annotate' => array(
|
| 10 |
'name' => t('Annotatable Node'),
|
| 11 |
'description' => t('A node with line by line annotation'),
|
| 12 |
'module' => 'node_annotate',
|
| 13 |
),
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implmentation of hook_access
|
| 19 |
*/
|
| 20 |
function node_annotate_access($op, $node) {
|
| 21 |
global $user;
|
| 22 |
|
| 23 |
if ($op == 'create') {
|
| 24 |
// Only users with permission to do so may create this node type.
|
| 25 |
return user_access('create annotate node');
|
| 26 |
}
|
| 27 |
|
| 28 |
// Users who create a node may edit or delete it later, assuming they have the
|
| 29 |
// necessary permissions.
|
| 30 |
if ($op == 'update' || $op == 'delete') {
|
| 31 |
if (user_access('edit own annotate nodes') && ($user->uid == $node->uid)) {
|
| 32 |
return TRUE;
|
| 33 |
}
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Implementation of hook_perm
|
| 39 |
*/
|
| 40 |
function node_annotate_perm() {
|
| 41 |
return array('create annotate node', 'edit own annotate nodes', 'annotate nodes', 'edit own annotations');
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implmentation of hook_menu
|
| 46 |
*/
|
| 47 |
function node_annotate_menu($may_cache) {
|
| 48 |
$items = array();
|
| 49 |
|
| 50 |
if ($may_cache) {
|
| 51 |
$items[] = array(
|
| 52 |
'path' => 'annotate',
|
| 53 |
'title' => t('Annotate Node'),
|
| 54 |
'callback' => 'node_annotate_add_annotation',
|
| 55 |
'access' => user_access('annotate nodes'),
|
| 56 |
'type' => MENU_CALLBACK
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_form
|
| 65 |
*/
|
| 66 |
|
| 67 |
function node_annotate_form(&$node, &$param) {
|
| 68 |
$type = node_get_types('type', $node);
|
| 69 |
|
| 70 |
$form['title'] = array(
|
| 71 |
'#type' => 'textfield',
|
| 72 |
'#title' => check_plain($type->title_label),
|
| 73 |
'#default_value' => $node->title,
|
| 74 |
'#required' => TRUE,
|
| 75 |
);
|
| 76 |
$form['body'] = array(
|
| 77 |
'#type' => 'textarea',
|
| 78 |
'#title' => check_plain($type->body_label),
|
| 79 |
'#rows' => 20,
|
| 80 |
'#default_value' => $node->body,
|
| 81 |
'#required' => TRUE,
|
| 82 |
);
|
| 83 |
$form['description'] = array(
|
| 84 |
'#type' => 'textarea',
|
| 85 |
'#title' => t('Description'),
|
| 86 |
'#default_value' => $node->description,
|
| 87 |
);
|
| 88 |
|
| 89 |
return $form;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Implementation of hook_insert
|
| 94 |
*/
|
| 95 |
function node_annotate_insert($node) {
|
| 96 |
$sql = 'INSERT INTO {node_annotate} (vid, nid, description) VALUES (%d, %d, \'%s\')';
|
| 97 |
db_query($sql, $node->vid, $node->nid, $node->description);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementaion of hook_update
|
| 102 |
*/
|
| 103 |
function node_annotate_update($node) {
|
| 104 |
// if this is a new node or we're adding a new revision,
|
| 105 |
if ($node->revision) {
|
| 106 |
node_example_insert($node);
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
$sql = 'UPDATE {node_annotate} SET description = \'%s\' WHERE vid = %d';
|
| 110 |
db_query($sql, $node->description, $node->vid);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Implementation of hook_nodeapi
|
| 116 |
*
|
| 117 |
* 'delete revision': Used in order to delete revisions from out table when they are deleted from the revision table
|
| 118 |
* 'alter' : Edit node_annotate type nodes to include line numbers and annotations
|
| 119 |
*/
|
| 120 |
function node_annotate_nodeapi(&$node, $op, $teaser, $page) {
|
| 121 |
switch ($op) {
|
| 122 |
case 'delete revision':
|
| 123 |
// Notice that we're matching a single revision based on the node's vid.
|
| 124 |
$sql = 'DELETE FROM {node_annotate} WHERE vid = %d';
|
| 125 |
db_query($sql, $node->vid);
|
| 126 |
break;
|
| 127 |
case 'alter':
|
| 128 |
if ($node->type != 'node_annotate') {
|
| 129 |
return ;
|
| 130 |
}
|
| 131 |
|
| 132 |
$node->body = _node_annotate_format_lines($node->body, $node->nid, $node->vid);
|
| 133 |
$node->teaser = _node_annotate_format_lines($node->teaser, $node->nid, $node->vid);
|
| 134 |
break;
|
| 135 |
// Indentation marker, just ignore me
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Implementation of hook_delete
|
| 141 |
*/
|
| 142 |
function node_annotate_delete($node) {
|
| 143 |
// Notice that we're matching all revision, by using the node's nid.
|
| 144 |
$sql = 'DELETE FROM {node_annotate} WHERE nid = %d';
|
| 145 |
db_query($sql, $node->nid);
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Implementation of hook_load
|
| 150 |
*/
|
| 151 |
function node_annotate_load($node) {
|
| 152 |
$sql = 'SELECT description FROM {node_annotate} WHERE vid = %d';
|
| 153 |
$result = db_query($sql, $node->vid);
|
| 154 |
$additions = db_fetch_object($result);
|
| 155 |
return $additions;
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Implementation of hook_view
|
| 160 |
*/
|
| 161 |
function node_annotate_view($node, $teaser = FALSE, $page = FALSE) {
|
| 162 |
// Shove our CSS in the page
|
| 163 |
drupal_add_css($path = 'sites/all/modules/node_annotate/node_annotate.css', $type = 'module', $media = 'all', $preprocess = TRUE);
|
| 164 |
|
| 165 |
$node = node_prepare($node, $teaser);
|
| 166 |
$node->content['description'] = array(
|
| 167 |
'#value' => theme('node_annotation_description', $node),
|
| 168 |
'#weight' => -1,
|
| 169 |
);
|
| 170 |
|
| 171 |
return $node;
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Custom theme function for global_annotation
|
| 176 |
*/
|
| 177 |
function theme_node_annotation_description($node) {
|
| 178 |
$output = '<div class="node_annotation_description">';
|
| 179 |
$output .= t(check_markup($node->description, FILTER_FORMAT_DEFAULT, FALSE));
|
| 180 |
$output .= '</div>'."\n";
|
| 181 |
$output .= '<!-- Description End Canary -->'."\n";
|
| 182 |
return $output;
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Callback for node_annotate_add_annotation
|
| 187 |
*/
|
| 188 |
function node_annotate_add_annotation($nid, $vid, $line, $annotation_number) {
|
| 189 |
global $user;
|
| 190 |
$annotations = _node_annotate_get_annotations($nid, $vid);
|
| 191 |
$annotations = unserialize($annotations);
|
| 192 |
|
| 193 |
if (isset($annotations[$line][$annotation_number])) {
|
| 194 |
if (user_access('edit own annotations') && ($annotations[$line][$annotation_number]['user']['id'] == $user->uid) && ($user->uid != 0)) {
|
| 195 |
drupal_set_title('Edit Annotation: Line '. $line .', No. '. ($annotation_number + 1));
|
| 196 |
return drupal_get_form('node_annotate_add_annotation_form', $nid, $vid, $line, $annotation_number, $annotations[$line]);
|
| 197 |
}
|
| 198 |
else {
|
| 199 |
drupal_access_denied();
|
| 200 |
}
|
| 201 |
}
|
| 202 |
else {
|
| 203 |
drupal_set_title('Add Annotation: Line '. $line);
|
| 204 |
return drupal_get_form('node_annotate_add_annotation_form', $nid, $vid, $line, $annotation_number, $annotations[$line]);
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Form generator function for node_annotate_add_annotation
|
| 210 |
*/
|
| 211 |
function node_annotate_add_annotation_form($nid, $vid, $line, $annotation_number, $linedata) {
|
| 212 |
|
| 213 |
$form['annotation_text'] = array(
|
| 214 |
'#type' => 'textfield',
|
| 215 |
'#title' => t("Annotation for line $line"),
|
| 216 |
'#default_value' => $linedata[$annotation_number]['text'],
|
| 217 |
);
|
| 218 |
$form['submit'] = array(
|
| 219 |
'#type' => 'submit',
|
| 220 |
'#value' => t('Annotate'),
|
| 221 |
);
|
| 222 |
// Pass the nid, vid and line as values so they can be recycled by node_annotate_add_annotation_form_submit
|
| 223 |
$form['nid'] = array(
|
| 224 |
'#type' => 'value',
|
| 225 |
'#value' => $nid,
|
| 226 |
);
|
| 227 |
$form['vid'] = array(
|
| 228 |
'#type' => 'value',
|
| 229 |
'#value' => $vid,
|
| 230 |
);
|
| 231 |
$form['line'] = array(
|
| 232 |
'#type' => 'value',
|
| 233 |
'#value' => $line,
|
| 234 |
);
|
| 235 |
$form['annotation_number'] = array(
|
| 236 |
'#type' => 'value',
|
| 237 |
'#value' => $annotation_number,
|
| 238 |
);
|
| 239 |
return $form;
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Submit function for node_annotate_add_annotation_form
|
| 244 |
*/
|
| 245 |
function node_annotate_add_annotation_form_submit($form_id, $form_values) {
|
| 246 |
global $user;
|
| 247 |
$annotations = _node_annotate_get_annotations($form_values['nid'], $form_values['vid']);
|
| 248 |
$annotations = unserialize($annotations);
|
| 249 |
|
| 250 |
if ($user->uid == 0) {
|
| 251 |
$username = 'Anonymous';
|
| 252 |
}
|
| 253 |
else {
|
| 254 |
$username = $user->name;
|
| 255 |
}
|
| 256 |
|
| 257 |
if (strlen($form_values['annotation_text'])) {
|
| 258 |
$annotations[$form_values['line']][$form_values['annotation_number']] = array(
|
| 259 |
'user' => array(
|
| 260 |
'id' => $user->uid,
|
| 261 |
'name' => $username,
|
| 262 |
),
|
| 263 |
'text' => $form_values['annotation_text'],
|
| 264 |
'date' => time(),
|
| 265 |
);
|
| 266 |
}
|
| 267 |
else {
|
| 268 |
unset($annotations[$form_values['line']][$form_values['annotation_number']]);
|
| 269 |
}
|
| 270 |
|
| 271 |
$annotations = serialize($annotations);
|
| 272 |
|
| 273 |
$sql = 'UPDATE {node_annotate} SET `annotations` = \'%s\' WHERE `vid` = %d AND `nid` = %d';
|
| 274 |
db_query($sql, $annotations, $form_values['vid'], $form_values['nid']);
|
| 275 |
|
| 276 |
$sql = 'DELETE FROM {cache_filter}';
|
| 277 |
db_query($sql);
|
| 278 |
|
| 279 |
drupal_set_message(t('Annotated!'));
|
| 280 |
return 'node/'. $form_values['nid'];
|
| 281 |
}
|
| 282 |
|
| 283 |
/**
|
| 284 |
* **** Internal Functions ****
|
| 285 |
*/
|
| 286 |
|
| 287 |
/**
|
| 288 |
* Get annotations from DB
|
| 289 |
*/
|
| 290 |
function _node_annotate_get_annotations($nid, $vid) {
|
| 291 |
$sql = 'SELECT `annotations` FROM {node_annotate} WHERE `vid` = %d AND `nid` = %d';
|
| 292 |
$result = db_query($sql, $vid, $nid);
|
| 293 |
$data = db_fetch_object($result);
|
| 294 |
$annotations = $data->annotations;
|
| 295 |
return $annotations;
|
| 296 |
}
|
| 297 |
|
| 298 |
/**
|
| 299 |
* Format lines and annotations
|
| 300 |
*/
|
| 301 |
function _node_annotate_format_lines($body, $nid, $vid) {
|
| 302 |
$annotations = _node_annotate_get_annotations($nid, $vid);
|
| 303 |
//$annotations = _node_annotate_explode_annotations($annotations);
|
| 304 |
$annotations = unserialize($annotations);
|
| 305 |
|
| 306 |
$allowed_tags = variable_get("node_annotate_allowed_html_tags", '<strong><em>');
|
| 307 |
$text = stristr($body, '</div>'."\n");
|
| 308 |
$description_end = stripos($body, '</div>'."\n");
|
| 309 |
$description = substr($body, 0, $description_end + 6);
|
| 310 |
|
| 311 |
$return = $description;
|
| 312 |
|
| 313 |
$text = strip_tags($text, $allowed_tags);
|
| 314 |
|
| 315 |
$text = split("[\n\r]+", $text);
|
| 316 |
$return .= '<table class="node_annotate_line_table">'."\n";
|
| 317 |
$index = 0;
|
| 318 |
foreach ($text as $line) {
|
| 319 |
|
| 320 |
if (strlen($line) != 0) {
|
| 321 |
// Increment $index so we start at 1
|
| 322 |
$index++;
|
| 323 |
|
| 324 |
// Is index odd or even?
|
| 325 |
if ($index % 2 == 0) {
|
| 326 |
$class = 'node_annotate_line_row_even';
|
| 327 |
}
|
| 328 |
else {
|
| 329 |
$class = 'node_annotate_line_row_odd';
|
| 330 |
}
|
| 331 |
|
| 332 |
$next_annotation = count($annotations[$index]);
|
| 333 |
|
| 334 |
$link = l($index, "annotate/$nid/$vid/$index/$next_annotation");
|
| 335 |
$line = str_replace(' ', ' ', $line);
|
| 336 |
|
| 337 |
// Throw all this together
|
| 338 |
$return .= "<tr class=\"$class\"><th>$link</th><td colspan=\"4\">$line</td></tr>\n";
|
| 339 |
|
| 340 |
if (isset($annotations[$index])) {
|
| 341 |
foreach ($annotations[$index] as $id => $data) {
|
| 342 |
$data['text'] = check_plain($data['text']);
|
| 343 |
$data['date'] = date('Y-m-d h:i:s', $data['date']);
|
| 344 |
$edit_link = l('Edit', "annotate/$nid/$vid/$index/$id");
|
| 345 |
if ($data['user']['id'] == 0) {
|
| 346 |
$user_link = $data['user']['name'];
|
| 347 |
}
|
| 348 |
else {
|
| 349 |
$user_link = l($data['user']['name'], 'user/'. $data['user']['id']);
|
| 350 |
}
|
| 351 |
$return .= '<tr class="node_annotate_line_annotation"><th><!--BLANK--></th>
|
| 352 |
<td class="node_annotate_line_annotation_user">'. $user_link .'</td>
|
| 353 |
<td class="node_annotate_line_annotation_text">'. $data['text'] .'</td>
|
| 354 |
<td class="node_annotate_line_annotation_date">'. $data['date'] .'</td>
|
| 355 |
<td class="node_annotate_line_annotation_edit">'. $edit_link .'</td></tr>'."\n";
|
| 356 |
}
|
| 357 |
}
|
| 358 |
}
|
| 359 |
}
|
| 360 |
$return .= '</table>';
|
| 361 |
return $return;
|
| 362 |
}
|