| 1 |
<?php
|
| 2 |
// $Id: translation_helpers.module,v 1.5 2009/08/17 04:54:15 nedjo Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides methods for other modules to use with translated content.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_nodeapi().
|
| 11 |
*
|
| 12 |
* Manages translation information for nodes.
|
| 13 |
*/
|
| 14 |
function translation_helpers_nodeapi(&$node, $op, $teaser, $page) {
|
| 15 |
// Only act if we are dealing with a content type supporting translations.
|
| 16 |
if ($op == 'delete' && translation_supported_type($node->type)) {
|
| 17 |
translation_helpers_invoke_translation_change($node);
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Call hook_nodeapi() to respond to a change in source translation.
|
| 23 |
*
|
| 24 |
* Follows logic in translation_remove_from_set().
|
| 25 |
*
|
| 26 |
* Sample implementation:
|
| 27 |
* <code>
|
| 28 |
* function example_nodeapi() {
|
| 29 |
* switch ($op) {
|
| 30 |
* case 'translation_change':
|
| 31 |
* if (isset($node->translation_change)) {
|
| 32 |
* // If there is only one node remaining, track by nid rather than tnid. Otherwise, use
|
| 33 |
* // the new tnid.
|
| 34 |
* $new = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
|
| 35 |
* db_query('UPDATE {example} SET id = %d WHERE id = %d', $new, $node->translation_change['old_tnid']);
|
| 36 |
* }
|
| 37 |
* break;
|
| 38 |
* }
|
| 39 |
* }
|
| 40 |
* </code>
|
| 41 |
*/
|
| 42 |
function translation_helpers_invoke_translation_change($node) {
|
| 43 |
if (isset($node->tnid)) {
|
| 44 |
if (db_result(db_query('SELECT COUNT(*) FROM {node} WHERE tnid = %d', $node->tnid)) == 1) {
|
| 45 |
// There is only one node left in the set.
|
| 46 |
$node->translation_change = array(
|
| 47 |
'old_tnid' => $node->tnid,
|
| 48 |
'new_tnid' => 0,
|
| 49 |
// Determine the remaining former member of the translation set.
|
| 50 |
// May be needed e.g. to reassign existing data from the tnid to this nid.
|
| 51 |
'remaining_nid' => db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d', $node->tnid)),
|
| 52 |
);
|
| 53 |
// Allow other modules to respond to the removal of this translation set.
|
| 54 |
node_invoke_nodeapi($node, 'translation_change');
|
| 55 |
}
|
| 56 |
else {
|
| 57 |
// If the node being removed was the source of the translation set,
|
| 58 |
// we pick a new source - preferably one that is up to date.
|
| 59 |
if ($node->tnid == $node->nid) {
|
| 60 |
$node->translation_change = array(
|
| 61 |
'old_tnid' => $node->tnid,
|
| 62 |
'new_tnid' => db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d ORDER BY translate ASC, nid ASC', $node->tnid)),
|
| 63 |
);
|
| 64 |
// Allow other modules to respond to the changed source for this translation set.
|
| 65 |
node_invoke_nodeapi($node, 'translation_change');
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Return the source translation of a node.
|
| 73 |
*/
|
| 74 |
function translation_helpers_get_source($node) {
|
| 75 |
if (isset($node->tnid) && translation_supported_type($node->type)) {
|
| 76 |
// A node can be its own source.
|
| 77 |
if ($node->nid == $node->tnid) {
|
| 78 |
return $node;
|
| 79 |
}
|
| 80 |
return node_load($node->tnid);
|
| 81 |
}
|
| 82 |
return FALSE;
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Return the translation of a node in a given language.
|
| 87 |
*/
|
| 88 |
function translation_helpers_get_translation($node, $language) {
|
| 89 |
if (isset($node->tnid) && translation_supported_type($node->type)) {
|
| 90 |
$translations = translation_node_get_translations($node->tnid);
|
| 91 |
if (isset($translations[$language])) {
|
| 92 |
return node_load($translations[$language]->nid);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
return FALSE;
|
| 96 |
}
|