View source
<?php
define('MESSAGE_REQUIRED_CTOOLS_API', '1.7');
function message_ctools_plugin_api($module, $api) {
if ($module == 'message' && $api == 'plugins') {
return array(
'version' => 1,
);
}
}
function message_ctools_plugin_directory($module, $type) {
if (!module_invoke('ctools', 'api_version', MESSAGE_REQUIRED_CTOOLS_API)) {
return;
}
if ($module == 'message' && $type == 'plugins') {
return 'plugins/message';
}
elseif ($type == 'export_ui') {
return 'plugins/export_ui';
}
}
function message_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'message') . '/includes',
);
}
function message_features_api() {
return array(
'message' => array(
'name' => 'Messages',
'api' => 'message',
'default_hook' => 'message_default_messages',
'current_version' => 1,
'module' => 'message',
'feature_source' => TRUE,
),
);
}
function message_theme() {
if (!module_invoke('ctools', 'api_version', MESSAGE_REQUIRED_CTOOLS_API)) {
return;
}
$items = array();
$items['message'] = array(
'arguments' => array(
'message' => array(),
),
);
return $items;
}
function message_views_pre_render(&$view) {
$process = FALSE;
$ids = array();
foreach ($view->field as $field) {
if ($field->definition['handler'] == 'message_handler_field_message_render') {
$field_alias = $field->field_alias;
$process = TRUE;
break;
}
}
if ($process && !empty($view->result)) {
foreach ($view->result as &$row) {
$ids[] = $row->{$field_alias};
}
}
if ($ids && ($message_instances = message_instance_load_multiple($ids))) {
$remove = array();
foreach ($message_instances as $key => $value) {
if (!empty($value->hide)) {
$remove[$key] = $key;
}
}
foreach ($view->result as $key => $value) {
if (in_array($value->iid, $remove)) {
unset($view->result[$key]);
unset($remove[$key]);
}
if (empty($remove)) {
break;
}
}
}
}
function message_ctools_plugin_plugins() {
return array(
'process' => 'message_message_process',
);
}
function message_message_process(&$plugin, $info) {
$plugin += array(
'realm' => FALSE,
'title' => $plugin['name'],
'description' => '',
'views' => array(
'handlers callback' => function_exists("{$plugin['name']}_views_handlers") ? "{$plugin['name']}_views_handlers" : FALSE,
'data callback' => function_exists("{$plugin['name']}_views_data") ? "{$plugin['name']}_views_data" : FALSE,
),
'access' => array(
'access callback' => function_exists("{$plugin['name']}_access") ? "{$plugin['name']}_access" : 'message_plugin_access',
'accessible ids callback' => function_exists("{$plugin['name']}_accessible_ids") ? "{$plugin['name']}_accessible_ids" : 'message_plugin_accessible_ids',
),
);
}
function message_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (!variable_get('message_entity_delete_cleanup', TRUE)) {
return;
}
if ($op == 'delete') {
message_deleted_entity_cleanup('node', $node->nid);
}
}
function message_comment(&$a1, $op) {
if (!variable_get('message_entity_delete_cleanup', TRUE)) {
return;
}
if ($op == 'delete') {
}
}
function message_user($op, &$edit, &$account, $category = NULL) {
if (!variable_get('message_entity_delete_cleanup', TRUE)) {
return;
}
if ($op == 'delete') {
message_deleted_entity_cleanup('user', $account->uid);
}
}
function message_save_message_to_realms($message_name, $entity_type, $eid, $arguments = array(), $realms = array(), $account = NULL, $extra_identifier = '') {
if (empty($account)) {
global $user;
$account = drupal_clone($user);
}
$return = array();
$message_instance = new stdClass();
$message_instance->name = $message_name;
$message_instance->uid = $account->uid;
$message_instance->entity_type = $entity_type;
$message_instance->eid = $eid;
$message_instance->arguments = $arguments;
$message_instance->extra_identifier = $extra_identifier;
$message_instance = message_instance_save($message_instance);
$return['message instance'] = $message_instance;
if (empty($realms)) {
$realms[] = array(
'realm' => 'user',
'realm id' => $account->uid,
);
}
foreach ($realms as $value) {
$realm = new stdClass();
$realm->iid = $message_instance->iid;
$realm->realm = $value['realm'];
$realm->realm_id = $value['realm id'];
$return['message realm'][] = message_realm_save($realm);
}
return $return;
}
function message_load($name = NULL, $reset = FALSE) {
ctools_include('export');
if ($reset) {
ctools_export_load_object_reset('message');
}
$messages = ctools_export_load_object('message', 'all');
if ($messages && module_exists('i18nstrings')) {
$cache = cache_get('message_i18nstrings');
$i18nstrings = array();
$set = FALSE;
foreach ($messages as $message) {
if (empty($cache->data[$message->name])) {
i18nstrings_update("messages:message:{$message->name}:message", $message->message);
$i18nstrings[$message->name] = TRUE;
$set = TRUE;
}
}
if ($set) {
cache_set('message_i18nstrings', $i18nstrings);
}
}
if (isset($name)) {
return isset($messages[$name]) ? $messages[$name] : FALSE;
}
return $messages;
}
function message_save($message) {
ctools_include('export');
return ctools_export_crud_save('message', $message);
}
function message_delete($message) {
ctools_include('export');
ctools_export_crud_delete('message', $message);
}
function message_instance_load_multiple($ids = array()) {
$message_instances =& ctools_static(__FUNCTION__, array());
$query_ids = array_diff($ids, array_keys($message_instances));
if (!empty($query_ids)) {
$placeholders = db_placeholders($query_ids);
$result = db_query("SELECT * FROM {message_instance} WHERE iid IN ({$placeholders})", $query_ids);
while ($row = db_fetch_object($result)) {
if (!empty($row->arguments)) {
$row->arguments = unserialize($row->arguments);
}
$message_instances[$row->iid] = $row;
}
drupal_alter('message_instances', $message_instances);
}
return $message_instances;
}
function message_instance_load($iid) {
$messages = message_instance_load_multiple(array(
$iid,
));
return !empty($messages[$iid]) ? $messages[$iid] : FALSE;
}
function message_instance_save($message_instance) {
if ($existing = message_instance_load($message_instance->iid)) {
drupal_write_record('message_instance', $message_instance, 'iid');
}
else {
$message_instance->timestamp = !empty($message_instance->timestamp) ? $message_instance->timestamp : time();
drupal_write_record('message_instance', $message_instance);
}
return $message_instance;
}
function message_instance_delete($iids = array()) {
$placeholders = db_placeholders($iids);
db_query("DELETE FROM {message_instance} WHERE iid IN({$placeholders})", explode(',', $iids));
message_realm_delete_by_message_instance($iids);
}
function message_realm_load_multiple_by_message($message_id) {
static $messages = array();
if (!isset($messages[$message_id])) {
$result = db_query("SELECT * FROM {message_realm} WHERE iid = %d", $message_id);
while ($row = db_fetch_object($result)) {
$messages[$message_id][$row->realm][$row->realm_id] = $row->realm_id;
}
}
return !empty($messages[$message_id]) ? $messages[$message_id] : array();
}
function message_realm_load($rid) {
return db_fetch_object(db_query("SELECT * FROM {message_realm} where rid = %d", $rid));
}
function message_realm_save($message_realm) {
if ($existing = message_realm_load($message_realm->rid)) {
drupal_write_record('message_realm', $message_realm, 'rid');
}
else {
drupal_write_record('message_realm', $message_realm);
}
return $message_realm;
}
function message_realm_delete($rids = array()) {
$placeholders = db_placeholders($rids);
db_query("DELETE FROM {message_realm} WHERE rid IN ({$placeholders})", explode(',', $rids));
}
function message_realm_delete_by_message_instance($iids = array()) {
$placeholders = db_placeholders($iids);
db_query("DELETE FROM {message_realm} WHERE iid IN ({$placeholders})", implode(',', $iids));
}
function message_realm_delete_by_realm($realm, $realm_id) {
db_query("DELETE FROM {message_realm} WHERE realm = '%s' AND eid = '%s'", $realm, $realm_id);
}
function message_get_plugin_message($plugin_name) {
ctools_include('plugins');
return ctools_get_plugins('message', 'plugins', $plugin_name);
}
function message_get_plugin_messages() {
ctools_include('plugins');
return ctools_get_plugins('message', 'plugins');
}
function message_show_message($message_instance, $skip_access = FALSE, $account = NULL) {
$output = '';
$access = FALSE;
if (empty($account)) {
global $user;
$account = drupal_clone($user);
}
if ($skip_access) {
$access = TRUE;
}
elseif ($realms = message_realm_load_multiple_by_message($message_instance->iid)) {
foreach (message_get_plugin_messages() as $plugin) {
if (in_array($plugin['realm'], array_keys($realms)) && message_show_message_access($plugin, $message_instance, $realms[$plugin['realm']])) {
$access = TRUE;
break;
}
}
}
if ($access) {
$output = theme('message', array(
'message' => $message_instance,
'output' => message_t($message_instance),
));
}
return $output;
}
function message_show_message_access($plugin, $message_instance, $realm_ids = array(), $account = NULL) {
$return = FALSE;
if (!empty($plugin['realm'])) {
if (empty($realm_ids)) {
$realms = message_realm_load_multiple_by_message($message_instance->iid);
$realm_ids = !empty($realms[$plugin['realm']]) ? $realms[$plugin['realm']] : array();
}
if ($realm_ids) {
if (empty($account)) {
global $user;
$account = drupal_clone($user);
}
$return = call_user_func($plugin['access']['access callback'], $plugin, $message_instance, $realm_ids, $account);
}
}
return $return;
}
function message_get_accessiable_ids_by($plugin, $account = NULL) {
if (empty($account)) {
global $user;
$account = drupal_clone($user);
}
return call_user_func($plugin['access']['accessible ids callback'], $account);
}
function message_t($message_instance) {
if (!empty($message_instance->hide)) {
return '';
}
$string = message_get_string_i18n($message_instance);
if (empty($message_instance->arguments)) {
return $string;
}
else {
foreach ($message_instance->arguments as $key => $value) {
if (is_array($value) && !empty($value['callback']) && function_exists($value['callback'])) {
$value = call_user_func_array($value['callback'], $value['callback arguments']);
}
switch ($key[0]) {
case '@':
$args[$key] = check_plain($value);
break;
case '%':
default:
$args[$key] = theme('placeholder', $value);
break;
case '!':
$args[$key] = $value;
}
}
return strtr($string, $args);
}
}
function message_get_string_i18n($message_instance) {
$message = message_load($message_instance->name);
if (!empty($message_instance->override)) {
$name_property = $message_instance->override_name;
$message_property = $message_instance->message;
}
else {
$name_property = $message->name;
$message_property = $message->message;
}
if (module_exists(i18nstrings)) {
$string = i18nstrings('messages:message:' . $name_property . ':message', $message_property);
}
else {
$string = $message_property;
}
return $string;
}
function message_override_message_instance(&$message_instance, $override_name, $override_message) {
$message = message_load($message_instance->name);
$message_instance->override_name = $message->name . '_' . $override_name;
$message_instance->message = $override_message;
$message_instance->override = TRUE;
if (module_exists('i18nstrings')) {
$cache = cache_get('message_i18nstrings');
$i18nstrings = array();
if (!empty($cache->data)) {
$i18nstrings = $cache->data;
}
$set = FALSE;
if (empty($i18nstrings[$message_instance->override_name])) {
i18nstrings_update("messages:message:{$message_instance->name}:message", $message_instance->message);
$i18nstrings[$message_instance->override_name] = TRUE;
$set = TRUE;
}
if ($set) {
cache_set('message_i18nstrings', $i18nstrings);
}
}
}
function theme_message($message = array()) {
return '<span class="message message-instance-' . $message['message']->iid . '">' . $message['output'] . '</span>';
}
function message_get_realm_types() {
$options =& ctools_static(__FUNCTION__, array());
if (empty($options)) {
foreach (message_get_plugin_messages() as $plugin) {
if (!empty($plugin['realm'])) {
$options[$plugin['name']] = check_plain($plugin['title']);
}
}
}
return $options;
}
function message_deleted_entity_cleanup($entity_type, $eid) {
$result = db_query("SELECT iid FROM {message_instance} WHERE entity_type = '%s' AND eid = %d", $entity_type, $eid);
$iids = array();
while ($row = db_fetch_object($result)) {
$iids[] = $row->iid;
}
if ($iids) {
message_instance_delete($iids);
message_realm_delete($entity_type, $eid);
}
}