summaryrefslogtreecommitdiffstats
path: root/i18n_taxonomy.inc
blob: 5b19d58f158fdf40dc104bc0b3a2e36b3c38af84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
// $Id$

/**
 * Internationalization (i18n) module
 *
 * @author Jose A. Reyero, 2004
 * 
 * Multilingual Taxonomy support
 */
 
/**
 * This is the callback for taxonomy translations
 * 
 * Gets the urls:
 * 		admin/taxonomy/i18n/term/xx
 * 		admin/taxonomy/i18n/term/new/xx
 */

function i18n_taxonomy_admin() {
  $op = $_POST['op'] ? $_POST['op'] : arg(3);
  $edit = $_POST['edit'];
  switch ($op) {
     case 'term':
      print theme('page', i18n_taxonomy_term_form(arg(4), $edit), t('Term Translations'));
      break;
      
     case t('Submit'):
      drupal_set_title(t('Submit'));
      i18n_taxonomy_term_save($edit);
      print theme('page', i18n_taxonomy_overview(), t("Term Translations"));
      break;
      
    case t('Delete'):
      //print theme('page', node_delete($edit), t('Delete'));
      break;
      
    default:
      print theme('page', i18n_taxonomy_overview(), t("Term Translations"));
  }      
}  
    
/**
 * Generate a tabular listing of translations for vocabularies.
 */

function i18n_taxonomy_overview() {
  $languages = i18n_supported_languages();
  $header = array_merge($languages, array(''));
  $vocabularies = taxonomy_get_vocabularies();
  //rows[] = array();
  foreach ($vocabularies as $vocabulary) {
    $links = array();
    $types = array();
 
    $rows[] = array(array('data' => $vocabulary->name, 'colspan' =>  count($languages), 'align' => 'center'), 
        l(t('new translation'), 'admin/taxonomy/i18n/term/new/'.$vocabulary->vid));
    // Get terms/translations for this vocab 
    $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);
    $list = array();
    while ($data = db_fetch_object($result)) {
      $list[] = $data;
    }
    if (!empty($list)) {
      $data = array();
       foreach ($list as $term) {
        if ($term->language) {
           if ($term->trid) {
             if (!array_key_exists($term->trid, $data)) {
               $data[$term->trid] = array();
             }
             $data[$term->trid][$term->language]= $term;
           }
           else {
             $untranslated[$term->language][] = $term;
           }
        }
      }
      // Reorder data for rows and languages
      foreach ($data as $trid => $terms) {
        $thisrow = array();
        foreach ($languages as $lang => $name) {
          if (array_key_exists($lang, $terms)) {
            $thisrow[] = $terms[$lang]->name;
           }
           else {
            $thisrow[] = '--';
           }
        }
        $thisrow[] = l(t('edit translations'), 'admin/taxonomy/i18n/term/'.$trid);
        $rows[] = $thisrow;
      }
    }
  }

  return theme('table', $header, $rows);
}

