You are here

class Message in Message 7

@file A class used for messages.

Hierarchy

Expanded class hierarchy of Message

11 string references to 'Message'
MessageArgumentsTestCase::getInfo in tests/MessageArgumentsTestCase.test
MessageCron::getInfo in tests/message.test
MessageCrud::getInfo in tests/message.test
MessageEntityDelete::getInfo in tests/message.test
MessageRulesIntegrationTestCase::getInfo in tests/message.test

... See full list

File

includes/message.message.inc, line 9
A class used for messages.

View source
class Message extends Entity {

  /**
   * The message type category of the message.
   *
   * @var string
   */
  public $type;

  /**
   * The message timestamp.
   *
   * @var string
   */
  public $timestamp;

  /**
   * The message arguments.
   *
   * @see MessageType::arguments()
   *
   * @var array
   */
  public $arguments = array();

  /**
   * The message data array.
   *
   * @see MessageType::data()
   *
   * @var array
   */
  public $data = array();
  public function __construct($values = array()) {
    if (!isset($values['uid']) && isset($values['user'])) {
      $values['uid'] = $values['user']->uid;
      unset($values['user']);
    }
    if (isset($values['type']) && is_object($values['type'])) {
      $values['type'] = $values['type']->name;
    }
    parent::__construct($values, 'message');
    if (!isset($this->uid)) {
      $this->uid = $GLOBALS['user']->uid;
    }
    if (!isset($this->timestamp)) {
      $this->timestamp = time();
    }
  }

  /**
   * Returns the user associated with the message.
   */
  public function user() {
    return user_load($this->uid);
  }

  /**
   * Sets a new user associated with the message.
   *
   * @param $account
   *   The user account object or the user account id (uid).
   */
  public function setUser($account) {
    $this->uid = is_object($account) ? $account->uid : $account;
  }

  /**
   * Gets the associated message type.
   *
   * @return MessageType
   */
  public function getType() {
    return message_type_load($this->type);
  }

  /**
   * {@inheritdoc}
   *
   * Generate an array for rendering the entity's content.
   *
   * Iterate over the extra field settings, and show the visible partials.
   *
   * @see entity_build_content()
   */
  public function buildContent($view_mode = 'full', $langcode = NULL) {

    // Get all the message text fields.
    $content = array();
    foreach (field_extra_fields_get_display('message', $this->type, $view_mode) as $key => $value) {
      if (!$value['visible']) {

        // Partial is hidden.
        continue;
      }

      // Field name might have double underscore as-well, so we need to
      // make sure we get it right. For this we inverse the string, and
      // exlpode the first double-underscores.
      // e.g. message__field-name__0
      $inverse = strrev($key);
      $argument = explode('__', $inverse, 2);
      if (count($argument) != 2) {
        continue;
      }
      $delta = strrev($argument[0]);

      // "message__" is 9 chars.
      $field_name = substr(strrev($argument[1]), 9);
      if (!is_numeric($delta)) {
        continue;
      }
      $field = field_info_field($field_name);
      if (empty($field['settings']['message_text'])) {
        continue;
      }
      $options = array(
        'partials' => TRUE,
        'partial delta' => $delta,
        'field name' => $field_name,
      );
      $content['message__' . $field_name . '__' . $delta] = array(
        '#markup' => $this
          ->getText($langcode, $options),
        '#weight' => $value['weight'],
      );
    }
    return entity_get_controller($this->entityType)
      ->buildContent($this, $view_mode, $langcode, $content);
  }

