Updated patches to HEAD. Changed default i18n_supported_langs to 'locale'.
[project/i18n.git] / i18n_taxonomy.inc
1 <?php
2 // $Id$
3
4 /**
5 * Internationalization (i18n) module
6 *
7 * @author Jose A. Reyero, 2004
8 *
9 * Multilingual Taxonomy support
10 */
11
12 /**
13 * This is the callback for taxonomy translations
14 *
15 * Gets the urls:
16 * admin/taxonomy/i18n/term/xx
17 * admin/taxonomy/i18n/term/new/xx
18 */
19
20 function i18n_taxonomy_admin() {
21 $op = $_POST['op'] ? $_POST['op'] : arg(3);
22 $edit = $_POST['edit'];
23 switch ($op) {
24 case 'term':
25 print theme('page', i18n_taxonomy_term_form(arg(4), $edit), t('Term Translations'));
26 break;
27
28 case t('Submit'):
29 drupal_set_title(t('Submit'));
30 i18n_taxonomy_term_save($edit);
31 print theme('page', i18n_taxonomy_overview(), t("Term Translations"));
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', i18n_taxonomy_overview(), t("Term Translations"));
40 }
41 }
42
43 /**
44 * Generate a tabular listing of translations for vocabularies.
45 */
46
47 function i18n_taxonomy_overview() {
48 $languages = i18n_supported_languages();
49 $header = array_merge($languages, array(''));
50 $vocabularies = taxonomy_get_vocabularies();
51 //rows[] = array();
52 foreach ($vocabularies as $vocabulary) {
53 $links = array();
54 $types = array();
55
56 $rows[] = array(array('data' => $vocabulary->name, 'colspan' => count($languages), 'align' => 'center'),
57 l(t('new translation'), 'admin/taxonomy/i18n/term/new/'.$vocabulary->vid));
58 // Get terms/translations for this vocab
59 $result = db_query('SELECT t.*,i.trid FROM {term_data} t LEFT JOIN {i18n_taxonomy_term} i USING(tid) WHERE t.vid=%d',$vocabulary->vid);
60 $list = array();
61 while ($data = db_fetch_object($result)) {
62 $list[] = $data;
63 }
64 if (!empty($list)) {
65 $data = array();
66 foreach ($list as $term) {
67 if ($term->language) {
68 if ($term->trid) {
69 if (!array_key_exists($term->trid, $data)) {
70 $data[$term->trid] = array();
71 }
72 $data[$term->trid][$term->language]= $term;
73 }
74 else {
75 $untranslated[$term->language][] = $term;
76 }
77 }
78 }
79 // Reorder data for rows and languages
80 foreach ($data as $trid => $terms) {
81 $thisrow = array();
82 foreach ($languages as $lang => $name) {
83 if (array_key_exists($lang, $terms)) {
84 $thisrow[] = $terms[$lang]->name;
85 }
86 else {
87 $thisrow[] = '--';
88 }
89 }
90 $thisrow[] = l(t('edit translations'), 'admin/taxonomy/i18n/term/'.$trid);
91 $rows[] = $thisrow;
92 }
93 }
94 }
95
96 return theme('table', $header, $rows);
97 }
98
99 function i18n_taxonomy_term_save($edit) {
100 // debug($edit);
101 $languages = i18n_supported_languages();
102 $translations = array();
103 foreach ($languages as $lang => $name) {
104 $tid = $edit[$lang]['tid'];
105 $old = $edit[$lang]['old'];
106 if ($tid) {
107 $translations[] = $tid;
108 }
109 }
110 $trid = $edit['trid'];
111 // Remove old translations
112 if (is_numeric($trid)){
113 db_query('DELETE FROM {i18n_taxonomy_term} WHERE trid=%d',$trid);
114 }
115 // Save new translations
116 if (!empty($translations)) {
117 // Save translations
118 if ($trid =='new') {
119 // New translation group
120 $trid = db_next_id('{i18n_taxonomy_term}_trid');
121 }
122 foreach ($translations as $tid ) {
123 //$value = $new ? $trid : 'null';
124 //db_query('UPDATE {i18n_taxonomy_term} SET trid=%d WHERE tid=%d', $value, $tid);
125 db_query('INSERT INTO {i18n_taxonomy_term}(trid,tid) VALUES(%d,%d)',$trid,$tid);
126
127 }
128 drupal_set_message(t('Term translations have been updated'));
129 }
130
131 }
132 function i18n_taxonomy_term_form($trid) {
133 $languages = i18n_supported_languages();
134 if ($trid == 'new') {
135 $translations = array();
136 $vid = arg(5);
137 }
138 else {
139 $translations =i18n_taxonomy_get_translations($trid);
140 $vid = $translations[key($translations)]->vid;
141 }
142 $vocabulary = taxonomy_get_vocabulary($vid);
143
144 // List of terms for languages
145 foreach ($languages as $lang => $langname) {
146 if ($trans = $translations[$lang]) {
147 $field1 = l($trans->name, 'admin/taxonomy/edit/term/'.$trans->tid);
148 $tidtrans = $trans->tid;
149 }
150 else {
151 $field1 = "--";
152 $tidtrans = 0;
153 }
154 // Available terms for this language
155 $list = i18n_array2list(i18n_vocabulary_get_terms($vid, $lang, 'all'), 'tid', 'name');
156 $list[0] ='';
157 $field2 = form_select('',
158 "$lang][tid",
159 $tidtrans,
160 $list);
161 $field2 .= form_hidden("$lang][old", $tidtrans);
162 $rows[] = array($langname, $field1, $field2);
163 }
164 $header = array(t('Language'), t('current translation'), t('change translation'));
165 // Prepares html form
166 $output = form_item(t("Vocabulary"), $vocabulary->name);
167 $output .= form_hidden('trid', $trid);
168 $output .= theme('table', $header, $rows);
169 $output .= form_submit(t('Submit'));
170 return form($output);
171 }
172
173 /**
174 * Adds language and translation fields to term edition form
175 *
176 * This is a hook for taxonomy module
177 */
178
179 function i18n_taxonomy_form_term($edit = array()) {
180 $tid = $edit['tid'];
181 $termlang= $edit['language'];
182 $vid = isset($edit['vid']) ? $edit['vid'] : arg(4);
183 // $trid= $edit['trid'];
184
185 $languages = i18n_supported_languages();
186 $selectlang = array_merge(array('' => ''), $languages);
187
188 $vocabulary = taxonomy_get_vocabulary($vid);
189 // If vocabulary has language, this should be the one for the term
190 if ($vocabulary->language) {
191 $edit['language'] = $vocabulary->language;
192 $output = form_item(t('Language'),$languages[$vocabulary->language],t('The vocabulary already has a language.'));
193 }
194 elseif ($termlang && $translations=i18n_term_get_translations($tid)) {
195 // Term has translations. Not all languages are valid
196 $selectlang = array('' => '');
197 foreach ($languages as $lang => $langname) {
198 if ($trans = $translations[$lang]) {
199 $rows[] = array($langname, l($trans->name, 'admin/taxonomy/edit/term/'.$trans->tid));
200 }
201 else {
202 $selectlang[$lang] = $langname;
203 }
204 }
205 $output = form_select(t('Language'), 'language', $termlang, $selectlang);
206
207 $header = array(t('Language'), t('translated term'));
208 $output .= form_group(t('term translations'), theme('table', $header, $rows));
209
210 } else {
211 $output = form_select(t('Language'), 'language', $termlang, $selectlang);
212 }
213 return $output;
214 }
215
216 function i18n_taxonomy_form_vocabulary($edit = array()) {
217 $tid = $edit['tid'];
218 $lang= $edit['language'];
219 $trid= $edit['trid'];
220
221 $languages = i18n_supported_languages();
222 $selectlang = array_merge(array('' => ''), $languages);
223 $output = form_select(t('Language'), 'language', $lang, $selectlang, t('This language will be set for all terms in this vocabulary'));
224 return $output;
225 }
226
227 /* Returns an url for the translated taxonomy-page, if exists */
228 function i18n_taxonomy_l($str_tids, $lang) {
229 if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
230 $separator = '+';
231 // The '+' character in a query string may be parsed as ' '.
232 $tids = preg_split('/[+ ]/', $str_tids);
233 }
234 else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
235 $separator = ',';
236 $tids = explode(',', $str_tids);
237 }
238 else {
239 return;
240 }
241
242 $translated_tids = array();
243 foreach ($tids as $tid) {
244 if ($translated_tid = i18n_term_get_translations($tid)) {
245 $translated_tids[] = $translated_tid[$lang]->tid;
246 }
247 }
248
249 return implode($separator, $translated_tids);
250 }
251
252 function i18n_taxonomy_where_sql($table, $type, $param = '') {
253 //debug("$table $type $param");
254 switch ($type) {
255 case 'node': // Param is nid
256 $lang = i18n_node_get_lang($param, i18n_get_lang());
257 return "AND $table.language in ('$lang', '')";
258 case 'guess': // This is a difficult one :-). Guess from path
259 return i18n_taxonomy_where_sql_guess($table);
260 default:
261 return "AND $table.language in ('". i18n_get_lang() . "','')";
262 }
263 }
264
265 function i18n_taxonomy_where_sql_guess($table) {
266 switch (arg(0)) {
267 case 'node':
268 if (is_numeric($nid = arg(1))) {
269 $lang = i18n_node_get_lang($nid, i18n_get_lang());
270 }
271 else {
272 $lang = i18n_get_lang();
273 }
274 break;
275
276 case 'admin':
277 // Get all languages
278 }
279 if ($lang) {
280 return "AND $table.language in ('$lang','')";
281 }
282 else {
283 return '';
284 }
285 }
286 /**
287 * Converts a list of arrays to an array of the form keyfield => namefield
288 */
289
290 function i18n_array2list($data, $keyfield, $namefield = 'name') {
291 foreach ($data as $key => $value) {
292 if (is_array($data)) {
293 $list[$value[$keyfield]] = $value[$namefield];
294 }
295 else {
296 $list[$value->$keyfield] = $value->$namefield;
297 }
298 }
299 return $list;
300 }
301
302 // returns array lang > term
303 function i18n_term_get_translations($tid, $getall = false) {
304 $sql = 'SELECT t.* FROM {term_data} t INNER JOIN {i18n_taxonomy_term} i USING(tid) INNER JOIN {i18n_taxonomy_term} i2 ON i.trid = i2.trid WHERE i2.tid=%d';
305 if ($getall) {
306 $result = db_query($sql, $tid);
307 }
308 else {
309 $result = db_query($sql. ' AND t.tid != %d',$tid, $tid);
310 }
311 while ($data = db_fetch_object($result)) {
312 $list[$data->language] = $data;
313 }
314 return $list;
315 }
316
317 function i18n_taxonomy_get_translations($trid) {
318 $result = db_query('SELECT t.*,i.trid FROM {term_data} t JOIN {i18n_taxonomy_term} i USING(tid) WHERE i.trid=%d',$trid);
319 $list = array();
320 while ($data = db_fetch_object($result)) {
321 $list[$data->language] = $data;
322 }
323 return $list;
324 }
325
326 function i18n_db_get_arrays($sql) {
327 $result = db_query($sql);
328 $list = array();
329 while ($data = db_fetch_array($result)) {
330 $list[] = $data;
331 }
332 return $list;
333 }
334
335 function i18n_db_get_objects($sql) {
336 $result = db_query($sql);
337 $list = array();
338 while ($data = db_fetch_object($result)) {
339 $list[] = $data;
340 }
341 return $list;
342 }
343 // returns array lang > term
344 function i18n_vocabulary_get_terms($vid, $lang, $status = 'all') {
345 if ($status=='translated') {
346 $andsql = ' AND i.trid > 0';
347 }elseif ( $status =='untranslated' ) {
348 $andsql = ' AND (i.trid = 0 or i.trid is null)';
349 }
350
351 $result = db_query("SELECT t.*, i.trid FROM {term_data} t LEFT JOIN {i18n_taxonomy_term} i USING(tid) WHERE t.vid=%d AND t.language='%s' " . $andsql, $vid, $lang);
352 $list = array();
353 while ($term = db_fetch_array($result)) {
354 $list[] = $term;
355 }
356 return $list;
357 }
358 /**
359 * Taxonomy hook
360 * $object parameter is an array, not an object !!
361 */
362 // $op = insert, update, delete
363 function i18n_taxonomy($op, $type, $edit) {
364 switch ("$type/$op") {
365 case 'term/insert':
366 case 'term/update':
367 if (!$edit['language']) {
368 // Removed language, remove trid
369 db_query('DELETE FROM {i18n_taxonomy_term} WHERE tid=%d', $edit['tid']);
370 drupal_set_message(t('Removed translation info from term'));
371 }
372 break;
373 case 'term/form pre':
374 return i18n_taxonomy_form_term($edit);
375 break;
376 case 'term/delete':
377 db_query('DELETE FROM {i18n_taxonomy_term} WHERE tid=%d', $edit['tid']);
378 break;
379 case 'vocabulary/form pre':
380 return i18n_taxonomy_form_vocabulary($edit);
381 break;
382 case 'vocabulary/update':
383 if (isset($edit['language'])) {
384 db_query("UPDATE {term_data} SET language='%s' WHERE vid='%d'",$edit['language'],$edit['vid']);
385 }
386 break;
387 }
388 }
389
390 ?>