i18n_node.pages.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the translation module.
  5. */
  6. /**
  7. * Replacement for node_add_page.
  8. */
  9. function i18n_node_add_page() {
  10. $item = menu_get_item();
  11. $content = system_admin_menu_block($item);
  12. // Bypass the node/add listing if only one content type is available.
  13. if (count($content) == 1) {
  14. $item = array_shift($content);
  15. drupal_goto($item['href']);
  16. }
  17. foreach ($content as &$item) {
  18. // Type machine name will be the first page argument
  19. $page_arguments = unserialize($item['page_arguments']);
  20. // Check whether this has a node type, other items may be here too, see #1264662
  21. $type = isset($page_arguments[0]) ? $page_arguments[0] : NULL;
  22. if ($type) {
  23. // We just need to translate the description, the title is translated by the menu system
  24. // The string will be filtered (xss_admin) on the theme layer
  25. $item['description'] = i18n_node_translate_type($type, 'description', $item['description'], array('sanitize' => FALSE));
  26. }
  27. }
  28. return theme('node_add_list', array('content' => $content));
  29. }
  30. /**
  31. * Overview page for a node's translations.
  32. *
  33. * @param $node
  34. * Node object.
  35. */
  36. function i18n_node_translation_overview($node) {
  37. include_once DRUPAL_ROOT . '/includes/language.inc';
  38. if (!empty($node->tnid)) {
  39. // Already part of a set, grab that set.
  40. $tnid = $node->tnid;
  41. $translations = translation_node_get_translations($node->tnid);
  42. }
  43. else {
  44. // We have no translation source nid, this could be a new set, emulate that.
  45. $tnid = $node->nid;
  46. $translations = array($node->language => $node);
  47. }
  48. $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  49. $header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
  50. // Modes have different allowed languages
  51. foreach (i18n_node_language_list($node) as $langcode => $language_name) {
  52. if ($langcode == LANGUAGE_NONE) {
  53. // Never show language neutral on the overview.
  54. continue;
  55. }
  56. $options = array();
  57. if (isset($translations[$langcode])) {
  58. // Existing translation in the translation set: display status.
  59. // We load the full node to check whether the user can edit it.
  60. $translation_node = node_load($translations[$langcode]->nid);
  61. $path = 'node/' . $translation_node->nid;
  62. $title = i18n_node_translation_link($translation_node->title, $path, $langcode);
  63. if (node_access('update', $translation_node)) {
  64. $text = t('edit');
  65. $path = 'node/' . $translation_node->nid . '/edit';
  66. $options[] = i18n_node_translation_link($text, $path, $langcode);
  67. }
  68. $status = $translation_node->status ? t('Published') : t('Not published');
  69. $status .= $translation_node->translate ? ' - <span class="marker">' . t('outdated') . '</span>' : '';
  70. if ($translation_node->nid == $tnid) {
  71. $language_name = t('<strong>@language_name</strong> (source)', array('@language_name' => $language_name));
  72. }
  73. }
  74. else {
  75. // No such translation in the set yet: help user to create it.
  76. $title = t('n/a');
  77. if (node_access('create', $node->type)) {
  78. $text = t('add translation');
  79. $path = 'node/add/' . str_replace('_', '-', $node->type);
  80. $query = array('query' => array('translation' => $node->nid, 'target' => $langcode));
  81. $options[] = i18n_node_translation_link($text, $path, $langcode, $query);
  82. }
  83. $status = t('Not translated');
  84. }
  85. $rows[] = array($language_name, $title, $status, implode(" | ", $options));
  86. }
  87. drupal_set_title(t('Translations of %title', array('%title' => $node->title)), PASS_THROUGH);
  88. $build['translation_node_overview'] = array(
  89. '#theme' => 'table',
  90. '#header' => $header,
  91. '#rows' => $rows,
  92. );
  93. if (user_access('administer content translations')) {
  94. $build['translation_node_select'] = drupal_get_form('i18n_node_select_translation', $node, $translations);
  95. }
  96. return $build;
  97. }
  98. /**
  99. * Create link for node translation. This may be a 'edit' or a 'add translation' link.
  100. */
  101. function i18n_node_translation_link($text, $path, $langcode, $options = array()) {
  102. $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  103. $links = language_negotiation_get_switch_links($type, $path);
  104. // When node not published, links don't have href so we use path instead
  105. // Note: this is a bug in Core translation module, see http://drupal.org/node/1137074
  106. if (!empty($links->links[$langcode]) && !empty($links->links[$langcode]['href'])) {
  107. $options += array('attributes' => array(), 'html' => FALSE);
  108. $options['attributes'] += $links->links[$langcode]['attributes'];
  109. $options += $links->links[$langcode];
  110. $path = $links->links[$langcode]['href'];
  111. }
  112. return l($text, $path, $options);
  113. }
  114. /**
  115. * Form to select existing nodes as translation
  116. *
  117. * This one uses autocomplete fields for all languages
  118. */
  119. function i18n_node_select_translation($form, &$form_state, $node, $translations) {
  120. $form['node'] = array('#type' => 'value', '#value' => $node);
  121. $form['translations'] = array(
  122. '#type' => 'fieldset',
  123. '#title' => t('Select translations for %title', array('%title' => $node->title)),
  124. '#tree' => TRUE,
  125. '#theme' => 'i18n_node_select_translation',
  126. '#description' => t("Alternatively, you can select existing nodes as translations of this one or remove nodes from this translation set. Only nodes that have the right language and don't belong to other translation set will be available here.")
  127. );
  128. foreach (i18n_node_language_list($node) as $langcode => $language_name) {
  129. if ($langcode != $node->language && $langcode != LANGUAGE_NONE) {
  130. $nid = isset($translations[$langcode]) ? $translations[$langcode]->nid : 0;
  131. $form['translations']['nid'][$langcode] = array(
  132. '#type' => 'value',
  133. '#value' => $nid,
  134. );
  135. $form['translations']['language'][$langcode] = array(
  136. '#type' => 'value',
  137. '#value' => $language_name,
  138. );
  139. $form['translations']['node'][$langcode] = array(
  140. '#type' => 'textfield',
  141. '#maxlength' => 255,
  142. '#autocomplete_path' => 'i18n/node/autocomplete/' . $node->type . '/' . $langcode,
  143. '#default_value' => $nid ? i18n_node_nid2autocomplete($nid) : '',
  144. );
  145. }
  146. }
  147. $form['actions'] = array('#type' => 'actions');
  148. $form['actions']['update'] = array(
  149. '#type' => 'submit',
  150. '#value' => t('Update translations'),
  151. );
  152. //$form['buttons']['clean'] = array('#type' => 'submit', '#value' => t('Delete translation set'));
  153. return $form;
  154. }
  155. /**
  156. * Form validation
  157. */
  158. function i18n_node_select_translation_validate($form, &$form_state) {
  159. foreach ($form_state['values']['translations']['node'] as $lang => $title) {
  160. if (!$title) {
  161. $nid = 0;
  162. }
  163. else {
  164. $nid = i18n_node_autocomplete2nid($title, "translations][node][$lang", array($form_state['values']['node']->type), array($lang));
  165. }
  166. $form_state['values']['translations']['nid'][$lang] = $nid;
  167. }
  168. }
  169. /**
  170. * Form submission: update / delete the translation set
  171. */
  172. function i18n_node_select_translation_submit($form, &$form_state) {
  173. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : NULL;
  174. $node = $form_state['values']['node'];
  175. $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array($node->language => $node);
  176. foreach ($translations as $trans) {
  177. $current[$trans->language] = $trans->nid;
  178. }
  179. $update = array($node->language => $node->nid) + array_filter($form_state['values']['translations']['nid']);
  180. // Compute the difference to see which are the new translations and which ones to remove
  181. $new = array_diff_assoc($update, $current);
  182. $remove = array_diff_assoc($current, $update);
  183. // The tricky part: If the existing source is not in the new set, we need to create a new tnid
  184. if ($node->tnid && in_array($node->tnid, $update)) {
  185. $tnid = $node->tnid;
  186. $add = $new;
  187. }
  188. else {
  189. // Create new tnid, which is the source node
  190. $tnid = $node->nid;
  191. $add = $update;
  192. }
  193. // Now update values for all nodes
  194. if ($add) {
  195. db_update('node')
  196. ->fields(array(
  197. 'tnid' => $tnid,
  198. ))
  199. ->condition('nid', $add)
  200. ->execute();
  201. entity_get_controller('node')->resetCache($add);
  202. if (count($new)) {
  203. drupal_set_message(format_plural(count($new), 'Added a node to the translation set.', 'Added @count nodes to the translation set.'));
  204. }
  205. }
  206. if ($remove) {
  207. db_update('node')
  208. ->fields(array(
  209. 'tnid' => 0,
  210. ))
  211. ->condition('nid', $remove)
  212. ->execute();
  213. entity_get_controller('node')->resetCache($remove);
  214. drupal_set_message(format_plural(count($remove), 'Removed a node from the translation set.', 'Removed @count nodes from the translation set.'));
  215. }
  216. }
  217. /**
  218. * Node title autocomplete callback
  219. */
  220. function i18n_node_autocomplete($type, $language, $string = '') {
  221. $params = array('type' => $type, 'language' => $language, 'tnid' => 0);
  222. $matches = array();
  223. foreach (_i18n_node_references($string, 'contains', $params) as $id => $row) {
  224. // Add a class wrapper for a few required CSS overrides.
  225. $matches[$row['title'] . " [nid:$id]"] = '<div class="reference-autocomplete">' . $row['rendered'] . '</div>';
  226. }
  227. drupal_json_output($matches);
  228. }
  229. /**
  230. * Generates 'title [nid:$nid]' for the autocomplete field
  231. */
  232. function i18n_node_nid2autocomplete($nid) {
  233. if ($node = node_load($nid)) {
  234. return $node->title . ' [nid:' . $nid . ']';
  235. }
  236. else {
  237. return t('Not found');
  238. }
  239. }
  240. /**
  241. * Reverse mapping from node title to nid
  242. *
  243. * We also handle autocomplete values (title [nid:x]) and validate the form
  244. */
  245. function i18n_node_autocomplete2nid($name, $field = NULL, $type, $language) {
  246. if (!empty($name)) {
  247. preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $name, $matches);
  248. if (!empty($matches)) {
  249. // Explicit [nid:n].
  250. list(, $title, $nid) = $matches;
  251. if (!empty($title) && ($node = node_load($nid)) && $title != $node->title) {
  252. if ($field) {
  253. form_set_error($field, t('Node title mismatch. Please check your selection.'));
  254. }
  255. $nid = NULL;
  256. }
  257. }
  258. else {
  259. // No explicit nid.
  260. $reference = _i18n_node_references($name, 'equals', array('type' => $type, 'language' => $language), 1);
  261. if (!empty($reference)) {
  262. $nid = key($reference);
  263. }
  264. elseif ($field) {
  265. form_set_error($field, t('Found no valid post with that title: %title', array('%title' => $name)));
  266. }
  267. }
  268. }
  269. return !empty($nid) ? $nid : NULL;
  270. }
  271. /**
  272. * Find node title matches.
  273. *
  274. * @param $string
  275. * String to match against node title
  276. * @param $match
  277. * Match mode: 'contains', 'equals', 'starts_with'
  278. * @param $params
  279. * Other query arguments: type, language or numeric ones
  280. */
  281. function _i18n_node_references($string, $match = 'contains', $params = array(), $limit = 10) {
  282. $query = db_select('node', 'n')
  283. ->fields('n', array('nid', 'title' , 'type'))
  284. ->orderBy('n.title')
  285. ->orderBy('n.type')
  286. ->range(0, $limit);
  287. foreach ($params as $key => $value) {
  288. $query->condition($key, $value);
  289. }
  290. switch ($match) {
  291. case 'equals':
  292. $query->condition('n.title', $string);
  293. break;
  294. case 'starts_with':
  295. $query->condition('n.title', $string . '%', 'LIKE');
  296. break;
  297. case 'contains':
  298. default:
  299. $query->condition('n.title', '%' . $string . '%', 'LIKE');
  300. break;
  301. }
  302. // Disable and reenable i18n selection mode so no language conditions are inserted
  303. i18n_select(false);
  304. $references = array();
  305. foreach ($query->execute() as $node) {
  306. $references[$node->nid] = array(
  307. 'title' => $node->title,
  308. 'rendered' => check_plain($node->title),
  309. );
  310. }
  311. i18n_select(true);
  312. return $references;
  313. }
  314. /**
  315. * Theme select translation form
  316. * @ingroup themeable
  317. */
  318. function theme_i18n_node_select_translation($variables) {
  319. $elements = $variables['element'];
  320. $output = '';
  321. if (isset($elements['nid'])) {
  322. $rows = array();
  323. foreach (element_children($elements['nid']) as $lang) {
  324. $rows[] = array(
  325. $elements['language'][$lang]['#value'],
  326. drupal_render($elements['node'][$lang]),
  327. );
  328. }
  329. $output .= theme('table', array('rows' => $rows));
  330. $output .= drupal_render_children($elements);
  331. }
  332. return $output;
  333. }