  /**
   * Replace arguments with their placeholders.
   *
   * @param $langcode
   *   Optional; The language to get the text in. If not set the current
   *   language will be used.
   * @param $options
   *   Optional; Array to be passed to MessageType::getText().
   *
   * @return mixed|string
   *   The message text compiled into a string or an array of the message
   *   partials.
   */
  public function getText($langcode = LANGUAGE_NONE, $options = array()) {
    $message_type = $this
      ->getType();
    if (!$message_type) {

      // Message type does not exist any more.
      return '';
    }
    $arguments = message_get_property_values($this, 'arguments');

    // Check if the current message have any arguments plugin.
    $handler = message_get_computed_arguments_handler($this);
    if ($handler && ($handler_arguments = $handler
      ->getArguments())) {
      $arguments = array_merge($arguments, $handler_arguments);
    }
    $output = $message_type
      ->getText($langcode, $options);
    if (!empty($arguments)) {
      $args = array();
      foreach ($arguments as $key => $value) {
        if (is_array($value) && !empty($value['callback']) && function_exists($value['callback'])) {

          // A replacement via callback function.
          $value += array(
            'pass message' => FALSE,
          );
          if ($value['pass message']) {

            // Pass the message object as-well.
            $value['callback arguments'][] = $this;
          }
          $value = call_user_func_array($value['callback'], $value['callback arguments']);
        }
        switch ($key[0]) {
          case '@':

            // Escaped only.
            $args[$key] = check_plain($value);
            break;
          case '%':
          default:

            // Escaped and placeholder.
            $args[$key] = drupal_placeholder($value);
            break;
          case '!':

            // Pass-through.
            $args[$key] = $value;
        }
      }
      $output = strtr($output, $args);
    }
    $token_replace = message_get_property_values($this, 'data', 'token replace', TRUE);
    if ($output && $token_replace) {

      // Message isn't explicetly denying token replace, so process the text.
      $context = array(
        'message' => $this,
      );
      $token_options = message_get_property_values($this, 'data', 'token options');
      $output = token_replace($output, $context, $token_options);
    }
    return $output;
  }

  /**
   * Implements Entity::save().
   *
   * Auto create arguments based on syntax.
   *
   * If in one of the message text fields there is a token in the
   * following syntax @{message:user:name}, message will check if it is a
   * valid token, and if so, will create a new arguments, e.g.:
   *
   * @code
   *   $message->arguments['@{message:user:name}'] = 'foo';
   * @endcode
   *
   * Like this, it is possible to hardcode a token value, upon the message
   * creation, without the need to retrieve the token value each time the
   * message is displayed.
   *
   * This can be used for example, to hardcode the user's name, assuming
   * it will not change on the site.
   */
  public function save() {
    if (empty($this->is_new) || !empty($this->data['skip token hardcode'])) {

      // Message isn't new, or message explicitly doesn't want token
      // hardcoding
      parent::save();
      return;
    }
    $context = array(
      'message' => $this,
    );
    $token_options = !empty($this->data['token options']) ? $this->data['token options'] : array();
    $message_type = $this
      ->getType();

    // Iterate over the text fields.
    $tokens = array();
    foreach (field_info_instances('message_type', $message_type->category) as $instance) {
      $field_name = $instance['field_name'];
      $field = field_info_field($field_name);
      if (!in_array($field['type'], array(
        'text',
        'text_long',
      ))) {

        // Not a text field.
        continue;
      }
      if (!($output = $message_type
        ->getText(NULL, array(
        'field name' => $field_name,
      )))) {

        // Field is empty.
        continue;
      }

      // Check for our hardcode syntax.
      $matches = array();
      preg_match_all('/[@|%|\\!]\\{([a-z0-9:_\\-]+?)\\}/i', $output, $matches);
      if (!$matches) {
        continue;
      }
      foreach ($matches[1] as $delta => $token) {
        $output = token_replace('[' . $token . ']', $context, $token_options);
        if ($output != '[' . $token . ']') {

          // Token was replaced.
          $argument = $matches[0][$delta];
          $tokens[$argument] = $output;
        }
      }
    }
    $this->arguments = array_merge($this->arguments, $tokens);
    parent::save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
Message::$arguments public property The message arguments.
Message::$data public property The message data array.
Message::$timestamp public property The message timestamp.
Message::$type public property The message type category of the message.
Message::buildContent public function Generate an array for rendering the entity's content. Overrides Entity::buildContent
Message::getText public function Replace arguments with their placeholders.
Message::getType public function Gets the associated message type.
Message::save public function Implements Entity::save(). Overrides Entity::save
Message::setUser public function Sets a new user associated with the message.
Message::user public function Returns the user associated with the message.
Message::__construct public function Overrides Entity::__construct