messaging_template.admin.inc in Messaging 6.3
Same filename and directory in other branches
Messaging Framework - Admin UI
This file includes the settings page and administration for message templates
By Development Seed, http://wwww.developmentseed.org
File
messaging_template/messaging_template.admin.incView source
<?php
/**
* @file
* Messaging Framework - Admin UI
*
* This file includes the settings page and administration for message templates
*
* By Development Seed, http://wwww.developmentseed.org
*/
/**
* Aditional help for admin pages, will be called from messaging_help().
*
* All paths here start with 'admin/messaging'
*/
function messaging_template_admin_help($path, $arg) {
$read_more = t('Read more about <a href="@messaging_template">message templates and the fallback system</a>.', array(
'@messaging_template' => url('admin/help/messaging_template'),
));
switch ($path) {
case 'admin/help#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;
case 'admin/messaging/template':
$output = '<p>' . t('Configure the templates for different types of messages. The fallback templates are the parents on this tree.') . " {$read_more}</p>";
return $output;
case 'admin/messaging/settings/method':
$output = '<p>' . t('Depending on your content format and the tokens you are using for messages it is important that you use the right filtering for the message body. Set up the filters you need using the <a href="@input_formats">Input formats</a> page', array(
'@input_formats' => url('admin/settings/filters'),
)) . '</p>';
return $output;
case 'admin/messaging/template/settings':
$output = '<p>' . t('These are all the templates provided by enabled modules. You can enable or disable templates and change their fallbacks.') . ' ' . $read_more . '</p>';
return $output;
default:
if ($arg[1] == 'settings' && $arg[2] == 'filters') {
return '<p>' . t('Filters are used also for messaging. If the input format is to be used only for messaging you don\'t need to allow any role for it.') . '</p>';
}
if ($arg[3] == 'edit' && ($name = $arg[4])) {
// Edit template groups
$output = '<p>' . t('These are the text parts to build up the message template for each sending method. You can override each part for each method or use the defaults provided.') . ' ' . $read_more . '</p>';
return $output;
}
}
}
/*
* Overview of message parts and sending methods
*/
function messaging_template_admin_overview() {
$output = '';
// List message groups
$templates = messaging_template_info();
// Check fallbacks and get root templates for tree-like display
$root = array();
foreach ($templates as $name => $info) {
if (!$info['enabled']) {
continue;
}
if ($fallback = messaging_template_fallback($name)) {
$templates[$fallback]['children'][] = $name;
}
else {
$root[] = $name;
}
}
// Now build table which will have some indentation
$rows = messaging_template_admin_tree($root, $templates);
$header = array(
t('Template'),
t('Parts'),
);
//array(t('Message groups'), t('Description'), t('Parts'));
$output .= theme('table', $header, $rows);
// List sending methods
$rows = array();
messaging_method_list();
foreach (messaging_method_info() as $method => $method_info) {
$rows[] = array(
'<strong>' . $method_info['name'] . '</strong>',
!empty($method_info['description']) ? $method_info['description'] : '',
);
}
$output .= theme('box', t('Sending methods'), theme('table', NULL, $rows));
return $output;
}
/**
* Template settings: enable/disable, fallback, etc...
*/
function messaging_template_admin_settings($form_state) {
$fallback_options = array(
'' => '',
) + messaging_template_info(NULL, 'title');
$form['templates'] = array(
'#tree' => TRUE,
'#theme' => 'messaging_template_admin_templates',
);
foreach (messaging_template_info() as $type => $info) {
$form['templates'][$type] = array(
'#type' => 'fieldset',
);
$form['templates'][$type]['module'] = array(
'#type' => 'value',
'#value' => $info['module'],
);
$form['templates'][$type]['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => $info['enabled'],
);
$form['templates'][$type]['fallback'] = array(
'#type' => 'select',
'#options' => $fallback_options,
'#default_value' => !empty($info['fallback']) ? $info['fallback'] : '',
);
}
$form['buttons']['update'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['buttons']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
);
return $form;
}
/**
* Template settings submission
*/
function messaging_template_admin_settings_submit($form, $form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
switch ($op) {
case t('Save configuration'):
foreach ($form_state['values']['templates'] as $type => $values) {
db_query("UPDATE {messaging_template} SET enabled = %d, fallback = '%s' WHERE type = '%s'", $values['enabled'], $values['fallback'], $type);
}
drupal_set_message('Your template options have been updated.');
break;
case t('Reset to defaults'):
db_query('DELETE FROM {messaging_template}');
drupal_set_message(t('Your template options have been restored to their default values.'));
break;
}
}
/**
* Recursively build a table tree for fallback methods
*/
function messaging_template_admin_tree($parents, $templates, $depth = 0) {
$rows = array();
foreach ($parents as $name) {
$info = $templates[$name];
$rows[] = array(
theme('indentation', $depth) . theme('messaging_template_admin_type', $info),
//!empty($info['description']) ? $info['description'] : '',
($parts = messaging_template_get_keys($name)) ? implode(', ', $parts) : '',
);
if (!empty($info['children'])) {
$rows = array_merge($rows, messaging_template_admin_tree($info['children'], $templates, $depth + 1));
}
}
return $rows;
}
/**
* Message groups edit page
*/
function messaging_template_admin_edit($name) {
drupal_add_css(drupal_get_path('module', 'messaging_template') . '/messaging_template.css', 'module');
$output = '';
if (messaging_template_info($name)) {
drupal_set_title(t('Message template for %name', array(
'%name' => messaging_template_info($name, 'title'),
)));
$output .= drupal_get_form('messaging_template_edit_form', $name);
}
return $output;
}
/**
* Edit message formats
*/
function messaging_template_edit_form($form_state, $name) {
global $language;
drupal_add_js(drupal_get_path('module', 'messaging_template') . '/messaging_template.js', 'module');
drupal_add_js(array(
'messagingTemplateEdit' => array(
'lookup' => url('admin/messaging/template/edit'),
),
), 'setting');
$info = messaging_template_info($name);
$form['description'] = array(
'#type' => 'item',
'#title' => $info['title'],
'#value' => messaging_template_admin_description($name),
);
$form['name'] = array(
'#type' => 'value',
'#value' => $name,
);
$form['language'] = array(
'#type' => 'value',
'#value' => $language->language,
);
$form['template'] = array(
//'#title' => t('Template'),
//'#type' => 'fieldset',
'#tree' => TRUE,
);
foreach (messaging_template_method_list() as $method => $methodname) {
$form['template'][$method] = array(
'#type' => 'fieldset',
'#title' => $methodname,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Get defaults for this template from modules with fallbacks
$defaults = messaging_template_get_defaults($name, $language, TRUE);
foreach (messaging_template_get_keys($name) as $key => $keyname) {
// First, get template without fallback
$template = messaging_template_get_template($name, $method, $key, $language, FALSE, FALSE);
if ($type_fallback = messaging_template_fallback($name)) {
$fallback = messaging_template_get_template($type_fallback, $method, $key, $language);
}
if ($method_default = messaging_template_method_default($method)) {
$default = messaging_template_get_template($name, $method_default, $key, $language);
}
else {
// If not default for 'default' method, try fallback
if (isset($defaults[$key])) {
$default = $defaults[$key];
}
else {
}
}
// Text will be NULL if no template nor fallback
if (!$template) {
$text = $default ? $default->template : '';
$status = MESSAGING_TEMPLATE_DEFAULT;
}
elseif ($template->options == MESSAGING_TEMPLATE_FALLBACK) {
$text = $fallback ? $fallback->template : FALSE;
$status = MESSAGING_TEMPLATE_FALLBACK;
}
else {
$text = $template->template;
$status = MESSAGING_TEMPLATE_OVERRIDE;
}
// Set template values to form elements
$form['template'][$method][$key] = array(
'#type' => 'fieldset',
'#title' => $keyname,
//'#theme' => 'messaging_template_text_part',
'#attributes' => array(
'class' => 'messaging-template-part',
),
);
$form['template'][$method][$key]['tptid'] = array(
'#type' => 'value',
'#value' => $template ? $template->tptid : NULL,
);
$form['template'][$method][$key]['template'] = array(
//'#title' => $keyname,
'#type' => 'textarea',
'#default_value' => $text ? $text : '',
// Adjust size to actual number of lines
'#rows' => $text ? count(explode("\n", $text)) : 2,
'#disabled' => $status != MESSAGING_TEMPLATE_OVERRIDE,
'#attributes' => array(
'class' => 'messaging-template-part-text',
),
);
// Store fallback and default texts if any
$form['template'][$method][$key]['fallback'] = array(
'#type' => 'hidden',
'#value' => $fallback ? $fallback->template : '',
);
$form['template'][$method][$key]['default'] = array(
'#type' => 'hidden',
'#value' => $default ? $default->template : '',
);
// Compute default options
$options[MESSAGING_TEMPLATE_OVERRIDE] = t('Override');
$options[MESSAGING_TEMPLATE_DEFAULT] = t('Default');
$options[MESSAGING_TEMPLATE_FALLBACK] = messaging_template_fallback_description($name, $method, $fallback);
$form['template'][$method][$key]['options'] = array(
//'#title' => $keyname,
'#type' => 'radios',
'#options' => $options,
'#default_value' => $status,
'#attributes' => array(
'class' => 'template-select-override',
),
);
}
}
// Tokens for text replacement
if ($tokens = messaging_template_token_get_list($name)) {
$form['tokens'] = array(
'#type' => 'item',
'#title' => t('Available Tokens'),
'#value' => messaging_template_token_description($tokens),
);
//$form['tokens'] += messaging_template_token_description($tokens);
}
$form['submit']['save'] = array(
'#type' => 'submit',
'#value' => t('Save template'),
);
$form['submit']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
);
return $form;
}
/**
* Token list and description
*/
function messaging_template_token_description($tokens) {
$token_info = messaging_template_invoke_all('token info');
$items = $others = array();
foreach ($tokens as $type => $type_tokens) {
if (isset($token_info[$type])) {
$items[$type] = messaging_template_token_group($token_info[$type]['name'], $type_tokens, $token_info[$type]['description']);
}
else {
$others += $type_tokens;
}
}
if (!empty($others)) {
$items['others'] = messaging_template_token_group(t('Others'), $others);
}
return drupal_render($items);
}
/**
* Build template description
*/
function messaging_template_admin_description($name) {
$info = messaging_template_info($name);
$output = '<p>';
if (!empty($info['description'])) {
$output .= $info['description'];
}
if ($fallback = messaging_template_fallback($name)) {
$output .= ' ' . t('The fallback template from which message parts will be taken if left blank is !template', array(
'!template' => theme('messaging_template_admin_type', $fallback),
));
}
$output .= '</p>';
if (!empty($info['help'])) {
$output .= '<p>' . $info['help'] . '</p>';
}
return $output;
}
/**
* Build description for template fallback
*/
function messaging_template_fallback_description($type, $method, $fallback) {
if (!$fallback) {
return t('Empty text (no fallback)');
}
else {
$variables = array(
'%method' => isset($fallback->method) ? messaging_template_method_list($fallback->method) : t('Default text'),
'!template' => theme('messaging_template_admin_type', $fallback->type),
);
if ($type == $fallback->type) {
return t('Fall back: %method from this template.', $variables);
}
else {
return t('Fall back: %method from template !template.', $variables);
}
}
}
/**
* Build method list for messaging template or get method name
*/
function messaging_template_method_list($method = NULL) {
$send_methods = array(
'default' => t('Default'),
);
$send_methods += messaging_method_list();
return $method ? $send_methods[$method] : $send_methods;
}
/**
* Get list of tokens for text replacement
*
* @param $name
* Message group to get tokens for
* @param $tokens
*
*/
function messaging_template_token_get_list($name) {
// First compile token types for this message group
$type_list = module_invoke_all('messaging_template', 'tokens', $name);
// Add known global tokens, will be always available
$type_list[] = 'global';
// Now get token list from token module for each type
$return = array();
foreach ($type_list as $type) {
// This is a shortcut for single tokens for digests, with the form (token, description)
if (is_array($type)) {
list($type, $token) = $type;
$list = token_get_list($type);
$return[$type][$token] = $list[$type][$token];
}
elseif ($list = token_get_list($type)) {
foreach ($list as $category => $tokens) {
foreach ($tokens as $token => $description) {
$return[$category][$token] = $description;
}
}
}
}
return $return;
}
function messaging_template_token_group($title, $list, $description = '') {
$rows = array();
//$headers = array(t('Token'), t('Replacement value'));
foreach ($list as $token => $name) {
$rows[] = array(
'[' . $token . ']',
$name,
);
}
$items = array(
'#type' => 'fieldset',
'#title' => $title,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => $description,
);
$items['list'] = array(
'#value' => theme('table', array(), $rows),
);
return $items;
}
/**
* Process and save message parts
*/
function messaging_template_edit_form_submit($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
$name = $form_state['values']['name'];
$language = $form_state['values']['language'];
switch ($op) {
case t('Save template'):
foreach ($form_state['values']['template'] as $method => $templates) {
foreach ($templates as $key => $text) {
$text += array(
'name' => $name,
'method' => $method,
'msgkey' => $key,
'language' => $language,
);
if ($text['options'] != MESSAGING_TEMPLATE_DEFAULT) {
// Save / update, depending on exiting tptid
drupal_write_record('messaging_template_text', $text, !empty($text['tptid']) ? 'tptid' : array());
}
elseif (!empty($text['tptid'])) {
// We had a row but as it's set to default we don't need it anymore
db_query('DELETE FROM {messaging_template_text} WHERE tptid = %d', $text['tptid']);
}
}
}
drupal_set_message('The template elements have been updated');
break;
case t('Reset to defaults'):
db_query("DELETE FROM {messaging_template} WHERE name = '%s'", $name);
db_query("DELETE FROM {messaging_template_text} WHERE name = '%s'", $name);
drupal_set_message(t('Your template elements have been restored to their default values.'));
break;
}
}
/**
* Theme template name with edit link
*
* @param $type
* Template type or full info array
*/
function theme_messaging_template_admin_type($type) {
$info = is_array($type) ? $type : messaging_template_info($type);
$options['attributes']['title'] = !empty($info['description']) ? check_plain($info['description']) : '';
return l($info['title'], 'admin/messaging/template/edit/' . $info['name'], $options);
}
/**
* Theme text part fieldset
*/
function theme_messaging_template_text_part(&$element) {
$output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
$output .= $element['#title'] ? '<legend>' . $element['#title'] . '</legend>' : '';
$output .= isset($element['#description']) && $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
$output .= !empty($element['#children']) ? $element['#children'] : '';
$output .= (isset($element['#value']) ? $element['#value'] : '') . "</fieldset>\n";
return $output;
}
/**
* Theme settings form
*/
function theme_messaging_template_admin_templates(&$elements) {
$output = '';
// Group them by module
$groups = array();
foreach (element_children($elements) as $key) {
$module = $elements[$key]['module']['#value'];
$groups[$module][] = array(
'',
drupal_render($elements[$key]['enabled']),
theme('messaging_template_admin_type', $key),
drupal_render($elements[$key]['fallback']),
);
}
// Now convert groups into a table
$header = array(
t('Module'),
t('Enabled'),
t('Template'),
t('Fall back'),
);
$rows = array();
// Get current list of modules.
$files = module_rebuild_cache();
foreach ($groups as $module => $templates) {
$templates[0][0] = $files[$module]->info['name'];
//, 'rowspan' => count($templates));
//$row = array_merge($row, array_shift($templates));
//$rows[] = $row;
//$templates[0][0] = array('data' => $files[$module]->info['name'], 'rowspan' => count($templates));
// Add the rest of the templates
$rows = array_merge($rows, $templates);
}
$output .= theme('table', $header, $rows);
//$output .= drupal_render($elements);
return $output;
}