function i18n_taxonomy_term_save($edit) {
  // debug($edit);
  $languages = i18n_supported_languages();
  $translations = array();
  foreach ($languages as $lang => $name) {
    $tid = $edit[$lang]['tid'];
    $old = $edit[$lang]['old'];
    if ($tid) {
      $translations[] = $tid;
    }
  }
  $trid = $edit['trid'];
  // Remove old translations
  if (is_numeric($trid)){
    db_query('DELETE FROM {i18n_taxonomy_term} WHERE trid=%d',$trid);  
  }
  // Save new translations
  if (!empty($translations)) {
    // Save translations
    if ($trid =='new') {
      // New translation group
      $trid = db_next_id('{i18n_taxonomy_term}_trid');
    } 
    foreach ($translations as $tid ) {
      //$value = $new ? $trid : 'null';
      //db_query('UPDATE {i18n_taxonomy_term} SET trid=%d WHERE tid=%d', $value, $tid);
      db_query('INSERT INTO {i18n_taxonomy_term}(trid,tid) VALUES(%d,%d)',$trid,$tid);

    }
    drupal_set_message(t('Term translations have been updated'));
  }

}
function i18n_taxonomy_term_form($trid) {
  $languages = i18n_supported_languages();
  if ($trid == 'new') {
    $translations = array();
    $vid = arg(5);
  }
  else {
    $translations =i18n_taxonomy_get_translations($trid);
    $vid = $translations[key($translations)]->vid;
  } 
  $vocabulary = taxonomy_get_vocabulary($vid);
  
  // List of terms for languages
  foreach ($languages as $lang => $langname) {
    if ($trans = $translations[$lang]) {
      $field1 = l($trans->name, 'admin/taxonomy/edit/term/'.$trans->tid);
      $tidtrans = $trans->tid;
     }
     else {
      $field1 = "--";
      $tidtrans = 0;
    }
    // Available terms for this language
    $list = i18n_array2list(i18n_vocabulary_get_terms($vid, $lang, 'all'), 'tid', 'name');
    $list[0] ='';
    $field2 = form_select('', 
          "$lang][tid", 
          $tidtrans, 
          $list);
    $field2 .= form_hidden("$lang][old", $tidtrans);
    $rows[] = array($langname, $field1, $field2);
  }
  $header = array(t('Language'), t('current translation'), t('change translation'));
  // Prepares html form
  $output = form_item(t("Vocabulary"), $vocabulary->name);
  $output .= form_hidden('trid', $trid);
  $output .= theme('table', $header, $rows);
  $output .= form_submit(t('Submit'));
  return form($output);
}

/**
 * Adds language and translation fields to term edition form
 * 
 * This is a hook for taxonomy module
 */

function i18n_taxonomy_form_term($edit = array()) {
  $tid = $edit['tid'];
  $termlang= $edit['language'];
  $vid = isset($edit['vid']) ? $edit['vid'] : arg(4);
  // $trid= $edit['trid'];
  
  $languages = i18n_supported_languages();
  $selectlang = array_merge(array('' => ''), $languages);

  $vocabulary = taxonomy_get_vocabulary($vid);
  // If vocabulary has language, this should be the one for the term
  if ($vocabulary->language) {
    $edit['language'] = $vocabulary->language;
    $output = form_item(t('Language'),$languages[$vocabulary->language],t('The vocabulary already has a language.'));
  } 
  elseif ($termlang && $translations=i18n_term_get_translations($tid)) {
    // Term has translations. Not all languages are valid
    $selectlang = array('' => '');
    foreach ($languages as $lang => $langname) {
      if ($trans = $translations[$lang]) {
        $rows[] = array($langname, l($trans->name, 'admin/taxonomy/edit/term/'.$trans->tid));  
      }
      else {
        $selectlang[$lang] = $langname;
      }
    }
    $output = form_select(t('Language'), 'language', $termlang, $selectlang);

    $header = array(t('Language'), t('translated term'));
    $output .= form_group(t('term translations'), theme('table', $header, $rows));
    
  } else {
    $output = form_select(t('Language'), 'language', $termlang, $selectlang);
  }
  return $output;
}

function i18n_taxonomy_form_vocabulary($edit = array()) {
  $tid = $edit['tid'];
  $lang= $edit['language'];
  $trid= $edit['trid'];
  
  $languages = i18n_supported_languages();
  $selectlang = array_merge(array('' => ''), $languages);
  $output = form_select(t('Language'), 'language', $lang, $selectlang, t('This language will be set for all terms in this vocabulary'));
  return $output;
}

/* Returns an url for the translated taxonomy-page, if exists */
function i18n_taxonomy_l($str_tids, $lang) {
  if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
    $separator = '+';
    // The '+' character in a query string may be parsed as ' '.
    $tids = preg_split('/[+ ]/', $str_tids);
  }
  else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
    $separator = ',';
    $tids = explode(',', $str_tids);
  }
  else {
    return;
  }
  
  $translated_tids = array();
  foreach ($tids as $tid) {
    if ($translated_tid = i18n_term_get_translations($tid)) {
      $translated_tids[] = $translated_tid[$lang]->tid;
    }
  }
  
  return implode($separator, $translated_tids);
}

