View source
<?php
function nodequeue_generate_menu() {
$items['admin/structure/nodequeue/generate_nodequeue'] = array(
'title' => 'Generate queue assignments',
'description' => 'Bulk add nodes into queues',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'nodequeue_generate_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'manipulate all queues',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function nodequeue_generate_form() {
$form['help'] = array(
'#markup' => '<p>' . t('Select which queues shall be <strong>emptied</strong> and re-populated with new nodes.') . '</p>',
);
$queues = nodequeue_load_queues(nodequeue_get_all_queues(25));
$header = array(
'name' => 'Queue name',
'max_nodes' => 'Max nodes',
'subqueues' => 'Subqueues',
);
$data = array();
foreach ($queues as $queue) {
$data[$queue->name]['name'] = check_plain($queue->title);
$data[$queue->name]['max_nodes'] = $queue->size == 0 ? t('Infinite') : $queue->size;
$data[$queue->name]['subqueues'] = $queue->subqueues;
}
$form['nodequeues'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $data,
'#empty' => t('There are no queues.'),
);
$form['nodequeue_generate_nodes_limit'] = array(
'#type' => 'textfield',
'#title' => t('Nodes limit'),
'#description' => t('How many nodes to insert in a queue. This value is only taken into consideration for infinite queues.'),
'#size' => 3,
'#default_value' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function nodequeue_generate_form_validate($form, &$form_state) {
$queues = array_keys(array_filter($form_state['values']['nodequeues']));
if (count($queues) < 1) {
form_set_error('nodequeues', t('You must select a Queue.'));
}
}
function nodequeue_generate_form_submit($form, &$form_state) {
$queues = array_keys(array_filter($form_state['values']['nodequeues']));
$nodes_limit = $form_state['values']['nodequeue_generate_nodes_limit'];
nodequeue_generate_rehash();
nodequeue_generate_repopulate_queues($queues, $nodes_limit);
}
function nodequeue_generate_repopulate_queues($queues, $nodes_limit = 10) {
db_query("DELETE FROM {nodequeue_nodes} WHERE name IN (:queues)", array(
':queues' => $queues,
));
$queues = nodequeue_load_queues($queues);
foreach ($queues as $queue) {
if (!empty($queue->types)) {
$limit = $queue->size ? $queue->size : $nodes_limit;
$callback = $queue->owner . '_nodequeue_generate';
if (function_exists($callback)) {
$callback($queue, $limit);
}
}
}
drupal_set_message(format_plural(count($queues), '1 queue populated', '@count queues populated.'));
}
function nodequeue_generate_rehash() {
db_query("DELETE ns FROM {nodequeue_subqueue} ns INNER JOIN {nodequeue_queue} nq ON ns.name = nq.name WHERE nq.owner = 'smartqueue_taxonomy'");
$queues = db_select('nodequeue_queue', 'nq')
->fields('nq', array(
'name',
))
->condition('owner', 'smartqueue_taxonomy')
->execute()
->fetchAll();
foreach ($queues as $name) {
$queue = nodequeue_load($name->name);
$fields = explode('-', $queue->reference);
$tids = array();
$nids = array();
foreach ($fields as $field) {
$query = db_select('field_data_' . $field, 'f');
$query
->condition('f.entity_type', 'node')
->condition('f.bundle', $queue->types, 'IN')
->condition('f.deleted', FALSE)
->addField('f', $field . '_tid', 'tid');
$query = $query
->distinct();
$query = $query
->execute();
$tids += $query
->fetchAll();
foreach ($tids as $tid) {
$query = db_select('field_data_' . $field, 'f');
$query
->condition('f.entity_type', 'node')
->condition('f.bundle', $queue->types, 'IN')
->condition('f.deleted', FALSE)
->condition('f.' . $field . '_tid', $tid->tid)
->fields('f', array(
'entity_id',
))
->range(0, 1);
$query = $query
->execute();
while ($item = $query
->fetchAssoc()) {
$nids[] = $item;
}
}
foreach ($nids as $nid) {
$node = node_load($nid['entity_id']);
nodequeue_api_subqueues($queue, $node);
}
}
}
}
function nodequeue_nodequeue_generate($queue, $limit) {
$subqueues = nodequeue_load_subqueues_by_queue($queue->name);
foreach ($subqueues as $subqueue) {
$nodes = db_select('node', 'n')
->condition('n.status', NODE_PUBLISHED)
->condition('n.type', $queue->types, 'IN')
->orderRandom()
->fields('n', array(
'nid',
))
->range(0, $limit)
->execute()
->fetchAll();
foreach ($nodes as $node) {
nodequeue_subqueue_add($queue, $subqueue, $node->nid);
}
}
}
function smartqueue_taxonomy_nodequeue_generate($queue, $limit) {
$subqueues = nodequeue_load_subqueues_by_queue($queue->name);
foreach ($subqueues as $subqueue) {
$nodes = db_select('taxonomy_index', 'tn');
$nodes
->join('node', 'n', 'n.nid=tn.nid');
$nodes
->fields('n', array(
'nid',
));
$nodes
->condition('n.status', NODE_PUBLISHED);
$nodes
->condition('n.type', $queue->types, 'IN');
$nodes
->condition('tn.tid', $subqueue->reference);
$nodes
->orderRandom();
$nodes
->range(0, $limit);
$nodes = $nodes
->execute();
$nodes = $nodes
->fetchAll();
foreach ($nodes as $node) {
nodequeue_subqueue_add($queue, $subqueue, $node->nid);
}
}
}