You are here

function nodequeue_save_subqueue_order in Nodequeue 7.2

Same name and namespace in other branches
  1. 6.2 includes/nodequeue.admin.inc \nodequeue_save_subqueue_order()
  2. 7.3 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()
hook_nodequeue_save_subqueue_order_alter in ./nodequeue.api.php
Allow altering a node reordering action.
nodequeue_arrange_subqueue_form_submit in includes/nodequeue.admin.inc
Submit handler for nodequeue drag'n'drop form. Updates node positions in {nodequeue_nodes}.

File

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

Code

function nodequeue_save_subqueue_order($nodes, $qid, $sqid) {
  $positions = array();
  $now = REQUEST_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, '_nodequeue_serialize');
  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);
  }

  // If queue has been marked as only containing a single node once, we have to
  // remove duplicate nodes from array. We favor first entry.
  if (!empty($queue->unique_entries)) {
    $filter_nodes = function ($elem) {
      static $nids = array();
      if (in_array($elem['nid'], $nids)) {
        return FALSE;
      }
      $nids[] = $elem['nid'];
      return TRUE;
    };
    $nodes = array_filter($nodes, $filter_nodes);
  }

  // clear the queue and save the new positions
  db_delete('nodequeue_nodes')
    ->condition('sqid', $sqid)
    ->execute();
  foreach ($nodes as $pos => $node) {
    $args = array();
    if ($pos != 'r') {
      $query = db_insert('nodequeue_nodes')
        ->fields(array(
        'sqid' => $sqid,
        'qid' => $qid,
        'nid' => $node['nid'],
        'position' => $pos,
        'timestamp' => $now,
      ))
        ->execute();
    }
  }
  if ($queue->size) {

    // only necessary if the subqueue is of finite length
    nodequeue_check_subqueue_size($queue, $subqueue);
  }

  // Invoke a hook to notify other modules of the updated queue.
  module_invoke_all('nodequeue_update', $qid, $sqid);

  // As there's no API for saving a subqueue, we have to trigger the nodequeue save event here.
  // There might be some other places where we have to add these lines.
  if (module_exists('rules')) {
    rules_invoke_event('nodequeue_saved', $subqueue, $nodes);
  }
  return array(
    NODEQUEUE_OK,
    'The queue has been updated.',
  );
}