You are here

abstract class ActivityActionHandler in Activity 7

Hierarchy

Expanded class hierarchy of ActivityActionHandler

File

./activity_action_handlers.inc, line 8

View source
abstract class ActivityActionHandler {

  /**
   * Templates created by the administrator.
   *
   * @var array
   */
  public $templates = array();

  /**
   * Options provided via the optionsFrom and optionsDefinition methods.
   *
   * @var array
   */
  public $options = array();

  /**
   * Type of activity.
   *
   * @var string
   */
  public $type = 'activity';

  /**
   * {actions}.aid for the template.
   *
   * @var integer
   */
  public $actions_id = 0;

  /**
   * {actions}.label for the template.
   *
   * @var string
   */
  public $label;

  /**
   * Boolean indicating if this handler can do batch operations.
   *
   * @var boolean
   */
  public $batch;

  /**
   * Generates the default options from the provided option definition.
   *
   * @param array $definition
   *   Option definition from the handler.
   *
   * @return array
   *   Default options.
   */
  public static function defaultOptions(array $definition) {
    $accumlator = array();
    foreach (element_children($definition) as $child) {
      if (isset($definition[$child]['#default_value'])) {
        $accumlator[$child] = $definition[$child]['#default_value'];
      }
      else {
        $grand_children = element_children($definition[$child]);
        if (!empty($grand_children)) {
          $accumlator[$child] = self::defaultOptions($definition[$child]);
        }
      }
    }
    return $accumlator;
  }

  /**
   * Load objects for tokenization.
   *
   * @param int $eid
   *   The entity identifier for this Activity.
   *
   * @return array
   */
  public abstract function loadObjects($eid);

  /**
   * Return option defaults and structure.
   *
   * @return array.
   */
  public function optionDefinition() {
    return array(
      'window' => array(
        '#default_value' => 0,
      ),
    );
  }

  /**
   * Display an FAPI form.
   *
   * @param &$form
   *   An FAPI form array.
   * @param $form_state
   *   The form_state from FAPI.
   */
  public function optionForm(&$form, $form_state) {
    $options = drupal_map_assoc(range(0, 7200, 300), 'format_interval');
    $options[0] = t('Unlimited');
    $form['window'] = array(
      '#type' => 'select',
      '#title' => t('Activity Window'),
      '#description' => t('Prevent repeat Activity from the same user for the same entity within this interval'),
      '#options' => $options,
      '#default_value' => $this->options['window'],
    );
  }

  /**
   * Determine if the current Action is valid for this object.
   *
   * @param $eid
   *   The entity id for the activity.
   * @param $actor
   *   The uid of the actor for this activity.
   * @param $timestamp
   *   Unix timestamp when this activity occurred.
   * @param array $objects
   *   The collection of objects for this action.
   * @param mixed $argument1
   *   The first argument passed to the action callback.
   * @param mixed $argument2
   *   The second argument passed to the action callback.
   *
   * @return boolean
   */
  public function valid($eid, $actor, $timestamp, $objects, $argument1, $argument2) {
    if (!empty($this->options['window'])) {
      $count = db_select('activity', 'a')
        ->fields('a', array(
        'aid',
      ))
        ->condition('eid', $eid, '=')
        ->condition('uid', $actor, '=')
        ->condition('created', $timestamp - $this->options['window'], '>')
        ->condition('actions_id', $this->actions_id, '=')
        ->countQuery()
        ->execute()
        ->fetchField();
      return $count == 0;
    }
    return TRUE;
  }

  /**
   * Display the token message form.
   *
   * @param &$form
   *   The FAPI form.
   * @param $form_state
   *   The state of the form.
   */
  public function messagesForm(&$form, $form_state) {
    $messages = $this
      ->messages() + array(
      'public' => array(
        'title' => 'Public Message',
        'description' => 'Message displayed to everyone who is not a part of this Activity',
      ),
    );
    foreach (activity_enabled_languages() as $id => $language) {
      $form[$id] = array(
        '#type' => 'fieldset',
        '#title' => t('@name messages', array(
          '@name' => $language->name,
        )),
      );
      foreach ($messages as $object_key => $information) {
        $current_message = '';
        if (isset($this->templates[$id]) && isset($this->templates[$id][$object_key])) {
          $current_message = $this->templates[$id][$object_key];
        }
        $form[$id][$object_key] = array(
          '#type' => 'textarea',
          '#title' => t($information['title']),
          '#description' => t($information['description']),
          '#default_value' => $current_message,
        );
      }
    }

    // The token.module provides the UI for the tokens. While not required,
    // it adds a nice UI.
    if (module_exists('token')) {
      $form['token_help'] = array(
        '#theme' => 'token_tree',
        '#token_types' => array_keys($messages),
      );
    }
  }

  /**
   * Return an array of message types.
   */
  protected function messages() {
    return array();
  }

