View source
<?php
define('GUESTBOOK_SITE_GUESTBOOK', 0x1);
define('GUESTBOOK_USER_GUESTBOOKS', 0x2);
define('GUESTBOOK_PAGER_ABOVE', 0x1);
define('GUESTBOOK_PAGER_BELOW', 0x2);
define('GUESTBOOK_TEXTAREA_ROWS', 8);
function guestbook_menu() {
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
$items['admin/config/content/guestbook'] = array(
'title' => 'Guestbook',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'guestbook_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
$items['guestbooks'] = array(
'title' => 'Guestbooks',
'page callback' => 'guestbook_list',
'access arguments' => array(
'access user guestbooks',
),
);
$items['user/%user/guestbook'] = array(
'title' => 'User guestbook',
'title callback' => '_guestbook_info',
'title arguments' => array(
1,
'title',
),
'page callback' => 'guestbook_page',
'page arguments' => array(
1,
),
'access callback' => 'guestbook_menu_access_user_guestbook',
'access arguments' => array(
1,
'access user guestbooks',
),
'type' => MENU_LOCAL_TASK,
);
$items['user/%user/guestbook/sign'] = array(
'title' => 'Add guestbook entry',
'page callback' => 'guestbook_page_form',
'page arguments' => array(
1,
),
'access callback' => 'guestbook_menu_access_user_guestbook',
'access arguments' => array(
1,
'post in user guestbooks',
),
'type' => MENU_CALLBACK,
);
}
if ($guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
$items['guestbook'] = array(
'title' => variable_get('guestbook_site_title', t('Site guestbook')),
'page callback' => 'guestbook_page',
'page arguments' => array(
0,
),
'access arguments' => array(
'access site guestbook',
),
);
$items['guestbook/sign'] = array(
'title' => 'Add guestbook entry',
'page callback' => 'guestbook_page_form',
'page arguments' => array(
'0',
),
'access arguments' => array(
'post in site guestbook',
),
'type' => MENU_CALLBACK,
);
}
return $items;
}
function guestbook_menu_access_user_guestbook($account, $permission) {
global $user;
if (empty($account->guestbook_status)) {
if ($user->uid == $account->uid || user_access($permission)) {
return TRUE;
}
}
return FALSE;
}
function guestbook_user_view($account, $view_mode, $langcode) {
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
if (user_access('access user guestbooks') && empty($account->data['guestbook_status'])) {
$title = t("Read @username's guestbook.", array(
'@username' => $account->name,
));
$link = l(t('View recent guestbook entries'), "user/{$account->uid}/guestbook", array(
'attributes' => array(
'title' => $title,
),
));
$account->content['summary']['guestbook'] = array(
'#type' => 'user_profile_item',
'#title' => t('Guestbook'),
'#markup' => $link,
'#attributes' => array(
'class' => 'guestbook',
),
);
}
}
}
function guestbook_form_user_profile_form_alter(&$form, &$form_state) {
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS && $form['#user_category'] == 'account') {
$settings = $form_state['user']->data;
$form['guestbook'] = array(
'#type' => 'fieldset',
'#title' => t('User guestbook'),
);
$form['guestbook']['guestbook_status'] = array(
'#type' => 'radios',
'#title' => t('Status'),
'#default_value' => isset($settings['guestbook_status']) ? $settings['guestbook_status'] : 0,
'#options' => array(
t('Enabled'),
t('Disabled'),
),
);
$form['guestbook']['guestbook_send_email'] = array(
'#type' => 'checkbox',
'#title' => t('Send email notification'),
'#description' => t("Uncheck if you don't wish to be notified of new entries to your guestbook."),
'#default_value' => isset($settings['guestbook_send_email']) ? $settings['guestbook_send_email'] : 0,
);
$form['guestbook']['guestbook_intro'] = array(
'#type' => 'textarea',
'#title' => t('Intro text'),
'#default_value' => isset($settings['guestbook_intro']) ? $settings['guestbook_intro'] : '',
'#cols' => 70,
'#rows' => GUESTBOOK_TEXTAREA_ROWS,
'#description' => t('The text that appears on top of your guestbook.'),
);
}
}
function guestbook_user_presave(&$edit, $account, $category) {
foreach (array(
'guestbook_status',
'guestbook_send_email',
'guestbook_intro',
) as $key) {
if (isset($edit[$key])) {
$edit['data'][$key] = $edit[$key];
}
}
}
function guestbook_user_cancel($account, $method) {
switch ($method) {
case 'user_cancel_reassign':
db_update('guestbook')
->fields(array(
'author' => 0,
))
->condition('author', $account->uid)
->execute();
db_update('guestbook')
->fields(array(
'commentauthor' => 0,
))
->condition('commentauthor', $account->uid)
->execute();
break;
}
}
function guestbook_user_delete($account) {
db_delete('guestbook')
->condition('recipient', $account->uid)
->execute();
}
function guestbook_permission() {
return array(
'moderate all guestbooks' => array(
'title' => t('Moderate all guestbooks'),
),
'moderate own guestbook' => array(
'title' => t('Moderate own guestbook'),
),
'access site guestbook' => array(
'title' => t('View site guestbook'),
),
'access user guestbooks' => array(
'title' => t('View user guestbooks'),
),
'post in site guestbook' => array(
'title' => t('Post in site guestbook'),
),
'post in user guestbooks' => array(
'title' => t('Post in user guestbooks'),
),
);
}
function guestbook_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t('Adds a site guestbook and individual user guestbooks.');
}
}
function guestbook_admin_settings() {
$form['guestbook_mode'] = array(
'#type' => 'radios',
'#title' => t('Mode'),
'#default_value' => variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS),
'#options' => array(
GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS => t('Site and user guestbooks'),
GUESTBOOK_SITE_GUESTBOOK => t('Site guestbook only'),
GUESTBOOK_USER_GUESTBOOKS => t('User guestbooks only'),
),
);
$form['site_guestbook'] = array(
'#type' => 'fieldset',
'#title' => t('Site guestbook'),
);
$form['site_guestbook']['guestbook_site_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => variable_get('guestbook_site_title', 'Site guestbook'),
'#size' => 30,
'#maxlength' => 128,
'#description' => t("The site guestbook's page title."),
);
$form['site_guestbook']['guestbook_site_intro'] = array(
'#type' => 'textarea',
'#title' => t('Intro text'),
'#default_value' => variable_get('guestbook_site_intro', ''),
'#cols' => 70,
'#rows' => GUESTBOOK_TEXTAREA_ROWS,
'#description' => t('The text that appears on top of the site guestbook.'),
);
$form['site_guestbook']['guestbook_send_email'] = array(
'#type' => 'textfield',
'#title' => t('Send an notification to the following e-mail address about new guestbook entries'),
'#description' => t("Leave blank if you don't wish to be notified"),
'#size' => 30,
'#maxlength' => 128,
'#default_value' => variable_get('guestbook_send_email', ''),
);
$form['user_guestbooks'] = array(
'#type' => 'fieldset',
'#title' => t('User guestbooks'),
'#description' => t('Users can individually disable their guestbook or add an intro text on the user account page.'),
);
$form['user_guestbooks']['guestbook_user_link_to'] = array(
'#type' => 'radios',
'#title' => t('User link to profile or guestbook'),
'#description' => t('When displaying a user should the link show the user profile or the user guestbook?'),
'#options' => array(
'profile' => t('User profile'),
'guestbook' => t('User guestbook'),
),
'#default_value' => variable_get('guestbook_user_link_to', 'profile'),
);
$form['display_options'] = array(
'#type' => 'fieldset',
'#title' => t('Display options'),
);
$form['display_options']['guestbook_entries_per_page'] = array(
'#type' => 'textfield',
'#title' => t('Entries per page'),
'#default_value' => variable_get('guestbook_entries_per_page', 20),
'#size' => 3,
'#maxlength' => 3,
'#description' => t('The number of guestbook entries per page.'),
);
$form['display_options']['guestbook_display'] = array(
'#type' => 'checkboxes',
'#title' => t('Toggle display'),
'#default_value' => variable_get('guestbook_display', array(
'date',
'email',
'website',
'comments',
)),
'#options' => array(
'date' => t('Submission date'),
'email' => t('Anonymous poster e-mail'),
'website' => t('Anonymous poster website'),
'comments' => t('Comments'),
),
);
$form['display_options']['guestbook_pager_position'] = array(
'#type' => 'radios',
'#title' => t('Position of pager'),
'#default_value' => variable_get('guestbook_pager_position', GUESTBOOK_PAGER_BELOW),
'#options' => array(
GUESTBOOK_PAGER_ABOVE => t('Above the entries'),
GUESTBOOK_PAGER_BELOW => t('Below the entries'),
GUESTBOOK_PAGER_ABOVE | GUESTBOOK_PAGER_BELOW => t('Above and below the entries'),
),
);
$form['posting_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Posting settings'),
);
$form['posting_settings']['guestbook_entry_default_status'] = array(
'#type' => 'checkbox',
'#title' => t('Publish entries by default'),
'#default_value' => variable_get('guestbook_entry_default_status', 1),
);
$form['posting_settings']['guestbook_anonymous_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Anonymous poster fields'),
'#default_value' => variable_get('guestbook_anonymous_fields', array(
'email',
'website',
)),
'#description' => t('Additional information that anonymous posters may supply.'),
'#options' => array(
'email' => 'E-mail',
'website' => 'Website',
),
);
$form['posting_settings']['guestbook_form_location'] = array(
'#type' => 'radios',
'#title' => t('Location of entry submission form'),
'#default_value' => variable_get('guestbook_form_location', 'above'),
'#options' => array(
'above' => t('Above entries'),
'below' => t('Below entries'),
'separate page' => t('Separate page'),
),
);
$form['array_filter'] = array(
'#type' => 'value',
'#value' => TRUE,
);
return system_settings_form($form);
}
function guestbook_page($account, $op = NULL, $op_id = NULL, $page = TRUE) {
global $user;
if (empty($account->uid)) {
$account = drupal_anonymous_user();
drupal_set_title(variable_get('guestbook_site_title', t('Site guestbook')));
}
if (!_guestbook_exists($account->uid)) {
if ($page) {
drupal_not_found();
}
return;
}
if ($account->uid > 0 && $account->uid == $user->uid) {
user_save($account, array(
'guestbook_visited' => time(),
));
}
$comment_entry = 0;
if (_guestbook_access('moderate', $account->uid) && is_numeric($op_id)) {
switch ($op) {
case 'edit':
return drupal_get_form('guestbook_form_entry_form', $account->uid, 'page', $op_id);
case 'delete':
return guestbook_delete_entry_confirm_page($account->uid, $op_id);
case 'comment':
$comment_entry = $op_id;
break;
}
}
$limit = variable_get('guestbook_entries_per_page', 20);
$query = db_select('guestbook', 'g', array(
'fetch' => PDO::FETCH_ASSOC,
));
if (empty($comment_entry)) {
$query = $query
->extend('PagerDefault');
$query
->limit($limit);
}
$query
->leftJoin('users', 'u1', 'u1.uid = g.author');
$query
->leftJoin('users', 'u2', 'u2.uid = g.commentauthor');
$query
->fields('g')
->fields('u1', array(
'uid',
'name',
'data',
'picture',
));
$query
->addField('u2', 'name', 'commentby');
$query
->condition('g.recipient', $account->uid)
->orderBy('g.created', 'desc');
if (!empty($comment_entry)) {
$query
->condition('g.id', $comment_entry);
}
if (!_guestbook_access('moderate', $account->uid) || !(user_access('moderate own guestbook') || user_access('moderate all guestbooks'))) {
$query
->condition('g.status', 1);
}
$entries = array();
foreach ($query
->execute() as $entry) {
$entries[] = $entry;
}
$build = array(
'#theme' => 'guestbook',
'#uid' => $account->uid,
'#entries' => $entries,
'#comment_entry' => $comment_entry,
'#limit' => $limit,
);
return $build;
}
function guestbook_page_form($account) {
if (empty($account->uid)) {
$account = drupal_anonymous_user();
}
if (!_guestbook_exists($account->uid)) {
drupal_not_found();
return;
}
return guestbook_form_entry($account->uid, 'page');
}
function guestbook_list() {
$limit = 40;
$guestbooks = array();
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
$header = array(
array(
'data' => t('user'),
'field' => 'u.name',
),
array(
'data' => t('entries'),
'field' => 'num',
),
array(
'data' => t('last update'),
'field' => 'created',
'sort' => 'desc',
),
);
$query = db_select('users', 'u', array(
'fetch' => PDO::FETCH_ASSOC,
))
->extend('TableSort')
->extend('PagerDefault');
$query
->leftJoin('guestbook', 'g', 'u.uid = g.recipient');
$query
->fields('u', array(
'uid',
'name',
'data',
));
$query
->addExpression('MAX(g.created)', 'created');
$query
->addExpression('COUNT(g.recipient)', 'num');
$query
->groupBy('u.uid');
$query
->groupBy('u.name');
$query
->groupBy('u.data');
$query
->groupBy('g.recipient');
$query
->orderByHeader($header);
$query
->limit($limit);
foreach ($query
->execute() as $guestbook) {
if ($guestbook['uid'] == 0 && user_access('access site guestbook') && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
$guestbooks[0] = $guestbook;
}
else {
if ($guestbook['uid'] > 0 && user_access('access user guestbooks')) {
$data = unserialize($guestbook['data']);
if (empty($data['guestbook_status'])) {
$guestbooks[$guestbook['uid']] = $guestbook;
}
}
}
}
$build = array(
'#theme' => 'guestbook_list',
'#guestbooks' => $guestbooks,
'#header' => $header,
'#limit' => $limit,
);
return $build;
}
function guestbook_form_entry($uid, $display = '') {
return drupal_get_form('guestbook_form_entry_form', $uid, $display);
}
function guestbook_form_entry_form($form, $form_state, $uid, $display = '', $entry_id = NULL) {
global $user;
$entry = array();
if (isset($entry_id) && _guestbook_access('moderate', $uid) && user_access('moderate own guestbook')) {
$entry = db_query("SELECT * FROM {guestbook} WHERE id = :id", array(
':id' => $entry_id,
))
->fetchAssoc();
}
$form = array();
if (!empty($entry)) {
$form['entry_id'] = array(
'#type' => 'value',
'#value' => $entry['id'],
);
$form['author'] = array(
'#type' => 'value',
'#value' => $entry['author'],
);
$form['#submit'] = array(
'guestbook_form_entry_form_edit_submit',
);
}
$anonymous_fields_access = !$user->uid || isset($entry['author']) && $entry['author'] == 0;
$anonymous_fields = (array) variable_get('guestbook_anonymous_fields', array(
'email',
'website',
));
$form['anonname'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
'#default_value' => !empty($entry['anonname']) ? $entry['anonname'] : '',
'#size' => 32,
'#maxlength' => 64,
'#access' => $anonymous_fields_access,
);
$form['anonemail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#default_value' => !empty($entry['anonemail']) ? $entry['anonemail'] : '',
'#size' => 32,
'#maxlength' => 128,
'#access' => $anonymous_fields_access && in_array('email', $anonymous_fields),
);
$form['anonwebsite'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#default_value' => !empty($entry['anonwebsite']) ? $entry['anonwebsite'] : '',
'#size' => 32,
'#maxlength' => 128,
'#access' => $anonymous_fields_access && in_array('website', $anonymous_fields),
);
$form['message'] = array(
'#type' => 'text_format',
'#title' => t('Message'),
'#rows' => GUESTBOOK_TEXTAREA_ROWS,
'#required' => TRUE,
'#default_value' => !empty($entry['message']) ? $entry['message'] : '',
'#format' => isset($entry['message_format']) ? $entry['message_format'] : NULL,
);
$form['status'] = array(
'#type' => 'checkbox',
'#title' => t('Published'),
'#default_value' => isset($entry['status']) ? $entry['status'] : variable_get('guestbook_entry_default_status', 1),
'#access' => !empty($entry),
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);
if (!empty($entry)) {
$form['#access'] = _guestbook_access('moderate', $uid) ? TRUE : FALSE;
}
else {
$form['#access'] = in_array(_guestbook_access('post', $uid), array(
'allowed',
'own guestbook',
));
}
if (variable_get('guestbook_form_location', 'above') == 'separate page') {
$form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : guestbook_path($uid);
}
$form['display'] = array(
'#type' => 'value',
'#value' => $display,
);
return $form;
}
function guestbook_form_entry_form_validate($form, &$form_state) {
if (!empty($form_state['values']['anonname'])) {
if ($existing = user_load_by_name($form_state['values']['anonname'])) {
form_set_error('anonname', t('%name is a registered user name. Please enter a different name.', array(
'%name' => $form_state['values']['anonname'],
)));
}
}
}
function guestbook_form_entry_form_submit($form, &$form_state) {
global $user;
$uid = $form_state['values']['uid'];
$message = $form_state['values']['message']['value'];
$entry = db_query("SELECT message FROM {guestbook} WHERE recipient = :uid ORDER BY id DESC LIMIT 1", array(
':uid' => $uid,
))
->fetchAssoc();
if ($entry['message'] == $message) {
return;
}
if ($message == '') {
return;
}
if (module_exists('spam')) {
$spamcheck = $form_state['values']['anonname'] . ' ' . $form_state['values']['anonemail'] . ' ' . $form_state['values']['anonwebsite'];
if (spam_content_filter('guestbook', 1, $spamcheck, $message, '_guestbook_spam')) {
return;
}
}
$iSendEmail = '';
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
if ($uid == 0 && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
$iSendEmail = variable_get('guestbook_send_email', '');
}
else {
if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
if ($uid != $user->uid) {
$guestbook_user = user_load_multiple(array(
$uid,
), array(
'status' => 1,
));
$guestbook_user = reset($guestbook_user);
}
else {
$guestbook_user = $user;
}
if ($guestbook_user->uid && empty($guestbook_user->data['guestbook_status']) && !empty($guestbook_user->data['guestbook_send_email'])) {
$iSendEmail = $guestbook_user->mail;
}
}
}
if ($iSendEmail) {
$params = array();
$params['body'] = $message;
$params['account'] = user_load($uid);
drupal_mail('guestbook', 'notification', $iSendEmail, user_preferred_language($params['account']), $params);
}
if ($user->uid == 0) {
$fields = array(
'anonname' => $form_state['values']['anonname'],
'anonemail' => $form_state['values']['anonemail'],
'anonwebsite' => $form_state['values']['anonwebsite'],
'author' => 0,
'recipient' => $uid,
'message' => $message,
'comment' => '',
'status' => $form_state['values']['status'],
'created' => REQUEST_TIME,
'message_format' => $form_state['values']['message']['format'],
);
}
else {
$fields = array(
'author' => $user->uid,
'recipient' => $uid,
'message' => $message,
'comment' => '',
'status' => $form_state['values']['status'],
'created' => REQUEST_TIME,
'message_format' => $form_state['values']['message']['format'],
);
}
$entryid = db_insert('guestbook')
->fields($fields)
->execute();
$entry = db_query('SELECT * FROM {guestbook} WHERE id = :id', array(
':id' => $entryid,
))
->fetchAssoc();
module_invoke_all('guestbook', 'insert', $entry);
if (empty($entry['status'])) {
if (!_guestbook_access('moderate', $uid)) {
drupal_set_message(t('Your message has been queued for review and will be published after approval.'));
}
}
else {
drupal_set_message(t('Your message has been added.'));
}
cache_clear_all();
}
function guestbook_form_entry_form_edit_submit($form, &$form_state) {
if (_guestbook_access('moderate', $form_state['values']['uid']) && $form_state['values']['submit'] == t('Send') && user_access('moderate own guestbook')) {
if ($form_state['values']['author'] == 0) {
db_update('guestbook')
->fields(array(
'status' => $form_state['values']['status'],
'anonname' => $form_state['values']['anonname'],
'anonemail' => $form_state['values']['anonemail'],
'anonwebsite' => $form_state['values']['anonwebsite'],
'message' => $form_state['values']['message']['value'],
'message_format' => $form_state['values']['message']['format'],
))
->condition('id', $form_state['values']['entry_id'])
->execute();
}
else {
if ($form_state['values']['author'] > 0) {
db_update('guestbook')
->fields(array(
'status' => $form_state['values']['status'],
'message' => $form_state['values']['message']['value'],
'message_format' => $form_state['values']['message']['format'],
))
->condition('id', $form_state['values']['entry_id'])
->execute();
}
}
}
$entry = db_query('SELECT * FROM {guestbook} WHERE id = :id', array(
':id' => $form_state['values']['entry_id'],
))
->fetchAssoc();
module_invoke_all('guestbook', 'update', $entry);
$form_state['redirect'] = guestbook_path($form_state['values']['uid']);
cache_clear_all();
}
function guestbook_mail($key, &$message, $params) {
$language = $message['language'];
switch ($key) {
case 'notification':
$message['subject'] = t('New guestbook entry at [site:name]', array(), array(
'langcode' => $language->language,
));
$message['subject'] = token_replace($message['subject'], array(), array(
'language' => $language,
'sanitize' => FALSE,
));
$message['body'][] = $params['body'];
break;
}
}
function guestbook_theme() {
return array(
'guestbook_form_entry_form' => array(
'render element' => 'form',
),
'guestbook' => array(
'variables' => array(
'uid' => NULL,
'entries' => array(),
'comment_entry' => NULL,
'limit' => NULL,
),
),
'guestbook_entry' => array(
'variables' => array(
'uid' => NULL,
'entry' => NULL,
'comment_entry' => NULL,
'zebra' => NULL,
'confirm_delete' => FALSE,
),
),
'guestbook_user_picture' => array(
'render element' => 'form',
),
'guestbook_entry_comment' => array(
'variables' => array(
'uid' => NULL,
'entry' => NULL,
'comment_entry' => NULL,
),
),
'guestbook_list' => array(
'variables' => array(
'guestbooks' => array(),
'header' => array(),
'limit' => 40,
),
),
);
}
function theme_guestbook_form_entry_form($variables) {
$form_state = $variables['form'];
$output = '';
$access = $form_state['#access'];
$display = $form_state['display']['#value'];
$uid = $form_state['uid']['#value'];
switch ($access) {
case 'allowed':
if ($display == 'link') {
$output .= '<p>» ' . l(t('Add guestbook entry'), guestbook_path($uid) . '/sign') . '</p>';
}
else {
$output .= $display == 'page' ? '' : '<h3>' . t('Add guestbook entry') . '</h3>';
$output .= drupal_render_children($form_state);
}
break;
case 'own guestbook':
if (isset($form_state['entry_id'])) {
drupal_set_title(t('Edit guestbook entry'));
$output .= drupal_render_children($form_state);
}
else {
$output .= ' ';
}
break;
case 'not logged in':
$output .= '<p class="links">» ' . t('You must be logged in to post a comment.') . '</p>';
break;
case 'not allowed':
$output .= '<p class="links">» ' . t('You are not allowed to post in this guestbook.') . '</p>';
break;
}
return $output;
}
function guestbook_form_comment($uid, $entry) {
return drupal_get_form('guestbook_form_comment_form', $uid, $entry);
}
function guestbook_form_comment_form($form, $form_state, $uid, $entry) {
$form['comment'] = array(
'#type' => 'text_format',
'#default_value' => $entry['comment'],
'#format' => $entry['comment_format'],
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Reply'),
);
$form['entry_id'] = array(
'#type' => 'value',
'#value' => $entry['id'],
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);
$form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : guestbook_path($uid);
return $form;
}
function guestbook_form_comment_form_submit($form, &$form_state) {
global $user;
if (_guestbook_access('moderate', $form_state['values']['uid'])) {
db_update('guestbook')
->fields(array(
'comment' => $form_state['values']['comment']['value'],
'commentauthor' => $user->uid,
'comment_format' => $form_state['values']['comment']['format'],
))
->condition('id', $form_state['values']['entry_id'])
->execute();
cache_clear_all();
}
}
function guestbook_delete_entry_confirm_page($uid, $entry_id) {
return drupal_get_form('guestbook_delete_entry_confirm', $uid, $entry_id);
}
function guestbook_delete_entry_confirm($form, $form_state, $uid, $entry_id) {
$query = db_select('guestbook', 'g', array(
'fetch' => PDO::FETCH_ASSOC,
));
$query
->leftJoin('users', 'u1', 'u1.uid = g.author');
$query
->leftJoin('users', 'u2', 'u2.uid = g.commentauthor');
$query
->fields('g')
->fields('u1', array(
'uid',
'name',
'data',
'picture',
));
$query
->addField('u2', 'name', 'commentby');
$query
->condition('g.recipient', $uid)
->condition('g.id', $entry_id);
$entry = $query
->execute()
->fetch();
$form['entry_id'] = array(
'#type' => 'value',
'#value' => $entry_id,
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);
$form['#redirect'] = !empty($_GET['destination']) ? $_GET['destination'] : guestbook_path($uid);
return confirm_form($form, t('Are you sure you want to delete this guestbook entry?'), NULL, theme('guestbook_entry', array(
'uid' => $uid,
'entry' => $entry,
'confirm_delete' => TRUE,
)), t('Delete'), t('Cancel'));
}
function guestbook_delete_entry_confirm_submit($form, &$form_state) {
if (_guestbook_access('moderate', $form_state['values']['uid']) && $form_state['values']['confirm']) {
db_delete('guestbook')
->condition('id', $form_state['values']['entry_id'])
->execute();
cache_clear_all();
}
}
function theme_guestbook($variables) {
global $user;
$uid = $variables['uid'];
$entries = $variables['entries'];
$comment_entry = $variables['comment_entry'];
$limit = $variables['limit'];
$form_location = variable_get('guestbook_form_location', 'above');
$pager_position = variable_get('guestbook_pager_position', GUESTBOOK_PAGER_BELOW);
$intro = _guestbook_info($uid, 'intro');
$output = $intro ? check_markup($intro, NULL, '', TRUE) : '';
if ($_GET['q'] != 'user/' . $uid) {
$output .= _guestbook_user_profile_link($uid);
}
if ($form_location == 'separate page') {
$build = guestbook_form_entry($uid, 'link');
$output .= drupal_render($build);
}
if ($form_location == 'above') {
$build = guestbook_form_entry($uid);
$output .= drupal_render($build);
}
$output .= $pager_position & GUESTBOOK_PAGER_ABOVE ? theme('pager') : '';
$i = 0;
foreach ($entries as $entry) {
$zebra = $i % 2 ? 'odd' : 'even';
$output .= theme('guestbook_entry', array(
'uid' => $uid,
'entry' => $entry,
'comment_entry' => $comment_entry,
'zebra' => $zebra,
));
$i++;
}
$output .= $pager_position & GUESTBOOK_PAGER_BELOW ? theme('pager') : '';
if ($form_location == 'below') {
$build = guestbook_form_entry($uid);
$output .= drupal_render($build);
}
if ($output == '') {
$output = '<div class="guestbook-empty">' . t('Nobody has signed this guestbook yet.') . '</div>';
}
return '<div class="guestbook">' . $output . "</div>\n";
}
function theme_guestbook_entry($variables) {
global $user;
$uid = $variables['uid'];
$entry = $variables['entry'];
$comment_entry = $variables['comment_entry'];
$zebra = $variables['zebra'];
$confirm_delete = $variables['confirm_delete'];
$output = '';
$display = (array) variable_get('guestbook_display', array(
'date',
'email',
'website',
'comments',
));
$output .= "\n<div class=\"guestbook-entry clear-block {$zebra}" . ($entry['status'] ? '' : ' guestbook-entry-unpublished') . "\">\n";
if ($comment_entry == $entry['id']) {
$output .= '<a name="comment-entry"></a>';
}
if ($entry['author'] == 0) {
$output .= "<b>" . check_plain($entry['anonname']) . "</b>";
}
else {
$entry['uid'] = $entry['author'];
$output .= theme('username', array(
'account' => (object) $entry,
'display' => 'guestbook',
));
}
$output .= '<div class="submitted">';
if (in_array('date', $display)) {
$output .= format_date($entry['created'], 'medium');
}
if (in_array('email', $display) && !empty($entry['anonemail'])) {
$output .= ' | <a href="mailto:' . check_url($entry['anonemail']) . '">' . t('E-mail') . '</a>';
}
if (in_array('website', $display) && !empty($entry['anonwebsite'])) {
if (strpos($entry['anonwebsite'], '://') === FALSE) {
$entry['anonwebsite'] = 'http://' . $entry['anonwebsite'];
}
$output .= ' | <a href="' . check_url($entry['anonwebsite']) . '">' . t('Website') . '</a> ';
}
$output .= '</div>';
if (!$entry['status']) {
$output .= '<div class="guestbook-entry-status">' . t('Not published') . '</div>';
}
$output .= '<div class="guestbook-message">' . check_markup($entry['message'], $entry['message_format'], '', TRUE) . '</div>';
$output .= theme('guestbook_entry_comment', array(
'uid' => $uid,
'entry' => $entry,
'comment_entry' => $comment_entry,
));
if (_guestbook_access('moderate', $uid) && !$confirm_delete) {
if ($comment_entry != $entry['id']) {
$links = array();
$pager = !empty($_GET['page']) ? array(
'page' => $_GET['page'],
) : array();
if (user_access('moderate own guestbook') || user_access('moderate all guestbooks')) {
$links['delete'] = array(
'title' => t('Delete entry'),
'href' => guestbook_path($uid) . '/delete/' . $entry['id'],
'query' => drupal_get_destination() + $pager,
);
$links['edit'] = array(
'title' => t('Edit entry'),
'href' => guestbook_path($uid) . '/edit/' . $entry['id'],
'query' => drupal_get_destination() + $pager,
);
}
$links['guestbook-comment'] = array(
'title' => $entry['comment'] == '' ? t('Add comment') : t('Edit comment'),
'href' => guestbook_path($uid) . '/comment/' . $entry['id'],
'query' => drupal_get_destination() + $pager,
'fragment' => 'comment-entry',
);
$output .= theme('links', array(
'links' => $links,
'attributes' => array(
'class' => array(
'guestbook-links',
'links',
'inline',
),
),
));
}
}
$output .= "\n</div>";
return $output;
}
function theme_guestbook_entry_comment($variables) {
$uid = $variables['uid'];
$entry = $variables['entry'];
$comment_entry = $variables['comment_entry'];
$display = (array) variable_get('guestbook_display', array(
'date',
'email',
'website',
'comments',
));
$output = '';
if ($comment_entry == $entry['id']) {
$build = guestbook_form_comment($uid, $entry);
$output .= drupal_render($build);
}
else {
if (in_array('comments', $display) && $entry['comment'] != '') {
$author = user_access('access user profiles') ? l($entry['commentby'], "user/{$entry['commentauthor']}") : $entry['commentby'];
$output .= '<div class="guestbook-comment-submitted">';
$output .= t('Comment by') . ' ' . $author;
$output .= '</div>';
$output .= '<div class="guestbook-comment-content">';
$output .= check_markup($entry['comment'], $entry['comment_format'], '', TRUE);
$output .= '</div>';
}
}
return !empty($output) ? '<div class="guestbook-comment">' . $output . '</div>' : '';
}
function theme_guestbook_list($variables) {
$guestbooks = $variables['guestbooks'];
$header = $variables['header'];
$limit = $variables['limit'];
$output = '';
if (isset($guestbooks[0])) {
$output .= '<p>' . l(variable_get('guestbook_site_title', t('Site guestbook')), 'guestbook');
$output .= ' (' . format_plural($guestbooks[0]['num'], '1 entry', '@count entries') . ', ' . t('last update') . ': ' . _guestbook_timeinterval($guestbooks[0]['created']) . ')</p>';
unset($guestbooks[0]);
}
if (count($guestbooks)) {
$output .= '<h4>' . t('User guestbooks') . '</h4>';
$rows = array();
foreach ($guestbooks as $guestbook) {
$rows[] = array(
l($guestbook['name'], guestbook_path($guestbook['uid'])),
format_plural($guestbook['num'], '1 entry', '@count entries'),
array(
'data' => _guestbook_timeinterval($guestbook['created']),
'align' => 'right',
),
);
}
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
));
}
$output .= theme('pager');
return $output;
}
function _guestbook_info($uid, $data) {
global $user;
static $info;
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
if (is_object($uid)) {
$uid = $uid->uid;
}
if (!isset($info[$uid])) {
if ($uid == 0 && $guestbook_mode & GUESTBOOK_SITE_GUESTBOOK) {
$info[$uid]['title'] = variable_get('guestbook_site_title', t('Site guestbook'));
$info[$uid]['intro'] = variable_get('guestbook_site_intro', '');
}
else {
if ($guestbook_mode & GUESTBOOK_USER_GUESTBOOKS) {
$guestbook_user = $uid != $user->uid ? user_load($uid) : $user;
if ($guestbook_user->uid && ($guestbook_user->status || user_access('administer users')) && empty($guestbook_user->guestbook_status)) {
if ($uid != $user->uid) {
$info[$uid]['title'] = t("@username's guestbook", array(
'@username' => $guestbook_user->name,
));
$info[$uid]['intro'] = !empty($guestbook_user->guestbook_intro) ? $guestbook_user->guestbook_intro : '';
}
else {
$unread = _guestbook_newentries();
$info[$uid]['title'] = t('My guestbook') . ($unread ? ' (' . $unread . ')' : '');
$info[$uid]['intro'] = !empty($guestbook_user->guestbook_intro) ? $guestbook_user->guestbook_intro : '';
}
}
}
}
}
return $info[$uid][$data];
}
function _guestbook_user_profile_link($uid) {
global $user;
$guestbook_mode = variable_get('guestbook_mode', GUESTBOOK_SITE_GUESTBOOK | GUESTBOOK_USER_GUESTBOOKS);
$output = '';
if ($uid && $guestbook_mode & GUESTBOOK_USER_GUESTBOOKS && user_access('access user profiles') && $uid != $user->uid) {
$guestbook_user = user_load_multiple(array(
$uid,
), array(
'status' => 1,
));
$guestbook_user = reset($guestbook_user);
if ($guestbook_user->uid && empty($guestbook_user->guestbook_status)) {
$namelink = l($guestbook_user->name, "user/{$uid}", array(
'attributes' => array(
'title' => t('View user profile.'),
),
));
$output .= '<div class="submitted">' . t("Visit !username's profile", array(
'!username' => $namelink,
)) . '</div>';
}
}
return $output;
}
function guestbook_path($uid = 0) {
$uid = (int) $uid;
if ($uid > 0) {
return 'user/' . $uid . '/guestbook';
}
else {
return 'guestbook';
}
}
function _guestbook_exists($uid) {
$title = _guestbook_info($uid, 'title');
return !empty($title);
}
function _guestbook_access($action, $uid) {
global $user;
switch ($action) {
case 'post':
if ($uid == 0 ? user_access('post in site guestbook') : user_access('post in user guestbooks')) {
if (!($user->uid == $uid && $user->uid > 0)) {
return 'allowed';
}
else {
return 'own guestbook';
}
}
else {
if ($user->uid == 0) {
return 'not logged in';
}
else {
if ($user->uid != $uid) {
return 'not allowed';
}
}
}
break;
case 'moderate':
return user_access('moderate all guestbooks') || $uid == $user->uid && $user->uid > 0;
}
}
function _guestbook_timeinterval($time) {
if ($time == 0) {
return t('never');
}
else {
return format_interval(time() - $time, 1);
}
}
function _guestbook_newentries() {
global $user;
$count = db_query("SELECT COUNT(created) FROM {guestbook} WHERE recipient = :recipient AND created > :created", array(
':recipient' => $user->uid,
':created' => isset($user->guestbook_visited) ? $user->guestbook_visited : REQUEST_TIME,
))
->fetchField();
return $count;
}
function _guestbook_spam($source, $id, $header, $body, $probability, $old, $action) {
if ($probability > 98) {
$msgtext = t('Entry is spam: ') . $header . ' ' . $body . ' probability: ' . $probability;
watchdog('guestbook', $msgtext, array(), WATCHDOG_WARNING);
drupal_set_message($msgtext, 'error');
return TRUE;
}
return FALSE;
}
function guestbook_mollom_form_list() {
$forms['guestbook_form_entry_form'] = array(
'title' => t('Entry form'),
'entity' => 'guestbook_entry',
'delete form' => 'guestbook_delete_entry_confirm',
);
return $forms;
}
function guestbook_mollom_form_info($form_id) {
$form_info = array(
'bypass access' => array(
'moderate all guestbooks',
),
'moderation callback' => 'guestbook_mollom_form_moderation',
'mail ids' => array(
'guestbook_notification',
),
'elements' => array(
'message' => t('Comment'),
),
'mapping' => array(
'post_id' => 'entry_id',
'author_name' => 'anonname',
'author_mail' => 'anonemail',
'author_url' => 'anonwebsite',
),
);
return $form_info;
}
function guestbook_mollom_form_moderation(&$form, &$form_state) {
$form_state['values']['status'] = 0;
}
function guestbook_panels_include_directory($plugintype) {
if ($plugintype == 'content_types') {
return 'panels';
}
}