| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* Helper functions for taxonomy administration
|
| 5 |
*/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* This is the callback for taxonomy translations
|
| 9 |
*
|
| 10 |
* Gets the urls:
|
| 11 |
* admin/content/taxonomy/%taxonomy_vocabulary/translation'
|
| 12 |
* admin/content/taxonomy/i18n/term/xx
|
| 13 |
* admin/content/taxonomy/i18n/term/new/xx
|
| 14 |
* admin/content/taxonomy/vid/translation/op/trid
|
| 15 |
*/
|
| 16 |
function i18ntaxonomy_page_vocabulary($vocabulary, $op = NULL, $tid = NULL) {
|
| 17 |
switch ($op) {
|
| 18 |
case 'edit':
|
| 19 |
drupal_set_title(t('Edit term translations'));
|
| 20 |
$output = drupal_get_form('i18ntaxonomy_translation_term_form', $vocabulary->vid, $tid);
|
| 21 |
break;
|
| 22 |
case t('Submit'):
|
| 23 |
drupal_set_title(t('Submit'));
|
| 24 |
i18ntaxonomy_translation_term_save($edit);
|
| 25 |
$output = i18ntaxonomy_translation_overview($vocabulary->vid);
|
| 26 |
break;
|
| 27 |
case 'delete':
|
| 28 |
//print theme('page', node_delete($edit), t('Delete'));
|
| 29 |
break;
|
| 30 |
default:
|
| 31 |
$output = i18ntaxonomy_translation_overview($vocabulary->vid);
|
| 32 |
}
|
| 33 |
return $output;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Produces a vocabulary translation form
|
| 38 |
*/
|
| 39 |
function i18ntaxonomy_translation_term_form($form_state, $vid, $trid = NULL, $edit = array()) {
|
| 40 |
$languages = i18n_supported_languages();
|
| 41 |
if ($trid == 'new') {
|
| 42 |
$translations = array();
|
| 43 |
$form['trid'] = array('#type' => 'hidden', '#value' => 0);
|
| 44 |
} else {
|
| 45 |
$form['trid'] = array('#type' => 'hidden', '#value' => $trid);
|
| 46 |
$translations = i18ntaxonomy_term_get_translations(array('trid' =>$trid));
|
| 47 |
}
|
| 48 |
//var_dump($translations);
|
| 49 |
$vocabulary = taxonomy_vocabulary_load($vid);
|
| 50 |
|
| 51 |
// List of terms for languages
|
| 52 |
foreach ($languages as $lang => $langname) {
|
| 53 |
$current = isset($translations[$lang]) ? $translations[$lang]->tid : '';
|
| 54 |
$list = i18ntaxonomy_vocabulary_get_terms($vid, $lang, 'all');
|
| 55 |
$list[''] = '--';
|
| 56 |
$form[$lang] = array('#type' => 'fieldset', '#tree' => TRUE);
|
| 57 |
$form[$lang]['tid'] = array(
|
| 58 |
'#type' => 'select',
|
| 59 |
'#title' => $langname,
|
| 60 |
'#default_value' => $current,
|
| 61 |
'#options' => $list
|
| 62 |
);
|
| 63 |
$form[$lang]['old'] = array('#type' => 'hidden', '#value' =>$current);
|
| 64 |
}
|
| 65 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
|
| 66 |
$form['destination'] = array('#type' => 'hidden', '#value' => 'admin/content/taxonomy/'.arg(3).'/translation');
|
| 67 |
return $form;
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Form callback: Process vocabulary translation form
|
| 72 |
*/
|
| 73 |
function i18ntaxonomy_translation_term_form_submit($form, &$form_state) {
|
| 74 |
$form_values = $form_state['values'];
|
| 75 |
i18ntaxonomy_translation_save($form_values, $form_values['trid']);
|
| 76 |
drupal_set_message(t('Term translations have been updated'));
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Save taxonomy term translations
|
| 81 |
*
|
| 82 |
* @param $terms
|
| 83 |
* Array of terms indexed by language
|
| 84 |
* @param $trid
|
| 85 |
* Optional translation set id
|
| 86 |
*/
|
| 87 |
function i18ntaxonomy_translation_save($terms, $trid = 0) {
|
| 88 |
// Delete old translations for this trid
|
| 89 |
if ($trid){
|
| 90 |
db_query("UPDATE {term_data} SET trid = 0 WHERE trid= %d", $trid);
|
| 91 |
}
|
| 92 |
// Now pick up all the tids in an array
|
| 93 |
$translations = array();
|
| 94 |
foreach (i18n_supported_languages() as $lang => $name) {
|
| 95 |
if (isset($terms[$lang]) && ($term = (array)$terms[$lang]) && $tid = $term['tid']) {
|
| 96 |
$translations[$lang] = $tid;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
// Now set a translation set with all these terms.
|
| 100 |
if (count($translations)) {
|
| 101 |
$trid = (is_numeric($trid) && $trid) ? $trid : i18ntaxonomy_next_trid();
|
| 102 |
db_query('UPDATE {term_data} SET trid = %d WHERE tid IN(%s)', $trid, implode(',',$translations));
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Get next free trid.
|
| 108 |
*/
|
| 109 |
function i18ntaxonomy_next_trid() {
|
| 110 |
$current = (int)db_result(db_query('SELECT max(trid) FROM {term_data}'));
|
| 111 |
return $current + 1;
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Generate a tabular listing of translations for vocabularies.
|
| 116 |
*/
|
| 117 |
function i18ntaxonomy_translation_overview($vid) {
|
| 118 |
$vocabulary = taxonomy_vocabulary_load($vid);
|
| 119 |
drupal_set_title(check_plain($vocabulary->name));
|
| 120 |
$output = '';
|
| 121 |
|
| 122 |
$languages = i18n_supported_languages();
|
| 123 |
$header = array_merge($languages, array(t('Operations')));
|
| 124 |
$links = array();
|
| 125 |
$types = array();
|
| 126 |
// Get terms/translations for this vocab
|
| 127 |
$result = db_query('SELECT * FROM {term_data} t WHERE vid=%d',$vocabulary->vid);
|
| 128 |
$terms = $messages = array();
|
| 129 |
while ($term = db_fetch_object($result)) {
|
| 130 |
if($term->trid && $term->language) {
|
| 131 |
$terms[$term->trid][$term->language] = $term;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
// Reorder data for rows and languages
|
| 135 |
$rows = array();
|
| 136 |
foreach ($terms as $trid => $terms) {
|
| 137 |
$thisrow = array();
|
| 138 |
foreach ($languages as $lang => $name) {
|
| 139 |
if (array_key_exists($lang, $terms)) {
|
| 140 |
$thisrow[] = $terms[$lang]->name;
|
| 141 |
}
|
| 142 |
else {
|
| 143 |
$thisrow[] = '--';
|
| 144 |
}
|
| 145 |
}
|
| 146 |
$thisrow[] = l(t('edit'), "admin/content/taxonomy/$vid/translation/edit/$trid");
|
| 147 |
$rows[] = $thisrow;
|
| 148 |
}
|
| 149 |
if ($rows) {
|
| 150 |
$output .= theme('table', $header, $rows);
|
| 151 |
} else {
|
| 152 |
$messages[] = t('No translations defined for this vocabulary');
|
| 153 |
}
|
| 154 |
$messages[]= l(t('Create new translation'), "admin/content/taxonomy/$vid/translation/edit/new");
|
| 155 |
$output .= theme('item_list', $messages);
|
| 156 |
return $output;
|
| 157 |
}
|