diff --git a/contrib/i18nsync.install b/contrib/i18nsync.install new file mode 100644 index 0000000000000000000000000000000000000000..9fbd53438733ca9067fc3c04e966ae74874f05b9 --- /dev/null +++ b/contrib/i18nsync.install @@ -0,0 +1,8 @@ + '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 6ca807c462759f48a5e1d17079d52bdaa0ed9256..188f4db76a75cfbc65b14a526b8f0fa40137e113 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: