uid >0) { $items[] = array('path' => 'guestbook/' . $user->uid, 'title' => t('my guestbook') . $unread, 'access' => user_access('access guestbooks'), 'type' => MENU_DYNAMIC_ITEM, 'callback' => '_guestbook_page'); } $items[] = array('path' => 'guestbook' , 'title' => '', 'access' => user_access('access guestbooks'), 'type' => MENU_HIDDEN_ITEM, 'callback' => '_guestbook_page'); } return $items; } /** * Implementation of hook_user() */ function guestbook_user($type, &$edit, &$user) { switch ($type) { case "view": if (user_access("access guestbooks", $user)) { $title = t("Read %username's guestbook.", array("%username" => $user->name)); $link = l(t("view recent guestbook entries"), "guestbook/$user->uid", array("title" => $title)); return array(t('History') => form_item(t("Guestbook"), $link)); } break; } } /** * Implementation of hook_perm() */ function guestbook_perm() { return array('access guestbooks', 'administer all guestbooks'); } /** * Implementation of hook_help() */ function guestbook_help($section) { switch ($section) { case 'admin/modules#description': return t('Adds individual user guestbooks and a site guestbook.'); } } /** * The guestbook page selector */ function _guestbook_page() { $uid = arg(1); if (is_numeric($uid)) _guestbook_page_user($uid); else _guestbook_list(); } /** * Shows a user or site guestbook */ function _guestbook_page_user($uid, $limit = 15) { global $user; $output = ""; // // Set book owner ($account with name $bookowner) // if ($uid == 0 ) { $bookowner = "" . variable_get('site_name', '') . ""; } else { $account = user_load(array((is_numeric($uid) ? "uid" : "name") => $uid, "status" => 1)); $bookowner= "uid\">$account->name"; } // // Set last visited time for own guestbook // if ($user->uid == $account->uid) { user_save( $user, array('guestbook_visited' => time()) ); } // // Add entry, or delete/update a comment // switch ($_POST["guestbook"]) { case "comment": if (array_key_exists('guestbook_comment', $_POST) && $user->uid == $uid) { $comment = addslashes(strip_tags($_POST["guestbook_comment"])); $entryid = intval($_POST["guestbook_entryid"]); $result = db_query("UPDATE {guestbook} SET comment = '" . $comment . "' WHERE id = $entryid"); } break; case "newentry": $message = $_POST["guestbook_newentry"]; if ( $message != "") { $message = strip_tags($message); // Make sure this isn't a dupe $result = db_query("SELECT message FROM {guestbook} WHERE recipient = %d ORDER BY id DESC LIMIT 1", $uid); $entry = db_fetch_array($result); if ($entry["message"] == $message) break; // Get new id $result = db_query("SELECT MAX(id)+1 AS id FROM {guestbook}"); $entry = db_fetch_array($result); $entryid = $entry["id"]; // Insert new message $anonname = strip_tags($_POST["guestbook_anonname"]); $email = strip_tags($_POST["guestbook_email"]); $website = strip_tags($_POST["guestbook_website"]); if ($user->uid == 0 && $anonname != "") { $result = db_query("INSERT INTO {guestbook} (id,anonname,email,website,author,recipient,message,created) VALUES('%d', '%s', '%s', '%s', '%d', '%d', '%s', '%d')", $entryid, $anonname, $email, $website, $user->uid, $uid, $message, time()); } else if ( $user->uid != $uid) { $result = db_query("INSERT INTO {guestbook} (id,author,email,website,recipient,message,created) VALUES('%d', '%s', '%s', '%d', '%d', '%s', '%d')", $entryid, $email, $website, $user->uid, $uid, $message, time()); } } break; } // // Delete a comment // if ($_GET["guestbook_deleteentry"] != "" && ( user_access("administer all guestbooks") || $uid == $user->uid ) && $user->uid > 0) { $result = db_query("DELETE FROM {guestbook} WHERE id = %d", $_GET["guestbook_deleteentry"]); } // // Output textarea for guestbook entry // if (( $user->uid != $uid && $user->uid > 0 ) || ( $user->uid == 0 && $uid == 0) ) { $output .= "
" . t('Add guestbook entry:') . "
"; if ( $user->uid == 0 ) { $output .= "" . t('Name:') . "
"; } $output .= "E-mail: " . t('this will be not visible for other users') . "
"; $output .= "Website: http://
"; $output .= "" . "" . "

\n"; } else if ( $user->uid == 0 ) { $output .= "
» You must be logged in to post a comment

"; } // // Display guestbook // $result = pager_query("SELECT g.id, g.anonname, g.email, g.website, g.author, g.recipient, g.comment, g.message, g.created, u.name, u.data, u.picture FROM {guestbook} g, {users} u WHERE g.author = u.uid AND g.recipient = '$uid' ORDER BY g.id DESC", $limit, 0, "SELECT COUNT(*) FROM {guestbook} WHERE recipient='$uid'"); while ($entry = db_fetch_array($result)) { if ( $entry["author"] == 0 && $entry["anonname"] != "" ) { $authorname = $entry["anonname"]; } else { if ($entry["picture"]) { $avatarpicture = ""; } else $avatarpicture = ""; $authorname = "" . $avatarpicture . $entry["name"] . ""; } $output .= "
\n
$authorname (" . format_date($entry["created"], "small") . ")\n"; if ( ( user_access("administer all guestbooks") || $entry["recipient"] == $user->uid ) && $user->uid > 0) { $output .= "  " . t('delete entry') . "\n"; } if ( ( user_access("administer all guestbooks") || $entry["recipient"] == $user->uid ) && $user->uid > 0) { $output .= "
\n
" . "" . $entry["email"] . " " . "\n"; } $output .= "
\n
" . "" . $entry["website"] . "" . "\n"; $output .= "
\n
" . $entry["message"] . "
\n"; $output .= "
\n"; if ( $entry["recipient"] == $user->uid && $user->uid >0 ) { if ( $_GET["guestbook_editcomment"] == $entry["id"] ) { $output .= "
" . "" . "" . "
"; } else if ( $entry["comment"] == "" ) { $output .= "" . t('add comment') . ""; } else { $output .= "  " . $entry["comment"] . "  " . "  " . t('edit comment') . ""; } } else if ( $entry["comment"] != "" ) { $output .= "  " . $entry["comment"] . ""; } $output .= "

"; } $output .= theme_pager(NULL, $limit, 0); $breadcrumb[] = l(t("Home"), NULL); $breadcrumb[] = l(t("Guestbooks"), "/guestbook"); $breadcrumb[] = t("%name's guestbook", array("%name" => $bookowner)); print theme("page", $output, t("Guestbook"), $breadcrumb); } /** * List all guestbooks */ function _guestbook_list($limit = 40) { $output = ""; $result = pager_query("SELECT u.uid, u.name, MAX(g.created) as created, COUNT(g.recipient) as num FROM {users} u LEFT OUTER JOIN {guestbook} g ON u.uid = g.recipient WHERE u.uid <> 0 GROUP BY u.uid, u.name, g.recipient ORDER BY created DESC", $limit, 0, "SELECT COUNT(*) FROM users"); $output .= "
» " . t("Site Guestbook") . "
"; $output .= "
" . t("User Guestbooks") . "
"; while ( $entry = db_fetch_array($result) ) { $output .= "» " . $entry["name"] . " (" . t('last update') . ": ". _guestbook_timeleft($entry["created"]) . ", " . _guestbook_entries($entry["num"]) . ")
"; } $output .= theme_pager(NULL,$limit,0); $output .= "
"; $breadcrumb[] = l(t("Home"), NULL); $breadcrumb[] = l(t("Guestbooks"), "/guestbook"); print theme("page", $output, t("Guestbook"), $breadcrumb); } /** * Switches between plural and singular forms of 'entry' */ function _guestbook_entries($num) { if ( $num == 1 ) return "$num " . t('entry'); else return "$num " . t('entries'); } /** * Parse timestamp into human readable form */ function _guestbook_timeleft($timetemp) { if ( $timetemp == 0 ) return t("never"); $minutesleft = floor(( time() - $timetemp ) / 60); if ( $minutesleft == 1 ) $timeleft = "1&bsp;" . t('minute'); if ( $minutesleft < 60 ) $timeleft = $minutesleft . " " . t('minutes'); else if ( $minutesleft >= 60 && $minutesleft < 120 ) $timeleft = "1 " . t('hour'); else if ( $minutesleft < (24*60) ) $timeleft = floor($minutesleft/60) . " " . t('hours'); else if ( $minutesleft >= (24*60) && $minutesleft < (48*60) ) $timeleft = "1 " . t('day'); else $timeleft = floor($minutesleft/(24*60)) . " " . t('days'); return $timeleft; } /** * Fetches number of new entries for current user */ function _guestbook_newentries() { global $user; $userdata = unserialize($user->data); $result = db_query("SELECT created FROM guestbook WHERE recipient = '%s' AND created > '%d'", $user->uid, $userdata["guestbook_visited"]); $count = 0; while (db_fetch_array($result)) $count++; return $count; } ?>