tmgmt_entity.ui.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Abstract entity ui controller class for source plugin that provides
  4. * getEntity() method to retrieve list of entities of specific type. It also
  5. * allows to implement alter hook to alter the entity query for a specific type.
  6. *
  7. * @ingroup tmgmt_source
  8. */
  9. abstract class TMGMTEntityDefaultSourceUIController extends TMGMTDefaultSourceUIController {
  10. /**
  11. * Entity source list items limit.
  12. *
  13. * @var int
  14. */
  15. public $pagerLimit = 25;
  16. /**
  17. * Gets entities data of provided type needed to build overview form list.
  18. *
  19. * @param $type
  20. * Entity type for which to get list of entities.
  21. * @param array $property_conditions
  22. * Array of key => $value pairs passed into
  23. * tmgmt_entity_get_translatable_entities() as the second parameter.
  24. *
  25. * @return array
  26. * Array of entities.
  27. */
  28. public function getEntitiesTranslationData($type, $property_conditions = array()) {
  29. $return_value = array();
  30. $entities = tmgmt_entity_get_translatable_entities($type, $property_conditions, TRUE);
  31. $entity_info = entity_get_info($type);
  32. $bundles = tmgmt_entity_get_translatable_bundles($type);
  33. // For retrieved entities add translation specific data.
  34. foreach ($entities as $entity) {
  35. list($entity_id, , $bundle) = entity_extract_ids($type, $entity);
  36. $entity_uri = entity_uri($type, $entity);
  37. // This occurs on user entity type.
  38. if (empty($entity_id)) {
  39. continue;
  40. }
  41. /**
  42. * @var EntityTranslationDefaultHandler $handler
  43. */
  44. $handler = entity_translation_get_handler($type, $entity);
  45. // Get existing translations and current job items for the entity
  46. // to determine translation statuses
  47. $translations = $handler->getTranslations();
  48. $source_lang = entity_language($type, $entity);
  49. $current_job_items = tmgmt_job_item_load_latest('entity', $type, $entity_id, $source_lang);
  50. // Load basic entity data.
  51. $return_value[$entity_id] = array(
  52. 'entity_type' => $type,
  53. 'entity_id' => $entity_id,
  54. 'entity_label' => entity_label($type, $entity),
  55. 'entity_uri' => $entity_uri['path'],
  56. );
  57. if (count($bundles) > 1) {
  58. $return_value[$entity_id]['bundle'] = isset($bundles[$bundle]) ? $bundles[$bundle] : t('Unknown');
  59. }
  60. // Load entity translation specific data.
  61. foreach (language_list() as $langcode => $language) {
  62. $translation_status = 'current';
  63. if ($langcode == $source_lang) {
  64. $translation_status = 'original';
  65. }
  66. elseif (!isset($translations->data[$langcode])) {
  67. $translation_status = 'missing';
  68. }
  69. elseif (!empty($translations->data[$langcode]['translate'])) {
  70. $translation_status = 'outofdate';
  71. }
  72. $return_value[$entity_id]['current_job_items'][$langcode] = isset($current_job_items[$langcode]) ? $current_job_items[$langcode]: NULL;
  73. $return_value[$entity_id]['translation_statuses'][$langcode] = $translation_status;
  74. }
  75. }
  76. return $return_value;
  77. }
  78. /**
  79. * Builds search form for entity sources overview.
  80. *
  81. * @param array $form
  82. * Drupal form array.
  83. * @param $form_state
  84. * Drupal form_state array.
  85. * @param $type
  86. * Entity type.
  87. *
  88. * @return array
  89. * Drupal form array.
  90. */
  91. public function overviewSearchFormPart($form, &$form_state, $type) {
  92. // Add search form specific styling.
  93. drupal_add_css(drupal_get_path('module', 'tmgmt_entity') . '/css/tmgmt_entity.admin.entity_source_search_form.css');
  94. $form = array();
  95. // Add entity type value into form array so that it is available in
  96. // the form alter hook.
  97. $form_state['entity_type'] = $type;
  98. $form['search_wrapper'] = array(
  99. '#prefix' => '<div class="tmgmt-sources-wrapper tmgmt-entity-sources-wrapper">',
  100. '#suffix' => '</div>',
  101. '#weight' => -15,
  102. );
  103. $form['search_wrapper']['search'] = array(
  104. '#tree' => TRUE,
  105. );
  106. $form['search_wrapper']['search_submit'] = array(
  107. '#type' => 'submit',
  108. '#value' => t('Search'),
  109. '#weight' => 10,
  110. );
  111. $form['search_wrapper']['search_cancel'] = array(
  112. '#type' => 'submit',
  113. '#value' => t('Cancel'),
  114. '#weight' => 11,
  115. );
  116. $entity_info = entity_get_info($type);
  117. $label_key = isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : NULL;
  118. if (!empty($label_key)) {
  119. $form['search_wrapper']['search'][$label_key] = array(
  120. '#type' => 'textfield',
  121. '#title' => t('@entity_name title', array('@entity_name' => $entity_info['label'])),
  122. '#size' => 25,
  123. '#default_value' => isset($_GET[$label_key]) ? $_GET[$label_key] : NULL,
  124. );
  125. }
  126. $language_options = array();
  127. foreach (language_list() as $langcode => $language) {
  128. $language_options[$langcode] = $language->name;
  129. }
  130. $form['search_wrapper']['search']['language'] = array(
  131. '#type' => 'select',
  132. '#title' => t('Source Language'),
  133. '#options' => $language_options,
  134. '#empty_option' => t('All'),
  135. '#default_value' => isset($_GET['language']) ? $_GET['language'] : NULL,
  136. );
  137. $bundle_key = $entity_info['entity keys']['bundle'];
  138. $bundle_options = tmgmt_entity_get_translatable_bundles($type);
  139. if (count($bundle_options) > 1) {
  140. $form['search_wrapper']['search'][$bundle_key] = array(
  141. '#type' => 'select',
  142. '#title' => t('@entity_name type', array('@entity_name' => $entity_info['label'])),
  143. '#options' => $bundle_options,
  144. '#empty_option' => t('All'),
  145. '#default_value' => isset($_GET[$bundle_key]) ? $_GET[$bundle_key] : NULL,
  146. );
  147. }
  148. // In case entity translation is not enabled for any of bundles
  149. // display appropriate message.
  150. elseif (count($bundle_options) == 0) {
  151. drupal_set_message(t('Entity translation is not enabled for any of existing content types. To use this functionality go to Content types administration and enable entity translation for desired content types.'), 'warning');
  152. unset($form['search_wrapper']);
  153. }
  154. $options = array();
  155. foreach (language_list() as $langcode => $language) {
  156. $options[$langcode] = $language->name;
  157. }
  158. $form['search_wrapper']['search']['target_language'] = array(
  159. '#type' => 'select',
  160. '#title' => t('Target language'),
  161. '#options' => $options,
  162. '#empty_option' => t('Any'),
  163. '#default_value' => isset($_GET['target_language']) ? $_GET['target_language'] : NULL,
  164. );
  165. $form['search_wrapper']['search']['target_status'] = array(
  166. '#type' => 'select',
  167. '#title' => t('Target status'),
  168. '#options' => array(
  169. 'untranslated_or_outdated' => t('Untranslated or outdated'),
  170. 'untranslated' => t('Untranslated'),
  171. 'outdated' => t('Outdated'),
  172. ),
  173. '#default_value' => isset($_GET['target_status']) ? $_GET['target_status'] : NULL,
  174. '#states' => array(
  175. 'invisible' => array(
  176. ':input[name="search[target_language]"]' => array('value' => ''),
  177. ),
  178. ),
  179. );
  180. return $form;
  181. }
  182. /**
  183. * Performs redirect with search params appended to the uri.
  184. *
  185. * In case of triggering element is edit-search-submit it redirects to
  186. * current location with added query string containing submitted search form
  187. * values.
  188. *
  189. * @param array $form
  190. * Drupal form array.
  191. * @param $form_state
  192. * Drupal form_state array.
  193. * @param $type
  194. * Entity type.
  195. */
  196. public function overviewSearchFormRedirect($form, &$form_state, $type) {
  197. if ($form_state['triggering_element']['#id'] == 'edit-search-cancel') {
  198. drupal_goto($_GET['q']);
  199. }
  200. elseif ($form_state['triggering_element']['#id'] == 'edit-search-submit') {
  201. $query = array();
  202. foreach ($form_state['values']['search'] as $key => $value) {
  203. $query[$key] = $value;
  204. }
  205. drupal_goto($_GET['q'], array('query' => $query));
  206. }
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function hook_menu() {
  212. $items = parent::hook_menu();
  213. if (isset($items['admin/tmgmt/sources/entity_node'])) {
  214. // We assume that nodes are the most important overview if enabled, so
  215. // make sure they show up first.
  216. $items['admin/tmgmt/sources/entity_node']['weight'] = -20;
  217. }
  218. return $items;
  219. }
  220. }