summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Reyero2009-04-27 14:22:28 (GMT)
committer Jose Reyero2009-04-27 14:22:28 (GMT)
commit1d40caddb97a341b4021af79cbb0724e11ee01e5 (patch)
treee05f01fade286081b054b764807be4c736b07e66
parent0698d0c8ad0f3e18e5531ae7624d39a6a1450136 (diff)
- Fixed typos, by ao2, #429850, dboulet, #396402
- New feature: allow to select existing node as translation, #295682
-rw-r--r--CHANGELOG.txt2
-rw-r--r--i18n.module27
-rw-r--r--i18n.pages.inc213
-rw-r--r--i18nsync/i18nsync.module2
4 files changed, 239 insertions, 5 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index c83b1cf..763d93d 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -2,6 +2,8 @@
6.x-1.0 to ........
--------------------
+- Fixed typos, by ao2, #429850, dboulet, #396402
+- New feature: allow to select existing node as translation, #295682
- Fixed: translation relationship broken with i18nsync, by smh67, Beban, #367118
- Fixed: translated node forgets its menu settings, by brucepearson, #364375
- Fixed: missing node languages for LANGUAGE_SUPPORT_EXTENDED_NOT_DISPLAYED, by Roger Lopez, #365574
diff --git a/i18n.module b/i18n.module
index 6c49e5f..8281839 100644
--- a/i18n.module
+++ b/i18n.module
@@ -94,7 +94,14 @@ function i18n_menu() {
'file' => 'i18n.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
-
+ // Autocomplete callback for nodes
+ $items['i18n/node/autocomplete'] = array(
+ 'title' => 'Node title autocomplete',
+ 'page callback' => 'i18n_node_autocomplete',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_CALLBACK,
+ 'file' => 'i18n.pages.inc',
+ );
return $items;
}
@@ -179,7 +186,7 @@ function i18n_translation_link_alter(&$links, $path) {
// Check for a node related path, and for its translations.
if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
- // make sure language support is set to LANUAGE_SUPPORT_EXTENDED, so links
+ // make sure language support is set to LANGUAGE_SUPPORT_EXTENDED, so links
// dont get added for LANGUAGE_SUPPORT_EXTENDED_NOT_DISPLAYED
if (variable_get('i18n_node_'. $node->type, LANGUAGE_SUPPORT_NORMAL) == LANGUAGE_SUPPORT_EXTENDED) {
$languages = language_list();
@@ -572,9 +579,23 @@ function i18n_form_alter(&$form, $form_state, $form_id) {
* - administer all languages
* Disables language conditions for administration pages, so the user can view objects for all languages at the same time.
* This applies for: menu items, taxonomy
+ * - administer translations
+ * Will allow to add/remove existing nodes to/from translation sets.
*/
function i18n_perm() {
- return array('administer all languages');
+ return array('administer all languages', 'administer translations');
+}
+
+/**
+ * Implementation of hook_theme()
+ */
+function i18n_theme() {
+ return array(
+ 'i18n_node_select_translation' => array(
+ 'arguments' => array('element' => NULL),
+ 'file' => 'i18n.pages.inc',
+ ),
+ );
}
/**
diff --git a/i18n.pages.inc b/i18n.pages.inc
index 032111f..0bf91db 100644
--- a/i18n.pages.inc
+++ b/i18n.pages.inc
@@ -59,5 +59,216 @@ function i18n_translation_node_overview($node) {
}
drupal_set_title(t('Translations of %title', array('%title' => $node->title)));
- return theme('table', $header, $rows);
+ $output = theme('table', $header, $rows);
+ if (user_access('administer translations')) {
+ $output .= drupal_get_form('i18n_node_select_translation', $node, $translations);
+ }
+ return $output;
+}
+
+/**
+ * Form to select existing nodes as translation
+ *
+ * This one uses autocomplete fields for all languages
+ */
+function i18n_node_select_translation($form_state, $node, $translations) {
+ $form['node'] = array('#type' => 'value', '#value' => $node);
+ $form['translations'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Select translations for %title', array('%title' => $node->title)),
+ '#tree' => TRUE,
+ '#theme' => 'i18n_node_select_translation',
+ '#description' => t('Alternatively, you can select existing nodes as translations of this one or remove nodes from this translation set. Only nodes that have the right language and don\'t belong to other translation set will be available here.')
+ );
+ foreach (language_list() as $language) {
+ if ($language->language != $node->language) {
+ $trans_nid = isset($translations[$language->language]) ? $translations[$language->language]->nid : 0;
+ $form['translations']['nid'][$language->language] = array('#type' => 'value', '#value' => $trans_nid);
+ $form['translations']['language'][$language->language] = array('#value' => $language->name);
+ $form['translations']['node'][$language->language] = array(
+ '#type' => 'textfield',
+ '#autocomplete_path' => 'i18n/node/autocomplete/' . $node->type . '/' . $language->language,
+ '#default_value' => $trans_nid ? i18n_node_nid2autocomplete($trans_nid) : '',
+ );
+ }
+ }
+ $form['buttons']['update'] = array('#type' => 'submit', '#value' => t('Update translations'));
+ //$form['buttons']['clean'] = array('#type' => 'submit', '#value' => t('Delete translation set'));
+ return $form;
+}
+
+/**
+ * Form validation
+ */
+function i18n_node_select_translation_validate($form, &$form_state) {
+ foreach ($form_state['values']['translations']['node'] as $lang => $title) {
+ if (!$title) {
+ $nid = 0;
+ }
+ else {
+ $nid = i18n_node_autocomplete2nid($title, "translations][node][$lang", array($node->type), array($lang));
+ }
+ $form_state['values']['translations']['nid'][$lang] = $nid;
+ }
+}
+
+/**
+ * Form submission: update / delete the translation set
+ */
+function i18n_node_select_translation_submit($form, &$form_state) {
+ $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : NULL;
+ $node = $form_state['values']['node'];
+ $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array($node->language => $node);
+ foreach ($translations as $trans) {
+ $current[$trans->language] = $trans->nid;
+ }
+ $update = array($node->language => $node->nid) + array_filter($form_state['values']['translations']['nid']);
+ // Compute the difference to see which are the new translations and which ones to remove
+ $new = array_diff_assoc($update, $current);
+ $remove = array_diff_assoc($current, $update);
+
+ // The tricky part: If the existing source is not in the new set, we need to create a new tnid
+ if ($node->tnid && in_array($node->tnid, $update)) {
+ $tnid = $node->tnid;
+ $add = $new;
+ }
+ else {
+ // Create new tnid, which is the source node
+ $tnid = $node->nid;
+ $add = $update;
+ }
+ // Now update values for all nodes
+ if ($add) {
+ $args = array('' => $tnid) + $add;
+ db_query('UPDATE {node} SET tnid = %d WHERE nid IN (' . db_placeholders($add) . ')', $args);
+ if (count($new)) {
+ drupal_set_message(format_plural(count($new), 'Added a node to the translation set.', 'Added @count nodes to the translation set.'));
+ }
+ }
+ if ($remove) {
+ db_query('UPDATE {node} SET tnid = 0 WHERE nid IN (' . db_placeholders($remove) . ')', $remove);
+ drupal_set_message(format_plural(count($remove), 'Removed a node from the translation set.', 'Removed @count nodes from the translation set.'));
+ }
+}
+
+/**
+ * Node title autocomplete callback
+ */
+function i18n_node_autocomplete($type, $language, $string = '') {
+ $params = array('type' => $type, 'language' => $language, 'tnid' => 0);
+ $matches = array();
+ foreach (_i18n_node_references($string, 'contains', $params) as $id => $row) {
+ // Add a class wrapper for a few required CSS overrides.
+ $matches[$row['title'] ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
+ }
+ drupal_json($matches);
}
+
+/**
+ * Generates 'title [nid:$nid]' for the autocomplete field
+ */
+function i18n_node_nid2autocomplete($nid) {
+ if ($node = node_load($nid)) {
+ return check_plain($node->title) . ' [nid:' . $nid .']';
+ }
+ else {
+ return t('Not found');
+ }
+}
+
+/**
+ * Reverse mapping from node title to nid
+ *
+ * We also handle autocomplete values (title [nid:x]) and validate the form
+ */
+function i18n_node_autocomplete2nid($name, $field = NULL, $type, $language) {
+ if (!empty($name)) {
+ preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $name, $matches);
+ if (!empty($matches)) {
+ // Explicit [nid:n].
+ list(, $title, $nid) = $matches;
+ if (!empty($title) && ($node = node_load($nid)) && $title != $node->title) {
+ if ($field) {
+ form_set_error($field, t('Node title mismatch. Please check your selection.'));
+ }
+ $nid = NULL;
+ }
+ }
+ else {
+ // No explicit nid.
+ $reference = _i18n_node_references($name, 'equals', array('type' => $type, 'language' => $language), 1);
+ if (!empty($reference)) {
+ $nid = key($reference);
+ }
+ elseif ($field) {
+ form_set_error($field, t('Found no valid post with that title: %title', array('%title' => $name)));
+ }
+ }
+ }
+ return !empty($nid) ? $nid : NULL;
+}
+
+/**
+ * Find node title matches.
+ *
+ * @param $string
+ * String to match against node title
+ * @param $match
+ * Match mode: 'contains', 'equals', 'starts_with'
+ * @param $params
+ * Other query arguments: type, language or numeric ones
+ *
+ * Some code from CCK's nodereference.module
+ */
+function _i18n_node_references($string, $match = 'contains', $params = array(), $limit = 10) {
+ $where = $args = array();
+ $match_operators = array(
+ 'contains' => "LIKE '%%%s%%'",
+ 'equals' => "= '%s'",
+ 'starts_with' => "LIKE '%s%%'",
+ );
+ foreach ($params as $key => $value) {
+ $type = in_array($key, array('type', 'language')) ? 'char' : 'int';
+ if (is_array($value)) {
+ $where[] = "n.$key IN (" . db_placeholders($value, $type) . ') ';
+ $args = array_merge($args, $value);
+ }
+ else {
+ $where[] = "n.$key = " . db_type_placeholder($type);
+ $args[] = $value;
+ }
+ }
+ $where[] = 'n.title '. (isset($match_operators[$match]) ? $match_operators[$match] : $match_operators['contains']);
+ $args[] = $string;
+ // Disable and reenable i18n selection mode so no language conditions are inserted
+ i18n_selection_mode('off');
+ $sql = db_rewrite_sql('SELECT n.nid, n.title, n.type FROM {node} n WHERE ' . implode(' AND ', $where) . ' ORDER BY n.title, n.type');
+ $result = db_query_range($sql, $args, 0, $limit) ;
+ i18n_selection_mode('reset');
+ $references = array();
+ while ($node = db_fetch_object($result)) {
+ $references[$node->nid] = array(
+ 'title' => check_plain($node->title),
+ 'rendered' => check_plain($node->title),
+ );
+ }
+ return $references;
+}
+
+/**
+ * Theme select translation form
+ * @ingroup themeable
+ */
+function theme_i18n_node_select_translation(&$elements) {
+ $output = '';
+ $rows = array();
+ foreach (element_children($elements['nid']) as $lang) {
+ $rows[] = array(
+ drupal_render($elements['language'][$lang]),
+ drupal_render($elements['node'][$lang]),
+ );
+ }
+ $output .= theme('table', array(), $rows);
+ $output .= drupal_render($elements);
+ return $output;
+} \ No newline at end of file
diff --git a/i18nsync/i18nsync.module b/i18nsync/i18nsync.module
index 592ee79..383e8eb 100644
--- a/i18nsync/i18nsync.module
+++ b/i18nsync/i18nsync.module
@@ -205,7 +205,7 @@ function i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
}
}
$i18nsync = FALSE;
- drupal_set_message(format_plural(count($translations) - 1, 'One node translation have been synchronized.', 'All @count node translations have been synchronized.'));
+ drupal_set_message(format_plural(count($translations) - 1, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
}
break;
}