/[drupal]/contributions/modules/notepad/notepad.module
ViewVC logotype

Contents of /contributions/modules/notepad/notepad.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sun Jun 10 09:16:44 2007 UTC (2 years, 5 months ago) by jondoesdrupal
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, DRUPAL-5--1-2, HEAD
Changes since 1.3: +2 -2 lines
File MIME type: text/x-php
http://drupal.org/node/150231 - fixed missing curly brackets around a table name causing issues with table prefixes.
1 <?php
2 // $Id: notepad.module,v 1.3 2007/04/28 15:30:10 jondoesdrupal Exp $
3
4 /**
5 * notepad.module
6 */
7
8 /*****************************************************************************************
9 * Drupal Hooks
10 ****************************************************************************************/
11
12 /**
13 * Implementation of hook_help().
14 */
15 function notepad_help($section) {
16 switch ($section) {
17 case "node/add#notepad":
18 return t("Create a new note");
19 break;
20 }
21 }
22
23 /**
24 * Implementation of hook_perm().
25 * Valid permissions for this module
26 * @return array An array of valid permissions for the onthisdate module
27 */
28 function notepad_perm() {
29 return array('can use notepad');
30 }
31
32 /**
33 * Implementation of hook_menu().
34 */
35 function notepad_menu($may_cache) {
36 $items = array();
37 if ($may_cache) {
38 $items[] = array(
39 'path' => 'notepad',
40 'title' => t('My Notes'),
41 'callback' => 'notepad_summary',
42 'callback arguments' => array('user'),
43 'access' => user_access('can use notepad'),
44 'type' => MENU_NORMAL_ITEM,
45 'weight' => 0,
46 );
47 $items[] = array(
48 'path' => 'notepad/list',
49 'title' => t('My Notes'),
50 'access' => user_access('can use notepad'),
51 'type' => MENU_DEFAULT_LOCAL_TASK,
52 );
53 $items[] = array(
54 'path' => 'notepad/list/user',
55 'title' => t('My Personal Notes'),
56 'callback' => 'notepad_summary',
57 'callback arguments' => array('user'),
58 'access' => user_access('can use notepad'),
59 'type' => MENU_LOCAL_TASK,
60 'weight' => 1,
61 );
62 $items[] = array(
63 'path' => 'notepad/list/content',
64 'title' => t('Content Notes'),
65 'callback' => 'notepad_summary',
66 'callback arguments' => array('content'),
67 'access' => user_access('can use notepad'),
68 'type' => MENU_LOCAL_TASK,
69 'weight' => 2,
70 );
71 $items[] = array(
72 'path' => 'notepad/add',
73 'title' => t('Add'),
74 'callback' => 'notepad_summary',
75 'callback arguments' => array('add'),
76 'access' => user_access('can use notepad'),
77 'type' => MENU_LOCAL_TASK,
78 'weight' => 2,
79 );
80 $items[] = array(
81 'path' => 'admin/settings/notepad',
82 'title' => t('Notepad'),
83 'description' => t('Choose the node types that can have notes.'),
84 'callback' => 'drupal_get_form',
85 'callback arguments' => array('notepad_settings'),
86 'access' => user_access('administer content types'),
87 'type' => MENU_NORMAL_ITEM,
88 );
89 }
90 else {
91 if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('can use notepad')) {
92 $nid = arg(1);
93 $sql = 'SELECT type FROM {node} WHERE nid=%d';
94 $type = db_result(db_query($sql, $nid));
95 $node_types = variable_get('notepad_node_types', array());
96 if (isset($node_types[$type])) {
97 $items[] = array(
98 'path' => 'node/'. arg(1) .'/notepad',
99 'title' => t('Notepad'),
100 'callback' => 'notepad_node',
101 'callback arguments' => array($nid),
102 'access' => user_access('can use notepad'),
103 'type' => MENU_LOCAL_TASK,
104 'weight' => 2
105 );
106 $items[] = array(
107 'path' => 'node/'. arg(1) .'/notepad/list',
108 'title' => t('List'),
109 'access' => user_access('can use notepad'),
110 'type' => MENU_DEFAULT_LOCAL_TASK,
111 );
112 $items[] = array(
113 'path' => 'node/'. arg(1) .'/notepad/add',
114 'title' => t('Add'),
115 'callback' => 'notepad_node',
116 'callback arguments' => array($nid, 'add'),
117 'access' => user_access('can use notepad'),
118 'type' => MENU_LOCAL_TASK,
119 'weight' => 2
120 );
121 }
122 }
123 }
124 return $items;
125 }
126
127 /**
128 * Implementation of hook_block().
129 *
130 * Generate HTML for the products link block
131 * @param op the operation from the URL
132 * @param delta offset
133 * @returns block HTML
134 */
135 function notepad_block($op = 'list', $delta = 0, $edit = array()) {
136 global $user;
137
138 switch ($op) {
139 case 'list':
140 // listing of blocks, such as on the admin/block page
141 $block[0]["info"] = t('Notepad');
142 return $block;
143 break;
144
145 case 'view':
146 if ($delta == 0) {
147 // Generate block info - only show if have perms to use module
148 if (user_access('can use notepad')) {
149 $result = db_query("SELECT n.nid, n.title, r.body, m.priority FROM {node} n LEFT JOIN {node_revisions} r ON n.vid = r.vid LEFT JOIN {notepad_node_metadata} m ON n.vid = m.vid WHERE n.type = 'notepad' AND n.uid = %d ORDER BY m.priority ASC LIMIT %d", $user->uid, variable_get('number_of_notes_to_show', 10));
150 if (db_num_rows($result) > 0) {
151 $rows = array();
152 while ($row = db_fetch_object($result)) {
153 $rows[] = $row;
154 }
155 }
156
157 // Now define the subject and pass the rows through the themeable helper function
158 $block['subject'] = t('My Notes');
159 $block['content'] = theme('notepad_block_content', $rows);
160 return $block;
161 }
162 }
163 break;
164
165 case 'configure':
166 if ($delta == 0) {
167 $form['number_of_notes_to_show'] = array(
168 '#type' => 'textfield',
169 '#title' => t('Number of notes to show'),
170 '#size' => 5,
171 '#description' => t('The number of results showm in the note block.'),
172 '#default_value' => variable_get('number_of_notes_to_show', 10)
173 );
174 return $form;
175 }
176 break;
177
178 case 'save':
179 if ($delta == 0) {
180 variable_set('number_of_notes_to_show', $edit['number_of_notes_to_show']);
181 }
182 break;
183 }
184 }
185
186 /**
187 * Implementation of hook_node_info().
188 */
189 function notepad_node_info() {
190 return array(
191 'notepad' => array(
192 'name' => t('Note'),
193 'module' => 'notepad',
194 'body_label' => t('Note'),
195 'description' => t("A node for recording simple notes."),
196 )
197 );
198 }
199
200 /**
201 * Implementation of hook_form().
202 */
203 function notepad_form(&$node) {
204 // Title
205 $form['title'] = array(
206 '#type' => 'textfield',
207 '#title' => t('Title'),
208 '#required' => TRUE,
209 '#default_value' => $node->title,
210 '#weight' => -5,
211 '#description' => t('The title of this note.')
212 );
213
214 // full description / body
215 $form['body_filter']['body'] = array(
216 '#type' => 'textarea',
217 '#title' => t('Note body'),
218 '#default_value' => $node->body,
219 '#rows' => 10,
220 '#required' => FALSE
221 );
222
223 $form['body_filter']['format'] = filter_form($node->format);
224
225 // Priority drop down box
226 $options = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
227 $form['priority'] = array(
228 '#type' => 'select',
229 '#title' => t('Priority'),
230 '#default_value' => $node->priority,
231 '#options' => $options,
232 '#multiple' => FALSE,
233 '#description' => t('The priority of the note (0 high, 9 low) - mainly to govern the order in which they are listed.'),
234 );
235
236 $related_nid = arg(3);
237 $related_nid = (is_numeric($related_nid) ? $related_nid : 0);
238 $form['related_nid'] = array('#type' => 'hidden', '#value' => $related_nid);
239
240 return $form;
241 }
242
243 /**
244 * Implementation of hook_nodeapi().
245 *
246 * "delete": The node is being deleted.
247 * "delete revision": The revision of the node is deleted. You can delete data associated with that revision.
248 * "insert": The node is being created (inserted in the database).
249 * "load": The node is about to be loaded from the database. This hook can be used to load additional data at this time.
250 * "prepare": The node is about to be shown on the add/edit form.
251 * "search result": The node is displayed as a search result. If you want to display extra information with the result, return it.
252 * "print": Prepare a node view for printing. Used for printer-friendly view in book_module
253 * "update": The node is being updated.
254 * "submit": The node passed validation and will soon be saved. Modules may use this to make changes to the node before it is saved to the database.
255 * "update index": The node is being indexed. If you want additional information to be indexed which is not already visible through nodeapi "view", then you should return it here.
256 * "validate": The user has just finished editing the node and is trying to preview or submit it. This hook can be used to check the node data. Errors should be set with form_set_error().
257 * "view": The node content is being assembled before rendering. The module may add elements $node->content prior to rendering. This hook will be called after hook_view(). The format of 4node->content is the same as used by Forms API.
258 * "alter": the $node->content array has been rendered, so the node body or teaser is filtered and now contains HTML. This op should only be used when text substitution, filtering, or other raw text operations are necessary.
259 * "rss item": An RSS feed is generated. The module can return properties to be added to the RSS item generated for this node. See comment_nodeapi() and upload_nodeapi() for examples. The $node passed can also be modified to add or remove contents to the feed item.
260 *
261 */
262 function notepad_nodeapi(&$node, $op, $teaser, $page) {
263 switch ($op) {
264 case 'delete revision':
265 // Notice that we're matching a single revision based on the node's vid.
266 db_query("DELETE FROM {notepad_node_metadata} WHERE vid = %d", $node->vid);
267 break;
268 }
269 }
270
271 /**
272 * Implementation of hook_load().
273 */
274 function notepad_load($node) {
275 // Look up the node metadata
276 $additions = db_fetch_object(db_query("SELECT priority FROM {notepad_node_metadata} WHERE vid = %d", $node->vid));
277 return $additions;
278 }
279
280 /**
281 * Implementation of hook_view().
282 */
283 function notepad_view($node, $teaser = FALSE, $page = FALSE) {
284 $node = node_prepare($node, $teaser);
285 return $node;
286 }
287
288 /**
289 * Implementation of hook_validate().
290 */
291 function notepad_validate(&$node) {
292 // Just return true for now
293 return TRUE;
294 }
295
296 /**
297 * Implementation of hook_insert().
298 */
299 function notepad_insert($node) {
300 $personal = ($node->related_nid ? 0 : 1);
301 db_query("INSERT INTO {notepad_node_metadata} (vid, nid, priority, personal) VALUES (%d, %d, %d, %d)", $node->vid, $node->nid, $node->priority, $personal);
302 if ($node->related_nid) {
303 global $user;
304
305 $related_node = node_load($node->related_nid);
306 db_query("INSERT INTO {notepad_nodes} (nid, node_id, node_type, uid) VALUES (%d, %d, '%s', %d)", $node->related_nid, $node->nid, $related_node->type, $user->uid);
307 }
308 }
309
310 /**
311 * Implementation of hook_update().
312 */
313 function notepad_update($node) {
314 // if this is a new node or we're adding a new revision,
315 if ($node->revision) {
316 notepad_insert($node);
317 }
318 else {
319 db_query("UPDATE {notepad_node_metadata} SET priority = %d WHERE vid = %d", $node->priority, $node->vid);
320 }
321 }
322
323 /**
324 * Implementation of hook_delete().
325 */
326 function notepad_delete($node) {
327 db_query("DELETE FROM {notepad_node_metadata} WHERE nid = %d", $node->nid);
328 db_query("DELETE FROM {notepad_nodes} WHERE nid = %d OR node_id = %d", $node->nid, $node->nid);
329 }
330
331 /**
332 * Implementation of hook_access().
333 */
334 function notepad_access($op, $node) {
335 global $user;
336
337 switch ($op) {
338 case 'view':
339 // Only owner can view a note
340 if ($user->uid == $node->uid) {
341 return TRUE;
342 }
343 break;
344
345 case 'create':
346 // Can only create notes if have perm
347 return user_access('can use notepad');
348 break;
349
350 case 'update':
351 // Can only update a note if you're the owner
352 if ($user->uid == $node->uid) {
353 return TRUE;
354 }
355 break;
356
357 case 'delete':
358 // Can only delete a note if you're the owner
359 if ($user->uid == $node->uid) {
360 return TRUE;
361 }
362 break;
363 }
364
365 return FALSE;
366 }
367
368 /**
369 * Implementation of hook_link().
370 */
371 function notepad_link($type, $node = NULL, $teaser = FALSE) {
372 $links = array();
373 if (!is_null($node)) {
374 $notepad_node_types = variable_get('notepad_node_types', array());
375 if (isset($notepad_node_types[$node->type]) && user_access('can use notepad')) {
376 $node_types = node_get_types('names');
377 $links['notepad'] = array(
378 'title' => t('Add Note'),
379 'href' => 'node/' . $node->nid . '/notepad/add',
380 'attributes' => array('title' => t('Add a new note to this '. $node_types[$node->type])),
381 );
382 }
383 }
384 return $links;
385 }
386
387 /*****************************************************************************************
388 * Menu Callbacks
389 ****************************************************************************************/
390
391 /**
392 *
393 */
394 function notepad_summary($op = 'user') {
395 global $user;
396
397 if ($op == 'add') {
398 drupal_goto('node/add/notepad');
399 }
400
401 switch ($op) {
402 case 'user':
403 // Headers
404 $header = array(
405 t('Priority'),
406 );
407
408 $sql = "SELECT n.nid, n.title, n.created, n.changed, m.priority FROM {node} n INNER JOIN {notepad_node_metadata} m ON n.vid=m.vid WHERE n.uid = %d AND m.personal = 1 ORDER BY m.priority, n.changed DESC";
409 break;
410
411 case 'content':
412 drupal_set_title(t('My Content Notes'));
413
414 // Headers
415 $header = array(
416 t('Type'),
417 t('Notes'),
418 );
419
420 $sql = "SELECT COUNT(*) as note_count, n.nid, n.title, n.created, n.changed, n.uid, s.node_type FROM {node} n INNER JOIN {notepad_nodes} s ON n.nid=s.nid WHERE s.uid = %d GROUP BY n.nid ORDER BY n.changed DESC";
421 break;
422 }
423
424 $header[] = t('Title');
425 $header[] = t('Created');
426 $header[] = t('Last Updated');
427
428 $rows = array();
429 $result = db_query($sql, $user->uid);
430 while ($row = db_fetch_object($result)) {
431 $data = array();
432 if ($op == 'content') {
433 $data[] = $row->node_type;
434 $data[] = $row->note_count;
435 $path = 'node/' . $row->nid .'/notepad';
436 }
437 else {
438 $data[] = $row->priority;
439 $path = 'node/' . $row->nid;
440 }
441
442 $data[] = array('data' => l(check_plain($row->title), $path), 'title' => check_plain($row->title), 'align' => 'left');
443 $data[] = format_date($row->created, 'small');
444 $data[] = t('!time ago', array('!time' => format_interval(time() - $row->changed)));
445
446 $rows[] = $data;
447 }
448
449 if (!$rows) {
450 $rows [] = array(array('data' => t('Sorry, no notes'), 'align' => 'center', 'colspan' => '4'));
451 }
452
453 // build table output
454 $output = theme('table', $header, $rows, array('width' => '98%'));
455
456 // Show the page
457 print theme("page", $output);
458 }
459
460 /**
461 *
462 */
463 function notepad_node($nid, $op = '') {
464 global $user;
465
466 if ($op == 'add') {
467 drupal_goto('node/add/notepad/'. $nid, 'destination=node/'. $nid .'/notepad');
468 }
469
470 // get all node types for display
471 $node_types = node_get_types('names');
472
473 // Set node title
474 $node = node_load($nid);
475 drupal_set_title($node->title);
476
477 // Headers
478 $header = array(
479 t('Priority'),
480 t('Title'),
481 t('Created'),
482 t('Last Updated'),
483 );
484
485 // Now a table of the results
486 $rows = array();
487 $result = db_query("SELECT n.nid, n.title, n.created, n.changed, s.node_type, m.priority FROM {node} n INNER JOIN {notepad_nodes} s ON n.nid=s.node_id INNER JOIN {notepad_node_metadata} m ON m.nid=s.node_id WHERE s.nid = %d AND n.uid = %d ORDER BY m.priority, n.created DESC", $nid, $user->uid);
488 while ($row = db_fetch_object($result)) {
489 $rows [] = array(
490 $row->priority,
491 array('data' => l(check_plain($row->title),'node/' . $row->nid), 'title' => check_plain($row->title), 'align' => 'left'),
492 format_date($row->created, 'small'),
493 t('!time ago', array('!time' => format_interval(time() - $row->changed))),
494 );
495 }
496
497 if (!$rows) {
498 $rows [] = array(array('data' => t('Sorry, no notes'), 'align' => 'center', 'colspan' => '4'));
499 }
500
501 // build table output
502 $output = theme('table', $header, $rows, array('width' => '98%'));
503
504 // Show the page
505 print theme("page", $output);
506 }
507
508 /**
509 *
510 */
511 function notepad_display_node($node) {
512 // Set the breadcrumb
513 drupal_set_breadcrumb(array(l(t('Home'), ''), l(t('your notes'), 'notepad')));
514
515 // Footer - priority info & actions link
516 $output = '<br /><p><strong>' . t('Priority') . ':</strong> ' . $node->priority . '<br /><strong>' . t('Actions') . ':</strong> ' . l(t('Add New Note'), 'node/add/notepad') . '</p>';
517
518 return $output;
519 }
520
521 /**
522 *
523 */
524 function notepad_settings() {
525 $output = '';
526
527 $node_types = node_get_types('names');
528 unset($node_types['notepad']);
529 $size = (count($node_types) <= 4 ? count($node_types) : 4);
530 $form['notepad_node_types'] = array(
531 '#type' => 'select',
532 '#title' => t('Node types'),
533 '#default_value' => variable_get('notepad_node_types', array()),
534 '#options' => $node_types,
535 '#multiple' => TRUE,
536 '#size' => $size,
537 '#description' => t("Select the node types that can have notes.")
538 );
539
540 return system_settings_form($form);
541 }
542
543 /*****************************************************************************************
544 * form functions
545 ****************************************************************************************/
546
547
548
549 /*****************************************************************************************
550 * theme functions
551 ****************************************************************************************/
552
553 function theme_notepad_block_content($rows) {
554 $items = array();
555 if (count($rows)) {
556
557 // Add the links to the header
558 $links[] = array('title' => t('My Notes'), 'href' => 'notepad');
559 $links[] = array('title' => t('Add New Note'), 'href' => 'node/add/notepad');
560 $output = theme('links', $links, array('class' => 'links inline'));
561
562 foreach ($rows as $row) {
563 $items[] = $row->priority . ' : ' . l($row->title, 'node/'. $row->nid, array('title' => check_plain($row->body)));
564 }
565 // Now render the list using theme_item_list
566 $output .= theme('item_list', $items, NULL, 'ul');
567 }
568 return $output;
569 }
570
571 /*****************************************************************************************
572 ****************************************************************************************/
573

  ViewVC Help
Powered by ViewVC 1.1.2