queue_ui.pages.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @file queue_ui.pages.inc
  4. */
  5. /**
  6. * Queue form handler.
  7. */
  8. function queue_ui_page($form, &$form_state) {
  9. //queue_ui_test(); // @todo remove before prod
  10. // Initialize.
  11. if ($form_state['rebuild']) {
  12. $form_state['input'] = array();
  13. }
  14. if (empty($form_state['storage'])) {
  15. // First step, so start with our overview form.
  16. $form_state['storage'] = array(
  17. 'step' => 'queue_ui_overview_form',
  18. );
  19. }
  20. // Return the form from the current step.
  21. $function = $form_state['storage']['step'];
  22. $form = $function($form, $form_state);
  23. return $form;
  24. }
  25. function queue_ui_page_submit($form, &$form_state) {
  26. $values = $form_state['values'];
  27. // Get submitted queues to act on.
  28. $queues = array_filter($values['queues']);
  29. if (empty($queues)) {
  30. // Nothing to do.
  31. return;
  32. }
  33. if (isset($values['step_submit'])) {
  34. // Pass off to step submit handler.
  35. $function = $values['step_submit'];
  36. $function($form, $form_state, $queues);
  37. }
  38. return;
  39. }
  40. function queue_ui_overview_form() {
  41. $queues = $options = array();
  42. // @todo activation status
  43. $header = array(
  44. 'name' => array('data' => t('Name')),
  45. 'title' => array('data' => t('Title')),
  46. 'items' => array('data' => t('Number of items')),
  47. //'operations' => array('data' => t('Operations')),
  48. );
  49. // Get queues defined via our hook.
  50. $defined_queues = queue_ui_defined_queues();
  51. // Get queues names.
  52. $queues = queue_ui_queues();
  53. foreach ($queues as $name => $queue) {
  54. $title = '';
  55. $operations = '';
  56. if (isset($defined_queues[$name])) {
  57. $title = $defined_queues[$name]['title'];
  58. }
  59. if (isset($defined_queues[$name]['batch'])) {
  60. $operations = 'batch';
  61. }
  62. $options[$name] = array(
  63. 'name' => array('data' => $name),
  64. 'title' => array('data' => $title),
  65. 'items' => array('data' => $queue['items']),
  66. //'operations' => array('data => $operations),
  67. );
  68. }
  69. $form['queues'] = array(
  70. '#type' => 'tableselect',
  71. '#header' => $header,
  72. '#options' => $options,
  73. '#empty' => t('No queues exist.'),
  74. );
  75. // @todo deactivate options
  76. // Option to run batch.
  77. $form['batch'] = array(
  78. '#type' => 'submit',
  79. '#value' => t('Batch process'),
  80. );
  81. // Option to remove lease timestamps.
  82. $form['release'] = array(
  83. '#type' => 'submit',
  84. '#value' => t('Remove leases'),
  85. );
  86. // Option to run via cron.
  87. $form['cron'] = array(
  88. '#type' => 'submit',
  89. '#value' => t('Cron process'),
  90. );
  91. // Option to delete queue.
  92. $form['delete'] = array(
  93. '#type' => 'submit',
  94. '#value' => t('Delete queues'),
  95. );
  96. // Specify our step submit callback.
  97. $form['step_submit'] = array('#type' => 'value', '#value' => 'queue_ui_overview_submit');
  98. return $form;
  99. }
  100. /**
  101. * Overview submit handler.
  102. */
  103. function queue_ui_overview_submit($form, &$form_state, $queues) {
  104. $values = $form_state['values'];
  105. // Switch off submitted action.
  106. switch ($values['op']) {
  107. case $values['cron']:
  108. // Set variables for cron to TRUE.
  109. $defined_queues = queue_ui_defined_queues();
  110. $intersect = array_intersect(array_keys($defined_queues), $queues);
  111. foreach ($intersect as $name) {
  112. if (isset($defined_queues[$name]['cron'])) {
  113. variable_set('queue_ui_cron_' . $name, TRUE);
  114. }
  115. }
  116. break;
  117. case $values['batch']:
  118. // Process queue(s) with batch.
  119. // We can only run batch on queues using our hook_queue_info() that define batch.
  120. $defined_queues = queue_ui_defined_queues();
  121. $intersect = array_intersect(array_keys($defined_queues), $queues);
  122. foreach ($intersect as $name) {
  123. // Only if queue_info implementation defined batch can we set it here.
  124. if (isset($defined_queues[$name]['batch'])) {
  125. $batch = $defined_queues[$name]['batch'];
  126. // Add queue as argument to operations by resetting the operations array.
  127. $operations = array();
  128. $queue = DrupalQueue::get($name);
  129. foreach ($batch['operations'] as $operation) {
  130. // First element is the batch process callback, second is the argument.
  131. $operations[] = array($operation[0], array_merge(array($queue), $operation[1]));
  132. }
  133. $batch['operations'] = $operations;
  134. // Set.
  135. batch_set($batch);
  136. }
  137. }
  138. break;
  139. case $values['delete']:
  140. // Confirm before deleting. Go multistep!
  141. $form_state['rebuild'] = TRUE;
  142. $form_state['storage']['queues'] = $queues;
  143. $form_state['storage']['step'] = 'queue_ui_confirm_delete';
  144. break;
  145. case $values['release']:
  146. foreach ($queues as $name) {
  147. $num_updated = db_update('queue')
  148. ->fields(array(
  149. 'expire' => 0,
  150. ))
  151. ->condition('name', $name, '=')
  152. ->execute();
  153. drupal_set_message(t('!count lease reset in queue !name', array('!count' => $num_updated, '!name' => $name)));
  154. }
  155. break;
  156. }
  157. return;
  158. }
  159. /**
  160. * Confirm form for deleting queues.
  161. */
  162. function queue_ui_confirm_delete($form, &$form_state) {
  163. $form['queues'] = array('#type' => 'value', '#value' => $form_state['storage']['queues']);
  164. $description = t('All items in each queue will be deleted, regardless of if leases exist. This operation cannot be undone.');
  165. // Specify our step submit callback.
  166. $form['step_submit'] = array('#type' => 'value', '#value' => 'queue_ui_delete_submit');
  167. return confirm_form($form,
  168. format_plural(count($form_state['storage']['queues']), 'Are you sure you want to delete the queue?', 'Are you sure you want to delete @count queues?'),
  169. 'admin/config/system/queue-ui',
  170. $description,
  171. t('Delete'),
  172. t('Cancel')
  173. );
  174. }
  175. /**
  176. * Submit handler for deleting queues.
  177. */
  178. function queue_ui_delete_submit($form, &$form_state, $queues) {
  179. $values = $form_state['values'];
  180. $defined_queues = queue_ui_defined_queues();
  181. foreach ($queues as $name) {
  182. $queue = DrupalQueue::get($name);
  183. if (isset($defined_queues[$name]['delete'])) {
  184. $function = $defined_queues[$name]['delete'];
  185. $function($queue);
  186. }
  187. $queue->deleteQueue();
  188. }
  189. drupal_set_message(format_plural(count($values['queues']), 'Queue deleted', '@count queues deleted'));
  190. return;
  191. }