queue_ui.pages.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $queue_classes = _queue_ui_array_keys_contain($values, 'queues');
  28. $queues = array();
  29. foreach ($queue_classes as $class_name) {
  30. foreach ($values[$class_name] as $queue) {
  31. $queues[] = $queue;
  32. }
  33. }
  34. // Get submitted queues to act on.
  35. $queues = array_filter($queues);
  36. if (empty($queues)) {
  37. // Nothing to do.
  38. return;
  39. }
  40. if (isset($values['step_submit'])) {
  41. // Pass off to step submit handler.
  42. $function = $values['step_submit'];
  43. $function($form, $form_state, $queues);
  44. }
  45. return;
  46. }
  47. function queue_ui_overview_form() {
  48. $queues = $options = array();
  49. // @todo activation status
  50. $header = array(
  51. 'name' => array('data' => t('Name')),
  52. 'title' => array('data' => t('Title')),
  53. 'items' => array('data' => t('Number of items')),
  54. 'class' => array('data' => t('Class')),
  55. 'inspect' => array('data' => t('Inspect')),
  56. //'operations' => array('data' => t('Operations')),
  57. );
  58. // Get queues defined via our hook.
  59. $defined_queues = queue_ui_defined_queues();
  60. // Get queues names.
  61. $queues = queue_ui_queues();
  62. foreach ($queues as $class => $classed_queues) {
  63. $options = array();
  64. $class_info = QueueUI::get('QueueUI' . $class);
  65. // Output information for each queue of the current class
  66. foreach($classed_queues as $name => $queue) {
  67. $title = '';
  68. $operations = '';
  69. $inspect = FALSE;
  70. if (isset($defined_queues[$name])) {
  71. $title = $defined_queues[$name]['title'];
  72. }
  73. if (isset($defined_queues[$name]['batch'])) {
  74. $operations = 'batch';
  75. }
  76. if (is_object($class_info) && $class_info->inspect) {
  77. $inspect = TRUE;
  78. }
  79. $options[$name] = array(
  80. 'name' => array('data' => $name),
  81. 'title' => array('data' => $title),
  82. 'items' => array('data' => $queue['items']),
  83. 'class' => array('data' => $class),
  84. //'operations' => array('data' => $operations),
  85. );
  86. // If queue inspection is enabled for this class, add to the options array.
  87. if ($inspect) {
  88. $options[$name]['inspect'] = array('data' => l(t('Inspect'), QUEUE_UI_BASE . "/inspect/$name"));
  89. }
  90. else {
  91. $options[$name]['inspect'] = '';
  92. }
  93. }
  94. $form[$class] = array(
  95. '#type' => 'fieldset',
  96. '#collapsible' => TRUE,
  97. '#title' => $class,
  98. );
  99. $form[$class]['queues_' . $class] = array(
  100. '#type' => 'tableselect',
  101. '#header' => $header,
  102. '#options' => $options,
  103. '#empty' => t('No queues exist for @class.', array('@class' => $class)),
  104. );
  105. }
  106. // @todo deactivate options
  107. // Option to run batch.
  108. $form['batch'] = array(
  109. '#type' => 'submit',
  110. '#value' => t('Batch process'),
  111. );
  112. // Option to remove lease timestamps.
  113. $form['release'] = array(
  114. '#type' => 'submit',
  115. '#value' => t('Remove leases'),
  116. );
  117. // Option to run via cron.
  118. $form['cron'] = array(
  119. '#type' => 'submit',
  120. '#value' => t('Cron process'),
  121. );
  122. // Option to delete queue.
  123. $form['delete'] = array(
  124. '#type' => 'submit',
  125. '#value' => t('Delete queues'),
  126. );
  127. // Specify our step submit callback.
  128. $form['step_submit'] = array('#type' => 'value', '#value' => 'queue_ui_overview_submit');
  129. return $form;
  130. }
  131. /**
  132. * Overview submit handler.
  133. */
  134. function queue_ui_overview_submit($form, &$form_state, $queues) {
  135. $values = $form_state['values'];
  136. // Switch off submitted action.
  137. switch ($values['op']) {
  138. case $values['cron']:
  139. // Set variables for cron to TRUE.
  140. $defined_queues = queue_ui_defined_queues();
  141. $intersect = array_intersect(array_keys($defined_queues), $queues);
  142. foreach ($intersect as $name) {
  143. if (isset($defined_queues[$name]['cron'])) {
  144. variable_set('queue_ui_cron_' . $name, TRUE);
  145. }
  146. }
  147. break;
  148. case $values['batch']:
  149. // Process queue(s) with batch.
  150. // We can only run batch on queues using our hook_queue_info() that define batch.
  151. $defined_queues = queue_ui_defined_queues();
  152. $intersect = array_intersect(array_keys($defined_queues), $queues);
  153. foreach ($intersect as $name) {
  154. // Only if queue_info implementation defined batch can we set it here.
  155. if (isset($defined_queues[$name]['batch'])) {
  156. $batch = $defined_queues[$name]['batch'];
  157. // Add queue as argument to operations by resetting the operations array.
  158. $operations = array();
  159. $queue = DrupalQueue::get($name);
  160. foreach ($batch['operations'] as $operation) {
  161. // First element is the batch process callback, second is the argument.
  162. $operations[] = array($operation[0], array_merge(array($queue), $operation[1]));
  163. }
  164. $batch['operations'] = $operations;
  165. // Set.
  166. batch_set($batch);
  167. }
  168. }
  169. break;
  170. case $values['delete']:
  171. // Confirm before deleting. Go multistep!
  172. $form_state['rebuild'] = TRUE;
  173. $form_state['storage']['queues'] = $queues;
  174. $form_state['storage']['step'] = 'queue_ui_confirm_delete';
  175. break;
  176. case $values['release']:
  177. foreach ($queues as $name) {
  178. $num_updated = db_update('queue')
  179. ->fields(array(
  180. 'expire' => 0,
  181. ))
  182. ->condition('name', $name, '=')
  183. ->execute();
  184. drupal_set_message(t('!count lease reset in queue !name', array('!count' => $num_updated, '!name' => $name)));
  185. }
  186. break;
  187. }
  188. return;
  189. }
  190. /**
  191. * Confirm form for deleting queues.
  192. */
  193. function queue_ui_confirm_delete($form, &$form_state) {
  194. $form['queues'] = array('#type' => 'value', '#value' => $form_state['storage']['queues']);
  195. $description = t('All items in each queue will be deleted, regardless of if leases exist. This operation cannot be undone.');
  196. // Specify our step submit callback.
  197. $form['step_submit'] = array('#type' => 'value', '#value' => 'queue_ui_delete_submit');
  198. return confirm_form($form,
  199. 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?'),
  200. 'admin/config/system/queue-ui',
  201. $description,
  202. t('Delete'),
  203. t('Cancel')
  204. );
  205. }
  206. /**
  207. * Submit handler for deleting queues.
  208. */
  209. function queue_ui_delete_submit($form, &$form_state, $queues) {
  210. $values = $form_state['values'];
  211. $defined_queues = queue_ui_defined_queues();
  212. foreach ($queues as $name) {
  213. $queue = DrupalQueue::get($name);
  214. if (isset($defined_queues[$name]['delete'])) {
  215. $function = $defined_queues[$name]['delete'];
  216. $function($queue);
  217. }
  218. $queue->deleteQueue();
  219. }
  220. drupal_set_message(format_plural(count($values['queues']), 'Queue deleted', '@count queues deleted'));
  221. return;
  222. }
  223. function _queue_ui_array_keys_contain($input, $search_value, $strict = FALSE) {
  224. $tmpkeys = array();
  225. $keys = array_keys($input);
  226. foreach ($keys as $k) {
  227. if ($strict && strpos($k, $search_value) !== FALSE) {
  228. $tmpkeys[] = $k;
  229. }
  230. elseif (!$strict && stripos($k, $search_value) !== FALSE) {
  231. $tmpkeys[] = $k;
  232. }
  233. }
  234. return $tmpkeys;
  235. }