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

Contents of /contributions/modules/shoutbox/shoutbox.module

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


Revision 1.31 - (show annotations) (download) (as text)
Mon May 12 14:00:30 2008 UTC (18 months, 2 weeks ago) by snpower
Branch: MAIN
CVS Tags: HEAD
Changes since 1.30: +1 -1 lines
File MIME type: text/x-php
Fixed the $Id$ lines.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * shoutbox module displays a block for users to create short
7 * messages for thw whole site. Uses AHAH to update the
8 * database and display content.
9 *
10 */
11
12
13 /**
14 * Implementation of hook_help().
15 */
16 function shoutbox_help($path, $arg) {
17 switch ($path) {
18 case 'admin/build/modules#description':
19 return t("This module enables you to display a shoutbox.");
20 }
21 }
22
23 /**
24 * Implementation of hook_menu().
25 */
26 function shoutbox_menu() {
27 $items = array();
28
29 $items['shoutbox/js/view'] = array(
30 'title' => 'View Shouts',
31 'page callback' => 'shoutbox_js_view',
32 'access arguments' => array('access content'),
33 'type' => MENU_CALLBACK,
34 );
35 $items['shoutbox/%shoutbox/edit'] = array(
36 'title' => 'Edit Shout',
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('shoutbox_edit_form', 1),
39 'access arguments' => array('edit own shouts'),
40 'type' => MENU_CALLBACK,
41 );
42 $items['shoutbox/%shoutbox/delete'] = array(
43 'title' => 'Delete Shout',
44 'page callback' => 'theme',
45 'page arguments' => array('shoutbox_delete_form', 1),
46 'access arguments' => array('delete own shouts'),
47 'type' => MENU_CALLBACK,
48 );
49 $items['shoutbox/%/promote'] = array(
50 'title' => 'Promote Shout',
51 'page callback' => 'shoutbox_promote',
52 'page arguments' => array(1),
53 'access arguments' => array('vote on shouts'),
54 'type' => MENU_CALLBACK,
55 );
56 $items['shoutbox/%/demote'] = array(
57 'title' => 'Demote Shout',
58 'page callback' => 'shoutbox_demote',
59 'page arguments' => array(1),
60 'access arguments' => array('vote on shouts'),
61 'type' => MENU_CALLBACK,
62 );
63 $items['admin/build/shoutbox'] = array(
64 'title' => 'Shoutbox',
65 'description' => 'Settings for displaying and deleting shouts',
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('shoutbox_admin_settings'),
68 'access arguments' => array('administer site configuration'),
69 'type' => MENU_NORMAL_ITEM,
70 );
71
72 return $items;
73 }
74
75 /**
76 * Implementation of hook_load()
77 *
78 */
79 function shoutbox_load($shout_id) {
80 $shout = NULL;
81 if (is_numeric($shout_id)) {
82 $shout = db_fetch_object(db_query('SELECT * FROM {shoutbox} WHERE shout_id = '. db_placeholders($shout_id), $shout_id));
83 if ($shout) {
84 _shoutbox_sanitize_shout($shout);
85 }
86 else {
87 $shout = FALSE;
88 }
89 }
90 return $shout;
91 }
92
93 /**
94 * Make the shout box block available. (Standard Drupal hook).
95 *
96 * @param $op
97 * "list" to request list of blocks this module exposes;
98 * any other value to display the stock quotes block.
99 * @param $delta
100 * integer block selector (only recognizes 0 = stock quotes).
101 * @return
102 * (if $op == "list") array containing list of blocks.
103 * (otherwise) HTML fragment for THE block.
104 */
105 function shoutbox_block($op = 'list', $delta = 0, $edit = array()) {
106 switch ($op) {
107 case 'list':
108 $blocks[0]["info"] = t("Shoutbox");
109 return $blocks;
110 break;
111
112 case 'view':
113 $block = array();
114 drupal_add_css(drupal_get_path('module', 'shoutbox') .'/shoutbox.css');
115 switch ($delta) {
116 case 0:
117 if (user_access("access content")) {
118 if (!stristr($_GET['q'], 'shoutbox')) {
119 // Bind submission to submit.
120 drupal_add_js('misc/jquery.form.js');
121 drupal_add_js(drupal_get_path('module', 'shoutbox') .'/shoutbox-form.js', 'module');
122 $block["subject"] = t("Shout Box");
123 $block["content"] = _shoutbox_get_view();
124 }
125 }
126 }
127 return $block;
128 break;
129 default :
130 break;
131 }
132 return;
133 }
134
135 /**
136 * Implementation of hook_cron().
137 */
138 function shoutbox_cron() {
139 $expiration = variable_get('shoutbox_expire', 0);
140 if ($expiration > 0) {
141 db_query('DELETE FROM {shoutbox} WHERE created < %d', time() - 60*60*24*$expiration);
142 }
143 }
144
145
146 /**
147 * Implementation of hook_perm().
148 */
149 function shoutbox_perm() {
150 return array('post shouts', 'administer shoutbox', 'moderate shoutbox',
151 'post shouts without approval', 'delete own shouts',
152 'edit own shouts', 'vote on shouts');
153 }
154
155 // CALLBACKS.
156
157 /**
158 * Javascript callback.
159 * Prints out shouts only.
160 */
161 function shoutbox_js_view() {
162 $show_amount = variable_get('shoutbox_showamount', '20');
163 $shoutbox_posts_data = _shoutbox_display_posts($show_amount);
164 $output = $shoutbox_posts_data['output'];
165 print $output;
166 }
167
168
169
170
171 /**
172 * Function to handle promotion of shouts.
173 *
174 * @param shout_id
175 * The shout id to be promoted.
176 */
177 function shoutbox_promote($shout_id = NULL) {
178 $output = "";
179 if (!is_numeric($shout_id)) {
180 drupal_access_denied();
181 }
182 else {
183 _shoutbox_vote($shout_id, 1);
184 }
185 drupal_goto('');
186 }
187
188
189 /**
190 * Function to handle demotion of shouts.
191 *
192 * @param shout_id
193 * The shout id to be demoted.
194 */
195 function shoutbox_demote($shout_id = NULL) {
196 $output = "";
197 if (!is_numeric($shout_id)) {
198 drupal_access_denied();
199 }
200 else {
201 _shoutbox_vote($shout_id, -1);
202 }
203 drupal_goto('');
204 }
205
206
207 // THEMES.
208
209 /**
210 * Implementation of hook_theme().
211 */
212 function shoutbox_theme() {
213 return array(
214 'shoutbox_links' => array(),
215 'shoutbox_post_forbidden' => array(),
216 'shoutbox_post' => array(
217 'arguments' => array('shout' => NULL, 'links' => array()),
218 ),
219 'shoutbox_page' => array(
220 'arguments' => array('content' => NULL, 'title' => 'Shoutbox'),
221 ),
222 'shoutbox_delete_form' => array(
223 'arguments' => array('shout' => NULL),
224 'file' => 'shoutbox.pages.inc',
225 ),
226 );
227 }
228
229
230 /**
231 * Theme function of shoutbox actions. Actions are edit, delete, promote
232 * and demote. NOTE: Function does not return html but rather an array
233 * with the actions as keys. See code.
234 */
235 function theme_shoutbox_links() {
236 $links['edit']['action'] = 'edit';
237 $links['edit']['title'] = 'Edit Shout';
238 $links['edit']['img'] = base_path() . drupal_get_path('module', 'shoutbox') .'/icon_edit.gif';
239 $links['edit']['img_width'] = 15;
240 $links['edit']['img_height'] = 15;
241 $links['delete']['action'] = 'delete';
242 $links['delete']['title'] = 'Delete Shout';
243 $links['delete']['img'] = base_path() . drupal_get_path('module', 'shoutbox') .'/icon_delete.gif';
244 $links['delete']['img_width'] = 15;
245 $links['delete']['img_height'] = 15;
246 $links['promote']['action'] = 'promote';
247 $links['promote']['title'] = 'Promote';
248 $links['promote']['img'] = base_path() . drupal_get_path('module', 'shoutbox') .'/thumb_up.gif';
249 $links['promote']['img_width'] = 15;
250 $links['promote']['img_height'] = 15;
251 $links['demote']['action'] = 'demote';
252 $links['demote']['title'] = 'Demote';
253 $links['demote']['img'] = base_path() . drupal_get_path('module', 'shoutbox') .'/thumb_down.gif';
254 $links['demote']['img_width'] = 15;
255 $links['demote']['img_height'] = 15;
256
257 return $links;
258 }
259
260 /**
261 * Theme function for shoutbox posts.
262 *
263 * @param shout
264 * The shout to be themed.
265 * @param links
266 * Links of possible actions that can be performed on this shout
267 * by the current user.
268 */
269 function theme_shoutbox_post($shout, $links = array()) {
270 // Get the registered username of the person who posted the shout.
271 if ($shout->uid > 0) {
272 $user = user_load(array("uid" => $shout->uid));
273 $shout->username = $user->name;
274 }
275 else {
276 $shout->username = 'an anonymous user';
277 }
278
279 // BUGBUG strstr returns from http:// till end
280 // we should use that instead of full url.
281 if (strstr($shout->url, "http://")) {
282 $shout->url = '<a href="'. $shout->url .'" target="_blank">'. $shout->nick .'</a>';
283 }
284 else {
285 $shout->url = $shout->nick;
286 }
287
288 if ($links) {
289 foreach ($links as $link) {
290 $linkattributes = $link['linkattributes'];
291 $link_html = '<img src="'. $link['img'] .'" width="'. $link['img_width'] .'" height="'. $link['img_height'] .'" alt="'. $link['title'] .'" class="shoutbox-imglink">';
292 $link_url = 'shoutbox/'. $shout->shout_id . '/' . $link['action'];
293 $img_links = l($link_html, $link_url, array('html' => TRUE)) . $img_links;
294 }
295 }
296
297 $title = 'Posted '. format_date($shout->created, 'custom', 'm/d/y') .' at '. format_date($shout->created, 'custom', 'h:ia') .' by '. $shout->username;
298 $shout_class = (($shout->color) ? ("odd") : ("even"));
299
300 return "<div class=\"shoutbox-$shout_class\" title=\"$title\">$img_links<b>$shout->url</b>: $shout->shout</div>\n";
301 }
302
303 /**
304 * Theme function for displaying the shoutbox page.
305 *
306 * @param $content
307 * The page content.
308 * @param $title
309 * The page title, defaults to 'Shoutbox'.
310 * @return
311 * String containing HTML formatted page.
312 */
313 function theme_shoutbox_page($content, $title = 'Shoutbox') {
314 $output .= $content;
315 return $output;
316 }
317
318 /**
319 * Theme function for displaying the access denied message.
320 *
321 * @return
322 * String containing HTML formatted access denied message.
323 */
324 function theme_shoutbox_post_forbidden() {
325 global $user;
326
327 if ($user->uid) {
328 return '<div class="shoutbox-msg">'. t("Your account does not have the permissions to post shouts") ."</div>\n";
329 }
330 else {
331 return '<div class="shoutbox-msg">'. t('<a href="!login" target="_top">Login</a> or <a href="!register" target="_top">register</a> to post shouts', array('!login' => url('user/login'), '!register' => url('user/register'))) ."</div>\n";
332 }
333 }
334
335
336
337 // FORMS.
338
339 /**
340 * Form for admin/settings/shoutox page.
341 */
342 function shoutbox_admin_settings() {
343 $form['display_settings'] = array(
344 '#type' => 'fieldset',
345 '#title' => t('Display Settings'),
346 '#collapsible' => true
347 );
348 $form['display_settings']['shoutbox_showamount'] = array(
349 '#type' => 'textfield',
350 '#title' => t('Number of posts to show'),
351 '#default_value' => variable_get('shoutbox_showamount', 20),
352 '#size' => 4,
353 '#maxlength' => 4,
354 '#description' => t("Set the number of shoutbox posts to show."),
355 );
356 $form['display_settings']['shoutbox_ascending'] = array(
357 '#type' => 'checkbox',
358 '#title' => t('Post newest shouts on top'),
359 '#default_value' => variable_get('shoutbox_ascending', 0),
360 '#description' => t('When checked, new shouts will appear on the top instead of the bottom.'),
361 );
362 $form['display_settings']['shoutbox_defaultname'] = array(
363 '#type' => 'checkbox',
364 '#title' => t('Default the name field to the logged in user name'),
365 '#default_value' => variable_get('shoutbox_defaultname', 1),
366 '#description' => t('When checked, "Your name/nick" will be replaced by the logged in user name'),
367 );
368 $form['display_settings']['shoutbox_shownamefield'] = array(
369 '#type' => 'checkbox',
370 '#title' => t('Show the name field for logged in users'),
371 '#default_value' => variable_get('shoutbox_shownamefield', 1),
372 '#description' => t('Uncheck to hide the name field for logged in users. Name will then be the user name, so previous option will be useless.'),
373 );
374 $form['display_settings']['shoutbox_showurlfield'] = array(
375 '#type' => 'checkbox',
376 '#title' => t('Show the url field'),
377 '#default_value' => variable_get('shoutbox_showurlfield', 1),
378 '#description' => t('Check to allow users to enter an url.'),
379 );
380 $form['time_settings'] = array(
381 '#type' => 'fieldset',
382 '#title' => t('Time Settings'),
383 '#collapsible' => true,
384 '#collapsed' => false
385 );
386 $form['time_settings']['shoutbox_refresh'] = array(
387 '#type' => 'textfield',
388 '#title' => t('Auto refresh (in seconds)'),
389 '#default_value' => variable_get('shoutbox_refresh', 0),
390 '#size' => 4,
391 '#maxlength' => 4,
392 '#description' => t("Shoutbox can be set to automatically refresh every x number of seconds. Set to 0 to turn off the auto refresh (default)."),
393 );
394 $form['time_settings']['shoutbox_anonymous_timeout'] = array(
395 '#type' => 'textfield',
396 '#title' => t('Number of minutes for which anonymous users may edit or delete their own posts'),
397 '#default_value' => variable_get('shoutbox_anonymous_timeout', 20),
398 '#size' => 4,
399 '#maxlength' => 4,
400 '#description' => t("Anonymous users can edit or delete their post within this amount of time from it being posted, as long as they have the same IP address as when they posted it. If you don't want shout editing and/or deleting, remove these permissions from Drupal's anonymous users role."),
401 );
402 $form['time_settings']['shoutbox_registered_timeout'] = array(
403 '#type' => 'textfield',
404 '#title' => t('Number of minutes for which registered users may edit or delete their own posts'),
405 '#default_value' => variable_get('shoutbox_registered_timeout', 1440),
406 '#size' => 4,
407 '#maxlength' => 4,
408 '#description' => t("Registered users can edit or delete their post within this amount of time from it being posted. If you don't want editing and/or deleting, remove these permissions from Drupal's registered users role."),
409 );
410 $form['time_settings']['shoutbox_expire'] = array(
411 '#type' => 'textfield',
412 '#title' => t('Number of days after which shouts will be purged from the database'),
413 '#default_value' => variable_get('shoutbox_expire', 0),
414 '#size' => 4,
415 '#maxlength' => 4,
416 '#description' => t("Shouts will be permanently deleted after the number of days specified. Shouts will never expire when this is set to zero."),
417 );
418
419 return system_settings_form($form);
420 }
421
422
423 /**
424 * Generates form for adding shouts.
425 */
426 function shoutbox_add_form() {
427 global $user;
428
429 if (isset($_COOKIE['shoutinfo'])) {
430 $info = explode("|", $_COOKIE['shoutinfo']);
431 $last_nick = $info[0];
432 $last_url = $info[1];
433 }
434
435 if (variable_get('shoutbox_defaultname', 0) && $user->uid) {
436 $default_nick = $user->name;
437 }
438 else {
439 $default_nick = t('Your Name/Nick');
440 }
441 $default_msg = t('Enter Message');
442 $default_url = t('Your Website URL');
443
444 $form = '';
445 if (!variable_get('shoutbox_shownamefield', 1) && $user->uid) {
446 $form['nick'] = array(
447 '#type' => 'hidden',
448 '#value' => $user->name,
449 );
450 }
451 else {
452 $form['nick'] = array(
453 '#type' => 'textfield',
454 '#default_value' => ($last_nick) ? $last_nick : $default_nick,
455 '#size' => 15,
456 '#maxlength' => 30,
457 );
458 }
459 $form['message'] = array(
460 '#type' => 'textfield',
461 '#default_value' => $default_msg,
462 '#size' => 15,
463 );
464 if (variable_get('shoutbox_showurlfield', 1)) {
465 $form['url'] = array(
466 '#type' => 'textfield',
467 '#default_value' => ($last_url) ? $last_url : $default_url,
468 '#size' => 15,
469 '#maxlength' => 255,
470 );
471 }
472 $form['#attributes'] = array('name' => 'shoutbox_add');
473 $form['#prefix'] = '<div class="shoutbox-add-form">';
474 $form['#suffix'] = '</div>';
475 $form['ajax'] = array(
476 '#type' => 'hidden',
477 '#default_value' => 0);
478 $form['nextcolor'] = array(
479 '#type' => 'hidden',
480 '#default_value' => $color);
481 $form[] = array('#type' => 'submit', '#value' => t('Shout'));
482
483 return $form;
484 }
485
486 /**
487 * Form for editing shouts.
488 *
489 * @param shout_id
490 * The shout id of the shout being edited.
491 */
492 function shoutbox_edit_form(&$form_state, $shout) {
493
494 if (user_access('administer shouts')) {
495 $form[] = array(
496 '#type' => 'item',
497 '#title' => t('Created'),
498 '#value' => date('m/d/y h:i:sa', $shout->created),
499 );
500 $form[] = array(
501 '#type' => 'item',
502 '#title' => t('Changed'),
503 '#value' => date('m/d/y h:i:sa', $shout->changed),
504 );
505 $form[] = array(
506 '#type' => 'item',
507 '#title' => t('Hostname(s)'),
508 '#value' => $shout->hostname,
509 );
510 $form['status'] = array(
511 '#type' => 'radios',
512 '#title' => t('Status'),
513 '#default_value' => $shout->status,
514 '#options' => array('not published', 'published'),
515 );
516 $users[0] = variable_get('anonymous', 'Anonymous');
517 $result = db_query("SELECT uid, name FROM {users} WHERE name <> '' ORDER BY name");
518 while ($user = db_fetch_object($result)) {
519 $users[$user->uid] = $user->name;
520 }
521 $form['uid'] = array(
522 '#type' => 'select',
523 '#title' => t('Author'),
524 '#default_value' => $shout->uid,
525 '#options' => $users,
526 );
527 }
528 if (_shoutbox_user_access('edit own shouts', $shout)) {
529 if (!variable_get('shoutbox_shownamefield', 1) && $user->uid) {
530 $form['nick'] = array(
531 '#type' => 'hidden',
532 '#value' => $shout->nick,
533 );
534 }
535 else {
536 $form['nick'] = array(
537 '#type' => 'textfield',
538 '#title' => t('Name/Nick'),
539 '#default_value' => $shout->nick,
540 '#size' => 16,
541 '#maxlength' => 55,
542 );
543 }
544 $form['shout'] = array(
545 '#type' => 'textarea',
546 '#title' => t('Shout'),
547 '#default_value' => $shout->shout,
548 '#cols' => 13,
549 '#rows' => 7,
550 );
551 if (variable_get('shoutbox_showurlfield', 1)) {
552 $form['url'] = array(
553 '#type' => 'textfield',
554 '#title' => t('URL'),
555 '#default_value' => $shout->url,
556 '#size' => 16,
557 '#maxlength' => 55,
558 );
559 }
560 $form['shout_id'] = array(
561 '#type' => 'hidden',
562 '#value' => $shout->shout_id,
563 );
564 }
565
566 $form[] = array('#type' => 'submit', '#value' => t('Update'));
567 $form[] = array('#type' => 'submit', '#value' => t('Cancel'));
568
569 return $form;
570 }
571
572 /**
573 * Displays a "Are you sure message ?" with a Yes and Cancel
574 * option.
575 *
576 * @param shout_id
577 * The shout id of the shout being edited.
578 */
579 function shoutbox_delete_form(&$form_state, $shout) {
580 $form['shout_id'] = array(
581 '#type' => 'value',
582 '#value' => $shout->shout_id,
583 );
584 $form = confirm_form($form, t('Are you sure you want to delete this shout?'), '' );
585 return $form;
586 }
587
588
589 // FORM SUBMITS.
590
591 /**
592 * Handles submission of a shout.
593 * Handles both ajax submission and regular form submission.
594 */
595 function shoutbox_add_form_submit($form, $form_state) {
596 global $user;
597
598 // Save the user's nick and url in a cookie for next time (expires in 30 days)
599 setcookie("shoutinfo", "{$form_state['values']['nick']}|{$form_state['values']['url']}", time()+60*60*24*30, '/');
600
601 // Check user's permission and set shout visibility status accordingly.
602 if (user_access('post shouts without approval')) {
603 $status = 1;
604 }
605 else {
606 $status = 0;
607 }
608 $created = time();
609
610 // Add shout to the database.
611 db_query("INSERT INTO {shoutbox} (uid, nick, shout, url, status, created, changed, hostname) VALUES (%d, '%s', '%s', '%s', %d, %d, %d, '%s')", $user->uid, $form_state['values']['nick'], $form_state['values']['message'], $form_state['values']['url'], $status, $created, $created, ip_address());
612
613 // If form was not submitted via javascript
614 // set a display message and redirect the user back to the form.
615 if ($form_state['values']['ajax'] == '0') {
616 drupal_set_message(t('Your shout has been submitted.'));
617 drupal_goto("");
618 }
619
620 // Form was submitted using ajax.
621 else {
622 // Pull shout out of db and display.
623 // We are pulling it out because thats the only way to get th shout_id
624 // which is need for edit, etc.
625 $shout = db_fetch_object(db_query("SELECT * FROM {shoutbox} WHERE nick = '%s' AND shout = '%s' AND created = %d AND hostname = '%s'", $form_state['values']['nick'], $form_state['values']['message'], $created, ip_address()) );
626
627 // Add shout color.
628 $shout->color = $form_state['values']['nextcolor'];
629
630 _shoutbox_sanitize_shout($shout);
631 // Add edit/delete links depending on user's permissions.
632 $shoutlinks = _shoutbox_get_links($shout);
633
634 $ajax_output = theme('shoutbox_post', $shout, $shoutlinks);
635 print $ajax_output;
636
637 // Exit required to stop drupal from redirecting page.
638 exit();
639 }
640 }
641
642 /**
643 * Handle the edit form submission.
644 */
645 function shoutbox_edit_form_submit($form, $form_state) {
646 global $user;
647
648 if ($_POST['op'] == t('Update')) {
649 // Get existing shout object.
650 $result = db_query("SELECT * FROM {shoutbox} WHERE shout_id = %d", $form_state['values']['shout_id']);
651 $existing_shout = db_fetch_object($result);
652
653 // If the user is a shoutbox admin they can edit any shout.
654 if (user_access('administer shoutbox')) {
655 db_query("UPDATE {shoutbox} SET uid=%d, nick='%s', shout='%s', url='%s', status='%s', changed=%d WHERE shout_id=%d", $form_state['values']['uid'], $form_state['values']['nick'], $form_state['values']['shout'], $form_state['values']['url'], $form_state['values']['status'], time(), $form_state['values']['shout_id']);
656 drupal_set_message(t('The shout has been saved.'));
657 }
658
659 // Otherwise they may be able to edit their own shout.
660 else if (_shoutbox_user_access('edit own shouts', $existing_shout)) {
661 db_query("UPDATE {shoutbox} SET nick='%s', shout='%s', url='%s', changed=%d WHERE shout_id=%d", $form_state['values']['nick'], $form_state['values']['shout'], $form_state['values']['url'], time(), $form_state['values']['shout_id']);
662 drupal_set_message(t('Your shout has been saved.'));
663 }
664
665 else {
666 shoutbox_set_message(t('You do not have permission to edit this shout.'));
667 }
668 }
669 drupal_goto('');
670 }
671
672 /**
673 * Handle the delete form submission.
674 */
675 function shoutbox_delete_form_submit($form, &$form_state) {
676
677 if ($form_state['clicked_button']['#value'] == 'Confirm') {
678 if (is_numeric($form_state['values']['shout_id'])) {
679 $result = db_query("SELECT * FROM {shoutbox} WHERE shout_id = %d", $form_state['values']['shout_id']);
680 if ($shout = db_fetch_object($result)) {
681 if (_shoutbox_user_access('delete own shouts', $shout)) {
682 db_query("DELETE FROM {shoutbox} WHERE shout_id =%d", $form_state['values']['shout_id']);
683 drupal_set_message(t('Your shout was deleted.'));
684 }
685 else {
686 drupal_set_message(t('You do not have permission to delete this post.'));
687 }
688 }
689 else {
690 drupal_not_found();
691 }
692 }
693 }
694 $form_state['redirect'] = '';
695 }
696 // FORM VALIDATE.
697
698 /**
699 * Makes sure uses don't submit default values.
700 */
701 function shoutbox_add_form_validate($form, $form_state) {
702 if ( ($form_state['values']['nick'] == t('Your Name/Nick')) || ($form_state['values']['message'] == t('Enter Message')) ) {
703 form_set_error('', t('Default values are not acceptable'));
704 }
705
706 if ( ($form_state['values']['nick'] == '') || ($form_state['values']['message'] == '') ) {
707 form_set_error('', t('You must enter a nick and a message.'));
708 }
709 // URL is optional.
710 if ( ($form_state['values']['url'] == t('Your Website URL')) ) {
711 $form_state['values']['url'] = '';
712 }
713 }
714
715 // INTERNAL FUNCTIONS.
716
717 /**
718 * Returns the themed HTML to be displayed in the block.
719 *
720 * @return
721 * Themed HTML content.
722 */
723 function _shoutbox_get_view() {
724 global $user;
725
726 // Output the existing shoutbox posts.
727 $show_amount = variable_get('shoutbox_showamount', '20');
728 $shoutbox_ascending = variable_get('shoutbox_ascending', FALSE);
729 $shoutbox_posts_data = _shoutbox_display_posts($show_amount);
730 $shoutbox_posts .= $shoutbox_posts_data['output'];
731 $output .= $shoutbox_posts;
732
733
734 // Output the shoutbox form.
735 if (user_access('post shouts') || user_access('post shouts without approval')) {
736 $output .= drupal_get_form('shoutbox_add_form');
737 }
738 else {
739 $output .= theme('shoutbox_post_forbidden');
740 }
741
742 $default_nick = t('Your Name/Nick');
743 $default_msg = t('Enter Message');
744 $default_url = t('Your Website URL');
745
746 // Variable needed by javascript code.
747 $js_settings = array(
748 'showAmount' => $show_amount,
749 // Convert to milliseconds.
750 'refreshDelay' => (1000 * variable_get('shoutbox_refresh', 0)),
751 'ascending' => $shoutbox_ascending,
752 'shownAmount' => $shoutbox_posts_data['count'],
753 'defaultNick' => $default_nick,
754 'defaultMsg' => $default_msg,
755 'defaultUrl' => $default_url,
756 'refreshPath' => base_path() . 'shoutbox/js/view',
757 );
758
759 drupal_add_js(array('shoutbox' => $js_settings), 'setting');
760
761 return theme('shoutbox_page', $output, $title);
762 }
763
764
765 /**
766 * Output existing shoutbox posts as html.
767 * Used by shoutbox_get_view.
768 *
769 * @param $show_amount
770 * The number of posts to show.
771 * @return
772 * HTML for show_amount number of posts.
773 */
774 function _shoutbox_display_posts($show_amount) {
775 global $user;
776
777 $color = 0;
778 $count = 0;
779
780 // Get the shoust from the database.
781 $result = db_query_range("SELECT * FROM {shoutbox} WHERE status=1 ORDER BY created DESC", 0, $show_amount);
782
783 $output = '';
784 while ($shout = db_fetch_object($result)) {
785 _shoutbox_sanitize_shout($shout);
786
787 // Add edit/delete links depending on user's permissions.
788 $shoutlinks = _shoutbox_get_links($shout);
789
790 // Alternate colors for each post (row of the shoutbox).
791 if ($color == 0) {
792 $color = 1;
793 }
794 else {
795 $color = 0;
796 }
797
798 $shout->color = $color;
799 // Figure out if we should display it in ascending or descending order.
800 $ascending = variable_get('shoutbox_ascending', false);
801
802 // Theme the shoutbox post.
803 if ($ascending) {
804 $output .= theme('shoutbox_post', $shout, $shoutlinks);
805 }
806 else {
807 $output = theme('shoutbox_post', $shout, $shoutlinks) . $output;
808 }
809
810 ++$count;
811 }
812
813 if (!$count) {
814 $output .= '<div class="shoutbox-even" title="no shouts">'. t("There are no shouts to view.") ."</div>\n";
815 }
816
817 // Wrap shout box messages.
818 $output = "<div id=\"shoutbox-posts\">\n". $output ."</div>\n";
819 $output_data['count'] = $count;
820 $output_data['output'] = $output;
821
822 return $output_data;
823 }
824
825 /**
826 * Returns an array containing the possible actions for the current user based
827 * on permissions and shout. The actions are edit, delete, moderate.
828 *
829 * @param shout
830 * The shout for which we are testing permissions.
831 * @return
832 * Array of themed actions.
833 */
834 function _shoutbox_get_links($shout) {
835 global $user;
836
837 $links = theme('shoutbox_links'); // Get array of links.
838 if (_shoutbox_user_access('edit own shouts', $shout)) {
839 $shoutlinks[] = $links['edit'];
840 }
841 if (_shoutbox_user_access('delete own shouts', $shout)) {
842 $shoutlinks[] = $links['delete'];
843 }
844 if (_shoutbox_user_access('vote on shouts', $shout)) {
845 $shoutlinks[] = $links['promote'];
846 $shoutlinks[] = $links['demote'];
847 }
848 return $shoutlinks;
849 }
850
851 /**
852 * Handles moderation. Moderation is handled by users demoting/promoting a post.
853 *
854 * @param $shout_id
855 * The id for the shout being moderated.
856 * @param $vote
857 * The number of votes the shout is receiving.
858 */
859 function _shoutbox_vote($shout_id, $vote) {
860 global $user;
861
862 $result = db_query("SELECT * FROM {shoutbox} WHERE shout_id = %d", $shout_id);
863 if ($shout = db_fetch_object($result)) {
864 if (_shoutbox_user_access('vote on shouts', $shout)) {
865 $result = db_query("SELECT * FROM {shoutbox_moderation} WHERE shout_id = %d AND uid = %d", $shout_id, $user->uid);
866 // Make sure the user hasn't already voted for this.
867 if ($moderate = db_fetch_object($result)) {
868 $message = t('You have already voted this shout.');
869 drupal_set_message($message);
870 }
871
872 // Insert moderation vote into the database.
873 else {
874 db_query("INSERT INTO {shoutbox_moderation} (shout_id, uid, vote, timestamp) VALUES (%d, %d, %d, %d)", $shout_id, $user->uid, $vote, time());
875 // Get the sum of all the votes for this shout and take the appropriate
876 // action.
877 $result = db_query("SELECT COUNT(vote) AS count1, SUM(vote) as sum1 FROM {shoutbox_moderation} WHERE shout_id = %d", $shout_id);
878
879 if ($votes = db_fetch_object($result)) {
880 }
881 $message = t('Thank you for voting.');
882 drupal_set_message($message);
883 }
884 }
885 else {
886 $message = t('You cannot vote on this shout.');
887 drupal_set_message($message);
888 }
889 }
890 else {
891 drupal_not_found();
892 }
893 }
894
895
896 /**
897 * This function is necessary because even if a user has permission
898 * (according to the user_access function), they still should not have
899 * some permissions, such as moderating their own posts, etc.
900 *
901 * @param $permission
902 * The user's permissions.
903 * @param $shout
904 * The shout post object.
905 * @return
906 * Returns 1 if user should have accces, 0 otherwise.
907 */
908 function _shoutbox_user_access($permission, $shout) {
909 global $user;
910
911 $user_timeout = FALSE;
912 $user_owned = FALSE;
913
914 if (user_access('administer shoutbox')) {
915 return TRUE;
916 }
917 // If user_access says no, it's definitely no.
918 else if (!user_access($permission)) {
919 return FALSE;
920 }
921
922 else {
923 // A registered user's own post.
924 if (($shout->uid > 0) && ($shout->uid == $user->uid)) {
925 if ($shout->created < time() - 60 * variable_get('shoutbox_registered_timeout', 1440)) {
926 $user_timeout = TRUE;
927 }
928 $user_owned = TRUE;
929
930 }
931
932 // An anonymous user's own post.
933 else if (($shout->uid == 0) && ($shout->hostname == ip_address())) {
934 if ($shout->created < (time() - 60 * variable_get('shoutbox_anonymous_timeout', 20))) {
935 $user_timeout = true;
936 }
937 $user_owned = true;
938 }
939
940 if (($permission == 'edit own shouts') || ($permission == 'delete own shouts')) {
941 // If user owns the post and editing priviledges have not timed out ...
942 if (($user_owned) && (!$user_timeout)) {
943 return 1; // Allow editing and deleting.
944 }
945 }
946 else if ($permission == 'vote on shouts') {
947 // users cannot vote on their own shouts
948 if (!$user_owned) { // if not user owned ...
949 $result = db_query("SELECT * FROM {shoutbox_moderation} WHERE shout_id=%d AND uid=%d", $shout->shout_id, $user->uid);
950 // And if the user has not yet voted.
951 if (!($votecheck = db_fetch_object($result))) {
952 return TRUE; // Allow voting.
953 }
954 }
955 //}
956 }
957 }
958 }
959
960 /**
961 * This function cleans the shout object before it is used.
962 *
963 * @param &$shout
964 * The shout post object.
965 */
966 function _shoutbox_sanitize_shout(&$shout) {
967 // All filtering (including urls, email addresses, censored words, and
968 // emoticons) is handled by the drupal filter system.
969 $shout->nick = check_plain($shout->nick);
970 $shout->shout = check_plain($shout->shout);
971 $shout->url = check_url($shout->url);
972 $shout->color = check_plain($shout->color);
973 }
974
975 function _shoutbox_print_r($object, $exit=TRUE) {
976 if ($exit) {
977 print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
978
979 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
980 <head>
981 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
982 <title>Edit view "internships" | GTVP</title>
983 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
984 <link rel="shortcut icon" href="/misc/favicon.ico" type="image/x-icon" />
985 <link type="text/css" rel="stylesheet" media="all" href="/modules/node/node.css?D" />
986 <link type="text/css" rel="stylesheet" media="all" href="/modules/system/admin.css?D" />
987 <link type="text/css" rel="stylesheet" media="all" href="/modules/system/defaults.css?D" />
988 <link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.css?D" />
989
990 <link type="text/css" rel="stylesheet" media="all" href="/modules/system/system-menus.css?D" />
991 <link type="text/css" rel="stylesheet" media="all" href="/modules/user/user.css?D" />
992 <script type="text/javascript" src="/misc/jquery.js?D"></script>
993 <script type="text/javascript" src="/misc/drupal.js?D"></script>
994 <script type="text/javascript" src="/misc/jquery.form.js?D"></script>
995 <script type="text/javascript" src="/misc/collapse.js?D"></script>
996 <script type="text/javascript" src="/misc/textarea.js?D"></script>
997 <script type="text/javascript" src="/misc/tabledrag.js?D"></script>
998
999 </head>
1000 <body class="adminpage">
1001 ');
1002 }
1003 print("<pre>");
1004 print_r($object);
1005 print("</pre>");
1006 if ($exit) {
1007 print('</body></html>');
1008 exit();
1009 }
1010 }

  ViewVC Help
Powered by ViewVC 1.1.2