View source
<?php
define('MESSAGING_TEMPLATE_EMPTY', '<none>');
function messaging_template_help($path, $arg) {
switch ($path) {
case 'admin/messaging/template':
$output = '<p>' . t('Configure the templates for different types of messages. Each message group is defined by other modules using the Messaging Framework. A typical message consists on the following parts:') . '</p>';
$output .= '<small><table>';
$output .= '<tr><td colspan="2"><em>' . t('Subject') . '</em></td><td>' . t('Single line with a short description') . '</td></tr>';
$output .= '<tr><td rowspan="3">' . t('Body') . '</td><td><em>' . t('Header') . '</em></td><td>' . t('Greetings line') . '</td></tr>';
$output .= '<tr><td><em>' . t('Content') . '</em></td><td>' . t('Message main content, usually longer with the full description') . '</td></tr>';
$output .= '<tr><td><em>' . t('Footer') . '</em></td><td>' . t('Closing part with site information, unsubscribe links, etc...') . '</td></tr>';
$output .= '</table></small>';
$output .= '<p>' . t('Here you\'ll be able to configure each of these parts for each sending method. When one of these parts is left blank, there is a fallback system which goes as follows:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('If a message part is left blank for a sending method, the text part from Default sending method will be used.') . '</li>';
$output .= '<li>' . t('If the Default part is blank too, the fallback template (the parent in this tree) will be used.') . '</li>';
$output .= '</ul>';
return $output;
default:
if ($arg[0] == 'admin' && $arg[1] == 'messaging' && $arg[2] == 'template' && $arg[3] == 'edit' && ($group = $arg[4])) {
$output = messaging_template_admin_info($group);
$output .= '<p>' . t('Leave blank to use the default texts or use \'%empty\' for an empty message part, preventing fallback to default message texts.', array(
'%empty' => MESSAGING_TEMPLATE_EMPTY,
)) . '</p>';
$output .= '<p>' . t('Optionally you can use Input formats for additional formatting after token replacement. <strong>Using Input formats can be unsafe if you use certain filters</strong> like the PHP filter.') . '</p>';
return $output;
}
}
}
function messaging_template_menu() {
$items['admin/messaging/template'] = array(
'title' => 'Message templates',
'description' => 'Configuration of message templates',
'page callback' => 'messaging_template_admin_template',
'access callback' => 'messaging_template_access',
'file' => 'messaging_template.admin.inc',
);
$default = language_default();
$items['admin/messaging/template/edit/%messaging_template'] = array(
'title' => 'Message templates',
'page callback' => 'messaging_template_admin_template_edit',
'page arguments' => array(
4,
$default->language,
),
'type' => MENU_CALLBACK,
'access callback' => 'messaging_template_access',
'file' => 'messaging_template.admin.inc',
);
foreach (messaging_template_language_list() as $langcode => $name) {
$items['admin/messaging/template/edit/%messaging_template/' . $langcode] = array(
'title' => check_plain($name),
'page callback' => 'messaging_template_admin_template_edit',
'page arguments' => array(
4,
5,
),
'type' => $default->language == $langcode ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
'access callback' => 'messaging_template_access',
'file' => 'messaging_template.admin.inc',
);
}
return $items;
}
function messaging_template_access() {
return user_access('administer messaging') || user_access('edit message templates');
}
function messaging_template_load($key) {
$groups = module_invoke_all('messaging', 'message groups');
if (isset($groups[$key])) {
return $groups[$key] + array(
'group' => $key,
);
}
}
function messaging_template_perm() {
return array(
'edit message templates',
);
}
function messaging_template_build($template, $send_method, $language, $objects = array(), $options = array(), $parts = array(
'subject',
'header',
'main',
'footer',
)) {
$textparts = array();
foreach ($parts as $key) {
$textparts[$key] = messaging_template_text_part($template, $key, $send_method, $language);
}
$text = messaging_template_text_replace($textparts, $objects, $language, $options);
if (isset($text['subject'])) {
$subject = $text['subject'];
unset($text['subject']);
}
else {
$subject = '';
}
return new Messaging_Message(array(
'subject' => $subject,
'body' => $text,
'language' => $language,
));
}
function messaging_template_message_part($group, $key, $method, $language, $getdefault = TRUE) {
$cache =& messaging_static(__FUNCTION__);
$langcode = $language->language;
if (!isset($cache[$langcode][$group][$method])) {
$cache[$langcode][$group][$method] = _messaging_template_message_part($group, $method, $language);
}
if (!$key) {
return $cache[$langcode][$group][$method];
}
if (!isset($cache[$langcode][$group][$method][$key])) {
if ($getdefault && ($fallback = messaging_template_method_fallback($method))) {
$cache[$langcode][$group][$method][$key] = messaging_template_message_part($group, $key, $fallback, $language);
}
else {
$cache[$langcode][$group][$method][$key] = FALSE;
}
}
return $cache[$langcode][$group][$method][$key];
}
function _messaging_template_message_part($group, $method, $language) {
$templates = array();
$result = db_query("SELECT * FROM {messaging_message_parts} WHERE type = '%s' AND method = '%s' AND language = '%s'", $group, $method, $language->language);
while ($part = db_fetch_object($result)) {
$templates[$part->msgkey] = $part;
}
if (empty($templates) && $method == 'default') {
foreach (messaging_template_message_defaults($group, $language) as $key => $text) {
$part = (object) array(
'type' => $group,
'msgkey' => $key,
'default' => $text,
'format' => 0,
'message' => is_array($text) ? implode("\n", $text) : $text,
);
$templates[$key] = $part;
}
}
return $templates;
}
function messaging_template_text_part($group, $key, $method, $language) {
$original_group = $group;
while ($group) {
if ($part = messaging_template_message_part($group, $key, $method, $language)) {
return $part->message === MESSAGING_TEMPLATE_EMPTY ? '' : $part;
}
else {
$group = messaging_template_group_fallback($group);
}
}
if ($language_fallback = messaging_template_language_fallback($language)) {
return messaging_template_text_part($original_group, $key, $method, $language_fallback);
}
}
function messaging_template_type_info($type = NULL, $property = NULL) {
$info =& messaging_static(__FUNCTION__);
if (!isset($info)) {
$info = module_invoke_all('messaging', 'message groups');
}
return messaging_array_info($info, $type, $property);
}
function messaging_template_group_fallback($group) {
return messaging_template_message_group($group, 'fallback');
}
function messaging_template_method_fallback($method) {
return $method == 'default' ? NULL : 'default';
}
function messaging_template_language_fallback($language) {
$default = language_default();
return $language->language != $default->language ? $default : NULL;
}
function messaging_template_message_defaults($group, $language) {
$info =& messaging_static(__FUNCTION__);
if (!isset($info[$language->language][$group])) {
$info[$language->language][$group] = module_invoke_all('messaging', 'messages', $group, $language);
}
return $info[$language->language][$group];
}
function messaging_template_message_group($group = NULL, $key = NULL) {
static $info;
if (!isset($info)) {
$info = module_invoke_all('messaging', 'message groups');
}
return messaging_array_info($info, $group, $key);
}
function messaging_template_extract_group($filter, $templates) {
$filter = is_array($filter) ? $filter : array(
$filter,
);
$selected = array();
foreach ($templates as $key => &$template) {
if (in_array($key, $filter)) {
$selected[$key] = $template;
while (!empty($template['fallback']) && ($fallback = $template['fallback']) && !isset($selected[$fallback])) {
$template = $templates[$fallback];
$selected[$fallback] = $template;
}
}
}
return $selected;
}
function messaging_template_text_replace($text, $objects, $language = NULL, $options = array()) {
$options += array(
'language' => $language ? $language : $GLOBALS['language'],
);
$options += array(
'langcode' => $options['language']->language,
);
messaging_include('text.inc');
$objects['global'] = NULL;
if (is_object($text)) {
$format = !empty($text->format) ? $text->format : NULL;
$text = $text->message;
}
elseif (is_array($text)) {
$filters = array();
foreach ($text as $key => $part) {
if (is_object($part)) {
$text[$key] = $part->message;
if (!empty($part->format)) {
$filters[$key] = $part->format;
}
}
}
}
$function = 'token_replace_multiple';
$param_arr = array(
$text,
$objects,
'[',
']',
$options,
);
if (module_exists('token_logic')) {
$function = 'token_logic_replace_multiple';
$param_array = array(
$text,
$objects,
);
}
if (is_array($text)) {
foreach ($text as $part => $line) {
$param_arr[0] = $line;
$text[$part] = call_user_func_array($function, $param_arr);
}
}
else {
$text = call_user_func_array($function, $param_arr);
}
if (is_array($text) && !empty($filters)) {
foreach ($filters as $key => $format) {
$text[$key] = messaging_text_check_markup($text[$key], $format);
}
}
elseif (isset($format)) {
$text = messaging_text_check_markup($text, $format);
}
return $text;
}
function messaging_template_language_list() {
if (function_exists('locale_language_list')) {
return locale_language_list();
}
else {
$default = language_default();
return array(
$default->language => $default->name,
);
}
}
function messaging_template_theme() {
return array(
'messaging_template_part_text' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'messaging_template.admin.inc',
),
);
}