You are here

function nodequeue_save_subqueue_order in Nodequeue 6.2

Same name and namespace in other branches
  1. 7.3 includes/nodequeue.admin.inc \nodequeue_save_subqueue_order()
  2. 7.2 includes/nodequeue.admin.inc \nodequeue_save_subqueue_order()

Validates new subqueue order information and if it passes validation, deletes the old subqueue data from the database and saves the new data.

Parameters

$nodes:: an array of nodes, keyed on the subqueue position.

$qid: the queue id

unknown_type $sqid: the subqueue id

Return value

An array where the first element is a numeric status code (0 means successfully saved) and the second element is a status message.

2 calls to nodequeue_save_subqueue_order()
nodequeue_arrange_subqueue_form_submit in includes/nodequeue.admin.inc
Submit handler for nodequeue drag'n'drop form. Updates node positions in {nodequeue_nodes}.
nodequeue_service_save_subqueue in addons/nodequeue_service/nodequeue_service.inc

File

includes/nodequeue.admin.inc, line 940
Admin page callbacks for the nodequeue module.

Code

function nodequeue_save_subqueue_order($nodes, $qid, $sqid) {
  $positions = array();
  $now = time();
  $queue = nodequeue_load($qid);
  $subqueue = nodequeue_load_subqueue($sqid);

  // cleanup the node array
  $clean = array();
  $count = 1;
  ksort($nodes);
  drupal_alter('nodequeue_sort', $nodes, $sqid);
  foreach ($nodes as $pos => $node) {
    if (!is_numeric($node['nid']) || $node['nid'] < 1) {
      return array(
        NODEQUEUE_INVALID_NID,
        'Invalid nid value. New subqueue order not saved.',
      );
    }
    if (is_numeric($pos)) {
      $clean[$count] = $node;
      $count++;
    }
    elseif ($pos == 'r') {

      // Remove the node from this subqueue.
      nodequeue_subqueue_remove_node($sqid, $node['nid']);
    }
    else {
      return array(
        NODEQUEUE_INVALID_POSITION,
        'Invalid position value. New subqueue order not saved.',
      );
    }
  }
  $nodes = $clean;
  array_walk($clean, create_function('&$node', '$node = serialize($node);'));
  if (count(array_unique($clean)) < count($nodes)) {
    return array(
      NODEQUEUE_DUPLICATE_POSITION,
      'Duplicate position values are not allowed. New subqueue order not saved.',
    );
  }

  // Allow other modules to alter the order of nodes.
  foreach (module_implements('nodequeue_save_subqueue_order_alter') as $module) {
    $function = $module . '_nodequeue_save_subqueue_order_alter';
    $function($sqid, $nodes);
  }

  // clear the queue and save the new positions
  db_query('DELETE FROM {nodequeue_nodes} WHERE sqid = %d', $sqid);
  foreach ($nodes as $pos => $node) {
    $args = array();
    if ($pos != 'r') {
      $positions[] = $pos;
      $placeholders = ' (%d, %d, %d, %d, %d)';
      $args = array(
        $sqid,
        $qid,
        $node['nid'],
        $pos,
        $now,
      );
    }
    if (!empty($args)) {
      $sql = 'INSERT INTO {nodequeue_nodes} (sqid, qid, nid, position, timestamp) VALUES ' . $placeholders;
      db_query($sql, $args);
    }
  }
  if ($queue->size) {

    // only necessary if the subqueue is of finite length
    nodequeue_check_subqueue_size($queue, $subqueue);
  }
  return array(
    NODEQUEUE_OK,
    'The queue has been updated.',
  );
}