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