You are here

shoutbox_user.module in Shoutbox 6.2

File

shoutbox_user/shoutbox_user.module
View source
<?php

/**
 * Implementation of hook_menu()
 */
function shoutbox_user_menu() {
  return array(
    'shoutbox/user/%user' => array(
      'title' => 'Shout box',
      'page callback' => 'shoutbox_user_page',
      'page arguments' => array(
        2,
      ),
      'access callback' => 'shoutbox_user_access',
      'description' => 'A dedicated shoutbox page for group shouts.',
      'type' => MENU_CALLBACK,
    ),
    'shoutbox/user/%user/js/view' => array(
      'title' => 'Shout tag',
      'page callback' => 'shoutbox_user_js_view',
      'page arguments' => array(
        2,
      ),
      'access callback' => 'shoutbox_user_access',
      'description' => 'Javascript callback for viewing a user\'s shouts',
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Page callback for user shoutbox page
 */
function shoutbox_user_page($user) {

  // Set the title with the user's name
  drupal_set_title(t('!name\'s shout box', array(
    '!name' => l($user->name, "user/{$user->uid}"),
  )));

  // Return normal shoutbox page
  return shoutbox_view();
}

/**
 * Access callback for user shouts
 */
function shoutbox_user_access() {
  if (user_access('view shouts') && user_access('access user profiles')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_shoutbox()
 */
function shoutbox_user_shoutbox($op, &$shout, &$a1 = NULL, $form_state = NULL) {
  switch ($op) {
    case 'context':
      if ($uid = shoutbox_user_get_current_uid()) {
        $a1['shoutbox_user'] = $uid;
      }
      break;
    case 'insert':

      // Determine if the shout was added for a user
      if ($uid = $form_state['values']['uid']) {
        shoutbox_user_save($shout, $uid);
      }
      break;
    case 'presave':

      // Determine if the shout was added for a user
      if ($uid = $form_state['values']['uid']) {

        // Tag this shout as belonging to this module
        $shout->module = 'shoutbox_user';
      }
    case 'delete':

      // Remove any tag references to this shout
      db_query("DELETE FROM {shoutbox_user} WHERE shout_id = %d", $shout->shout_id);
      break;
    case 'js path':

      // Alter the js update path so only shouts with the given
      // tag are shown
      if ($uid = shoutbox_user_get_current_uid()) {
        $a1 = "shoutbox/user/{$uid}/js/view/";
      }
      break;
    case 'form':

      // Remove the shout form if we're viewing a tag
      if ($uid = shoutbox_user_get_current_uid()) {
        $a1['uid'] = array(
          '#type' => 'value',
          '#value' => $uid,
        );
      }
      break;
    case 'link':
      if ($uid = shoutbox_user_get_current_uid()) {
        $a1 = "shoutbox/user/{$uid}";
      }
      break;
  }
}

/**
 * Implementation of hook_db_rewrite_sql()
 */
function shoutbox_user_db_rewrite_sql($query, $table, $primary, $contexts) {
  if ($table == 'shoutbox' && isset($contexts['shoutbox_user'])) {
    $query_mods['join'] = "INNER JOIN {shoutbox_user} shoutbox_user ON shoutbox.shout_id = shoutbox_user.shout_id\r\n";
    $query_mods['where'] = "shoutbox_user.uid = " . (int) $contexts['shoutbox_user'];
    return $query_mods;
  }
}

/**
 * Determine the user ID of the user we're viewing
 * 
 * @return
 *   The user id being viewed, otherwise FALSE is we're not viewing one
 */
function shoutbox_user_get_current_uid() {

  // Detect user/%uid
  if (arg(0) == 'user' && is_numeric(arg(1))) {
    return arg(1);
  }
  else {
    if (arg(0) == 'shoutbox' && arg(1) == 'user' && is_numeric(arg(2))) {
      return arg(2);
    }
  }
  return FALSE;
}

/**
 * Save a shout for a given user
 *
 * @param $shout
 *   A shout object
 * @param $user
 *   The user, or uid, whom the shout is directed towards
 */
function shoutbox_user_save($shout, $user) {
  $record = new stdClass();
  $record->shout_id = $shout->shout_id;
  $record->uid = is_object($user) ? $user->uid : $user;
  drupal_write_record('shoutbox_user', $record);
}

/**
 * Javascript callback for viewing shouts for a given tag
 */
function shoutbox_user_js_view($user) {

  // This callback is only needed to keep the
  // URL like shoutbox/user/%uid so the query gets altered
  shoutbox_js_view();
}

Functions

Namesort descending Description
shoutbox_user_access Access callback for user shouts
shoutbox_user_db_rewrite_sql Implementation of hook_db_rewrite_sql()
shoutbox_user_get_current_uid Determine the user ID of the user we're viewing
shoutbox_user_js_view Javascript callback for viewing shouts for a given tag
shoutbox_user_menu Implementation of hook_menu()
shoutbox_user_page Page callback for user shoutbox page
shoutbox_user_save Save a shout for a given user
shoutbox_user_shoutbox Implementation of hook_shoutbox()