heartbeat.common.inc in Heartbeat 6.3
Same filename and directory in other branches
Commonly functions used in heartbeat
File
heartbeat.common.incView source
<?php
// $Id $
/**
* @file
* Commonly functions used in heartbeat
*/
/**
* Decode heartbeat message variables
*/
function heartbeat_decode_message_variables($string, $object = false) {
// Variable string need to be cleared from spaces to decode properly
$array = explode("-|-", $string);
$variables = array();
if (!empty($array)) {
foreach ($array as $varvalue) {
$parts = explode("=|=", $varvalue);
if (isset($parts[0]) && !empty($parts[0])) {
if (eregi("\\*\\*\\*", $parts[1])) {
$parts[1] = explode("***", $parts[1]);
}
$variables[$parts[0]] = !is_array($parts[1]) ? (string) $parts[1] : $parts[1];
}
}
}
//$variables = unserialize($string);
return $object ? (object) $variables : (array) $variables;
}
/**
* Encode heartbeat message variables
*/
function heartbeat_encode_message_variables($array) {
$string = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = implode('***', $value);
}
$string .= $key . '=|=' . $value . '-|-';
}
//$string = serialize((object)$array);
return $string;
}
/**
* compares variables that need to be included
* with variables that are already there and
* check their values to fit as default value
* in a textarea
*
* @param array $settings
* @param array $form_settings
* @param string $variables
* @return string fit for textarea
*/
function heartbeat_variables_compare($settings, $form_variables = null, $variables = array(), $flipped = true) {
$default_values = '';
// form_state first, while editing the form (changing message id for instance)
if (isset($form_variables)) {
$vars = heartbeat_decode_message_variables($form_variables, false);
}
else {
if (isset($settings['variables_param'])) {
$vars = heartbeat_decode_message_variables($settings['variables_param'], false);
}
else {
$vars = heartbeat_decode_message_variables($message->variables, false);
}
}
if ($flipped) {
$variables = array_flip($variables);
}
// See if there is already a token assignment on a variable
foreach ($variables as $variable_word => $key) {
// Standardize possible variable formats
$variable_normal = !eregi("@", $variable_word) ? "@" . $variable_word : $variable_word;
$variable_part = !eregi("#", $variable_word) ? "#" . $variable_word : $variable_word;
if (!empty($vars[$variable_normal])) {
$default_values .= $variable_normal . "=" . $vars[$variable_normal] . "\r\n";
}
elseif (!empty($vars[$variable_part])) {
$default_values .= $variable_part . "=" . $vars[$variable_part] . "\r\n";
}
elseif (!empty($vars[$variable_word])) {
$default_values .= $variable_normal . "=" . $vars[$variable_word] . "\r\n";
}
else {
$default_values .= $variable_normal . "=\r\n";
}
}
return $default_values;
}
/**
* Function looks for variables in an array of given strings
* @return array of words and variables found (separately stored)
* @remark
* %targetnames% do not have to be tokenized
*/
function heartbeat_match_variables() {
$args = func_get_args();
$string = '';
$string .= implode(' ', array_values($args));
preg_match_all("/\\!([a-zA-Z0-9_]+)/", $string, $matches);
$words = $matches[1];
$variables = $matches[0];
$found = array(
'words' => $words,
'variables' => $variables,
);
return $found;
}
/**
* Helper function
*/
function _heartbeat_messages_format_interval($messages) {
$content = '';
$date_format = variable_get("heartbeat_activity_date_format", "Y-m-d H:i:s");
$show_time = variable_get('heartbeat_show_message_times', 1);
$ago = t('ago');
$i = 0;
foreach ($messages as $key => $message) {
$message_date = format_interval($_SERVER['REQUEST_TIME'] - $message->timestamp, 1, 'nl');
$content .= '<div class="heartbeat-message-block ' . ($i % 2 ? 'odd' : 'even') . '">';
$content .= '<span class="beat-item">' . $message->message . '</span>';
// Don't show time if the message is a merged message
if ($show_time) {
if ($message->target_count <= 1 || variable_get('heartbeat_show_time_grouped_items', 1)) {
$content .= '<span class="heartbeat_times"> ' . $message_date . ' ' . $ago . '</span> ';
}
}
if (!empty($message->concat_args['flags'])) {
$content .= '<div>';
$extras = array();
foreach ($message->concat_args['flags'] as $flagname) {
$extras[] = flag_create_link($flagname, $message->uaid);
}
$content .= implode(' - ', $extras);
$content .= '</div>';
}
$content .= '</div>';
$i++;
}
return $content;
}
/**
* Helper function to map a array to dropdown
* with a field and value for the options
*
* @param array $options
* @param string target $field
* @param sring target $value
* @return array mapped for options dropdown
*/
function _heartbeat_map_assoc($options, $field, $value) {
$mapped = array();
foreach ($options as $heartbeat_activity) {
$mapped[$heartbeat_activity->{$field}] = $heartbeat_activity->{$value};
}
return $mapped;
}
/**
* Helper function to get the fields
*/
function _heartbeat_get_fields($table) {
$fields = array();
$schema = drupal_get_schema($table, true);
// Go through our schema
foreach ($schema['fields'] as $field => $info) {
$fields[$field] = isset($info['alias']) ? $info['alias'] : $field;
}
return $fields;
}
/**
* Helper function to sync default messages
* with messages already cached
*
* @param array $defaults
* @param array $cache
* @return array with unique default keys ready to be imported
*/
function _heartbeat_array_udiff($defaults, $cache) {
// First rebuild the arrays so that the keys make sense
$cache_array = $default_array = array();
foreach ($cache as $cached) {
$cache_array[$cached['message_id']] = $cached;
}
// Build the defaults messages array and set the unexisting keys
foreach ($defaults as $key => $default) {
$default_array[$default['message_id']] = $default;
// set keys that are not defined
if (!isset($default['perms'])) {
$defaults[$key]['perms'] = 0;
}
if (!isset($default['message_type'])) {
$defaults[$key]['message_type'] = 'normal';
}
if (!isset($default['message'])) {
unset($defaults[$key]);
}
}
return array_diff_assoc($default_array, $cache_array);
}
Functions
Name | Description |
---|---|
heartbeat_decode_message_variables | Decode heartbeat message variables |
heartbeat_encode_message_variables | Encode heartbeat message variables |
heartbeat_match_variables | Function looks for variables in an array of given strings |
heartbeat_variables_compare | compares variables that need to be included with variables that are already there and check their values to fit as default value in a textarea |
_heartbeat_array_udiff | Helper function to sync default messages with messages already cached |
_heartbeat_get_fields | Helper function to get the fields |
_heartbeat_map_assoc | Helper function to map a array to dropdown with a field and value for the options |
_heartbeat_messages_format_interval | Helper function |