tmgmt_i18n_string.ui.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @file
  4. * Provides the I18nString source controller.
  5. */
  6. /**
  7. * Class TMGMTI18nStringDefaultSourceUIController
  8. *
  9. * UI Controller fo i18n strings translation jobs.
  10. */
  11. class TMGMTI18nStringDefaultSourceUIController extends TMGMTDefaultSourceUIController {
  12. /**
  13. * Gets overview form header.
  14. *
  15. * @return array
  16. * Header array definition as expected by theme_tablesort().
  17. */
  18. public function overviewFormHeader() {
  19. $languages = array();
  20. foreach (language_list() as $langcode => $language) {
  21. $langcode = str_replace('-', '', $langcode);
  22. $languages['langcode-' . $langcode] = array(
  23. 'data' => check_plain($language->name),
  24. );
  25. }
  26. $header = array(
  27. 'title' => array('data' => t('Label (in source language)')),
  28. 'type' => array('data' => t('Type')),
  29. ) + $languages;
  30. return $header;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function overviewForm($form, &$form_state, $type) {
  36. $form += $this->overviewSearchFormPart($form, $form_state, $type);
  37. $form['items'] = array(
  38. '#type' => 'tableselect',
  39. '#header' => $this->overviewFormHeader($type),
  40. '#empty' => t('No strings matching given criteria have been found.')
  41. );
  42. $search_data = $this->getSearchFormSubmittedParams();
  43. $i18n_strings = tmgmt_i18n_string_get_strings($type, $search_data['label'], $search_data['target_language'], $search_data['target_status']);
  44. foreach ($this->getTranslationData($i18n_strings, $form_state['item_type']) as $id => $data) {
  45. $form['items']['#options'][$id] = $this->overviewRow($type, $data);
  46. }
  47. $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
  48. return $form;
  49. }
  50. /**
  51. * Helper function to create translation data list for the sources page list.
  52. *
  53. * @param array $i18n_strings
  54. * Result of the search query returned by tmgmt_i18n_string_get_strings().
  55. * @param string $type
  56. * I18n object type.
  57. *
  58. * @return array
  59. * Structured array with translation data.
  60. */
  61. protected function getTranslationData($i18n_strings, $type) {
  62. $objects = array();
  63. $source_language = variable_get_value('i18n_string_source_language');
  64. foreach ($i18n_strings as $i18n_string) {
  65. $wrapper = tmgmt_i18n_string_get_wrapper($type, $i18n_string);
  66. if ($wrapper instanceof i18n_string_object_wrapper) {
  67. $id = $i18n_string->job_item_id;
  68. // Get existing translations and current job items for the entity
  69. // to determine translation statuses
  70. $current_job_items = tmgmt_job_item_load_latest('i18n_string', $wrapper->get_type(), $id, $source_language);
  71. $objects[$id] = array(
  72. 'id' => $id,
  73. 'object' => $wrapper->get_strings(array('empty' => TRUE)),
  74. 'wrapper' => $wrapper,
  75. );
  76. // Load entity translation specific data.
  77. foreach (language_list() as $langcode => $language) {
  78. $langcode = str_replace('-', '', $langcode);
  79. $translation_status = 'current';
  80. if ($langcode == $source_language) {
  81. $translation_status = 'original';
  82. }
  83. elseif ($i18n_string->{'lang_' . $langcode} === NULL) {
  84. $translation_status = 'missing';
  85. }
  86. $objects[$id]['current_job_items'][$langcode] = isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL;
  87. $objects[$id]['translation_statuses'][$langcode] = $translation_status;
  88. }
  89. }
  90. }
  91. return $objects;
  92. }
  93. /**
  94. * Builds search form for entity sources overview.
  95. *
  96. * @param array $form
  97. * Drupal form array.
  98. * @param $form_state
  99. * Drupal form_state array.
  100. * @param $type
  101. * Entity type.
  102. *
  103. * @return array
  104. * Drupal form array.
  105. */
  106. public function overviewSearchFormPart($form, &$form_state, $type) {
  107. $options = array();
  108. foreach (language_list() as $langcode => $language) {
  109. $options[$langcode] = $language->name;
  110. }
  111. $default_values = $this->getSearchFormSubmittedParams();
  112. $form['search_wrapper'] = array(
  113. '#prefix' => '<div class="tmgmt-sources-wrapper tmgmt-i18n_string-sources-wrapper">',
  114. '#suffix' => '</div>',
  115. '#weight' => -15,
  116. );
  117. $form['search_wrapper']['search'] = array(
  118. '#tree' => TRUE,
  119. );
  120. $form['search_wrapper']['search']['label'] = array(
  121. '#type' => 'textfield',
  122. '#title' => t('Label in source language'),
  123. '#default_value' => isset($default_values['label']) ? $default_values['label'] : NULL,
  124. );
  125. // Unset the source language as it should not be listed among target
  126. // languages.
  127. unset($options[i18n_string_source_language()]);
  128. $form['search_wrapper']['search']['target_language'] = array(
  129. '#type' => 'select',
  130. '#title' => t('Target language'),
  131. '#options' => $options,
  132. '#empty_option' => t('Any'),
  133. '#default_value' => isset($default_values['target_language']) ? $default_values['target_language'] : NULL,
  134. );
  135. $form['search_wrapper']['search']['target_status'] = array(
  136. '#type' => 'select',
  137. '#title' => t('Target status'),
  138. '#options' => array(
  139. 'untranslated_or_outdated' => t('Untranslated or outdated'),
  140. 'untranslated' => t('Untranslated'),
  141. 'outdated' => t('Outdated'),
  142. ),
  143. '#default_value' => isset($default_values['target_status']) ? $default_values['target_status'] : NULL,
  144. '#states' => array(
  145. 'invisible' => array(
  146. ':input[name="search[target_language]"]' => array('value' => ''),
  147. ),
  148. ),
  149. );
  150. $form['search_wrapper']['search_submit'] = array(
  151. '#type' => 'submit',
  152. '#value' => t('Search'),
  153. );
  154. return $form;
  155. }
  156. /**
  157. * Gets submitted search params.
  158. *
  159. * @return array
  160. */
  161. public function getSearchFormSubmittedParams() {
  162. $params = array(
  163. 'label' => NULL,
  164. 'target_language' => NULL,
  165. 'target_status' => NULL,
  166. );
  167. if (isset($_GET['label'])) {
  168. $params['label'] = $_GET['label'];
  169. }
  170. if (isset($_GET['target_language'])) {
  171. $params['target_language'] = $_GET['target_language'];
  172. }
  173. if (isset($_GET['target_status'])) {
  174. $params['target_status'] = $_GET['target_status'];
  175. }
  176. return $params;
  177. }
  178. /**
  179. * Builds a table row for overview form.
  180. *
  181. * @param string $type
  182. * i18n type.
  183. * @param array $data
  184. * Data needed to build the list row.
  185. *
  186. * @return array
  187. */
  188. public function overviewRow($type, $data) {
  189. // Set the default item key, assume it's the first.
  190. $item_title = reset($data['object']);
  191. $type_label = i18n_object_info($type, 'title');
  192. $row = array(
  193. 'id' => $data['id'],
  194. 'title' => $item_title->get_string() ? t('@title (@id)', array('@title' => $item_title->get_string(), '@id' => $data['id'])) : $data['id'],
  195. 'type' => empty($type_label) ? t('Unknown') : $type_label,
  196. );
  197. foreach (language_list() as $langcode => $language) {
  198. $langcode = str_replace('-', '', $langcode);
  199. $row['langcode-' . $langcode] = theme('tmgmt_ui_translation_language_status_single', array(
  200. 'translation_status' => $data['translation_statuses'][$langcode],
  201. 'job_item' => isset($data['current_job_items'][$langcode]) ? $data['current_job_items'][$langcode] : NULL,
  202. ));
  203. }
  204. return $row;
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function overviewFormSubmit($form, &$form_state, $type) {
  210. // Handle search redirect.
  211. $this->overviewSearchFormRedirect($form, $form_state, $type);
  212. $items = array_filter($form_state['values']['items']);
  213. $type = $form_state['item_type'];
  214. $source_lang = variable_get_value('i18n_string_source_language');
  215. // Create only single job for all items as the source language is just
  216. // the same for all.
  217. $job = tmgmt_job_create($source_lang, NULL, $GLOBALS['user']->uid);
  218. // Loop through entities and create individual jobs for each source language.
  219. foreach ($items as $item) {
  220. $job->addItem('i18n_string', $type, $item);
  221. }
  222. $form_state['redirect'] = array('admin/tmgmt/jobs/' . $job->tjid,
  223. array('query' => array('destination' => current_path())));
  224. drupal_set_message(t('One job needs to be checked out.'));
  225. }
  226. /**
  227. * Performs redirect with search params appended to the uri.
  228. *
  229. * In case of triggering element is edit-search-submit it redirects to
  230. * current location with added query string containing submitted search form
  231. * values.
  232. *
  233. * @param array $form
  234. * Drupal form array.
  235. * @param $form_state
  236. * Drupal form_state array.
  237. * @param $type
  238. * Entity type.
  239. */
  240. public function overviewSearchFormRedirect($form, &$form_state, $type) {
  241. if ($form_state['triggering_element']['#id'] == 'edit-search-submit') {
  242. $query = array();
  243. foreach ($form_state['values']['search'] as $key => $value) {
  244. $query[$key] = $value;
  245. }
  246. drupal_goto($_GET['q'], array('query' => $query));
  247. }
  248. }
  249. }