nodequeue_generate.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function nodequeue_generate_menu() {
  6. $items['admin/structure/nodequeue/generate_nodequeue'] = array(
  7. 'title' => 'Generate queue assignments',
  8. 'description' => 'Bulk add nodes into queues',
  9. 'page callback' => 'drupal_get_form',
  10. 'page arguments' => array('nodequeue_generate_form'),
  11. 'access callback' => 'user_access',
  12. 'access arguments' => array('manipulate all queues'),
  13. 'type' => MENU_LOCAL_TASK,
  14. );
  15. return $items;
  16. }
  17. /**
  18. * Menu callback; Returns the nodequeue generate form.
  19. */
  20. function nodequeue_generate_form() {
  21. $form['help'] = array(
  22. '#markup' => '<p>' . t('Select which queues shall be <strong>emptied</strong> and re-populated with new nodes.') . '</p>'
  23. );
  24. $queues = nodequeue_load_queues(nodequeue_get_all_qids(25));
  25. // Tableselect header.
  26. $header = array(
  27. 'name' => 'Queue name',
  28. 'max_nodes' => 'Max nodes',
  29. 'subqueues' => 'Subqueues',
  30. );
  31. // Tableselect data.
  32. $data = array();
  33. foreach ($queues as $queue) {
  34. $data[$queue->qid]['name'] = check_plain($queue->title);
  35. $data[$queue->qid]['max_nodes'] = $queue->size == 0 ? t('Infinite') : $queue->size;
  36. $data[$queue->qid]['subqueues'] = $queue->subqueues;
  37. }
  38. // Table select element.
  39. $form['nodequeues'] = array(
  40. '#type' => 'tableselect',
  41. '#header' => $header,
  42. '#options' => $data,
  43. '#empty' => t('There are no queues.'),
  44. );
  45. $form['nodequeue_generate_nodes_limit'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Nodes limit'),
  48. '#description' => t('How many nodes to insert in a queue. This value is only taken into consideration for infinite queues.'),
  49. '#size' => 3,
  50. '#default_value' => 10,
  51. );
  52. $form['submit'] = array(
  53. '#type' => 'submit',
  54. '#value' => t('Generate'),
  55. );
  56. return $form;
  57. }
  58. /**
  59. * Validate nodequeue generate form submission.
  60. *
  61. * Make sure that at least one queue was selected.
  62. */
  63. function nodequeue_generate_form_validate($form, &$form_state) {
  64. $qids = array_keys(array_filter($form_state['values']['nodequeues']));
  65. if (count($qids) < 1) {
  66. form_set_error('nodequeues', t('You must select a Queue.'));
  67. }
  68. }
  69. function nodequeue_generate_form_submit($form, &$form_state) {
  70. // Get qids of all nodequeues that need to be re-populated and repopulate them.
  71. $qids = array_keys(array_filter($form_state['values']['nodequeues']));
  72. $nodes_limit = $form_state['values']['nodequeue_generate_nodes_limit'];
  73. nodequeue_generate_rehash();
  74. nodequeue_generate_repopulate_queues($qids, $nodes_limit);
  75. }
  76. /**
  77. * Re-populates nodequeues with nodes.
  78. *
  79. * @param $qids Array of queues, that need to be repopulated.
  80. */
  81. function nodequeue_generate_repopulate_queues($qids, $nodes_limit = 10) {
  82. // Remove existing nodes from queues.
  83. db_query("DELETE FROM {nodequeue_nodes} WHERE qid IN (:qids)", array(':qids' => $qids));
  84. // Load all queues and their subqueues.
  85. $queues = nodequeue_load_queues($qids);
  86. $subqueues = nodequeue_load_subqueues_by_queue($qids);
  87. // Re-populate subqueues
  88. foreach ($qids as $qid) {
  89. $queue = nodequeue_load($qid);
  90. // Skip nodequeues that do not belong to any node types.
  91. if (!empty($queue->types)) {
  92. $limit = $queue->size ? $queue->size : $nodes_limit;
  93. $callback = $queue->owner . '_nodequeue_generate';
  94. if (function_exists($callback)) {
  95. $callback($queue, $limit);
  96. }
  97. }
  98. }
  99. drupal_set_message(format_plural(count($qids), '1 queue populated', '@count queues populated.'));
  100. }
  101. /**
  102. * Rebuild all smartqueue_taxonomy queues. Useful after a data migration has wiped your terms.
  103. * When more smartqueue modules arrive, revisit this function.
  104. *
  105. * @param vids
  106. * An array of vocabulary ids.
  107. */
  108. function nodequeue_generate_rehash() {
  109. // Delete existing smartqueue taxonomy subqueues
  110. db_query("DELETE ns FROM {nodequeue_subqueue} ns INNER JOIN {nodequeue_queue} nq ON ns.qid = nq.qid WHERE nq.owner = 'smartqueue_taxonomy'");
  111. // Get all queues, owned by Smartqueue taxonomy.
  112. $qids = db_select('nodequeue_queue', 'nq')
  113. ->fields('nq', array('qid'))
  114. ->condition('owner', 'smartqueue_taxonomy')
  115. ->execute()
  116. ->fetchAll();
  117. foreach ($qids as $qid) {
  118. $queue = nodequeue_load($qid->qid);
  119. $fields = explode('-', $queue->reference);
  120. $tids = array();
  121. foreach ($fields as $field) {
  122. // Get all possible tids from this field.
  123. $query = db_select('field_data_' . $field, 'f');
  124. $query
  125. ->condition('f.entity_type', 'node')
  126. ->condition('f.bundle', $queue->types, 'IN')
  127. ->condition('f.deleted', FALSE)
  128. ->addField('f', $field . '_tid', 'tid');
  129. $query = $query->distinct();
  130. $query = $query->execute();
  131. $tids += $query->fetchAll();
  132. }
  133. // Rehash for each tid.
  134. $nids = array();
  135. foreach ($tids as $tid) {
  136. foreach ($fields as $field) {
  137. $query = db_select('field_data_' . $field, 'f')
  138. ->condition('f.entity_type', 'node')
  139. ->condition('f.bundle', $queue->types, 'IN')
  140. ->condition('f.deleted', FALSE)
  141. ->condition('f.' . $field . '_tid', $tid->tid)
  142. ->fields('f', array('entity_id'))
  143. ->range(0, 1)
  144. ->execute();
  145. $nids += $query->fetchAll();
  146. }
  147. foreach ($nids as $nid) {
  148. $node = node_load($nid->entity_id);
  149. nodequeue_api_subqueues($queue, $node);
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * Implements hook_nodequeue_generate() for owner 'nodequeue'.
  156. */
  157. function nodequeue_nodequeue_generate($queue, $limit) {
  158. $subqueues = nodequeue_load_subqueues_by_queue($queue->qid);
  159. foreach ($subqueues as $subqueue) {
  160. $nodes = db_select('node', 'n')
  161. ->condition('n.status', NODE_PUBLISHED)
  162. ->condition('n.type', $queue->types, 'IN')
  163. ->orderRandom()
  164. ->fields('n', array('nid'))
  165. ->range(0, $limit)
  166. ->execute()
  167. ->fetchAll();
  168. foreach ($nodes as $node) {
  169. nodequeue_subqueue_add($queue, $subqueue, $node->nid);
  170. }
  171. }
  172. }
  173. /**
  174. * Implements hook_nodequeue_generate() for owner 'smartqueue_taxonomy'.
  175. */
  176. function smartqueue_taxonomy_nodequeue_generate($queue, $limit) {
  177. $subqueues = nodequeue_load_subqueues_by_queue($queue->qid);
  178. foreach ($subqueues as $subqueue) {
  179. $nodes = db_select('taxonomy_index', 'tn');
  180. $nodes->join('node', 'n', 'n.nid=tn.nid');
  181. $nodes->fields('n', array('nid'));
  182. $nodes->condition('n.status', NODE_PUBLISHED);
  183. $nodes->condition('n.type', $queue->types, 'IN');
  184. $nodes->condition('tn.tid', $subqueue->reference);
  185. $nodes->orderRandom();
  186. $nodes->range(0, $limit);
  187. $nodes = $nodes->execute();
  188. $nodes = $nodes->fetchAll();
  189. foreach ($nodes as $node) {
  190. nodequeue_subqueue_add($queue, $subqueue, $node->nid);
  191. }
  192. }
  193. }