View source
<?php
function language_sections_filter_tips($delta, $format, $long = false) {
extract(_language_sections_get_ids($format));
$tvars = array(
'%all' => t('all'),
'%other' => t('other'),
);
$short_help = t(_language_sections_setting($mod_prefix, 'short_help'), $tvars);
$long_help = $short_help;
return t($long ? $long_help : $short_help, $tvars);
}
function _language_sections_get_ids($format) {
$mod_name = 'Language Sections';
$mod_id = 'language_sections';
$mod_prefix = $mod_id . '_' . $format . '_';
if (_language_sections_setting($mod_prefix, 'shared')) {
$mod_prefix = $mod_id . '_shared_';
}
return compact('mod_name', 'mod_id', 'mod_prefix');
}
function language_sections_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = FALSE) {
extract(_language_sections_get_ids($format));
switch ($op) {
case 'process':
if (isset($GLOBALS['language_sections_disable'])) {
return $text;
}
$pattern = _language_sections_setting($mod_prefix, 'pattern');
$triggers = _language_sections_get_triggers($mod_prefix);
extract(_language_sections_context('match_types'));
$n1 = $n2 = 2;
$n3 = 4;
$matches = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$out = $matches[0];
$show_default = true;
for ($i = $n1; $i < count($matches); $i += $n3) {
$trigger = strtolower($matches[$i]);
if (!isset($triggers[$trigger])) {
continue;
}
switch ($triggers[$trigger]) {
case $current_language:
$out .= $matches[$i + $n2];
$show_default = false;
break;
case $all_languages:
$out .= $matches[$i + $n2];
break;
case $other_languages:
if ($show_default) {
$out .= $matches[$i + $n2];
}
else {
$show_default = true;
}
break;
}
}
if (function_exists('ls_titles_process')) {
$out = ls_titles_process('set', $out, $format, $cache_id);
}
return $out;
case 'no cache':
$patched = defined('check_markup_language_patch_1');
$no_cache = !$patched;
return $no_cache;
case 'list':
return array(
0 => t($mod_name),
);
case 'description':
return t('Allows you to define content for several languages in a single text area.');
case 'settings':
return _language_sections_settings($mod_name, $mod_prefix);
default:
return $text;
}
}
function _language_sections_get_triggers($mod_prefix, $language = FALSE, $rebuild = FALSE) {
if (!$language) {
global $language;
}
$varname = $mod_prefix . 'triggers_' . $language->language;
$triggers = variable_get($varname, null);
$check = crc32(serialize($language));
if ($rebuild || $triggers['check'] !== $check) {
$triggers = array(
'types' => _language_sections_build_triggers($mod_prefix, $language),
'check' => $check,
);
variable_set($varname, $triggers);
}
return $triggers['types'];
}
function _language_sections_build_triggers($mod_prefix, $language) {
extract(_language_sections_context());
foreach ($elements as $element) {
$key = 'trigger_' . $element;
if (_language_sections_setting($mod_prefix, $key)) {
$triggers[$language->{$element}] = $match_types['current_language'];
}
}
foreach ($specials as $name => $desc) {
$key = 'trigger_special_' . $name;
$matches = explode('|', _language_sections_setting($mod_prefix, $key));
foreach ($matches as $match) {
$triggers[strtolower($match)] = $match_types[$name . '_languages'];
}
}
return $triggers;
}
function _language_sections_setting($mod_prefix, $key, $get_default = FALSE) {
$defaults = array(
'pattern' => '/(=+ *([a-z]{2}[a-z\\-]*) *=+\\s?)(.*?)/i',
'trigger_language' => 1,
'trigger_special_all' => 'all|qq',
'trigger_special_other' => 'other|qz',
'short_help' => 'Mark language-dependent sections with <strong>== marker ==</strong> ' . 'where <em>marker</em> is a language code or other valid text such as %all or %other.',
);
if (!variable_get($mod_prefix . 'version', FALSE)) {
variable_set($mod_prefix . 'version', '2.5');
if (!variable_get($mod_prefix . 'alt', FALSE)) {
variable_del($mod_prefix . 'pattern');
}
}
return $get_default ? $defaults[$key] : variable_get($mod_prefix . $key, $defaults[$key]);
}
function _language_sections_context($topic = FALSE) {
$context = array(
'elements' => array(
'language',
'name',
'prefix',
),
'specials' => array(
'all' => t('all languages'),
'other' => t('other languages'),
),
'match_types' => array(
'current_language' => 1,
'all_languages' => 2,
'other_languages' => 3,
),
);
return $topic ? $context[$topic] : $context;
}
function _language_sections_settings($mod_name, $mod_prefix) {
global $language;
$textsize = 30;
extract(_language_sections_context());
$section =& $form[$mod_name];
$shared = _language_sections_setting($mod_prefix, 'shared');
$section = array(
'#type' => 'fieldset',
'#title' => $shared ? sprintf('%s (%s)', t($mod_name), t('shared configuration')) : t($mod_name),
'#collapsible' => TRUE,
);
$fieldset =& $section['triggers'];
$fieldset = array(
'#type' => 'fieldset',
'#title' => t('Current language triggers'),
'#description' => t('Which elements from current language can be used in language-section markers?'),
);
$title = '%element';
$desc = 'Language:%element is currently %value.';
foreach ($elements as $element) {
$key = 'trigger_' . $element;
$tvars = array(
'%element' => $element,
'%value' => $language->{$element} ? $language->{$element} : t('<blank>'),
);
$fieldset[$mod_prefix . $key] = array(
'#type' => 'checkbox',
'#title' => t($title, $tvars),
'#default_value' => _language_sections_setting($mod_prefix, $key),
'#description' => t($desc, $tvars),
);
}
$examples = array(
'all' => 'all|todos|toutes',
'other' => 'english|other|otro|autre',
);
foreach ($specials as $type => $desc) {
$key = 'trigger_special_' . $type;
$tvars = array(
'%desc' => $desc,
'%ex' => t($examples[$type]),
);
$section[$mod_prefix . $key] = array(
'#type' => 'textfield',
'#title' => t('Triggers for %desc sections', $tvars),
'#size' => $textsize,
'#default_value' => _language_sections_setting($mod_prefix, $key),
'#description' => t('Text that may mark an %desc section. Leave blank to disable, multiple entries allowed, e.g: %ex', $tvars),
);
}
$fieldset =& $section['advanced'];
$fieldset = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$key = 'shared';
$fieldset[$mod_prefix . $key] = array(
'#type' => 'checkbox',
'#title' => t('Shared configuration'),
'#default_value' => $shared,
'#description' => t('Use the same configuration for all filters. If you change this, save and then check all configuration values.'),
);
$key = 'short_help';
$fieldset[$mod_prefix . $key] = array(
'#type' => 'textarea',
'#title' => t('User help'),
'#rows' => 2,
'#default_value' => _language_sections_setting($mod_prefix, $key),
'#description' => t('Filter-help shown to the user. This text is passed through t().'),
);
$key = 'alt';
$use_alt = _language_sections_setting($mod_prefix, $key);
$fieldset[$mod_prefix . $key] = array(
'#type' => 'checkbox',
'#title' => t('Use alternative pattern'),
'#default_value' => $use_alt,
'#description' => t('If set, sections can be defined using the pattern given below. Otherwise, the default pattern will be used.'),
);
$key = 'pattern';
if (!$use_alt && variable_get($mod_prefix . $key, FALSE)) {
variable_del($mod_prefix . $key);
}
$pattern = _language_sections_setting($mod_prefix, $key);
$fieldset[$mod_prefix . $key] = array(
'#type' => 'textfield',
'#title' => t('Alternative pattern'),
'#size' => 40,
'#default_value' => $pattern,
'#description' => t('If enabled above, this pattern will be used for finding sections in the text.' . ' Initially, this is set to the module\'s internal default.' . ' You should not change the number of parenthesised groups in the expression.'),
);
$msg = array(
'Patch for $func is not installed - output cannot be cached. See included README.txt',
'Patch for $func is installed - output can be cached.',
);
$fieldset['cache'] = array(
'#type' => 'fieldset',
'#title' => t('Output caching'),
'msg' => array(
'#value' => t($msg[defined('check_markup_language_patch_1')], array(
'$func' => 'check_markup()',
)),
),
);
if (!empty($_POST)) {
foreach (language_list() as $lang) {
_language_sections_get_triggers($mod_prefix, $lang, TRUE);
}
}
return $form;
}
function language_sections_format_check($format) {
static $formats;
if (!isset($formats[$format])) {
$formats[$format] = false;
$filters = filter_list_format($format);
foreach ($filters as $filter) {
if ($filter->module == 'language_sections') {
$formats[$format] = true;
break;
}
}
}
return $formats[$format];
}