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

Contents of /contributions/modules/usernode_guestbook/usernode_guestbook.module

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


Revision 1.8 - (show annotations) (download) (as text)
Sun Nov 11 16:26:53 2007 UTC (2 years ago) by marcor
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +3 -2 lines
File MIME type: text/x-php
Issue 'usernode guestbook interferes with userpoints' http://drupal.org/node/191255 fixed. We use usernode_save_node() instead of node_save().
1 <?php // usernode_guestbook.module by Marco.Rademacher@web.de
2 // $Id: usernode_guestbook.module,v 1.7 2007/11/09 09:27:14 marcor Exp $
3
4 /**
5 * @file
6 * Usernodes give the opportunity to use comments on a user as a guestbook
7 *
8 * Features:
9 * - Shows guestbook in user-profile (hook_user 'view')
10 * - Shows comment-form in user-profile (hook_user 'view')
11 * - Themes guestbook-entries the same way as other comments (REALLY hard work, haha!)
12 * - Touches usernode if guestbook is watched to unset new status of viewed comments (node_tag_new)
13 * - Guestbook tab additional to user profiles (_usernode_guestbook_tab)
14 * - Admin settings page for default activation and appearence (_usernode_guestbook_admin_settings)
15 * - Jump directly to the guestbook-section (hook_link 'fragment')
16 * - .pot-file
17 * - German informal translation
18 * - User can switch own guestbook functionality like comments (disable, read only, read and write)
19 * even if usernode access is restricted (hook_user 'form', 'submit')
20 *
21 * TODO:
22 * - Notification by e-mail on new entries (subscriptions.module?)
23 * - README file explains install and gives config advice (comments chrono, subscriptions, etc)
24 * - Get rid of doubled formatting of the guestbook entry in preview mode to enable previewing again
25 * - Create an own, themable guestbook form that uses comment form theming by default
26 * - Show only a smaller number of latest guestbook entries on profile page
27 * - Put form at the end of the entries depending on comment order
28 *
29 * TODO in version 6:
30 * - If future hook_link_alter also refers to links on comments,
31 * unset 'reply' Link for usernode guestbook
32 * - Make the usernode_guestbook block an independent jQueryly solution
33 */
34
35
36 /**
37 * Implementation of hook_help()
38 * Display help and module information
39 * @param section which section of the site we're displaying help
40 * @return help text for section
41 */
42 function usernode_guestbook_help($section='') {
43 $output = '';
44 switch ($section) {
45 case 'admin/help#usernode_guestbook':
46 $output .= '<p>' . t('Configure appearence of user @guestbook', _usernode_guestbook_translations()) . '</p>';
47 break;
48 }
49 return $output;
50 }
51
52 /**
53 * Implementation of hook menu.
54 *
55 * We have a settings page and guestbook tab to each user profile, if you want.
56 *
57 * @param bool $may_cache
58 * @return array of menu items
59 */
60 function usernode_guestbook_menu($may_cache) {
61
62 $items = array();
63 if ($may_cache) {
64 // static menu items go in here
65 $items[] = array(
66 'path' => 'admin/settings/usernode_guestbook',
67 'title' => t('Usernode @guestbook', _usernode_guestbook_translations()),
68 'description' => t('Configure the usernode guestbook module.'),
69 'callback' => 'drupal_get_form',
70 'callback arguments' => '_usernode_guestbook_admin_settings',
71 'access' => user_access('administer site configuration'),
72 'type' => MENU_NORMAL_ITEM, // optional
73 );
74 }
75 else {
76 if ((arg(0)=='user') && is_numeric(arg(1)) && variable_get('usernode_guestbook_tab', TRUE)) {
77 $account = user_load(array('uid' => arg(1)));
78 $items[] = array(
79 'path' => 'user/'. arg(1) .'/guestbook',
80 'title' => t('@Guestbook', _usernode_guestbook_translations()),
81 'callback' => '_usernode_guestbook_tab',
82 'callback arguments' => array($account),
83 'access' => user_access('access user profiles'),
84 'type' => MENU_LOCAL_TASK,
85 'weight' => variable_get('usernode_guestbook_tab_weight', 5),
86 );
87 }
88 }
89 return $items;
90 }
91
92
93 /**
94 * Administration settings for the usernode guestbook module
95 *
96 * @return system settings page of $form
97 */
98 function _usernode_guestbook_admin_settings() {
99 if (!user_access('administer comments')) {
100 return drupal_access_denied();
101 }
102
103 $form = array();
104
105 $form['comment_usernode'] = array(
106 '#type' => 'radios',
107 '#title' => t('Guestbook default setting'),
108 '#default_value' => variable_get('comment_usernode', COMMENT_NODE_READ_WRITE),
109 '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
110 '#description' => t('Set the @guestbook default for new users. This is the same setting as on !link.', _usernode_guestbook_translations() + array('!link' => l('administer nodetype usernode page', 'admin/content/types/usernode'))),
111 );
112
113 $form['usernode_guestbook_set_defaults'] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Set all already existing usernodes to default'),
116 '#return_value' => TRUE,
117 '#default_value' => FALSE,
118 '#description' => t('WARNING: Use this option only when setting up this module the first time. If checked, usersettings will be overwritten!'),
119 );
120
121 $form['usernode_guestbook_profile'] = array(
122 '#type' => 'checkbox',
123 '#title' => t('Guestbook overview on user profile'),
124 '#return_value' => TRUE,
125 '#default_value' => variable_get('usernode_guestbook_profile', TRUE),
126 '#description' => t('Check if user profile should contain latest @guestbook entries.', _usernode_guestbook_translations()),
127 );
128
129 $form['usernode_guestbook_tab'] = array(
130 '#type' => 'checkbox',
131 '#title' => t('Guestbook tab in user profiles'),
132 '#return_value' => TRUE,
133 '#default_value' => variable_get('usernode_guestbook_tab', TRUE),
134 '#description' => t('Check if an additional @guestbook tab should be displayed.', _usernode_guestbook_translations()),
135 );
136
137 $form['usernode_guestbook_tab_weight'] = array(
138 '#type' => 'textfield',
139 '#title' => t('Weight of guestbook tab'),
140 '#default_value' => variable_get('usernode_guestbook_tab_weight', 5),
141 '#size' => 2,
142 '#maxlength' => 2,
143 '#description' => t('Enter the weight of guestbook tab to change the order of the tabs.'),
144 );
145
146 return system_settings_form($form);
147 }
148
149 /**
150 * Sets default comment option to existing usernodes, overwrites users' settings.
151 * Runs only if according checkbox is checked.
152 *
153 * TODO: Validate function is even run, if other form's values are invalid.
154 */
155 function _usernode_guestbook_admin_settings_validate($form_id, $form_values) {
156 if($form_values['usernode_guestbook_set_defaults'] === TRUE) {
157 _usernode_guestbook_update_settings($form_values['comment_usernode']);
158 }
159 }
160
161 /**
162 * Implementation of hook_form_alter to change appearence of comment_form
163 * especially for usernodes.
164 *
165 * @param string $form_id
166 * name of the form
167 * @param array $form
168 * editable form array by reference
169 */
170 function usernode_guestbook_form_alter($form_id, &$form) {
171 if ($form_id == 'comment_form') {
172 $node = node_load(array('nid' => $form['nid']['#value']));
173 if ($node->type == 'usernode') {
174 unset($form['subject']);
175 $form['comment_filter']['comment']['#rows'] = 3;
176 $form['comment_filter']['comment']['#title'] = t('Leave a message');
177 /*
178 if (variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) {
179 unset($form['preview']);
180 }
181 else {
182 $op = isset($_POST['op']) ? $_POST['op'] : '';
183 $form['preview']['#value'] = t('Preview');
184 if ($op == t('Preview')) {
185 $form['#after_build'] = array('comment_form_add_preview');
186 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 20);
187 }
188 }
189 */
190 // Previewing is ugly because the form is rendered as a comment
191 // and preview is rendered inside of this again. Always show submit button.
192 unset($form['preview']);
193 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 20);
194 $form['#redirect'] = _usernode_guestbook_redirect($node->uid, 'comments');
195 }
196 }
197 }
198
199
200 /**
201 * Implementation of hook_link
202 *
203 * It's nice to have users post into each others guestbooks instead of talking just inside one.
204 * So we have a link below each guestbook comment to reach a poster's questbook directly.
205 *
206 * @param string $type
207 * Is this a node or a comment? We will only add a link to certain comments.
208 * @param object $comment
209 * @param bool $teaser
210 * @return array
211 */
212 function usernode_guestbook_link($type = 'comment', $comment = NULL, $teaser = FALSE) {
213 $links = array();
214 if ($type == 'comment' && $comment->uid) {
215 $node = node_load(array('nid' => $comment->nid));
216 if($node->type == 'usernode' && $comment->uid != $node->uid) {
217 $author = $comment->name;
218 $links['usernode_guestbook_authorsgb'] = array(
219 'title' => t('reply in @name\'s @guestbook', _usernode_guestbook_translations($author)),
220 'href' => _usernode_guestbook_redirect($comment->uid),
221 'fragment' => 'comments',
222 );
223 }
224 }
225 return $links;
226 }
227
228 /**
229 * Implementation of hook_user().
230 *
231 * Let's have a guestbook section in the user profile.
232 */
233 function usernode_guestbook_user($type, &$edit, &$user, $category = NULL) {
234 switch ($type) {
235 // case 'load': // we don't need the user's guestbook here
236 // case 'register':
237 // case 'insert': // comment module handles guestbook activation
238 // case 'delete': // usernode module handles usernode comment deletion
239 case 'view':
240 $node = usernode_get_node($user);
241 if (variable_get('usernode_guestbook_profile', TRUE)) {
242 $items[t('@Guestbook', _usernode_guestbook_translations())]['entries'] = array(
243 'value' => usernode_guestbook_box($user),
244 );
245 }
246 return $items;
247 case 'form':
248 if ($category == 'account') {
249 // load usernode
250 $node = usernode_get_node($user);
251
252 // offer options to sitch usernode option on / off
253 $form['guestbook_settings'] = array(
254 '#type' => 'fieldset',
255 '#title' => t('@Guestbook settings', _usernode_guestbook_translations()),
256 '#collapsible' => TRUE,
257 '#weight' => 6,
258 );
259 $form['guestbook_settings']['comment'] = array(
260 '#title' => t('Access to your @guestbook', _usernode_guestbook_translations()),
261 '#type' => 'radios',
262 '#default_value' => $node->comment,
263 '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
264 );
265 // Later, these values need to be saved with the usernode, not with the user object!
266 return $form;
267 }
268
269 case 'submit':
270 if (isset($edit['comment'])) {
271 // Save usernode options and delete values from form array
272 // before usernode gets saved
273 $node = usernode_get_node($user);
274 $node->comment = $edit['comment'];
275 // instead of node_save($node) to invoke all hooks properly
276 usernode_save_node($node);
277 unset($edit['comment']);
278 }
279 break;
280 // case 'after_update':
281 // case 'validate':
282 // case 'categories':
283 }
284 }
285
286
287 /**
288 * Render the usernode's comments to be displayed additionally in a page.
289 *
290 * @param object $account
291 * user object of the user whose guestbook is viewed.
292 * @return string of HTML output
293 */
294 function usernode_guestbook_box($account) {
295 $node = usernode_get_node($account);
296 $output = '';
297 switch ($node->comment) {
298 case COMMENT_NODE_READ_WRITE: // 2
299 $output .= comment_form_box(array('nid' => $node->nid), NULL);
300 case COMMENT_NODE_READ_ONLY: // 1
301 $output .= comment_render($node);
302 break;
303 case COMMENT_NODE_DISABLED: // 0
304 default:
305 $output .= t('@name\'s @guestbook is currently deactivated.', _usernode_guestbook_translations(check_plain($account->name)));
306 }
307 // Update the history table, stating that this user viewed the usernode.
308 node_tag_new($node->nid);
309 return $output;
310 }
311
312
313 /**
314 * Render a user tab to display the guestbook only
315 *
316 * @param object $account
317 */
318 function _usernode_guestbook_tab($account) {
319 drupal_set_title(t('@name\'s @guestbook', _usernode_guestbook_translations($account->name)));
320 $output = usernode_guestbook_box($account);
321 return $output;
322 }
323
324
325 /**
326 * Implementation of hook_nodeapi().
327 *
328 * New usernodes' comment options should be initialized with default guestbook option
329 *
330 * @param object $node
331 * @param string $op
332 */
333 function usernode_guestbook_nodeapi(&$node, $op, $arg = 0) {
334
335 if ($op == 'insert' && $node->type == 'usernode') {
336 $node->comment = variable_get('usernode_guestbook', TRUE);
337 }
338 }
339
340
341 /**
342 * Implementation for hook_block
343 *
344 * This block should behave like a shouldbox once.
345 * For now it displays the latest guestbook entries.
346 */
347 function usernode_guestbook_block($op = 'list', $delta = 0) {
348
349 $block = array();
350 if ($op == 'list') {
351 $block[0]['info'] = t('Shoutbox as usernode comments block');
352 return $block;
353 }
354
355 else if ($op == 'view') {
356 switch ($delta) {
357 case 0 :
358 if (module_exists('comment')) {
359 if (arg(0) == 'node' && is_numeric(arg(1))) {
360 $sql = 'SELECT c.uid, c.name, c.nid, c.subject, c.cid, c.timestamp, c.comment, n.title
361 FROM {node} n
362 INNER JOIN {comments} c ON n.nid = c.nid
363 WHERE n.status = 1
364 AND c.status = %d
365 AND n.nid = %d
366 ORDER BY c.timestamp DESC';
367 $result = db_query_range(db_rewrite_sql($sql, 'c'), COMMENT_PUBLISHED, arg(1), 0, 5);
368 }
369 else if (arg(0) == 'user' && is_numeric(arg(1))) {
370 if (module_exists('usernode')) {
371 $sql = 'SELECT c.uid, c.name, c.nid, c.subject, c.cid, c.timestamp, c.comment, n.title
372 FROM {users} u
373 INNER JOIN {usernode} un ON u.uid = un.uid
374 INNER JOIN {node} n ON n.nid = un.nid
375 INNER JOIN {comments} c ON n.nid = c.nid
376 WHERE n.status = 1
377 AND c.status = %d
378 AND u.uid = %d
379 ORDER BY c.timestamp DESC';
380 $result = db_query_range(db_rewrite_sql($sql, 'c'), COMMENT_PUBLISHED, arg(1), 0, 5);
381 }
382 else {
383 $block['content'] = t('Comments on user profiles work with usernodes only. Please install usernode module.');
384 }
385 }
386
387 if (db_affected_rows($result)==0) {
388 $block['content'] = '<p>'.t('No comments available.')."</p>";
389 }
390 else {
391 $users = array();
392 $addon = array();
393 while ($comment = db_fetch_object($result)) {
394 if (strlen($comment->name) > 15) {
395 $comment->name = substr($comment->name,0,14);
396 }
397 $text = $comment->comment;
398 if (strlen($text) > 500) {
399 $text = substr($text,0,499).'...';
400 }
401 if ($comment->uid > 0) {
402 $users[$comment->uid] = user_load(array('uid' => $comment->uid));
403 } else {
404 $users[$comment->uid] = $comment;
405 }
406 $thisaddon = t('%time ago', array('%time' => format_interval(time() - $comment->timestamp, 1)))
407 . ":<br />\n". '<div class="shout" style="margin-top:2px;margin-bottom:2px;">'.$text ."</div>\n";
408 if (array_key_exists($comment->uid, $addon)) {
409 $addon[$comment->uid] .= $thisaddon;
410 }
411 else {
412 $addon[$comment->uid] = $thisaddon;
413 }
414 }
415 $block['content'] = theme('user_list', $users, NULL, $addon, array("addon"));
416 }
417 $block['subject'] = t('Shoutbox');
418 }
419 return $block;
420
421 }//switch
422 }
423 }
424
425
426 /**
427 * Returns an array with useful substitutions in t() functions.
428 * Notice that you can change the name to some other name,
429 * if you change just "translate" the word "guestbook".
430 *
431 * @param string $name
432 * We need a user's name sometimes inside a string.
433 * @return array
434 */
435 function _usernode_guestbook_translations($name = '') {
436 if (empty($name)) {
437 $name = t('anonymous');
438 }
439 return array(
440 '@Guestbook' => t('Guestbook'),
441 '@guestbook' => t('guestbook'),
442 '@name' => $name,
443 );
444 }
445
446
447 /**
448 * Get a link to a user's guestbook
449 *
450 * Redirect to guestbook-tab, if the system is configured to show one.
451 * Otherwise redirect to user profile.
452 *
453 * @param int $uid
454 * uid of the user whose guestbook should be linked
455 * @return string of the link if one argument
456 * array of link, null and fragment to be passed to drupal_goto if fragment given
457 */
458 function _usernode_guestbook_redirect($uid, $fragment=null) {
459 if (variable_get('usernode_guestbook_tab', TRUE)) {
460 $link = 'user/' . $uid . '/guestbook';
461 }
462 else {
463 $link = 'user/' . $uid; //array('user/' . $uid, null, 'comments');
464 }
465 if (is_string($fragment)) {
466 return array($link, null, $fragment);
467 }
468 else {
469 return $link;
470 }
471 }
472
473
474 /**
475 * Sets comment settings of all existing usernodes
476 *
477 * @param int $activation
478 * int representing the comment setting, defauls to Read/Write access
479 * @return bool
480 * TRUE if update was successful, otherwise FALSE
481 */
482 function _usernode_guestbook_update_settings($activation = COMMENT_NODE_READ_WRITE) {
483 $query = "UPDATE {node} n SET comment=%d WHERE TYPE='usernode';";
484 $result = db_query($query, $activation);
485 if ($result) {
486 drupal_set_message(t('All usernode guestbooks have been set to default (@value).',
487 array('@value' => $activation)));
488 return TRUE;
489 }
490 else {
491 drupal_set_message(t('The update of usernode guestbook settings failed.'));
492 return FALSE;
493 }
494 }

  ViewVC Help
Powered by ViewVC 1.1.2