| 1 |
<?php
|
| 2 |
// $Id: translatable.page-translations.inc,v 1.2 2008/09/12 17:01:11 smk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Callback for translation management page.
|
| 6 |
*/
|
| 7 |
function translatable_page_translations($op = '', $args = array()) {
|
| 8 |
$output = '';
|
| 9 |
$op = isset($_POST['op']) ? $_POST['op'] : $op;
|
| 10 |
|
| 11 |
switch ($op) {
|
| 12 |
case 'select':
|
| 13 |
$node = node_load(arg(1));
|
| 14 |
$output .= translatable_node_overview($node);
|
| 15 |
$output .= translatable_node_select($node, $args[0] ? $args[0] : translatable_get_adminlocale());
|
| 16 |
break;
|
| 17 |
|
| 18 |
case t('Assign'):
|
| 19 |
$node = node_load(arg(1));
|
| 20 |
$output .= translatable_node_select($node, $args[0] ? $args[0] : translatable_get_adminlocale());
|
| 21 |
break;
|
| 22 |
|
| 23 |
case 'remove':
|
| 24 |
if ($node = node_load($args[0])) {
|
| 25 |
$translation = translatable_findbyid('node', $node->nid);
|
| 26 |
if ($translation['tnid'] != $node->nid) {
|
| 27 |
translatable_save('node', array('nid' => $node->nid, 'tnid' => $node->nid, 'language' => $node->language));
|
| 28 |
drupal_set_message('The content has been removed from the translation set.');
|
| 29 |
}
|
| 30 |
else {
|
| 31 |
drupal_set_message('The source content of a translation set cannot be removed.', 'error');
|
| 32 |
}
|
| 33 |
drupal_goto('node/'. arg(1) .'/translations');
|
| 34 |
}
|
| 35 |
|
| 36 |
default:
|
| 37 |
if (!isset($node)) {
|
| 38 |
$node = node_load(arg(1));
|
| 39 |
}
|
| 40 |
$output .= translatable_node_overview($node);
|
| 41 |
}
|
| 42 |
return $output;
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Hook for the overview of a node.
|
| 47 |
* Implements the translations tab interface.
|
| 48 |
*/
|
| 49 |
function translatable_node_overview($node) {
|
| 50 |
drupal_set_title(t('Translations of %title', array('%title' => $node->title)));
|
| 51 |
|
| 52 |
$header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
|
| 53 |
$rows = array();
|
| 54 |
$status_text = array(0 => t('Unpublished'), 1 => t('Published'), -1 => t('Not translated'));
|
| 55 |
foreach (translatable_available_languages() as $locale => $language) {
|
| 56 |
$options = array();
|
| 57 |
$translation = translatable_find('node', 'tnid = '. (int)$node->tnid ." AND language = '". $locale ."'", FALSE);
|
| 58 |
if ($translation) {
|
| 59 |
$trnode = db_fetch_object(db_query('SELECT n.nid, n.title, n.status, t.language, t.tnid, t.any FROM {node} n INNER JOIN {node_translatable} t ON n.nid = t.nid AND n.nid = %d', $translation['nid']));
|
| 60 |
$title = l($trnode->title, 'node/'. $trnode->nid, NULL, 'locale='. $trnode->language);
|
| 61 |
if ($trnode->nid == $node->tnid) {
|
| 62 |
$language = '<strong>'. check_plain($language) .'</strong> '. t('(source)');
|
| 63 |
$options[] = l(t('Edit'), translatable_url("node/$trnode->nid/edit", $trnode->language));
|
| 64 |
}
|
| 65 |
else {
|
| 66 |
$options[] = l(t('Edit'), translatable_url("node/$trnode->tnid/edit/translation/$trnode->language", $trnode->language));
|
| 67 |
$options[] = l(t('Remove'), "node/$node->tnid/translations/remove/$trnode->nid");
|
| 68 |
}
|
| 69 |
if ($trnode->any) {
|
| 70 |
$language .= ' '. t('(any language)');
|
| 71 |
}
|
| 72 |
$status = $trnode->status ? t('Published') : t('Unpublished');
|
| 73 |
}
|
| 74 |
else {
|
| 75 |
$title = t('n/a');
|
| 76 |
$status = t('Not translated');
|
| 77 |
$options[] = l(t('Create'), translatable_url("node/$node->nid/edit/translation/$locale", $locale));
|
| 78 |
$options[] = l(t('Select'), "node/$node->nid/translations/select/$locale");
|
| 79 |
}
|
| 80 |
$rows[] = array($language, $title, $status, implode(' | ', $options));
|
| 81 |
}
|
| 82 |
|
| 83 |
return theme('table', $header, $rows);
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Construct an absolute URL if switch by hostname is enabled.
|
| 88 |
*/
|
| 89 |
function translatable_url($path, $locale) {
|
| 90 |
if (variable_get('translatable_switch_byhostname', FALSE)) {
|
| 91 |
$url = 'http://';
|
| 92 |
$url .= variable_get('translatable_switch_hostname_'. $locale, $locale .'.'. translatable_get_domain());
|
| 93 |
$url .= base_path();
|
| 94 |
$url .= $path;
|
| 95 |
return $url;
|
| 96 |
}
|
| 97 |
return $path;
|
| 98 |
}
|
| 99 |
|
| 100 |
function translatable_node_select($node, $locale) {
|
| 101 |
if (!translatable_validate_locale($locale)) {
|
| 102 |
return;
|
| 103 |
}
|
| 104 |
|
| 105 |
$form['node'] = array('#type' => 'value', '#value' => $node);
|
| 106 |
|
| 107 |
// Only select nodes that do not belong to another translation set.
|
| 108 |
$selectables = translatable_find('node', '', TRUE, TRUE, array('group by' => 'tnid', 'having' => array('COUNT(tnid) = 1', "language = '$locale'")));
|
| 109 |
if ($selectables) {
|
| 110 |
$items = array();
|
| 111 |
$result = pager_query(db_prefix_tables("SELECT n.nid, n.title FROM {node} n WHERE n.nid IN (%s) AND n.type = '%s' ORDER BY n.title"), 40, 0, NULL, implode(', ', array_keys($selectables)), $node->type);
|
| 112 |
while ($trnode = db_fetch_object($result)) {
|
| 113 |
$items[$trnode->nid] = l($trnode->title, "node/$trnode->nid");
|
| 114 |
}
|
| 115 |
}
|
| 116 |
if (!empty($items)) {
|
| 117 |
$output = drupal_get_form('translatable_node_select_form', $node, $locale, $items);
|
| 118 |
}
|
| 119 |
else {
|
| 120 |
$languages = translatable_available_languages();
|
| 121 |
$output = t('No unassigned contents available in %language.', array('%language' => $languages[$locale]));
|
| 122 |
}
|
| 123 |
return $output;
|
| 124 |
}
|
| 125 |
|
| 126 |
function translatable_node_select_form($node, $locale, $items) {
|
| 127 |
$languages = translatable_available_languages();
|
| 128 |
$translations = translatable_node_get_translations($node->nid);
|
| 129 |
$form['#submit'] = array('translatable_node_select_form_submit' => array());
|
| 130 |
$form['tnid'] = array(
|
| 131 |
'#type' => 'hidden',
|
| 132 |
'#value' => $node->tnid,
|
| 133 |
);
|
| 134 |
$form['language'] = array(
|
| 135 |
'#type' => 'hidden',
|
| 136 |
'#value' => $locale,
|
| 137 |
);
|
| 138 |
$form['redirect'] = array(
|
| 139 |
'#type' => 'value',
|
| 140 |
'#value' => "node/$node->nid/translations",
|
| 141 |
);
|
| 142 |
$form['nodes']['nid'] = array(
|
| 143 |
'#type' => 'radios',
|
| 144 |
'#title' => t('Select %language translation', array('%language' => $languages[$locale])),
|
| 145 |
'#default_value' => isset($translations[$locale]) ? $translations[$locale]->nid : '',
|
| 146 |
'#options' => $items,
|
| 147 |
'#description' => t('<strong>Note:</strong> This list only contains contents that do not belong to another translation set. If a content from another translation set should be assigned to this translation set, that content has to be removed from the existing set first.'),
|
| 148 |
);
|
| 149 |
$form['pager'] = array('#value' => theme('pager'));
|
| 150 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Assign'));
|
| 151 |
return $form;
|
| 152 |
}
|
| 153 |
|
| 154 |
function translatable_node_select_form_submit($form_id, $form_values) {
|
| 155 |
if ($form_values['tnid'] && $form_values['nid']) {
|
| 156 |
translatable_save('node', array(
|
| 157 |
'nid' => $form_values['nid'],
|
| 158 |
'tnid' => $form_values['tnid'],
|
| 159 |
'language' => $form_values['language'],
|
| 160 |
));
|
| 161 |
drupal_set_message(t('The translation has been assigned to this content.'));
|
| 162 |
drupal_goto($form_values['redirect']);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Fetch translations of a node.
|
| 168 |
*
|
| 169 |
* @param $nid
|
| 170 |
* The id of the node.
|
| 171 |
*/
|
| 172 |
function translatable_node_get_translations($nid) {
|
| 173 |
$nodes = array();
|
| 174 |
$translation = translatable_findbyid('node', $nid);
|
| 175 |
$result = db_query("SELECT n.nid, n.title, n.status, t.language FROM {node} n INNER JOIN {node_translatable} t ON n.nid = t.nid AND t.tnid = %d AND t.nid <> %d", $translation['tnid'], $nid);
|
| 176 |
while ($node = db_fetch_object($result)) {
|
| 177 |
$nodes[$node->language] = $node;
|
| 178 |
}
|
| 179 |
return $nodes;
|
| 180 |
}
|
| 181 |
|
| 182 |
|