QueueUISystemQueue.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class QueueUISystemQueue implements QueueUIInterface {
  3. public $inspect;
  4. public function __construct() {
  5. $this->inspect = TRUE;
  6. }
  7. /**
  8. * SystemQueue implements all default QueueUI methods.
  9. *
  10. * @return array
  11. * An array of available QueueUI methods. Array key is system name of the
  12. * operation, array key value is the display name.
  13. */
  14. public function getOperations() {
  15. return array(
  16. 'view' => t('View'),
  17. 'release' => t('Release'),
  18. 'delete' => t('Delete'),
  19. );
  20. }
  21. /**
  22. * View the queue items in a queue and expose additional methods for inspection.
  23. *
  24. * @param string $queue_name
  25. * @return string
  26. */
  27. public function inspect($queue_name) {
  28. $query = db_select('queue', 'q')
  29. ->fields('q', array('item_id', 'expire', 'created'))
  30. ->condition('q.name', $queue_name)
  31. ->extend('PagerDefault')
  32. ->limit(25)
  33. ->execute();
  34. foreach ($query as $record) {
  35. $result[] = $record;
  36. }
  37. $header = array(
  38. t('Item ID'),
  39. t('Expires'),
  40. t('Created'),
  41. array('data' => t('Operations'), 'colspan' => '3'),
  42. );
  43. $rows = array();
  44. foreach ($result as $item) {
  45. $row = array();
  46. $row[] = $item->item_id;
  47. $row[] = ($item->expire ? date(DATE_RSS, $item->expire) : $item->expire);
  48. $row[] = date(DATE_RSS, $item->created);
  49. foreach ($this->getOperations() as $op => $title) {
  50. $row[] = l($title, QUEUE_UI_BASE . "/$queue_name/$op/$item->item_id");
  51. }
  52. $rows[] = array('data' => $row);
  53. }
  54. $content = theme('table', array('header' => $header, 'rows' => $rows));
  55. $content .= theme('pager');
  56. return $content;
  57. }
  58. /**
  59. * View the item data for a specified queue item.
  60. *
  61. * @param int $item_id
  62. * @return string
  63. */
  64. public function view($item_id) {
  65. $queue_item = $this->loadItem($item_id);
  66. $rows[] = array(
  67. 'data' => array(
  68. 'header' => t('Item ID'),
  69. 'data' => $queue_item->item_id,
  70. ),
  71. );
  72. $rows[] = array(
  73. 'data' => array(
  74. 'header' => t('Queue name'),
  75. 'data' => $queue_item->name,
  76. ),
  77. );
  78. $rows[] = array(
  79. 'data' => array(
  80. 'header' => t('Expire'),
  81. 'data' => ($queue_item->expire ? date(DATE_RSS, $queue_item->expire) : $queue_item->expire),
  82. ),
  83. );
  84. $rows[] = array(
  85. 'data' => array(
  86. 'header' => t('Created'),
  87. 'data' => date(DATE_RSS, $queue_item->created),
  88. ),
  89. );
  90. $rows[] = array(
  91. 'data' => array(
  92. 'header' => array('data' => t('Data'), 'style' => 'vertical-align:top'),
  93. 'data' => '<pre>' . print_r(unserialize($queue_item->data), TRUE) . '</pre>',
  94. // @TODO - should probably do something nicer than print_r here...
  95. ),
  96. );
  97. return theme('table', array('rows' => $rows));
  98. }
  99. public function delete($item_id) {
  100. // @TODO - try... catch...
  101. drupal_set_message("Deleted queue item " . $item_id);
  102. db_delete('queue')
  103. ->condition('item_id', $item_id)
  104. ->execute();
  105. return TRUE;
  106. }
  107. public function release($item_id) {
  108. // @TODO - try... catch...
  109. drupal_set_message("Released queue item " . $item_id);
  110. db_update('queue')
  111. ->condition('item_id', $item_id)
  112. ->fields(array('expire' => 0))
  113. ->execute();
  114. return TRUE;
  115. }
  116. /**
  117. * Load a specified SystemQueue queue item from the database.
  118. *
  119. * @param $item_id
  120. * The item id to load
  121. * @return
  122. * Result of the database query loading the queue item.
  123. */
  124. private function loadItem($item_id) {
  125. // Load the specified queue item from the queue table.
  126. $query = db_select('queue', 'q')
  127. ->fields('q', array('item_id', 'name', 'data', 'expire', 'created'))
  128. ->condition('q.item_id', $item_id)
  129. ->range(0, 1) // item id should be unique
  130. ->execute();
  131. foreach ($query as $record) {
  132. $result[] = $record;
  133. }
  134. return $result[0];
  135. }
  136. }