  /**
   * Tokenize based on the provided objects
   *
   * @param array $objects
   *  The objects used in the tokenization process.
   *
   * @return array
   *  An array of messages keyed by language and uid.
   */
  public function tokenize($objects) {
    $messages = array();
    $languages = array();
    foreach ($this->templates as $language_id => $language_patterns) {
      foreach ($language_patterns as $target_key => $text) {
        $uid = $this
          ->getUid($target_key, $objects);
        if ($uid !== FALSE) {
          $options = array(
            'clear' => TRUE,
            'sanitize' => FALSE,
          );
          if (drupal_multilingual()) {
            if (empty($languages)) {
              $languages = language_list();
            }
            $options['language'] = $languages[$language_id];
          }
          $message = token_replace($text, $objects, $options);
          if (!empty($message)) {
            $messages[$language_id][$uid] = $message;
          }
        }
      }
    }
    return $messages;
  }

  /**
   * Return the user id that matches the provided key in the templates.
   *
   * @param string $key
   *   The message key.
   * @param array $objects
   *   All the objects for the Activity.
   *
   * @return int
   */
  protected function getUid($key, $objects) {
    if ($key == 'public') {
      return 0;
    }
    elseif ($key == 'current_user') {
      return $GLOBALS['user']->uid;
    }
    if (isset($objects[$key]) && isset($objects[$key]->uid)) {
      return $objects[$key]->uid;
    }
    return FALSE;
  }

  /**
   * Return the nid of this Activity.
   *
   * @param array $objects
   *  An array of objects used in tokenization.
   *
   * @return int / NULL
   */
  public function determineNid($objects) {
    $nid = NULL;
    if (isset($objects['node']->nid)) {
      $nid = $objects['node']->nid;
    }

    // NOTICE: no elseif(). This is because if the comment is part of the
    // objects, use that as the basis for the nid. Doubt that will ever be an
    // issue.
    if (isset($objects['comment']->nid)) {
      $nid = $objects['comment']->nid;
    }
    return $nid;
  }

  /**
   * Return the eid field for this Activity.
   *
   * @param array $context
   *  The context array passed into the action callback.
   *
   * @return int / NULL
   */
  public abstract function determineEid($context);

  /**
   * Return the uid that is responsible for this action.
   *
   * @param array $objects
   *  The objects used in tokenization
   *
   * @return int
   */
  public function determineActor($objects) {
    return $GLOBALS['user']->uid;
  }

  /**
   * Return the timestamp that this Activity happened at.
   *
   * @param array $objects
   *   The objects used in tokenization.
   *
   * @return int
   */
  public function determineTimestamp($objects) {
    return REQUEST_TIME;
  }

  /**
   * Return whether or not the Activity is published.
   *
   * @param array $objects
   *   The objects used in tokenization.
   * @param int $actor
   *   The uid of the actor for this activity.
   */
  public function isPublished($objects, $actor) {

    // Key is to determine if the Actor is not blocked.
    if ($actor == 0) {

      // Anon user is blocked by default and cannot be unblocked. i.e their
      // status can never and will never change, always being 0. Thus, anon
      // gets a pass through.
      return TRUE;
    }
    $status = db_query("SELECT status FROM {users} WHERE uid = :uid", array(
      ":uid" => $actor,
    ))
      ->fetchField();
    return $status == 1;
  }

  /**
   * List all eids for this handler, used for batch regeneration and backfilling.
   *
   * @param int $offset
   *   The offset for the query.
   * @param int $limit
   *   The limit for the query.
   */
  public function listEids($offset, $limit) {
    return array(
      'total' => 0,
      'arguments' => array(),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActivityActionHandler::$actions_id public property {actions}.aid for the template.
ActivityActionHandler::$batch public property Boolean indicating if this handler can do batch operations.
ActivityActionHandler::$label public property {actions}.label for the template.
ActivityActionHandler::$options public property Options provided via the optionsFrom and optionsDefinition methods.
ActivityActionHandler::$templates public property Templates created by the administrator.
ActivityActionHandler::$type public property Type of activity.
ActivityActionHandler::defaultOptions public static function Generates the default options from the provided option definition.
ActivityActionHandler::determineActor public function Return the uid that is responsible for this action. 2
ActivityActionHandler::determineEid abstract public function Return the eid field for this Activity. 2
ActivityActionHandler::determineNid public function Return the nid of this Activity.
ActivityActionHandler::determineTimestamp public function Return the timestamp that this Activity happened at. 2
ActivityActionHandler::getUid protected function Return the user id that matches the provided key in the templates. 1
ActivityActionHandler::isPublished public function Return whether or not the Activity is published. 2
ActivityActionHandler::listEids public function List all eids for this handler, used for batch regeneration and backfilling. 2
ActivityActionHandler::loadObjects abstract public function Load objects for tokenization. 2
ActivityActionHandler::messages protected function Return an array of message types. 2
ActivityActionHandler::messagesForm public function Display the token message form.
ActivityActionHandler::optionDefinition public function Return option defaults and structure. 2
ActivityActionHandler::optionForm public function Display an FAPI form. 2
ActivityActionHandler::tokenize public function Tokenize based on the provided objects
ActivityActionHandler::valid public function Determine if the current Action is valid for this object. 2