You are here

class HeartbeatActivity in Heartbeat 6.3

Same name and namespace in other branches
  1. 6.4 includes/heartbeatactivity.inc \HeartbeatActivity
  2. 7 includes/heartbeatactivity.inc \HeartbeatActivity

Class to handle user activity data

Hierarchy

Expanded class hierarchy of HeartbeatActivity

File

includes/heartbeatactivity.inc, line 8

View source
class HeartbeatActivity {
  public $m_uaid = 0;
  public $m_uid = 0;
  public $m_uid_target = 0;
  public $m_nid_target = 0;
  public $m_hid = 0;
  public $m_access = HEARTBEAT_PUBLIC_TO_ALL;
  public $m_description = '';
  public $m_message_id = '';
  public $m_message = '';
  public $m_message_concat = '';

  // count for same instance
  public $m_count = 0;

  // count for same instance that is merged by target
  public $m_target_count = 0;
  public $m_concat_args = array();
  public $m_variables = array();
  public $m_variables_array = array();
  public $m_variables_string = '';
  public $message_type = '';
  public $module = '';
  public $perms = 0;

  /**
   * constructor
   */
  function __construct($data = null) {
    if (isset($data)) {
      $this
        ->set_data($data);
    }
  }

  /**
   * Set data into members
   */
  public function set_data($data) {
    if (is_object($data)) {
      $data = (array) $data;
    }
    foreach ($data as $key => $value) {
      if (isset($this->{$key})) {
        $this->{$key} = $value;
      }
      if (isset($this->{'m_' . $key})) {
        $this->{'m_' . $key} = $value;
      }
    }

    // Data variables are more complicated
    if (isset($data['variables']) && is_string($data['variables'])) {
      $this->m_variables_string = $data['variables'];
    }

    // if the data variables have not been included
    // as normal members, do so now to be available when asked for
    // @see __get
    if ($this->m_variables_array == array() && $this->m_variables_string != '') {
      $this->m_variables_array = heartbeat_decode_message_variables($this->m_variables_string);
    }
    $this->m_concat_args = heartbeat_decode_message_variables($data['concat_args']);
  }

  /**
   * Method gets a heartbeat_activity variable
   *
   * @desc The magic getter method fetches a variable
   *       in members. If not found there, it will always
   *       check the variables as well
   */
  public function __get($variable) {

    // a private member is asked
    $var = null;
    if (property_exists($this, 'm_' . $variable)) {
      $var = $this->{'m_' . $variable};
    }
    else {
      if (property_exists($this, $variable)) {
        $var = $this->{$variable};
      }
      else {
        if (array_key_exists($variable, $this->m_variables_array)) {
          $var = $this->m_variables_array[$variable];
        }
      }
    }
    return $var;
  }

  /**
   * Method sets a heartbeat_activity property
   *
   * @desc The magic setter method sets a variable to member
   *  If not found, just stash in the variables array
   */
  public function __set($variable, $value) {
    if (property_exists($this, 'm_' . $variable)) {
      $this->{'m_' . $variable} = $value;
    }
    else {
      if (property_exists($this, $variable)) {
        $this->{$variable} = $value;
      }
      else {
        $this->m_variables_array[$variable] = $value;
      }
    }
  }

  /**
   * Public function to save activity to database
   * @param array raw argument to enforce as is (pre-renderd)
   */
  public function save($raw_args = array()) {
    if (module_exists('locale')) {
      return $this
        ->save_locale($raw_args);
    }
    else {
      return $this
        ->_save($raw_args);
    }
  }

  /**
   * Saves logs per language
   */

  /**
   * Save activity log with multilingual content
   * and multilingual parts to pre-translate
   *
   * @param array $raw_args
   */
  private function save_locale($raw_args = array()) {
    $logged = FALSE;
    $args = $this
      ->rebuild_arguments($raw_args, true);
    $locale = $args['locale'];
    unset($args['locale']);

    // Save activity by logging a row for each active language
    // Translations only when locale exists
    $languages = locale_language_list();
    foreach ($languages as $language => $human_language) {

      // preprocess multilingual message "parts"
      // for all flagged token replacements
      foreach ($this->m_variables_array as $key => $value) {
        if (isset($locale[$key])) {
          $amp_token = str_replace("#", "!", $key);
          $args[$amp_token] = locale($locale[$key], $language);
        }
      }
      $logged = $this
        ->log_message($args, $language);
    }
    return $logged;
  }

