You are here

class NodeActivityActionHandler in Activity 7

Activity handler for node module.

Hierarchy

Expanded class hierarchy of NodeActivityActionHandler

1 string reference to 'NodeActivityActionHandler'
node_activity_api in ./activity.module
Implements hook_activity_api().

File

./activity_action_handlers.inc, line 360

View source
class NodeActivityActionHandler extends ActivityActionHandler {

  /**
   * Return the eid field for this Activity.
   *
   * @param array $context
   *   The context argument passed into the action callback.
   *
   * @return int
   */
  public function determineEid($context) {
    return $context['node']->nid;
  }

  /**
   * Return the uid that is responsible for this action.
   *
   * @param array $objects
   *  The objects used in tokenization
   *
   * @return int
   */
  public function determineActor($objects) {
    if ($this->type == 'node_insert') {
      return $objects['node']->uid;
    }
    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) {
    if ($this->type == 'node_insert') {
      return $objects['node']->created;
    }
    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) {
    return parent::isPublished($objects, $actor) && $objects['node']->status;
  }

  /**
   * Load objects for tokenization.
   *
   * @param int $eid
   *   The entity identifier for this Activity.
   *
   * @return array
   */
  public function loadObjects($eid) {
    $objects['node'] = node_load($eid);
    return $objects;
  }

  /**
   * Return option defaults and structure.
   *
   * @return array.
   */
  public function optionDefinition() {
    $options = parent::optionDefinition();
    $options['types'] = array(
      '#default_value' => array(),
    );
    $options['view_modes'] = array(
      '#default_value' => array(),
    );
    return $options;
  }

  /**
   * Display an FAPI form.
   *
   * @param &$form
   *   An FAPI form array.
   * @param $form_state
   *   The form_state from FAPI.
   */
  public function optionForm(&$form, $form_state) {
    parent::optionForm($form, $form_state);
    $node_types = array();
    foreach (node_type_get_types() as $type) {
      $node_types[$type->type] = check_plain($type->name);
    }
    $form['types'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Allowed Node Types'),
      '#options' => $node_types,
      '#default_value' => $this->options['types'],
    );
    $node_entity_info = entity_get_info('node');
    $view_mode_options = array();
    foreach ($node_entity_info['view modes'] as $mode => $information) {
      $view_mode_options[$mode] = $information['label'];
    }

    // This is a node_view specific option.
    $form['view_modes'] = array(
      '#type' => 'checkboxes',
      '#title' => t('View Modes'),
      '#options' => $view_mode_options,
      '#default_value' => $this->options['view_modes'],
      '#access' => $this->type == 'node_view',
    );
  }

  /**
   * 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) {
    $types = array_filter($this->options['types']);
    $type_check = TRUE;
    $view_modes_check = TRUE;
    if (!empty($types)) {
      $type_check = in_array($objects['node']->type, $types);
    }
    $view_modes = array_filter($this->options['view_modes']);
    if (!empty($view_modes)) {

      // When the trigger is fired, the build mode is passed to the actions
      // callback as $a1. activity_record() takes the two arguments and places
      // them into the context array under the 'additional_arguments' key.
      // @see activity_record
      $view_modes_check = in_array($argument1, $view_modes);
    }
    return parent::valid($eid, $actor, $timestamp, $objects, $argument1, $argument2) && $type_check && $view_modes_check;
  }

  /**
   * Return an array of message types.
   */
  protected function messages() {
    $messages = parent::messages();
    $messages['node'] = array(
      'title' => 'Node Author',
      'description' => 'Author of the node or the user who updated the node',
    );
    if ($this->type == 'node_update') {

      // If node->uid == $GLOBALS['user']->uid, this message template will be
      // used instead of $messages['node'].
      // This key 'current_user' is a fake one, and its existance is handled in
      // getUid() method.
      $messages['current_user'] = array(
        'title' => 'Updating User',
        'description' => 'The user who actually updated the node',
      );
    }
    elseif ($this->type == 'node_view') {
      $messages['current_user'] = array(
        'title' => 'Current User',
        'description' => 'The user viewing the node',
      );
    }
    return $messages;
  }

  /**
   * 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) {
    $types = array_filter($this->options['types']);

    // Because the op view currently isn't batchable, it can be skipped.
    $query = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ), 'eid');
    if (!empty($types)) {
      $query
        ->condition('type', $types, 'IN');
    }
    $count_query = clone $query;
    $total = $count_query
      ->countQuery()
      ->execute()
      ->fetchField();
    $query
      ->range($offset, $limit);
    $arguments = array();
    foreach ($query
      ->execute()
      ->fetchCol() as $eid) {
      $arguments[$eid] = array(
        'argument1' => NULL,
        'argument2' => NULL,
      );
    }
    return array(
      'total' => $total,
      'arguments' => $arguments,
    );
  }

}

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::determineNid public function Return the nid of this Activity.
ActivityActionHandler::getUid protected function Return the user id that matches the provided key in the templates. 1
ActivityActionHandler::messagesForm public function Display the token message form.
ActivityActionHandler::tokenize public function Tokenize based on the provided objects
NodeActivityActionHandler::determineActor public function Return the uid that is responsible for this action. Overrides ActivityActionHandler::determineActor 1
NodeActivityActionHandler::determineEid public function Return the eid field for this Activity. Overrides ActivityActionHandler::determineEid 1
NodeActivityActionHandler::determineTimestamp public function Return the timestamp that this Activity happened at. Overrides ActivityActionHandler::determineTimestamp 1
NodeActivityActionHandler::isPublished public function Return whether or not the Activity is published. Overrides ActivityActionHandler::isPublished 1
NodeActivityActionHandler::listEids public function List all eids for this handler, used for batch regeneration and backfilling. Overrides ActivityActionHandler::listEids 1
NodeActivityActionHandler::loadObjects public function Load objects for tokenization. Overrides ActivityActionHandler::loadObjects 1
NodeActivityActionHandler::messages protected function Return an array of message types. Overrides ActivityActionHandler::messages 1
NodeActivityActionHandler::optionDefinition public function Return option defaults and structure. Overrides ActivityActionHandler::optionDefinition
NodeActivityActionHandler::optionForm public function Display an FAPI form. Overrides ActivityActionHandler::optionForm
NodeActivityActionHandler::valid public function Determine if the current Action is valid for this object. Overrides ActivityActionHandler::valid