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

Contents of /contributions/modules/translation_overview/translation_overview.module

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


Revision 1.15 - (show annotations) (download) (as text)
Mon Sep 21 18:11:06 2009 UTC (2 months ago) by drewish
Branch: MAIN
CVS Tags: DRUPAL-6--2-3, HEAD
Changes since 1.14: +2 -2 lines
File MIME type: text/x-php
#330528 by ru.meta: return destination on links is not set correctly.
1 <?php
2 // $Id: translation_overview.module,v 1.14 2009/05/19 17:21:02 drewish Exp $
3
4 define('TRANSLATION_OVERVIEW_HIGH', 2);
5 define('TRANSLATION_OVERVIEW_NORMAL', 1);
6 define('TRANSLATION_OVERVIEW_IGNORE', 0);
7
8 /**
9 * Implementation of hook_help().
10 */
11 function translation_overview_help($path, $arg) {
12 switch ($path) {
13 case 'admin/content/translation_overview_manage':
14 $args = array();
15 $states = array('original', 'current', 'outofdate', 'missing');
16 foreach ($states as $state) {
17 $args['!'. $state] = theme('translation_overview_translation_link', $state);
18 }
19 return '<p>'. t('The table uses the following symbols to indicate the translation status: !original, !current, !outofdate, !missing.', $args) .'</p>';
20 }
21 }
22
23 /**
24 * Implementation of hook_theme().
25 */
26 function translation_overview_theme() {
27 return array(
28 'translation_overview_translation_link' => array(
29 'arguments' => array('state' => NULL, 'link' => array(), 'properties' => array()),
30 ),
31 'translation_overview_node_form' => array(
32 'arguments' => array('form' => array()),
33 'file' => 'translation_overview.pages.inc',
34 ),
35 );
36 }
37
38
39 /**
40 * Implementation of hook_perm().
41 */
42 function translation_overview_perm() {
43 $perms = array('view translation overview assigments');
44 foreach (locale_language_list() as $lang_code => $language) {
45 $perms[] = 'manage '. check_plain($lang_code) .' translation overview priorities';
46 }
47
48 return $perms;
49 }
50
51
52 /**
53 * Implementation of hook_menu().
54 */
55 function translation_overview_menu() {
56 $default_language = language_default('language');
57
58 $items = array();
59
60 $items['admin/content/translation_overview_manage'] = array(
61 'title' => 'Translation overview',
62 'type' => MENU_NORMAL_ITEM,
63 'description' => "View the translation status of the site's content.",
64 'page callback' => 'translation_overview_manager_page',
65 'file' => 'translation_overview.pages.inc',
66 'access callback' => 'translation_overview_is_manager',
67 );
68 $items['admin/content/translation_overview_assignments'] = array(
69 'title' => 'Translator assigments',
70 'description' => "View the translations assignments.",
71 'type' => MENU_NORMAL_ITEM,
72 'page callback' => 'translation_overview_assignment_lang_page',
73 'file' => 'translation_overview.pages.inc',
74 'access arguments' => array('view translation overview assigments'),
75 );
76 $i = 0;
77 foreach (language_list() as $language) {
78 if ($language->enabled) {
79 $items['admin/content/translation_overview_assignments/'. $language->language] = array(
80 'title' => $language->name,
81 'description' => $language->name,
82 'type' => MENU_NORMAL_ITEM,
83 'weight' => $i++,
84 'page callback' => 'translation_overview_assignment_page',
85 'page arguments' => array($language->language),
86 'file' => 'translation_overview.pages.inc',
87 'access arguments' => array('view translation overview assigments'),
88 );
89 }
90 }
91 return $items;
92 }
93
94 /**
95 * Implementation of hook_menu_alter().
96 */
97 function translation_overview_menu_alter(&$callbacks) {
98 // Replace the translation module's node tab with our own.
99 $callbacks['node/%node/translate']['module'] = 'translation_overview';
100 $callbacks['node/%node/translate']['page callback'] = 'translation_overview_node_page';
101 $callbacks['node/%node/translate']['file'] = 'translation_overview.pages.inc';
102 }
103
104
105 /**
106 * Implementation of hook_form_alter().
107 *
108 * We inject our own submit handler into the locale module's forms that add
109 * or remove languages so that when they're submitted we rebuild the active
110 * translation table.
111 */
112 function translation_overview_form_alter(&$form, $form_state, $form_id) {
113 switch ($form_id) {
114 case 'locale_languages_delete_form':
115 $form['#submit'][] = 'translation_overview_schema_remove_submit';
116 break;
117
118 case 'locale_languages_predefined_form':
119 case 'locale_languages_custom_form':
120 $form['#submit'][] = 'translation_overview_schema_add_submit';
121 break;
122 }
123 }
124
125 /**
126 * Implementation of hook_nodeapi().
127 *
128 * Track nodes to monitor their translation priority.
129 */
130 function translation_overview_nodeapi(&$node, $op) {
131 switch ($op) {
132 case 'delete':
133 db_query("DELETE FROM {translation_overview_priority} WHERE tnid = %d", $node->nid);
134 break;
135
136 case 'insert':
137 // Only want to create a priority for translatable nodes that are untranslated.
138 $types = translation_overview_node_types();
139 if (empty($node->tnid) && !empty($types[$node->type])) {
140 translation_overview_get_node_priority($node);
141 }
142 }
143 }
144
145 /**
146 * Determine the name of the field in the {translation_overview_priority} table
147 * for a language.
148 *
149 * @param $lang_code
150 * String with a language code.
151 * @return
152 * String with a properly escaped field name.
153 */
154 function translation_overview_field_name($lang_code) {
155 return 'lang_' . db_escape_table($lang_code);
156 }
157
158 /**
159 * Determine the priority of a node.
160 *
161 * If no priority has been set this module will create a record in the
162 * {translation_overview_priority} table for it.
163 *
164 * @param $node
165 * @param $reset
166 * Boolean indcating that the function's cache should be reset.
167 * @return The priority:
168 * - TRANSLATION_OVERVIEW_HIGH
169 * - TRANSLATION_OVERVIEW_NORMAL
170 * - TRANSLATION_OVERVIEW_IGNORE
171 */
172 function translation_overview_get_node_priority($node, $reset = FALSE) {
173 static $cache;
174
175 if (!isset($cache) || $reset) {
176 $cache = array();
177 }
178
179 $tnid = empty($node->tnid) ? $node->nid : $node->tnid;
180 if (isset($cache[$tnid])) {
181 return $cache[$tnid];
182 }
183
184 $languages = array_keys(language_list('language'));
185 $pri = array();
186 $result = db_query('SELECT * FROM {translation_overview_priority} WHERE tnid = %d', $tnid);
187 $row = db_fetch_array($result);
188
189 // Insert a record if one isn't found.
190 if (empty($row)) {
191 $row = array('tnid' => empty($node->tnid) ? $node->nid : $node->tnid);
192 foreach ($languages as $lang_code) {
193 $row[translation_overview_field_name($lang_code)] = TRANSLATION_OVERVIEW_NORMAL;
194 }
195 drupal_write_record('translation_overview_priority', $row);
196 }
197
198 // We need to adjust the array's keys. The fieldnames are encoded language codes.
199 foreach (array_keys(language_list('language')) as $language) {
200 $pri[$language] = isset($row[translation_overview_field_name($language)]) ? $row[translation_overview_field_name($language)] : TRANSLATION_OVERVIEW_NORMAL;
201 }
202 return $cache[$tnid] = $pri;
203 }
204
205 /**
206 * Determine if a user is a manager for a language (or any langauge if none is
207 * specified).
208 *
209 * @param $language String with the language code. If no value is provided the
210 * a check will be made to see if they manage any language.
211 * @param $account Drupal user object.
212 * @return Boolean
213 */
214 function translation_overview_is_manager($lang_code = NULL, $account = NULL) {
215 if (empty($lang_code)) {
216 foreach (locale_language_list() as $lang_code => $language) {
217 if (user_access('manage '. check_plain($lang_code) .' translation overview priorities', $account)) {
218 return TRUE;
219 }
220 }
221 return FALSE;
222 }
223
224 return user_access('manage '. check_plain($lang_code) .' translation overview priorities', $account);
225 }
226
227 /**
228 * Get a list of the node types that have translation support enabled.
229 *
230 * @return array of with node types as keys and their names as values.
231 */
232 function translation_overview_node_types() {
233 $types = array();
234 foreach (node_get_types('names') as $type => $name) {
235 if (translation_supported_type($type)) {
236 $types[$type] = $name;
237 }
238 }
239 return $types;
240 }
241
242 function translation_overview_trimmed_title($node, $len = 25) {
243 // Shorten down the title
244 $title = $node->title;
245 if (drupal_strlen($title) > $len) {
246 $title = drupal_substr($title, 0, $len) .'...';
247 }
248 return $title;
249 }
250
251 /**
252 * Build a link to the translated node.
253 *
254 * @param $node Source node object
255 * @param $translation_node Optional, translated node.
256 * @param $language Language code.
257 * @param $show_long Boolean, indicating if a longer version of the text should
258 * be displayed.
259 * @return Link to the node if the user has permission or else just text.
260 */
261 function translation_overview_translation_link($node, $translation_node = NULL, $language, $show_long = FALSE) {
262 $link = array(
263 'path' => 'node/'. $node->nid,
264 'options' => array(),
265 );
266 $priorities = translation_overview_get_node_priority($node);
267 $properties = array(
268 'priority' => isset($priorities[$language]) ? $priorities[$language] : TRANSLATION_OVERVIEW_NORMAL,
269 'published' => $node->status,
270 'show_long' => $show_long,
271 'has_note' => FALSE,
272 );
273
274 if ($language == $node->language) {
275 $properties['has_note'] = !empty($node->field_translator_note[0]['value']);
276 return theme('translation_overview_translation_link', 'original', $link, $properties);
277 }
278 // Determine the status of the translation.
279 if (!empty($translation_node->nid)) {
280 $properties['has_note'] = !empty($translation_node->field_translator_note[0]['value']);
281 if (node_access('update', $translation_node)) {
282 $link['path'] = "node/$translation_node->nid/edit";
283 $link['options']['query'] = drupal_get_destination();
284 }
285 else {
286 $link['path'] = 'node/'. $translation_node->nid;
287 }
288
289 if ($translation_node->translate) {
290 return theme('translation_overview_translation_link', 'outofdate', $link, $properties);
291 }
292 return theme('translation_overview_translation_link', 'current', $link, $properties);
293 }
294
295 // Assume it's missing, see if we can create a translation.
296 if (node_access('create', $node)) {
297 $link['path'] = 'node/add/'. str_replace('_', '-', $node->type);
298 $link['options']['query'] = array('destination' => $_GET['q'], 'translation' => $node->nid, 'language' => $language);
299 }
300 else {
301 $link = array();
302 }
303 return theme('translation_overview_translation_link', 'missing', $link, $properties);
304 }
305
306 function theme_translation_overview_translation_link($state, $link = array(), $properties = array()) {
307 // Merge in defaults.
308 $properties += array(
309 'priority' => TRANSLATION_OVERVIEW_NORMAL,
310 'has_note' => FALSE,
311 'published' => TRUE,
312 'show_long' => TRUE,
313 );
314
315 $long = '&nbsp;';
316 $link['options']['html'] = TRUE;
317
318 $priorities = array(
319 TRANSLATION_OVERVIEW_HIGH => array('display' => t('High priority'), 'css' => 'trov-priority-high'),
320 TRANSLATION_OVERVIEW_NORMAL => array('display' => t('Normal'), 'css' => 'trov-priority-normal'),
321 TRANSLATION_OVERVIEW_IGNORE => array('display' => t('Ignored'), 'css' => 'trov-priority-ignore'),
322 );
323
324 $link_title = array();
325 if (!empty($priorities[$properties['priority']]['display'])) {
326 $link_title[] = $priorities[$properties['priority']]['display'];
327 }
328 if (empty($properties['published'])) {
329 $link_title[] = t('unpublished');
330 }
331 switch ($state) {
332 case 'original':
333 $long = t('Original');
334 $link_title[] = t('original');
335 break;
336 case 'current':
337 $long = t('Complete');
338 $link_title[] = t('completed translation');
339 break;
340 case 'outofdate':
341 $long = t('Out-of-date');
342 $link_title[] = t('out-of-date translation');
343 break;
344 case 'missing':
345 $long = t('Untranslated');
346 $link_title[] = t('untranslated content');
347 break;
348 }
349 if (!empty($properties['has_note'])) {
350 $link_title[] = t('with a note');
351 }
352
353 if (isset($link['path'])) {
354 if (preg_match('/node\/\d*\/edit/', $link['path'])) {
355 $action = t('Click to edit it.');
356 }
357 else if (preg_match('/node\/add/', $link['path'])) {
358 $action = t('Click to add it.');
359 }
360 else if (preg_match('/node\/\d*/', $link['path'])) {
361 $action = t('Click to view it.');
362 }
363 $link['options']['attributes'] = array('title' => drupal_ucfirst(trim(implode(', ', $link_title))) .'. '. $action);
364 }
365
366 $class_note = $properties['has_note'] ? 'trov-has-note' : '';
367 $class_state = $properties['published'] ? "trov-$state" : "trov-$state-unpublished";
368 $class_priority = $priorities[$properties['priority']]['css'];
369
370 $output = "<span class='trov-node $class_note'><span class='$class_state $class_priority'></span>";
371 $output .= "</span>";
372 if ($properties['show_long']) {
373 $output .= "<span class='trov-description'>$long</span>";
374 }
375 if (!empty($link['path'])) {
376 $output = l($output, $link['path'], $link['options']);
377 }
378 return $output;
379 }
380
381 /**
382 * Alter the schema when new languages are added.
383 */
384 function translation_overview_schema_add_submit($form, &$form_state) {
385 $fieldname = translation_overview_field_name($form_state['values']['langcode']);
386 if (!db_column_exists('translation_overview_priority', $fieldname)) {
387 $ret = array();
388 $field = array(
389 'type' => 'int',
390 'size' => 'tiny',
391 'unsigned' => TRUE,
392 'not null' => TRUE,
393 'default' => TRANSLATION_OVERVIEW_NORMAL,
394 );
395 $keys = array(
396 'indexes' => array($fieldname => array($fieldname))
397 );
398 db_add_field($ret, 'translation_overview_priority', $fieldname, $field, $keys);
399 watchdog('translation_overview', 'Added a column for %langcode to the translation_overview_priority table.', array('%langcode' => $form_state['values']['langcode']));
400 }
401 }
402
403 /**
404 * Alter the schema when languages are removed.
405 */
406 function translation_overview_schema_remove_submit($form, &$form_state) {
407 $fieldname = db_escape_table($form_state['values']['langcode']);
408 if (db_column_exists('translation_overview_priority', $fieldname)) {
409 db_drop_index($ret, 'translation_overview_priority', $fieldname);
410 db_drop_field($ret, 'translation_overview_priority', $fieldname);
411 watchdog('translation_overview', 'Dropped the %langcode column from the translation_overview_priority table.', array('%langcode' => $form_state['values']['langcode']));
412 }
413 }

  ViewVC Help
Powered by ViewVC 1.1.2