Removed previous node.module patch
[project/i18n.git] / i18n_node.inc
CommitLineData
d469e0bb
JR
1<?php
2// $Id$
3
4/**
5 * Internationalization (i18n) module
6 *
7 * @author Jose A. Reyero, 2004
8 *
9 * Multilingual Nodes support
10 */
11
12/**
c6713f3d
JR
13 * Implementation of hook_node_rewrite_sql()
14 */
15function i18n_node_rewrite_sql($hint,$node_alias){
16 $result = array();
17 if ($hint & NODE_REWRITE_SQL_RELATED_LIST) {
18 $result['join'] = "INNER JOIN {node} USING(nid)";
19 $result['where'] = "{node}.language='".i18n_get_lang()."'";
20 } elseif ( $hint & NODE_REWRITE_SQL_SEARCH) {
21
22 } else {
23 $result['where'] = "$node_alias.language ='".i18n_get_lang()."'" ;
24 }
25 return $result;
26}
27/**
d469e0bb
JR
28 * This is the callback for the tab 'translations' for nodes
29 */
30function i18n_node_translation() {
31 $op = $_POST['op'];
32 $edit = $_POST['edit'];
33
34 if (empty($op)) {
35 $op = arg(2);
36 }
37
38 switch ($op) {
39 case 'translation':
40 $output = i18n_node_translation_status(arg(1));
41 break;
42 }
43 print theme('page', $output);
44}
45
46function i18n_node_translation_status($nid) {
47 $languages = i18n_supported_languages();
48 $translations = i18n_node_get_translations($nid, true);
49 if (!$translations) {
50 $node = node_load(array('nid' => $nid));
51 $translations[$node->language]= $node;
52 }
53 foreach ($languages as $lang => $name) {
54 if (array_key_exists($lang, $translations)) {
55 $node = $translations[$lang];
56 $rows[] = array($name, l($node->title, 'node/'.$node->nid), l(t('edit'), 'node/'.$node->nid.'/edit'));
57 if ($node->nid == $nid) {
58 drupal_set_title(t('Translations of ').$node->title);
59 }
60 }
61 else {
62 $rows[] = array($name, t('Not translated'), l(t('create translation'), 'translation/node/'.$nid.'/'.$lang));
63 }
64 }
65 $header = array(t('Language'), t('Title'), t('Actions'));
66 $output .= t('<h2>Translation Status</h2>') .'<br />';
67 $output .= theme('table', $header, $rows);
68 return $output;
69}
70
71/**
72 * Hook nodeapi
73 */
74function i18n_nodeapi(&$node, $op, $arg = 0) {
75
76 if (variable_get("i18n_node_$node->type", 0)) {
77 switch ($op) {
78 case 'fields':
79 return array('language');
80 break;
81
82 case 'validate':
83 break;
84
85 case 'insert':
86 if ( arg(0) == 'translation' ) {
87 i18n_node_create_translation($node->nid, $node->language, $node->i18n_snid);
88 }
89 break;
90
91 case 'delete':
92 db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid);
93 break;
94
95 case 'update':
96 // Check language changed?
97 break;
98
99 case 'form pre':
100 $languages = i18n_supported_languages();
101 if ($node->nid) {
102 $translations = i18n_node_get_translations($node->nid);
103 }
104 elseif (arg(0) == 'translation') {
105 // We are translating a node
106 $snid = arg(2);
107 $node->language = arg(3);
108 $form = form_hidden('i18n_snid', $node->i18n_snid ? $node->i18n_snid : $snid);
109 // Load the node to be translated
110 $trans = node_load(array('nid' => $snid));
111 foreach ($trans as $field=>$value) {
112 if ($field != 'nid' && empty($node->$field)) {
113 $node->$field = $value;
114 }
115 }
116 $translations = i18n_node_get_translations($snid, true);
117
118 if (!$translations) {
119 $translations[$trans->language]= $trans;
120 }
121 }
122
123 if ($translations) {
124 foreach ($languages as $lang => $langname) {
125 if ($trans = $translations[$lang]) {
126 $rows[] = array($langname, l($trans->title, 'node/'.$trans->nid.'/edit'));
127 }
128 else {
129 $langselect[$lang]= $langname;
130 }
131 }
132 }
133 else {
134 $langselect = $languages;
135 }
136
137 $langselect = array_merge(array('' => ''), $langselect);
138 $form .= form_select(t('Language'), 'language', $node->language ? $node->language : i18n_get_lang(), $langselect);
139 if ($rows) {
140 $header = array(t('Language'), t('Title'));
141 $form .= form_group(t('translations'), theme('table', $header, $rows));
142 }
143
144 return $form;
145 break;
146 }
147 }
148}
149
150function i18n_node_available_langs($node) {
151 $languages = i18n_supported_languages();
152 if ($node->nid) {
153 $usedlangs = i18n_node_get_translations($node->nid);
154 }
155 elseif ($node->i18n_snid) {
156 $usedlangs = i18n_node_get_translations($node->i18n_snid, true);
157 }
158 else {
159 return $languages;
160 }
161
162 foreach ($languages as $lang=>$name) {
163 if (!array_key_exists($lang, $usedlangs)) {
164 $newlangs[$lang] = $name;
165 }
166 }
167
168 return $newlangs;
169
170}
171
172function i18n_node_get_lang($nid, $default = '') {
173 $lang = db_result(db_query('SELECT language FROM {node} WHERE nid=%d',$nid));
174 return $lang ? $lang : $default ;
175}
176
177/**
178 * getall = true to get the also node itself
179 */
180
181function i18n_node_get_translations($nid, $getall = false) {
182 $sql = 'SELECT n.nid, n.language, n.title, a.trid FROM {node} n INNER JOIN {i18n_node} a ON n.nid = a.nid INNER JOIN {i18n_node} b ON a.trid = b.trid AND b.nid =%d';
183 if ($getall) {
184 $result = db_query($sql, $nid);
185 }
186 else {
187 $result = db_query($sql . ' WHERE n.nid != %d', $nid, $nid);
188 }
189 $items = array();
190 while ($node = db_fetch_object($result)) {
191 $items[$node->language] = $node;
192 }
193 //debug($items);
194 return $items;
195}
196
197// snid = source nid
198function i18n_node_create_translation($nid, $lang, $snid) {
199 $trid = db_result(db_query('SELECT trid FROM {i18n_node} WHERE nid=%d', $snid));
200 if (!$trid) {
201 $trid = db_next_id('{i18n_node}_trid');
202 db_query('INSERT INTO {i18n_node} (trid, nid) VALUES (%d, %d)', $trid, $snid);
203 }
204 db_query('INSERT INTO {i18n_node}(trid, nid) VALUES (%d, %d)', $trid, $nid);
205}
206
207function i18n_node_where_sql() {
208 return " AND ( n.language='" . i18n_get_lang(). "' OR n.language='')";
209}
210
211function i18n_get_node_type($nid) {
212 return db_result(db_query('SELECT type FROM {node} WHERE nid=%d', $nid));
213}
214
215?>