summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Reyero2007-05-03 09:57:07 (GMT)
committer Jose Reyero2007-05-03 09:57:07 (GMT)
commitb9c95c57f8a24e5098d3f3e7acaed69de3a1268b (patch)
tree06c5ea8cd82cd4eaf10a2016828d67cb2e11dcbd
parentd54fdd05a91f167a95cda0fe5b8b3a7816b7de6a (diff)
- Backported i18nsync module, by Alex Barth4.7.x-1.1
- Fixed wrong query, #76689
-rw-r--r--contrib/i18nsync.install8
-rw-r--r--contrib/i18nsync.module107
-rw-r--r--translation.module2
3 files changed, 116 insertions, 1 deletions
diff --git a/contrib/i18nsync.install b/contrib/i18nsync.install
new file mode 100644
index 0000000..9fbd534
--- /dev/null
+++ b/contrib/i18nsync.install
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Set module weight
+ * Make sure this runs after taxonomy, i18n and translation modules
+ */
+function i18nsync_install() {
+ db_query("UPDATE {system} SET weight = 10 WHERE name = 'i18nsync' AND type = 'module'");
+} \ No newline at end of file
diff --git a/contrib/i18nsync.module b/contrib/i18nsync.module
new file mode 100644
index 0000000..5f16971
--- /dev/null
+++ b/contrib/i18nsync.module
@@ -0,0 +1,107 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Internationalization (i18n) package. Synchronization of translations
+ *
+ * Keeps vocabulary terms in sync for translations.
+ * This is a per-vocabulary option
+ *
+ * Ref: http://drupal.org/node/115463
+ *
+ * Notes:
+ * This module needs to run after taxonomy, i18n, translation. Check module weight
+ */
+
+ /**
+ * Implementation of hook_form_alter().
+ */
+function i18nsync_form_alter($form_id, &$form) {
+ if ($form_id == 'taxonomy_form_vocabulary') { // Taxonomy vocabulary
+ $nodesync = variable_get('i18n_vocabulary_nodesync', array());
+ $form['i18n']['nodesync'] = array(
+ '#type' => 'checkbox', '#title' => t('Synchronize node translations'),
+ '#default_value' => isset($form['vid']) && is_numeric($form['vid']['#value']) && $nodesync[$form['vid']['#value']],
+ '#description' => t('Synchronize terms of this vocabulary for node translations.')
+ );
+ }
+}
+
+/**
+ * Implementation of hook taxonomy.
+ */
+function i18nsync_taxonomy($op, $type = NULL, $edit = NULL) {
+ switch ("$type/$op") {
+ case 'vocabulary/insert':
+ case 'vocabulary/update':
+ $current = variable_get('i18n_vocabulary_nodesync', array());
+ if ($edit['nodesync']) {
+ $current[$edit['vid']] = 1;
+ } else {
+ unset($current[$edit['vid']]);
+ }
+ variable_set('i18n_vocabulary_nodesync', $current);
+ break;
+ }
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+ if (variable_get("i18n_node_$node->type", 0)) {
+ switch ($op) {
+ case 'insert':
+ case 'submit':
+ // Taxonomy synchronization
+ if($node->language && $node->trid && $sync = variable_get('i18n_vocabulary_nodesync', array())) {
+ // Get vocabularies synchronized for this node type
+ $result = db_query("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' AND v.vid IN (%s)", $node->type, implode(',', array_keys($sync)));
+ $count = 0;
+ while($vocabulary = db_fetch_object($result)) {
+ i18nsync_node_taxonomy($node, $vocabulary);
+ $count++;
+ }
+ if ($count) {
+ drupal_set_message(t('Node translations have been synchronized.'));
+ }
+ }
+ // No need to refresh cache. It will be refreshed on node insert/update anyway
+ break;
+ }
+ }
+}
+
+/**
+ * Actual synchronization for node, vocabulary
+ *
+ * These are the 'magic' db queries.
+ */
+function i18nsync_node_taxonomy($node, $vocabulary) {
+ // Paranoid extra check. These queries may really delete data
+ if ($vocabulary->language || !$node->nid || !$node->trid || !$node->language || !$vocabulary->vid) return;
+ drupal_set_message(dprint_r($node, true) . dprint_r($vocabulary, true));
+ // Reset all terms for this vocabulary for other nodes in the translation set
+ db_query("DELETE FROM {term_node} WHERE nid != %d ".
+ " AND nid IN (SELECT nid FROM {i18n_node} WHERE trid = %d) ".
+ " AND tid IN (SELECT tid FROM {term_data} WHERE vid = %d)",
+ $node->nid, $node->trid, $vocabulary->vid);
+
+ // Copy terms with no language
+ db_query("INSERT INTO {term_node}(tid, nid) SELECT tn.tid, n.nid " .
+ " FROM {i18n_node} n , {term_node} tn " .
+ " INNER JOIN {term_data} td ON tn.tid = td.tid " . // This one to check no language
+ " WHERE tn.nid = %d AND n.nid != %d AND n.trid = %d AND td.vid = %d " .
+ " AND td.language = '' OR td.language IS NULL", // Only terms without language
+ $node->nid, $node->nid, $node->trid, $vocabulary->vid);
+
+ // Now copy terms translating on the fly
+ db_query("INSERT INTO {term_node}(tid, nid) SELECT tdt.tid, n.nid " .
+ " FROM {i18n_node} n , {term_data} tdt " . // This will be term data translations
+ " INNER JOIN {term_data} td ON tdt.trid = td.trid " . // Same translation set
+ " INNER JOIN {term_node} tn ON tn.tid = td.tid " .
+ " INNER JOIN {term_data} tnd ON tn.tid = tnd.tid ".
+ " WHERE tdt.language = n.language AND n.nid != %d AND tn.nid = %d AND n.trid = %d AND td.vid = %d",
+ $node->nid, $node->nid, $node->trid, $vocabulary->vid);
+}
diff --git a/translation.module b/translation.module
index 6ca807c..188f4db 100644
--- a/translation.module
+++ b/translation.module
@@ -312,7 +312,7 @@ function translation_node_page() {
break;
case 'remove':
case t('Remove'):
- db_query("UPDATE {i18n_node} SET trid = NULL WHERE nid=%d", $node->nid);
+ db_query("UPDATE {i18n_node} SET trid = 0 WHERE nid=%d", $node->nid);
drupal_set_message("The node has been removed from the translation set");
drupal_goto("node/$node->nid/translation");
default: