Projet

Général

Profil

Paste
Télécharger (8,21 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_taxonomy / i18n_taxonomy.admin.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Helper functions for taxonomy administration.
6
 */
7

    
8
/**
9
 * This is the callback for taxonomy translations.
10
 *
11
 * Gets the urls:
12
 *  admin/content/taxonomy/%taxonomy_vocabulary/translation
13
 *  admin/content/taxonomy/i18n/term/xx
14
 *  admin/content/taxonomy/i18n/term/new/xx
15
 *  admin/content/taxonomy/vid/translation/op/trid
16
 */
17
function i18n_taxonomy_page_vocabulary($vocabulary, $op = NULL, $tid = NULL) {
18
  switch ($op) {
19
    case 'edit':
20
      drupal_set_title(t('Edit term translations'));
21
      $output = drupal_get_form('i18n_taxonomy_translation_term_form', $vocabulary, $tid);
22
      break;
23

    
24
    default:
25
      $output = i18n_taxonomy_translation_overview($vocabulary);
26
  }
27
  return $output;
28
}
29

    
30
/**
31
 * Produces a vocabulary translation form.
32
 */
33
function i18n_taxonomy_translation_term_form($form, $form_state, $vocabulary, $translation_set = NULL, $source = NULL) {
34
  $form['translation_set'] = array('#type' => 'value', '#value' => $translation_set);
35
  $translations = $translation_set ? $translation_set->get_translations() : array();
36
  $form['vocabulary'] = array('#type' => 'value', '#value' => $vocabulary);
37
  $form['translations'] = array(
38
    '#type' => 'fieldset',
39
    '#title' => t('Select translations'),
40
    '#description' => t('Select existing terms or type new ones that will be created for each language.'),
41
    '#tree' => TRUE
42
  );
43
  // List of terms for languages.
44
  foreach (i18n_language_list() as $lang => $langname) {
45
    if ($source && $source->language == $lang) {
46
      // This is the source term, we don't change it
47
      $form['source_term'] = array('#type' => 'value', '#value' => $source);
48
      $form['translations'][$lang] = array(
49
        '#title' => $langname,
50
        '#type' => 'item',
51
        '#markup' => $source->name,
52
        '#langcode' => $lang,
53
      );
54
    }
55
    else {
56
      $form['translations'][$lang] = array(
57
        '#title' => $langname,
58
        '#type' => 'textfield',
59
        '#default_value' => isset($translations[$lang]) ? $translations[$lang]->name : '',
60
        '#autocomplete_path' => 'i18n/taxonomy/autocomplete/vocabulary/' . $vocabulary->machine_name . '/' . $lang,
61
        '#langcode' => $lang,
62
        '#maxlength' => 1024,
63
        '#element_validate' => array('i18n_taxonomy_autocomplete_validate'),
64
      );
65
    }
66

    
67
  }
68
  $form['submit'] = array(
69
    '#type' => 'submit',
70
    '#value' => t('Save')
71
  );
72
  if ($translation_set) {
73
    $form['delete'] = array(
74
      '#type' => 'submit',
75
      '#value' => t('Delete')
76
    );
77
  }
78
  return $form;
79
}
80

    
81
/**
82
 * Form element validate handler for taxonomy term autocomplete element.
83
 */
84
function i18n_taxonomy_autocomplete_validate($element, &$form_state) {
85
  // Autocomplete widgets do not send their tids in the form, so we must detect
86
  // them here and process them independently.
87
  $value = array();
88
  if ($tags = $element['#value']) {
89
    // Collect candidate vocabularies.
90
    $vocabulary = $form_state['values']['vocabulary'];
91
    $vocabularies[$vocabulary->vid] = $vocabulary;
92

    
93
    // Translate term names into actual terms.
94
    $typed_terms = drupal_explode_tags($tags);
95
    foreach ($typed_terms as $typed_term) {
96
      // See if the term exists in the chosen vocabulary and return the tid;
97
      // otherwise, create a new 'autocreate' term for insert/update.
98
      if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => $vocabulary->vid, 'language' => $element['#langcode']))) {
99
        $term = array_pop($possibilities);
100
      }
101
      else {
102
        $vocabulary = reset($vocabularies);
103
        $term = array(
104
          'tid' => 'autocreate',
105
          'vid' => $vocabulary->vid,
106
          'name' => $typed_term,
107
          'vocabulary_machine_name' => $vocabulary->machine_name,
108
          'language' => $element['#langcode'],
109
        );
110
      }
111
      $value[] = (array)$term;
112
    }
113
  }
114

    
115
  form_set_value($element, $value, $form_state);
116
}
117

    
118
/**
119
 * Form callback: Process vocabulary translation form.
120
 */
