You are here

protected function HeartbeatStream::createQuery in Heartbeat 7

createQuery().

This query is the most important preparation to get the messages in a stream. Note that the access logged at the time of activity "ha.access" as well as the access at display time "hut.status". The perms property in the template is not used by design. This is something to discuss. @todo Discuss

Parameters

$text:

Return value

HeartbeatParser object

1 call to HeartbeatStream::createQuery()
HeartbeatStream::result in includes/heartbeatstream.inc
result Prepares a query, makes it available to alter it and finally executes it.
1 method overrides HeartbeatStream::createQuery()
SingleActivity::createQuery in streams/singleactivity.inc
Override the createQuery method.

File

includes/heartbeatstream.inc, line 592
HeartbeatStream object is the object that takes stream configuration to create a stream of activity objects. It is the controlling organ at the pre-query, query and post-query phases.

Class

HeartbeatStream
Abstract class HeartbeatStream This base class has final template methods which are used by the derived concretes. The HeartbeatStream is a state object that is given to the HeartbeatStreamBuilder to set the access to the current request.

Code

protected function createQuery() {
  $this->query = db_select('heartbeat_activity', 'ha')
    ->extend('PagerActivity');
  $this->query
    ->fields('ha', array(
    'uaid',
    'message_id',
    'uid',
    'access',
  ));

  //    $this->query->addField('ha', 'variables', 'variables');
  //    $this->query->addField('ha', 'access', 'access');
  // Override the permission based on the user profile status.
  $this->query
    ->leftJoin('heartbeat_user_templates', 'hut', ' ha.uid = hut.uid AND ha.message_id = hut.message_id ');
  $this->query
    ->addField('hut', 'status', 'access_status');

  // The user_access field holds the least access known. (NULL = HEARTBEAT_PUBLIC_TO_ALL).
  $access_expression = "LEAST ( IFNULL ( ha.access, " . HEARTBEAT_PUBLIC_TO_ALL . "), IFNULL ( hut.status, " . HEARTBEAT_PUBLIC_TO_ALL . "))";
  $this->query
    ->addExpression($access_expression, 'user_access');

  // Exclude unpublished comments.
  if (module_exists('comment')) {
    $this->query
      ->leftJoin('comment', 'c', 'ha.cid = c.cid');
    $this->query
      ->where("c.status = 1 OR ha.cid = 0");
  }

  // Include only the required language(s).
  $db_or = db_or();
  foreach ($this
    ->getCurrentLanguages() as $lang_code) {
    $db_or
      ->condition('ha.language', $lang_code, "=");
  }
  if ($db_or
    ->count()) {
    $this->query
      ->conditions($db_or);
  }

  // Condition on time of creation.
  if ($this->latest_activity_id > 0) {

    // Calls with an offset uaid will fetch newer messages
    $this->query
      ->setLastActivityId($this->latest_activity_id);
  }
  else {

    // Otherwise we'll set oldest and newest date.
    $this->query
      ->setOffsetTime($this->_offset_time, $this->oldest_date);
  }

  // Condition to exclude organic groups specific messages.
  if ($this->exclude_og) {
    $this->query
      ->condition('ha.in_group', 1, '<>');
  }

  // Prepare "OR" clause for logical activity access conditions.
  $or = db_or();

  // Include messages where the actor addressed the viewer.
  $or
    ->condition(db_and()
    ->where($access_expression . " = " . HEARTBEAT_PUBLIC_TO_ADDRESSEE)
    ->condition(db_or()
    ->condition(db_and()
    ->condition('ha.uid_target', $this->viewer->uid)
    ->condition('ha.uid_target', 0, '!='))
    ->condition(db_and()
    ->condition('ha.uid', $this->viewer->uid)
    ->condition('ha.uid', 0, '!='))));

  // Include messages where the actor is somehow connected to the viewer.
  $this->viewer->relations = heartbeat_related_uids($this->viewer->uid);
  $or
    ->condition(db_and()
    ->where($access_expression . " >= " . HEARTBEAT_PUBLIC_TO_CONNECTED)
    ->condition(db_or()
    ->condition('ha.uid', $this->viewer->relations, 'IN')
    ->condition('ha.uid_target', $this->viewer->relations, 'IN')));

  // Include messages that are public to everyone.

  //$or->condition('ha.access', HEARTBEAT_PUBLIC_TO_ALL, '=');
  $or
    ->where($access_expression . " = " . HEARTBEAT_PUBLIC_TO_ALL);

  // Include message where the viewer is the actor and sees all own activity.
  if (!$this
    ->skipActiveUser()) {

    // Include private messages.
    $or
      ->condition(db_and()
      ->condition("ha.uid", $this->viewer->uid)
      ->condition("ha.access", HEARTBEAT_PRIVATE, '>='));
  }
  else {
    $this->query
      ->condition('ha.uid', $this->viewer->uid, '!=');
  }
  if (count($or
    ->conditions())) {
    $this->query
      ->condition($or);
  }

  // Exclude denied message templates.
  if (!empty($this->templates_denied)) {
    $this->query
      ->where("ha.message_id NOT IN (:messages)", array(
      ':messages' => array_unique($this->templates_denied),
    ));
  }

  // Order by.
  $this->query
    ->orderBy('ha.timestamp', 'DESC')
    ->limit($this->config->num_load_max);
  $this->query
    ->orderBy('ha.uaid', 'DESC');
}