tmgmt_ui.controller.translator.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Contains the translator UI controller.
  5. */
  6. /**
  7. * Entity UI controller for the Translator Entity.
  8. */
  9. class TMGMTTranslatorUIController extends EntityDefaultUIController {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function hook_menu() {
  14. $items = parent::hook_menu();
  15. $items[$this->path . '/add']['title'] = 'Add Translator';
  16. unset($items[$this->path . '/add']['title callback']);
  17. unset($items[$this->path . '/add']['title arguments']);
  18. if (!empty($this->entityInfo['exportable'])) {
  19. $items[$this->path . '/import']['title'] = 'Import Translator';
  20. unset($items[$this->path . '/import']['title callback']);
  21. unset($items[$this->path . '/import']['title arguments']);
  22. }
  23. return $items;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function overviewForm($form, &$form_state) {
  29. $form['translators']['#tree'] = TRUE;
  30. $form['translators']['#theme'] = 'tmgmt_ui_translator_overview_form';
  31. $form['translators']['#entity_info'] = $this->entityInfo;
  32. // Load all translator entities.
  33. $translators = tmgmt_translator_load_multiple(FALSE);
  34. foreach ($translators as $key => $translator) {
  35. $form['translators'][$key] = $this->overviewFormRow(array(), $form_state, $translator, $key);
  36. $form['translators'][$key]['#translator'] = $translator;
  37. }
  38. $form['actions']['#type'] = 'actions';
  39. $form['actions']['submit'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Save'),
  42. );
  43. return $form;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function overviewFormSubmit($form, &$form_state) {
  49. // Update image effect weights.
  50. if (!empty($form_state['values']['translators'])) {
  51. $translators = tmgmt_translator_load_multiple(array_keys($form_state['values']['translators']));
  52. foreach ($form_state['values']['translators'] as $key => $item) {
  53. if (isset($translators[$key])) {
  54. $translators[$key]->weight = $item['weight'];
  55. entity_save($this->entityType, $translators[$key]);
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * Helper method for building a row in the overview form.
  62. */
  63. protected function overviewFormRow($form, &$form_state, $entity, $id) {
  64. $form['#weight'] = isset($form_state['input']['translators']) ? $form_state['input']['translators'][$id]['weight'] : NULL;
  65. $form['label'] = array(
  66. '#theme' => 'tmgmt_ui_translator_overview_item',
  67. '#attached' => array('css' => array(drupal_get_path('module', 'tmgmt_ui') . '/css/tmgmt_ui.admin.css')),
  68. '#label' => $entity->label(),
  69. '#name' => !empty($this->entityInfo['exportable']) ? entity_id($this->entityType, $entity) : FALSE,
  70. '#url' => FALSE,
  71. '#description' => $entity->description,
  72. '#entity_type' => $this->entityType,
  73. );
  74. // Add a row for the exportable status.
  75. if (!empty($this->entityInfo['exportable'])) {
  76. $form['status'] = array(
  77. '#theme' => 'entity_status',
  78. '#status' => $entity->{$this->statusKey},
  79. );
  80. }
  81. $wrapper = entity_metadata_wrapper($this->entityType, $entity);
  82. // Add a column to show the translator plugin via the metadata wrapper.
  83. $form['plugin'] = array(
  84. '#markup' => $wrapper->plugin->label(),
  85. '#status' => $entity->{$this->statusKey},
  86. );
  87. $controller = $entity->getController();
  88. $form['configured'] = array(
  89. '#markup' => $controller->isAvailable($entity) ? t('Yes') : t('No'),
  90. );
  91. $form['weight'] = array(
  92. '#type' => 'weight',
  93. '#delta' => 30,
  94. '#default_value' => $entity->weight,
  95. );
  96. // Add operations depending on the status.
  97. if (entity_has_status($this->entityType, $entity, ENTITY_FIXED)) {
  98. $form['operations']['clone'] = array(
  99. '#type' => 'link',
  100. '#title' => t('clone'),
  101. '#href' => $this->path . '/manage/' . $id . '/clone',
  102. );
  103. }
  104. else {
  105. $form['operations']['edit'] = array(
  106. '#type' => 'link',
  107. '#title' => t('edit'),
  108. '#href' => $this->path . '/manage/' . $id,
  109. );
  110. if (!empty($this->entityInfo['exportable'])) {
  111. $form['operations']['clone'] = array(
  112. '#type' => 'link',
  113. '#title' => t('clone'),
  114. '#href' => $this->path . '/manage/' . $id . '/clone',
  115. );
  116. }
  117. if (empty($this->entityInfo['exportable']) || !entity_has_status($this->entityType, $entity, ENTITY_IN_CODE)) {
  118. $form['operations']['delete'] = array(
  119. '#type' => 'link',
  120. '#title' => t('delete'),
  121. '#href' => $this->path . '/manage/' . $id . '/delete',
  122. '#options' => array('query' => drupal_get_destination()),
  123. );
  124. }
  125. elseif (entity_has_status($this->entityType, $entity, ENTITY_OVERRIDDEN)) {
  126. $form['operations']['revert'] = array(
  127. '#type' => 'link',
  128. '#title' => t('revert'),
  129. '#href' => $this->path . '/manage/' . $id . '/revert',
  130. '#options' => array('query' => drupal_get_destination()),
  131. );
  132. }
  133. else {
  134. $row[] = '';
  135. }
  136. }
  137. if (!empty($this->entityInfo['exportable'])) {
  138. $form['operations']['export'] = array(
  139. '#type' => 'link',
  140. '#title' => t('export'),
  141. '#href' => $this->path . '/manage/' . $id . '/export',
  142. );
  143. }
  144. return $form;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function applyOperation($op, $entity) {
  150. if ($op == 'delete' && tmgmt_translator_busy($entity->name)) {
  151. drupal_set_message(t("The translator %translator could not be deleted because it is currently being used by at least one active translation job.", array('%translator' => $entity->label())), 'error');
  152. return FALSE;
  153. }
  154. return parent::applyOperation($op, $entity);
  155. }
  156. }