121
function i18n_taxonomy_translation_term_form_submit($form, &$form_state) {
122
  $translation_set = $form_state['values']['translation_set'];
123
  $vocabulary = $form_state['values']['vocabulary'];
124
  switch ($form_state['values']['op']) {
125
    case t('Save'):
126
      $term_translations = array_filter($form_state['values']['translations']);
127
      foreach ($term_translations as $lang => $lang_terms) {
128
        $item = reset($lang_terms);
129
        if ($item['tid'] == 'autocreate') {
130
          $term = (object) $item;
131
          unset($term->tid);
132
          taxonomy_term_save($term);
133
        }
134
        else {
135
          $term = (object) $item;
136
        }
137
        $translations[$lang] = $term;
138
      }
139
      if (!empty($form_state['values']['source_term'])) {
140
        $term = $form_state['values']['source_term'];
141
        $translations[$term->language] = $term;
142
      }
143
      if (!empty($translations)) {
144
        $translation_set = $translation_set ? $translation_set : i18n_translation_set_create('taxonomy_term', $vocabulary->machine_name);
145
        $translation_set
146
          ->reset_translations($translations)
147
          ->save(TRUE);
148
        drupal_set_message(t('Term translations have been updated.'));
149
      }
150
      else {
151
        drupal_set_message(t('There are no translations to be saved.'), 'error');
152
      }
153
      break;
154

    
155
    case t('Delete'):
156
      $translation_set->delete(TRUE);
157
      drupal_set_message(t('The term translation has been deleted.'));
158
      $form_state['redirect'] = 'admin/structure/taxonomy/' . $form_state['values']['vocabulary']->machine_name . '/translation';
159
      break;
160
  }
161
}
162

    
163
/**
164
 * Generate a tabular listing of translations for vocabularies.
165
 */
166
function i18n_taxonomy_translation_sets_overview($vocabulary) {
167
  module_load_include('admin.inc', 'i18n_translation');
168
  drupal_set_title($vocabulary->name);
169
  $query = db_select('i18n_translation_set', 't')
170
    ->condition('t.bundle', $vocabulary->machine_name);
171
  return i18n_translation_admin_overview('taxonomy_term', $query);
172
}
173

    
174
/**
175
 * Callback for term translation tab.
176
 */
177
function i18n_taxonomy_translation_term_overview($term) {
178
  if ($term->i18n_tsid) {
179
    // Already part of a set, grab that set.
180
    $i18n_tsid = $term->i18n_tsid;
181
    $translation_set = i18n_translation_set_load($term->i18n_tsid);
182
    $translation_set->get_translations();
183
    $translations = $translation_set->get_translations();
184
  }
185
  else {
186
    // We have no translation source nid, this could be a new set, emulate that.
187
    $i18n_tsid = $term->tid;
188
    $translations = array($term->language => $term);
189
  }
190
  $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
191
  $header = array(t('Language'), t('Title'), t('Operations'));
192

    
193
  foreach (i18n_language_list() as $langcode => $language_name) {
194
    $options = array();
195
    if (isset($translations[$langcode])) {
196
      // Existing translation in the translation set: display status.
197
      // We load the full node to check whether the user can edit it.
198
      $translation_term = taxonomy_term_load($translations[$langcode]->tid);
199
      $path = 'taxonomy/term/' . $translation_term->tid;
200
      $title = l($translation_term->name, $path);
201

    
202
      $options['edit'] = array(
203
        '#type' => 'link',
204
        '#title' => t('edit'),
205
        '#href' => $path . '/edit',
206
        '#options' => array(
207
          'query' => drupal_get_destination(),
208
        ),
209
      );
210

    
211
      if ($translation_term->tid == $i18n_tsid) {
212
        $language_name = t('<strong>@language_name</strong> (source)', array('@language_name' => $language_name));
213
      }
214
    }
215
    else {
216
      // No such translation in the set yet: help user to create it.
217
      $title = t('n/a');
218
      $options['add'] = array(
219
        '#type' => 'link',
220
        '#title' => t('add translation'),
221
        '#href' => 'admin/structure/taxonomy/' . $term->vocabulary_machine_name . '/add',
222
        '#options' => array(
223
          'query' => array('translation' => $term->tid, 'target' => $langcode) + drupal_get_destination()
224
        ),
225
      );
226
    }
227
    $rows[$langcode] = array(
228
      'language' => $language_name,
229
      'title' => $title,
230
      'operations' => array('data' => $options),
231
    );
232
  }
233

    
234
  drupal_set_title(t('Translations of term %title', array('%title' => $term->name)), PASS_THROUGH);
235

    
236
  $build['translation_overview'] = array(
237
    '#theme' => 'table',
238
    '#header' => $header,
239
    '#rows' => $rows,
240
  );
241

    
242
  return $build;
243
}