You are here

function shoutbox_display_posts in Shoutbox 7

Same name and namespace in other branches
  1. 6.2 shoutbox.module \shoutbox_display_posts()
  2. 7.2 shoutbox.module \shoutbox_display_posts()

Output existing shoutbox posts as html.

Parameters

$show_amount: The number of posts to show.

$wrap: Whether or not to wrap posts in <div id="shoutbox-posts">

$pager: Whether or not to use pager_query() instead of db_query_range(), defaults to FALSE.

Return value

An array containing the count, an array of themed rows, all shouts themed not in a table, and an array of raw shouts

2 calls to shoutbox_display_posts()
shoutbox_js_view in ./shoutbox.module
Javascript callback.
shoutbox_view in ./shoutbox.module
View the shoutbox.

File

./shoutbox.module, line 776
Shoutbox module displays a block for users to create short messages for the whole site. Uses AHAH to update the database and display content.

Code

function shoutbox_display_posts($show_amount, $pager = FALSE) {
  $posts = array();
  $rows = array();

  // Figure out if we should display it in ascending or descending order.
  $ascending = variable_get('shoutbox_ascending', TRUE);

  // Allow other modules to alter the query.
  $query = db_select('shoutbox', 's')
    ->orderBy('created', 'DESC')
    ->fields('s')
    ->addTag('shouts');

  // We use context to get the meta data to be used for the query.
  $contexts = array();
  $blank = new stdClass();
  shoutbox_invoke('context', $blank, $contexts);
  foreach ($contexts as $key => $metaData) {
    $query
      ->addMetaData($key, $metaData);
  }

  // TODO: (disterics) if no shoutbox modules are enabled
  // we can speed up the query by removing the condition.
  if (variable_get('shoutbox_restrict_general_shouts', 1) && empty($contexts)) {
    $query
      ->condition('s.module', 'shoutbox', '=');
  }
  $delta = 0;
  $weight = $show_amount + $delta;

  // Add the post to the output either ascending or descending.
  // Use the delta to manage the weight for asceneding or descending.
  if ($ascending) {
    $delta = 1;
  }
  else {
    $delta = -1;
  }

  // TODO - Make sure modules honor tags.
  // TODO - What about contexts?
  if (!$pager) {
    $query
      ->range(0, $show_amount);
  }
  else {
    $query = $query
      ->extend('PagerDefault')
      ->limit($show_amount);
  }
  $result = $query
    ->execute();
  foreach ($result as $shout) {
    if ($shout->moderate == 0 || _shoutbox_user_access('moderate shoutbox') || _shoutbox_is_user_owned($shout)) {

      // Filter shout.
      _shoutbox_sanitize_shout($shout);

      // Add edit/delete links depending on user's permissions.
      $shoutlinks = _shoutbox_get_links($shout);

      //replace /n lines with <br/>
      $shout->shout = nl2br($shout->shout);

      // Allow other modules to alter the shout before it's viewed.
      shoutbox_invoke('view', $shout);

      // Theme the shoutbox post.
      $post = array(
        '#theme' => 'shoutbox_post',
        '#shout' => $shout,
        '#links' => $shoutlinks,
        '#weight' => $weight,
      );

      // Store the raw shout.
      $rows[] = $post;
      $weight += $delta;
    }
  }
  $posts['#theme'] = 'shoutbox_page';
  $posts['#rows'] = $rows;
  return $posts;
}