tmgmt.rules.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration.
  5. */
  6. /**
  7. * {@inheritdoc}
  8. */
  9. function tmgmt_rules_action_info() {
  10. $info['tmgmt_rules_job_request_translation'] = array(
  11. 'label' => t('Request Job translation'),
  12. 'group' => t('Translation Management'),
  13. 'parameter' => array(
  14. 'job' => array(
  15. 'type' => 'tmgmt_job',
  16. 'label' => t('Translation Job'),
  17. 'description' => t('The translation job for which translations should be requested.'),
  18. ),
  19. ),
  20. 'access callback' => 'tmgmt_rules_job_submit_access',
  21. );
  22. $info['tmgmt_rules_job_accept_translation'] = array(
  23. 'label' => t('Accept Job translation'),
  24. 'group' => t('Translation Management'),
  25. 'parameter' => array(
  26. 'job' => array(
  27. 'type' => 'tmgmt_job',
  28. 'label' => t('Translation Job'),
  29. 'description' => t('The translation job for which translations should be accepted.'),
  30. ),
  31. 'message' => array(
  32. 'type' => 'text',
  33. 'label' => t('An optional message'),
  34. 'description' => t('Will be stored in the job message and displayed to the user.'),
  35. 'optional' => TRUE,
  36. ),
  37. ),
  38. 'access callback' => 'tmgmt_rules_job_accept_translation_access',
  39. );
  40. $info['tmgmt_rules_job_abort_translation'] = array(
  41. 'label' => t('Abort translation job'),
  42. 'group' => t('Translation Management'),
  43. 'parameter' => array(
  44. 'job' => array(
  45. 'type' => 'tmgmt_job',
  46. 'label' => t('Translation Job'),
  47. 'description' => t('The translation job that should be aborted.'),
  48. ),
  49. ),
  50. 'access callback' => 'tmgmt_rules_job_submit_access',
  51. );
  52. $info['tmgmt_rules_job_delete'] = array(
  53. 'label' => t('Delete Job'),
  54. 'group' => t('Translation Management'),
  55. 'parameter' => array(
  56. 'job' => array(
  57. 'type' => 'tmgmt_job',
  58. 'label' => t('Translation Job'),
  59. 'description' => t('The translation job that should be deleted.'),
  60. ),
  61. ),
  62. 'access callback' => 'tmgmt_rules_job_delete_access',
  63. );
  64. $info['tmgmt_rules_job_checkout'] = array(
  65. 'label' => t('Checkout a job'),
  66. 'group' => t('Translation Management'),
  67. 'parameter' => array(
  68. 'job' => array(
  69. 'type' => 'tmgmt_job',
  70. 'label' => t('Translation Job'),
  71. 'description' => t('The translation job that should be checked out.'),
  72. ),
  73. ),
  74. 'access callback' => 'tmgmt_rules_job_submit_access',
  75. );
  76. $info['tmgmt_get_first_from_node_list'] = array(
  77. 'label' => t('Get first item from a list of nodes'),
  78. 'group' => t('Data'),
  79. 'parameter' => array(
  80. 'list' => array(
  81. 'type' => 'list<node>',
  82. 'label' => t('List'),
  83. 'restriction' => 'selector',
  84. ),
  85. ),
  86. 'provides' => array(
  87. 'first_node' => array(
  88. 'type' => 'node',
  89. 'label' => t('Node'),
  90. ),
  91. ),
  92. );
  93. $info['tmgmt_rules_create_job'] = array(
  94. 'label' => t('Create a job for a given source language'),
  95. 'group' => t('Translation Management'),
  96. 'parameter' => array(
  97. 'source_language' => array(
  98. 'type' => 'text',
  99. 'label' => t('Source Language'),
  100. 'description' => t('The language from which should be translated'),
  101. 'options list' => 'entity_metadata_language_list',
  102. ),
  103. ),
  104. 'provides' => array(
  105. 'job' => array(
  106. 'label' => t('Job'),
  107. 'type' => 'tmgmt_job',
  108. ),
  109. ),
  110. );
  111. $info['tmgmt_rules_job_add_item'] = array(
  112. 'label' => t('Add an item to a job'),
  113. 'group' => t('Translation Management'),
  114. 'parameter' => array(
  115. 'job' => array(
  116. 'type' => 'tmgmt_job',
  117. 'label' => t('Translation Job'),
  118. 'description' => t('The translation job that should be canceled.'),
  119. ),
  120. 'plugin' => array(
  121. 'type' => 'token',
  122. 'label' => t('Source plugin'),
  123. 'description' => t('The source plugin of this item'),
  124. //'options list' => 'entity_metadata_language_list',
  125. ),
  126. 'item_type' => array(
  127. 'type' => 'token',
  128. 'label' => t('Item type'),
  129. 'description' => t('The item type'),
  130. //'options list' => 'entity_metadata_language_list',
  131. ),
  132. 'item_id' => array(
  133. 'type' => 'text',
  134. 'label' => t('Item ID'),
  135. 'description' => t('ID of the referenced item'),
  136. ),
  137. ),
  138. );
  139. return $info;
  140. }
  141. /**
  142. * Rules callback to request a translation of a job.
  143. */
  144. function tmgmt_rules_job_request_translation(TMGMTJob $job) {
  145. if ($job->isTranslatable()) {
  146. $job->requestTranslation();
  147. }
  148. }
  149. /**
  150. * Rules callback to accept a translation of a job.
  151. */
  152. function tmgmt_rules_job_accept_translation(TMGMTJob $job, $message) {
  153. foreach ($job->getItems() as $item) {
  154. if ($item->isNeedsReview()) {
  155. $item->acceptTranslation();
  156. }
  157. }
  158. }
  159. /**
  160. * Rules callback to cancel a translation job.
  161. */
  162. function tmgmt_rules_job_abort_translation(TMGMTJob $job) {
  163. $job->abortTranslation();
  164. }
  165. /**
  166. * Rules callback to redirect to a translation job.
  167. */
  168. function tmgmt_rules_job_checkout(TMGMTJob $job) {
  169. $redirects = tmgmt_ui_job_checkout_multiple(array($job));
  170. // If necessary, do a redirect.
  171. if ($redirects) {
  172. tmgmt_ui_redirect_queue_set($redirects, current_path());
  173. drupal_goto(tmgmt_ui_redirect_queue_dequeue());
  174. // Count of the job messages is one less due to the final redirect.
  175. drupal_set_message(format_plural(count($redirects), t('One job needs to be checked out.'), t('@count jobs need to be checked out.')));
  176. }
  177. }
  178. /**
  179. * Rules callback to get the job for a specific language combination.
  180. */
  181. function tmgmt_rules_create_job($source_language) {
  182. return array(
  183. 'job' => tmgmt_job_create($source_language, ''),
  184. );
  185. }
  186. /**
  187. * Rules callback to add an item to a job.
  188. */
  189. function tmgmt_rules_job_add_item(TMGMTJob $job, $plugin, $item_type, $item_id) {
  190. try {
  191. $job->addItem($plugin, $item_type, $item_id);
  192. }
  193. catch (TMGMTException $e) {
  194. watchdog_exception('tmgmt', $e);
  195. drupal_set_message(t('Unable to add job item of type %type with id %id. Make sure the source content is not empty.',
  196. array('%type' => $item_type, '%id' => $item_id)), 'error');
  197. }
  198. }
  199. /**
  200. * Rules action to extract the first node from a node list.
  201. */
  202. function tmgmt_get_first_from_node_list($list) {
  203. return array(
  204. 'first_node' => reset($list),
  205. );
  206. }
  207. /**
  208. * Rules action to delete a translation job.
  209. */
  210. function tmgmt_rules_job_delete(TMGMTJob $job) {
  211. // Prevent users without job delete permission to be able to delete jobs.
  212. if (tmgmt_job_access('delete')) {
  213. $job->delete();
  214. }
  215. }
  216. /**
  217. * Checks access to rules job delete action.
  218. */
  219. function tmgmt_rules_job_delete_access() {
  220. return tmgmt_job_access('delete');
  221. }
  222. /**
  223. * Checks access to rules job submit like actions.
  224. */
  225. function tmgmt_rules_job_submit_access() {
  226. return tmgmt_job_access('submit');
  227. }
  228. /**
  229. * Checks access to rules accept translation action.
  230. */
  231. function tmgmt_rules_job_accept_translation_access() {
  232. return tmgmt_job_access('accept');
  233. }