You are here

nodequeue_generate.module in Nodequeue 7.3

File

nodequeue_generate.module
View source
<?php

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Menu callback; Returns the nodequeue generate form.
 */
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));

  // Tableselect header.
  $header = array(
    'name' => 'Queue name',
    'max_nodes' => 'Max nodes',
    'subqueues' => 'Subqueues',
  );

  // Tableselect data.
  $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;
  }

  // Table select element.
  $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;
}

/**
 * Validate nodequeue generate form submission.
 *
 * Make sure that at least one queue was selected.
 */
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) {

  // Get a list of all nodequeues that need to be re-populated and repopulate
  // them.
  $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);
}

/**
 * Re-populates nodequeues with nodes.
 *
 * @param $queues Array of queues, that need to be repopulated.
 */
function nodequeue_generate_repopulate_queues($queues, $nodes_limit = 10) {

  // Remove existing nodes from queues.
  db_query("DELETE FROM {nodequeue_nodes} WHERE name IN (:queues)", array(
    ':queues' => $queues,
  ));

  // Load all queues and their subqueues.
  $queues = nodequeue_load_queues($queues);

  // Re-populate subqueues
  foreach ($queues as $queue) {

    // Skip nodequeues that do not belong to any node types.
    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.'));
}

/**
 * Rebuild all smartqueue_taxonomy queues. Useful after a data migration has wiped your terms.
 * When more smartqueue modules arrive, revisit this function.
 *
 * @param vids
 *   An array of vocabulary ids.
 */
function nodequeue_generate_rehash() {

  // Delete existing smartqueue taxonomy subqueues
  db_query("DELETE ns FROM {nodequeue_subqueue} ns INNER JOIN {nodequeue_queue} nq ON ns.name = nq.name WHERE nq.owner = 'smartqueue_taxonomy'");

  // Get all queues, owned by 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) {

      // Get all possible tids from this 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();

      // Rehash for each tid.
      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);
      }
    }
  }
}

/**
 * Implements hook_nodequeue_generate() for owner 'nodequeue'.
 */
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);
    }
  }
}

/**
 * Implements hook_nodequeue_generate() for owner 'smartqueue_taxonomy'.
 */
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);
    }
  }
}

Functions

Namesort descending Description
nodequeue_generate_form Menu callback; Returns the nodequeue generate form.
nodequeue_generate_form_submit
nodequeue_generate_form_validate Validate nodequeue generate form submission.
nodequeue_generate_menu Implements hook_menu().
nodequeue_generate_rehash Rebuild all smartqueue_taxonomy queues. Useful after a data migration has wiped your terms. When more smartqueue modules arrive, revisit this function.
nodequeue_generate_repopulate_queues Re-populates nodequeues with nodes.
nodequeue_nodequeue_generate Implements hook_nodequeue_generate() for owner 'nodequeue'.
smartqueue_taxonomy_nodequeue_generate Implements hook_nodequeue_generate() for owner 'smartqueue_taxonomy'.