function i18n_taxonomy_where_sql($table, $type, $param = '') {
  //debug("$table $type $param");
  switch ($type) {
    case 'node':  // Param is nid
      $lang = i18n_node_get_lang($param, i18n_get_lang());
      return "AND $table.language in ('$lang', '')";
    case 'guess': // This is a difficult one :-). Guess from path
      return i18n_taxonomy_where_sql_guess($table);
    default:
      return "AND $table.language in ('". i18n_get_lang() . "','')";
  }
}

function i18n_taxonomy_where_sql_guess($table) {
  switch (arg(0)) {
    case 'node':
      if (is_numeric($nid = arg(1))) {
        $lang = i18n_node_get_lang($nid, i18n_get_lang());
      }
      else {
        $lang = i18n_get_lang();
      }
      break;
      
    case 'admin':
      // Get all languages
  }
  if ($lang) {
    return "AND $table.language in ('$lang','')";
  }
  else {
    return '';
  }
}
/**
 * Converts a list of arrays to an array of the form keyfield => namefield
 */
 
function i18n_array2list($data, $keyfield, $namefield = 'name') {
  foreach ($data as $key => $value) {
    if (is_array($data)) {
      $list[$value[$keyfield]] = $value[$namefield];
    }
    else {
      $list[$value->$keyfield] = $value->$namefield;
    }
  }
  return $list;
}

// returns array lang > term
function i18n_term_get_translations($tid, $getall = false) {
  $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';
  if ($getall) {
    $result = db_query($sql, $tid);
  }
  else {
    $result = db_query($sql. ' AND t.tid != %d',$tid, $tid);
  }
  while ($data = db_fetch_object($result)) {
    $list[$data->language] = $data;
  }
  return $list;  
}

function i18n_taxonomy_get_translations($trid) {
  $result = db_query('SELECT t.*,i.trid FROM {term_data} t JOIN {i18n_taxonomy_term} i USING(tid) WHERE i.trid=%d',$trid);
  $list = array();
  while ($data = db_fetch_object($result)) {
    $list[$data->language] = $data;
  }
  return $list;     
}

function i18n_db_get_arrays($sql) {
  $result = db_query($sql);
  $list = array();
  while ($data = db_fetch_array($result)) {
    $list[] = $data;
  }
  return $list;  
}

function i18n_db_get_objects($sql) {
  $result = db_query($sql);
  $list = array();
  while ($data = db_fetch_object($result)) {
    $list[] = $data;
  }
  return $list;    
}
// returns array lang > term
function i18n_vocabulary_get_terms($vid, $lang, $status = 'all') {
  if ($status=='translated') {
    $andsql = ' AND i.trid > 0';
  }elseif ( $status =='untranslated' ) {
    $andsql = ' AND (i.trid = 0 or i.trid is null)';
  }

  $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);
  $list = array();
  while ($term = db_fetch_array($result)) {
     $list[] = $term;
  }
  return $list;  
}
/**
 * Taxonomy hook
 * $object parameter is an array, not an object !!
 */
 // $op = insert, update, delete
function i18n_taxonomy($op, $type, $edit) {
  switch ("$type/$op") {
    case 'term/insert':
    case 'term/update':      
	      if (!$edit['language']) {
	        // Removed language, remove trid
	        db_query('DELETE FROM {i18n_taxonomy_term} WHERE tid=%d', $edit['tid']);
	        drupal_set_message(t('Removed translation info from term'));
	      }
        break;
    case 'term/form pre':
        return i18n_taxonomy_form_term($edit);
        break;  
    case 'term/delete':
	      db_query('DELETE FROM {i18n_taxonomy_term} WHERE tid=%d', $edit['tid']);
        break;
    case 'vocabulary/form pre':
        return i18n_taxonomy_form_vocabulary($edit);
        break;
    case 'vocabulary/update':
        if (isset($edit['language'])) {
          db_query("UPDATE {term_data} SET language='%s' WHERE vid='%d'",$edit['language'],$edit['vid']);
        }
        break;
  }
} 
 
?>