  /**
   * Save activity log
   *
   * @param array $raw_args
   */
  private function _save($raw_args = array()) {

    // Rebuild arguments with tokens
    $args = $this
      ->rebuild_arguments($raw_args);
    return $this
      ->log_message($args);
  }

  /**
   * Logs a heartbeat message
   * @param string language optional
   *
   */
  private function log_message($args, $lang = '') {
    if ($lang == '') {
      global $language;
      $lang = $language->language;
    }

    // Mis-usage of the t-function, but how could i fix this?
    $message = t($this->m_message, $args, $lang);
    $message_concat = t($this->m_message_concat, $args, $lang);

    // Checks if there should be logging what so ever
    if (empty($message)) {
      watchdog('heartbeat', 'Error in logging user activity: it is not possible to log empty message', array(), WATCHDOG_ERROR);
      return false;
    }
    if (!is_numeric($this->m_uid) || $this->m_uid <= 0) {
      watchdog('heartbeat', 'Error in logging user activity: no information on the user invoking heartbeat activity', array(), WATCHDOG_ERROR);
      return false;
    }

    //dsm($message);

    // Log relational message to user activity
    return db_query("INSERT INTO {heartbeat_activity} SET uid=%d, uid_target=%d, nid_target=%d, message_id='%s', language='%s',\n    access = %d, message ='%s',message_concat ='%s', timestamp=%d, variables='%s'", $this->m_uid, $this->m_uid_target, $this->m_nid_target, $this->m_message_id, $lang, $this->m_access, $message, $message_concat, $_SERVER['REQUEST_TIME'], $this->m_variables_string);
  }

  /**
   * Rebuild the arguments for variables
   * to share within this object
   *
   * @param array $raw_input of arguments
   */
  private function rebuild_arguments($raw_input, $locale = false) {
    $args = array();
    if ($locale) {

      // Variables that need to be pre-translated go here
      $args['locale'] = array();
    }

    // Rebuild arguments with language tokens
    foreach ($this->m_variables_array as $key => $value) {

      // Leave $key[0] == "!"  asis
      if ($key[0] != "@" && $key[0] != "#") {
        continue;

        // bad argument
      }
      $oldkey = $key;

      // Reset the key of the arguments to ! to parse the next
      // tokenization asis.
      if ($key[0] == "@") {
        $key[0] = "!";
      }

      // # and @ token replacement prefixes are kept,
      // but set a flag for it in the raw_arguments
      if ($key[0] == "#") {

        //dsm($key.' '.$value);

        // if it has to be translated ...
        if ($locale) {
          $args['locale'][$key] = $value;
        }

        // Now reset the key
        $key[0] = "!";
      }

      // if argument is prefilled, override
      if (isset($raw_args[$oldkey])) {
        $args[$key] = $raw_args[$oldkey];
        continue;
      }

      // Argument gets the value as in variables
      $args[$key] = $value;
    }
    return $args;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HeartbeatActivity::$message_type public property
HeartbeatActivity::$module public property
HeartbeatActivity::$m_access public property
HeartbeatActivity::$m_concat_args public property
HeartbeatActivity::$m_count public property
HeartbeatActivity::$m_description public property
HeartbeatActivity::$m_hid public property
HeartbeatActivity::$m_message public property
HeartbeatActivity::$m_message_concat public property
HeartbeatActivity::$m_message_id public property
HeartbeatActivity::$m_nid_target public property
HeartbeatActivity::$m_target_count public property
HeartbeatActivity::$m_uaid public property
HeartbeatActivity::$m_uid public property
HeartbeatActivity::$m_uid_target public property
HeartbeatActivity::$m_variables public property
HeartbeatActivity::$m_variables_array public property
HeartbeatActivity::$m_variables_string public property
HeartbeatActivity::$perms public property
HeartbeatActivity::log_message private function Logs a heartbeat message
HeartbeatActivity::rebuild_arguments private function Rebuild the arguments for variables to share within this object
HeartbeatActivity::save public function Public function to save activity to database
HeartbeatActivity::save_locale private function Save activity log with multilingual content and multilingual parts to pre-translate
HeartbeatActivity::set_data public function Set data into members
HeartbeatActivity::_save private function Save activity log
HeartbeatActivity::__construct function constructor
HeartbeatActivity::__get public function Method gets a heartbeat_activity variable
HeartbeatActivity::__set public function Method sets a heartbeat_activity property