View source
<?php
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 = '';
public $m_count = 0;
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;
function __construct($data = null) {
if (isset($data)) {
$this
->set_data($data);
}
}
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;
}
}
if (isset($data['variables']) && is_string($data['variables'])) {
$this->m_variables_string = $data['variables'];
}
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']);
}
public function __get($variable) {
$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;
}
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 save($raw_args = array()) {
if (module_exists('locale')) {
return $this
->save_locale($raw_args);
}
else {
return $this
->_save($raw_args);
}
}
private function save_locale($raw_args = array()) {
$logged = FALSE;
$args = $this
->rebuild_arguments($raw_args, true);
$locale = $args['locale'];
unset($args['locale']);
$languages = locale_language_list();
foreach ($languages as $language => $human_language) {
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;
}
private function _save($raw_args = array()) {
$args = $this
->rebuild_arguments($raw_args);
return $this
->log_message($args);
}
private function log_message($args, $lang = '') {
if ($lang == '') {
global $language;
$lang = $language->language;
}
$message = t($this->m_message, $args, $lang);
$message_concat = t($this->m_message_concat, $args, $lang);
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;
}
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);
}
private function rebuild_arguments($raw_input, $locale = false) {
$args = array();
if ($locale) {
$args['locale'] = array();
}
foreach ($this->m_variables_array as $key => $value) {
if ($key[0] != "@" && $key[0] != "#") {
continue;
}
$oldkey = $key;
if ($key[0] == "@") {
$key[0] = "!";
}
if ($key[0] == "#") {
if ($locale) {
$args['locale'][$key] = $value;
}
$key[0] = "!";
}
if (isset($raw_args[$oldkey])) {
$args[$key] = $raw_args[$oldkey];
continue;
}
$args[$key] = $value;
}
return $args;
}
}