/[drupal]/contributions/modules/language_sections/language_sections.module
ViewVC logotype

Contents of /contributions/modules/language_sections/language_sections.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Tue Jul 29 14:43:32 2008 UTC (15 months, 3 weeks ago) by netgenius
Branch: MAIN
CVS Tags: DRUPAL-6--1-6, DRUPAL-5--1-5, HEAD
Branch point for: DRUPAL-5
Changes since 1.5: +22 -4 lines
File MIME type: text/x-php
Added support for "default" sections, uses "qz" pseudo-language-code.  Updated help and readme to match.
Was a feature request - http://drupal.org/node/287515
1 <?php
2 // $Id:
3 // Language Sections module for Drupal.
4 // Contact: andy inman: drupal_dev(at)netgenius(dot)co(dot)uk
5 // License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6
7 // Currently this .module file is compatible with both Drupal 5.x and 6.x
8
9 // Implementation of hook_filter_tips()
10 function language_sections_filter_tips($delta, $format, $long = false) {
11 $short_help = "Mark language-dependent sections with <strong>== lc ==</strong> where <em>lc</em> (or <em>lc-xx</em>) is a language code. Use <em>qz</em> for <em>default</em> and <em>qq</em> for <em>all languages.</em>";
12 $long_help = $short_help;
13
14 return t($long? $long_help : $short_help);
15 }
16
17 // Get language code for current language (handles Drupal 5/6 compatibility).
18 function _language_sections_get_language() {
19 $versions = explode('.', VERSION, 2);
20 switch($versions[0]) {
21 case 6:
22 global $language;
23 return $language->language;
24
25 default:
26 global $locale;
27 return $locale;
28 }
29 }
30
31 // Implementation of hook_filter()
32 function language_sections_filter($op, $delta = 0, $format = -1, $text = '') {
33 // Define these to save space and help avoid typing errors below:
34 $mod_id = 'language_sections'; $mod_name = 'Language Sections';
35 // Define the prefix used for config values:
36 $prefix = $mod_id.'_'.$format.'_';
37 // Define the default pattern used to find matches:
38 $def_pattern = '/(=+ *([a-z]{2}|[a-z]{2}-[a-z]{2}) *=+\s?)(.*?)/';
39
40 switch ($op) {
41 case 'process':
42 // Get settings, and reset the default pattern if needed:
43 $use_alt = variable_get($prefix.'alt', FALSE);
44 $pattern = $use_alt? variable_get($prefix.'pattern', $def_pattern) : $def_pattern;
45 $n1 = 2; $n2 = 2; $n3 = 4; // indexes to use with array from preg_split().
46 $matches = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
47
48 /************* debug *****************
49 // Display the contents of the array of substrings
50 $cr = "<br>\r\n";
51 $out = "pattern: " . $pattern . $cr;
52 $out .= "array: ";
53 for($i = 0; $i < count($matches); $i++) {
54 if ($matches[$i]) $out .= $i . ': {' . $matches[$i] . '}'. $cr;
55 }
56 $out .= $cr."(end)".$cr;
57 return $out;
58 *************************************/
59
60 // Build the output string, keeping only the parts we want...
61 // Todo: A better preg pattern - this approach is less than optimal with a large number of languages.
62 // Todo: 'qq' and 'qz' codes should probably be configurable.
63 $current_language = _language_sections_get_language();
64 $out = $matches[0]; $default = true;
65 for($i = $n1; $i < count($matches); $i += $n3) {
66 switch ($matches[$i]) {
67 // case: a section for the current language, use it and clear "use default" flag.
68 case $current_language:
69 $out .= $matches[$i+$n2];
70 $default = false;
71 break;
72 // case: a section for "all languages", use it.
73 case 'qq':
74 $out .= $matches[$i+$n2];
75 break;
76 // case: a "default" section, use it if we haven't already used a language-specific section...
77 case 'qz':
78 if ($default)
79 $out .= $matches[$i+$n2];
80 else
81 $default = true;
82 break;
83 }
84 }
85 return $out;
86
87 case 'no cache':
88 return (variable_get($prefix.'cache', FALSE) == FALSE);
89
90 case 'list':
91 return array(0 => t($mod_name));
92
93 case 'description':
94 return t('Allows you to define content for several languages in a single text area.');
95
96 case 'settings':
97 return _language_sections_settings($mod_id, $mod_name, $prefix, $def_pattern);
98
99 default:
100 return $text;
101 }
102 }
103
104 function _language_sections_settings($mod_id, $mod_name, $prefix, $def_pattern) {
105 //require_once(dirname(__FILE__) . '/help.html');
106 $form[$mod_id] = array(
107 '#type' => 'fieldset',
108 '#title' => t($mod_name),
109 '#collapsible' => TRUE,
110 );
111
112 /*
113 $form[$mod_id]['help'] = array(
114 '#type' => 'markup',
115 '#value' => '<p>Help goes here.</p>',
116 );
117 */
118
119 $field = $prefix.'cache';
120 $form[$mod_id][$field] = array(
121 '#type' => 'checkbox',
122 '#title' => t("Allow caching"),
123 '#default_value' => variable_get($field, FALSE),
124 '#description' => t("Allow the filtered text to be cached by the Drupal core, which may improve site performance. NOTE: Depending on your site configuration, this may cause text for the wrong language to be displayed. Even if this is set, other modules might still prevent the text from being cached."),
125 );
126
127 $field = $prefix.'alt';
128 $use_alt = variable_get($field, FALSE);
129 $form[$mod_id][$field] = array(
130 '#type' => 'checkbox',
131 '#title' => t("Use alternative pattern"),
132 '#default_value' => $use_alt,
133 '#description' => t("If set, $mod_name can be defined using the pattern given below. Otherwise, the default pattern will be used."),
134 );
135
136 $field = $prefix.'pattern';
137 $form[$mod_id][$field] = array(
138 '#type' => 'textfield',
139 '#title' => t("Alternative pattern"),
140 '#size' => 64,
141 '#maxlength' => 128,
142 '#default_value' => $use_alt? variable_get($field, $def_pattern) : $def_pattern,
143 '#description' => t("If enabled above, this pattern will be used for finding the $mod_name in the text. Initially, this is set to the $mod_name module's internal default. NOTE: You should not change the number of parenthesised groups in the expression."),
144 );
145
146 return $form;
147 }
148
149 // --- Drupal docs advise NOT closing PHP tags ---

  ViewVC Help
Powered by ViewVC 1.1.2