You are here

shoutbox_group.module in Shoutbox 7

File

shoutbox_group/shoutbox_group.module
View source
<?php

/**
 * Implementation of hook_perm().
 */
function shoutbox_group_permission() {
  return array(
    'administer shoutbox_group' => array(
      'title' => t('Administer group shoutbox'),
    ),
    'post shouts in all groups' => array(
      'title' => t('Post shouts in all groups'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function shoutbox_group_menu() {
  return array(
    'shoutbox/group/%node' => array(
      'title' => 'Shout box',
      'page callback' => 'shoutbox_group_page',
      'page arguments' => array(
        2,
      ),
      'access callback' => 'shoutbox_group_group_access',
      //'access arguments' => array('access shoutbox'),
      'access arguments' => array(
        'view',
        2,
      ),
      'description' => 'A dedicated shoutbox page for group shouts.',
      'type' => MENU_CALLBACK,
    ),
    'shoutbox/group/%node/js/view' => array(
      'title' => 'Shout box',
      'page callback' => 'shoutbox_group_js_view',
      'page arguments' => array(
        2,
      ),
      'access callback' => 'shoutbox_group_group_access',
      //'access arguments' => array('access shoutbox'),
      'access arguments' => array(
        'view',
        2,
      ),
      'description' => 'Javascript callback for shout updates.',
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Generate dedicated shoutbox page for group
 * 
 * @param $group
 *   The group node whom this shoutbox page belongs to
 */
function shoutbox_group_page($group) {

  // View permissions checked in shoutbox_group_shoutbox_get_shouts()
  // Post permissions check in shoutbox_group_form_shoutbox_add_form_alter()
  // Check that we're in a real group
  if (!og_is_group_type('node', $group->type)) {
    return t('The group does not exist');
  }

  // Tell OG this is a group-specific page
  og_context('node', $group);

  // Set the page title with group name
  drupal_set_title(t('Shout box @title', array(
    '@title' => $group->title,
  )));

  // Set the breadcrumb
  drupal_set_breadcrumb(array(
    l(t('Home'), '<front>'),
    l(t('Groups'), 'groups'),
    l($group->title, 'node/' . $group->nid),
  ));

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

/**
 * Alter the shoutbox add form
 * Add the group ID to the shout form (if one)
 * 
 * @param $form
 *   The shoutbox add form
 */
function _shoutbox_group_alter_form(&$form) {

  // Check that shoutbox is being used with a group
  if ($group = shoutbox_group_get_group()) {

    // If user is not a group member, or lacks permissions, remove the ability to shout
    if (!shoutbox_group_group_access('post', $group)) {
      $form = array(
        '#type' => 'item',
        '#value' => t('Only group members may post here.'),
      );
    }
    else {

      // Append the current group ID to form
      $form['group_id'] = array(
        '#type' => 'value',
        '#value' => $group['gid'],
      );
    }
  }
}

/**
 * Implementation of hook_shoutbox()
 */
function shoutbox_group_shoutbox($op, &$shout, &$a1 = NULL, $form_state = NULL) {
  global $group;
  switch ($op) {
    case 'insert':

      // Determine if the shout was added to a group
      if ($group_id = $form_state['values']['group_id']) {

        // Add the shout reference to the group
        $record = new stdClass();
        $record->shout_id = $shout->shout_id;
        $record->nid = $group_id;
        drupal_write_record('shoutbox_groups', $record);
      }
      break;
    case 'presave':

      // Determine if the shout was added to a group
      if ($group_id = $form_state['values']['group_id']) {

        // Tag this shout as belonging to this module
        $shout->module = 'shoutbox_group';
      }
      break;
    case 'delete':
      db_query("DELETE FROM {shoutbox_groups} WHERE shout_id = :id", array(
        ':id' => $shout->shout_id,
      ));
      break;
    case 'link':

      // If in group context, set the shoutbox page path
      if ($group = shoutbox_group_get_group()) {
        $a1 = "shoutbox/group/{$group['gid']}";
      }
      break;
    case 'form':
      _shoutbox_group_alter_form($a1);
      break;
    case 'context':
      if ($group = shoutbox_group_get_group()) {

        // A group is being viewed, display only group shouts

        //$etid=isset($group->etid) ? $group->etid : $group->gid;
        $a1['shoutbox_group'] = $group['gid'];
      }
      break;
    case 'js path':
      if ($group = shoutbox_group_get_group()) {

        //$etid=isset($group->etid) ? $group->etid : $group->gid;
        $etid = $group['gid'];
        $a1 = "shoutbox/group/{$etid}/js/view/";
      }
      break;
    case 'view':
      break;
  }
}

/**
 * Implementation of hook_query_alter()
 */
function shoutbox_group_query_shouts_alter(QueryAlterableInterface $query) {
  global $group;
  if (!is_array($group)) {
    return;
  }

  //$nid= isset($group->etid) ? $group->etid : $group->gid;
  $nid = $group['gid'];
  $query
    ->innerJoin('shoutbox_groups', 'sg', 's.shout_id = sg.shout_id');

  //JOIN node with users
  $query
    ->condition('nid', $nid, '=');
}

/**
 * Implementation of hook_og_links_alter()
 * 
 * Add a shoutbox link to OG menu block
 */
function shoutbox_group_og_links_alter(&$links) {
  if (user_access('access shoutbox')) {
    if ($group = og_context()) {
      $links['shoutbox'] = l(t('Shout box'), "shoutbox/group/{$group->etid}");
    }
  }
}

/**
 * Javascript callback to handle auto-update shouts
 * 
 * @param $group
 *   The group node
 */
function shoutbox_group_js_view($group) {

  // Check URL input
  if (is_array($group)) {
    $nid = $group['gid'];
  }
  else {
    $node = $group;
  }

  //$nid=isset($group->etid) ? $group->etid : $group->gid;

  //$node=node_load($nid);
  if (!og_is_group_type('node', $node->type)) {
    print t('Invalid argument supplied.');
    exit;
  }

  // Reset the group context
  og_context($group);

  // Fetch items as usual
  shoutbox_js_view();
}

/**
 * Determined by og_get_group_context for the block, or URL arg's for the page
 * 
 * @return
 *     The group currently in context, otherwise FALSE
 */
function shoutbox_group_get_group() {
  static $group = FALSE;

  // Retrieve the group if we haven't yet already
  if (!$group) {

    // First check og context for block view
    if ($group = og_context()) {
      return $group;
    }
    else {
      if (arg(0) == 'shoutbox' && arg(1) == 'group' && is_numeric(arg(2))) {

        // Check that arg(2) is a real group (already cleared as numeric value)
        if ($node = node_load(arg(2))) {

          // Make sure it's a group
          if (og_is_group_type('node', $node->type)) {
            $group = og_context('node', $node->nid);

            //          print "<pre>";
            //          print_r($group);
            //          print "</pre>";die();

            //$group = og_get_group('node', $node->nid);
          }
        }
      }
    }
  }
  return $group;
}

/**
 * Determine if current user can view the current group
 * 
 * @param $op
 *     The operation which access is checked on (View, Post)
 * @param $group
 *     The group node
 * @param $user
 *     Optionally specify the user, or user ID
 * @return
 *     Whether or not the user has access to the given group operation
 */
function shoutbox_group_group_access($op, $group, $user = NULL) {

  // Make sure the group is a group

  //$nid=isset($group->etid) ? $group->etid : $group->gid;

  //$nid = $group['gid'];

  //$node=node_load($nid);
  if (is_array($group)) {
    $node = node_load($group['gid']);
  }
  else {
    $node = $group;
  }
  if (!og_is_group_type('node', $node->type)) {
    return FALSE;
  }

  // If high access, just return
  if (user_access('administer shoutbox_group')) {
    return TRUE;
  }

  // Load the user
  if (!$user) {
    global $user;
  }
  else {
    if (is_numeric($user)) {
      $user = user_load($user);
    }
  }

  // Check the operation specified
  switch ($op) {
    case 'view':

      // Determine node view access
      return node_access('view', $group, $user);
    case 'post':

      // First check higher access
      if (user_access('post shouts in all groups')) {
        return TRUE;
      }

      // Determine group membership
      return og_is_member('group', $group->gid);
    default:
      return FALSE;
  }
}

/**
 * Implementation of hook_node_delete()
 * 
 * If a group is deleted, group shouts and references must be purged!
 */
function shoutbox_group_node_delete($node) {

  // If the node acts as a group
  if (og_is_group_type('node', $node->type)) {

    // Gather shout ID's
    $shouts = db_query("SELECT shout_id FROM {shoutbox_groups} WHERE nid = %d", $node->nid);

    // Delete all of the shouts
    while ($shout = db_fetch_object($shouts)) {

      // Hook thrown in shoutbox_delete_shout() will delete group reference as well
      shoutbox_delete_shout($shout->shout_id);
    }
  }
}

Functions

Namesort descending Description
shoutbox_group_get_group Determined by og_get_group_context for the block, or URL arg's for the page
shoutbox_group_group_access Determine if current user can view the current group
shoutbox_group_js_view Javascript callback to handle auto-update shouts
shoutbox_group_menu Implementation of hook_menu().
shoutbox_group_node_delete Implementation of hook_node_delete()
shoutbox_group_og_links_alter Implementation of hook_og_links_alter()
shoutbox_group_page Generate dedicated shoutbox page for group
shoutbox_group_permission Implementation of hook_perm().
shoutbox_group_query_shouts_alter Implementation of hook_query_alter()
shoutbox_group_shoutbox Implementation of hook_shoutbox()
_shoutbox_group_alter_form Alter the shoutbox add form Add the group ID to the shout form (if one)