shouts.module in Heartbeat 6.4
Same filename and directory in other branches
Gives the possibility to the user to shout a message.
File
modules/shouts/shouts.moduleView source
<?php
// by Zuuperman and Stalski - Menhir - www.menhir.be
/**
* @file
* Gives the possibility to the user to shout a message.
*/
/**
* Implementation of hook_init().
*/
function shouts_init() {
drupal_add_js(drupal_get_path('module', 'shouts') . '/shouts.js');
}
/**
* Implementation of hook_perm().
*/
function shouts_perm() {
return array(
'make shout',
'administer shouts',
);
}
/**
* Implementation of hook_theme().
*/
function shouts_theme($existing, $type, $theme, $path) {
return array(
'shoutform_message' => array(
'arguments' => array(
'latest_shout' => NULL,
),
),
);
}
/**
* Implementation of hook_block().
*/
function shouts_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0]['info'] = t('Shout form');
return $blocks;
}
elseif ($op == 'view' && $delta == 0) {
$block['subject'] = t('Make shout');
$block['content'] = drupal_get_form('shouts_shout_form', variable_get('show_latest_shout', 0));
return $block;
}
elseif ($op == 'configure' && $delta == 0) {
$form['show_latest_shout'] = array(
'#type' => 'checkbox',
'#title' => t('Show latest shout'),
'#default_value' => variable_get('show_latest_shout', 0),
);
return $form;
}
elseif ($op == 'save') {
variable_set('show_latest_shout', $edit['show_latest_shout']);
}
}
/**
* Implementation of hook_menu().
*/
function shouts_menu() {
$items['shout/post'] = array(
'title' => 'Shout',
'description' => 'Make a shout',
'access arguments' => array(
'make shout',
),
'page callback' => 'shouts_shout_form_submit',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_user().
*/
function shouts_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch ($op) {
case 'load':
if ($user->uid > 0) {
$user->latest_shout = get_latest_shout($user->uid);
}
break;
case 'view':
$latest_shout = get_latest_shout($account->uid);
if (!empty($latest_shout->message)) {
$account->content['shouts'] = array(
'#type' => 'user_profile_category',
'#title' => t('Lastest shout'),
'#attributes' => array(
'class' => 'shouts-profile',
),
'#weight' => 1,
'latest_shout' => array(
'#type' => 'user_profile_item',
'#value' => theme('shoutform_message', $latest_shout->message, false, false, $latest_shout->time),
),
);
}
break;
}
}
/**
* Implementation of hook_heartbeat_message_info()
*/
function shouts_heartbeat_message_info() {
return array(
'heartbeat_shout' => array(
'message_id' => 'heartbeat_shout',
'message' => '!user !message',
'message_concat' => '',
'perms' => '1',
'custom' => HEARTBEAT_MESSAGE_DEFAULT,
'concat_args' => array(
'type' => 'single',
'merge_target' => '',
'merge_separator' => '',
'merge_end_separator' => '',
),
'description' => 'shout message',
'variables' => array(
'@user' => '[user:user-name-url]',
'@message' => '[shout:comment-body-raw]',
),
),
);
}
/**
* Implementation of hook_token_values().
*/
function shouts_token_values($type, $object = NULL, $options = array()) {
$values = array();
switch ($type) {
case 'shout':
// Cast to an object just in case fussy Drupal gave us an array
$shout = (object) $object;
$values['shout-id'] = $shout->shout_id;
$values['shout-actor-avatar'] = l(theme('user_picture', heartbeat_user_load($shout->uid)), 'user/' . $shout->uid, array(
'html' => TRUE,
));
$values['shout-body'] = check_markup($shout->message);
$values['shout-body-raw'] = $shout->message;
$values['shout-author-uid'] = $shout->uid;
$values['shout-date'] = $shout->time;
break;
}
return $values;
}
/**
* Implementation of hook_token_list().
*/
function shouts_token_list($type = 'all') {
if ($type == 'shout' || $type == 'all') {
$tokens['shout']['shout-id'] = t('Shout ID');
$tokens['shout']['shout-actor-avatar'] = t('Shout actor avatar');
$tokens['shout']['shout-body'] = t('Shout body');
$tokens['shout']['shout-body-raw'] = t('Shout body. WARNING - raw user input');
$tokens['shout']['shout-author-uid'] = t("Shout author's user id");
$tokens['shout']['shout-date'] = t("Shout creation year (four digit)");
return $tokens;
}
}
/**
* Theme the latest shout of a user.
*
* @param $latest_shout the shout message
* @param $update is it an ajax update of the shout?
*/
function theme_shoutform_message($latest_shout, $update, $time = '') {
$shout_time = '';
if ($time) {
$ago = t('ago');
$time_diff = $_SERVER['REQUEST_TIME'] - strtotime($time);
// don't show minutes if less then 1 minute ago.
if ($time_diff < 60) {
$date = t('a moment');
}
else {
$date = format_interval($time_diff, 1, 'nl');
}
$shout_time = '<span class="shout_ago">' . $date . ' ' . $ago . '</span>';
}
$latest_shout = '<div class="latest_shout">' . $latest_shout . '</div>';
$output = '<div class="inner">' . $latest_shout . $shout_time . '</div>';
if (!$update) {
$output = '<div id="shout-wrapper">' . $output . '</div>';
}
return $output;
}
/**
* Show the shoutform
*/
function shouts_shout_form($form_state = array(), $show_latest_shout = TRUE) {
global $user;
$show_form = user_access('make shout');
$form = array();
if (empty($user->latest_shout) || !$show_latest_shout) {
$latest_shout->message = '';
$latest_shout->time = '';
}
else {
$latest_shout = $user->latest_shout;
}
if ($show_form) {
$form['#prefix'] = theme('shoutform_message', $latest_shout->message, false, $latest_shout->time);
$form['shout'] = array(
'#type' => 'textarea',
'#rows' => 1,
'#required' => TRUE,
/*'#resizable' => FALSE,*/
'#attributes' => array(
'class' => 'shout-message',
),
'#default_value' => t('What are you doing?'),
);
if ($show_latest_shout) {
$form['shout']['#title'] = t('Share');
}
$form['submit'] = array(
'#prefix' => '<span class="shouts-form-wrapper">',
'#suffix' => '<span class="heartbeat-messages-throbber"> </span></span>',
'#type' => 'submit',
'#value' => t('Shout'),
'#attributes' => array(
'class' => 'shout-submit',
'onclick' => 'javascript:Drupal.heartbeat.Shouts.shout(this, \'shout/post\'); return false;',
),
);
$form['heartbeat_shout_token'] = array(
'#id' => 'heartbeat_shout_post_' . $GLOBALS['user']->uid,
'#default_value' => drupal_get_token('heartbeat_shout_post_' . $GLOBALS['user']->uid),
'#type' => 'hidden',
);
}
return $form;
}
/**
* User submitted the shoutform, save the shout.
*/
function shouts_shout_form_submit($form = array(), &$form_state = array()) {
$ahah = empty($form);
global $user;
$uid = $user->uid;
// Check the token.
$token = 'heartbeat_shout_post_' . $uid;
$token_value = $ahah ? $_POST['heartbeat_shout_token'] : $form_state['values']['heartbeat_shout_token'];
if (!drupal_valid_token($token_value, $token)) {
drupal_json(array(
'status' => FALSE,
'data' => t('Access denied'),
));
exit;
}
$message = $ahah ? $_POST['shout'] : $form_state['values']['shout'];
if (!user_access('make shout') || empty($message)) {
drupal_json(array(
'status' => FALSE,
'data' => t('No message recorded'),
));
exit;
}
$success = db_query("INSERT INTO {shouts} (uid, message, time) VALUES (%d, '%s', '%s')", $uid, $message, date('Y-m-d H:i:s'));
$shout_id = db_last_insert_id('shouts', 'shout_id');
$shout = shout_load($shout_id);
if (!empty($shout)) {
// Give other modules a chance to hook.
module_invoke_all('shout', $user, $shout);
// Let rules know there has been a shout event
if (module_exists('rules')) {
rules_invoke_event('shout_post', $user, $shout);
}
if ($ahah) {
drupal_json(array(
'status' => TRUE,
'data' => theme('shoutform_message', $message, true, true, date('Y-m-d H:i:s')),
));
exit;
}
else {
drupal_set_message(t('Shout has been posted.'));
}
return;
}
if ($ahah) {
drupal_json(array(
'status' => TRUE,
'data' => 'error',
));
exit;
}
else {
drupal_set_message(t('Error while posting shout.'));
}
}
/**
* Load a shout
* @param Integer $shout_id The Id of the shout message
* @return Object $shout A shout record from the database
*/
function shout_load($shout_id) {
return db_fetch_object(db_query("SELECT * FROM {shouts} WHERE shout_id = %d", $shout_id));
}
/**
* Get the latest shout of a user.
*
* @param $uid user_id of shout to load
*/
function get_latest_shout($uid) {
static $uids = array();
if (!isset($uids[$uid])) {
$shout = db_fetch_object(db_query_range('SELECT message, cleared, time FROM {shouts} WHERE uid = %d ORDER BY time DESC', $uid, 0, 1));
if (isset($shout->message)) {
$shout->message = filter_xss($shout->message);
$uids[$uid] = $shout;
}
else {
$uids[$uid] = '';
}
}
return $uids[$uid];
}
/**
* Clear the latest shout from a user.
*/
function clear_shout() {
global $user;
$result = db_query("UPDATE {shouts} SET cleared = 1 WHERE uid = %d ORDER BY shout_id DESC LIMIT 1", $user->uid);
// if destination is set, there was a javascript error. Redirect to destination
if (isset($_GET['destination'])) {
drupal_goto();
}
if ($result) {
drupal_json(array(
'status' => TRUE,
'data' => theme_shoutform_message(t('Post new shout'), false, true),
));
}
else {
drupal_json(array(
'status' => FALSE,
'data' => theme_shoutform_message(t('Error while clearing shout.'), false, true),
));
}
exit;
}
Functions
Name | Description |
---|---|
clear_shout | Clear the latest shout from a user. |
get_latest_shout | Get the latest shout of a user. |
shouts_block | Implementation of hook_block(). |
shouts_heartbeat_message_info | Implementation of hook_heartbeat_message_info() |
shouts_init | Implementation of hook_init(). |
shouts_menu | Implementation of hook_menu(). |
shouts_perm | Implementation of hook_perm(). |
shouts_shout_form | Show the shoutform |
shouts_shout_form_submit | User submitted the shoutform, save the shout. |
shouts_theme | Implementation of hook_theme(). |
shouts_token_list | Implementation of hook_token_list(). |
shouts_token_values | Implementation of hook_token_values(). |
shouts_user | Implementation of hook_user(). |
shout_load | Load a shout |
theme_shoutform_message | Theme the latest shout of a user. |