tmgmt_ui.controller.job.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Contains the job UI controller.
  5. */
  6. /**
  7. * Entity UI controller for the Job Entity.
  8. */
  9. class TMGMTJobUIController extends EntityDefaultUIController {
  10. /**
  11. * Translation job that is a duplicate of an aborted job.
  12. *
  13. * @var TMGMTJob
  14. */
  15. private $jobToResubmit = NULL;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function hook_menu() {
  20. $id_count = count(explode('/', $this->path));
  21. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  22. $items[$this->path . '/' . $wildcard] = array(
  23. 'title callback' => 'entity_label',
  24. 'title arguments' => array($this->entityType, $id_count),
  25. 'page callback' => 'tmgmt_ui_job_view',
  26. 'page arguments' => array($id_count),
  27. 'load arguments' => array($this->entityType),
  28. 'access callback' => 'entity_access',
  29. 'access arguments' => array('view', $this->entityType, $id_count),
  30. 'file' => $this->entityInfo['admin ui']['file'],
  31. 'file path' => $this->entityInfo['admin ui']['file path'],
  32. );
  33. $items[$this->path . '/' . $wildcard . '/delete'] = array(
  34. 'page callback' => 'drupal_get_form',
  35. 'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $id_count, $id_count + 1),
  36. 'load arguments' => array($this->entityType),
  37. 'access callback' => 'entity_access',
  38. 'access arguments' => array('delete', $this->entityType, $id_count),
  39. 'type' => MENU_CALLBACK,
  40. );
  41. $items[$this->path . '/' . $wildcard . '/abort'] = array(
  42. 'page callback' => 'drupal_get_form',
  43. 'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $id_count, $id_count + 1),
  44. 'load arguments' => array($this->entityType),
  45. 'access callback' => 'entity_access',
  46. 'access arguments' => array('abort', $this->entityType, $id_count),
  47. 'type' => MENU_CALLBACK,
  48. );
  49. $items[$this->path . '/' . $wildcard . '/resubmit'] = array(
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $id_count, $id_count + 1),
  52. 'load arguments' => array($this->entityType),
  53. 'access callback' => 'entity_access',
  54. 'access arguments' => array('resubmit', $this->entityType, $id_count),
  55. 'type' => MENU_CALLBACK,
  56. );
  57. return $items;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function operationForm($form, &$form_state, $entity, $op) {
  63. switch ($op) {
  64. case 'delete':
  65. $confirm_question = t('Are you sure you want to delete the translation job %label?', array('%label' => $entity->label()));
  66. return confirm_form($form, $confirm_question, $this->path);
  67. case 'abort':
  68. return confirm_form($form, t('Abort this job?'), 'admin/tmgmt/jobs/' . $entity->tjid,
  69. t('This will send a request to the translator to abort the job. After the action the job translation process will be aborted and only remaining action will be resubmitting it.'));
  70. case 'resubmit':
  71. return confirm_form($form, t('Resubmit as a new job?'), 'admin/tmgmt/jobs/' . $entity->tjid,
  72. t('This creates a new job with the same items which can then be submitted again. In case the sources meanwhile changed, the new job will reflect the update.'));
  73. }
  74. drupal_not_found();
  75. exit;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function applyOperation($op, $entity) {
  81. switch ($op) {
  82. case 'delete':
  83. $entity->delete();
  84. return t('Deleted the translation job %label.', array('%label' => $entity->label()));
  85. case 'abort':
  86. if (!$entity->abortTranslation()) {
  87. // This is the case when a translator does not support the abort operation.
  88. // It would make more sense to not display the button for the action,
  89. // however we do not know if the translator is able to abort a job until
  90. // we trigger the action.
  91. foreach ($entity->getMessagesSince() as $message) {
  92. if ($message->type == 'debug') {
  93. continue;
  94. }
  95. if ($text = $message->getMessage()) {
  96. // We want to persist also the type therefore we will set the
  97. // messages directly and not return them.
  98. drupal_set_message(filter_xss($text), $message->type);
  99. }
  100. }
  101. }
  102. break;
  103. case 'resubmit':
  104. $this->jobToResubmit = $entity->cloneAsUnprocessed();
  105. $this->jobToResubmit->uid = $GLOBALS['user']->uid;
  106. $this->jobToResubmit->save();
  107. /** @var TMGMTJobItem $item */
  108. foreach ($entity->getItems() as $item) {
  109. $item_to_resubmit = $item->cloneAsActive();
  110. $this->jobToResubmit->addExistingItem($item_to_resubmit);
  111. }
  112. $entity->addMessage('Job has been duplicated as a new job <a href="@url">#@id</a>.',
  113. array('@url' => url('admin/tmgmt/jobs/' . $this->jobToResubmit->tjid), '@id' => $this->jobToResubmit->tjid));
  114. $this->jobToResubmit->addMessage('This job is a duplicate of the previously aborted job <a href="@url">#@id</a>',
  115. array('@url' => url('admin/tmgmt/jobs/' . $entity->tjid), '@id' => $entity->tjid));
  116. return t('The aborted job has been duplicated. You can resubmit it now.');
  117. }
  118. return FALSE;
  119. }
  120. /**
  121. * Operation form submit callback.
  122. */
  123. public function operationFormSubmit($form, &$form_state) {
  124. parent::operationFormSubmit($form, $form_state);
  125. if ($form_state['op'] == 'resubmit') {
  126. $form_state['redirect'] = $this->path . '/' . $this->jobToResubmit->tjid;
  127. }
  128. }
  129. }