function nodeorder_move_in_category in Node Order 7
Same name and namespace in other branches
- 5 nodeorder.module \nodeorder_move_in_category()
- 6 nodeorder.module \nodeorder_move_in_category()
Move a node up or down in its category.
Parameters
string $direction: Direction: up or down.
string $nid: Node id.
string $tid: Term id.
1 string reference to 'nodeorder_move_in_category'
- nodeorder_menu in ./
nodeorder.module - Implements hook_menu().
File
- ./
nodeorder.module, line 495 - Nodeorder module.
Code
function nodeorder_move_in_category($direction, $nid, $tid) {
// Note that it would be nice to wrap this in a transaction.
$error = FALSE;
$node = node_load($nid);
$term = taxonomy_term_load($tid);
if (empty($node)) {
$error = drupal_set_message("The node doesn't exist", 'error');
}
elseif (empty($term)) {
$error = drupal_set_message("The term doesn't exist", 'error');
}
if (!$error) {
// Get the weight for current node.
$weight = db_select('taxonomy_index', 'ti')
->fields('ti', array(
'weight',
))
->condition('ti.nid', $node->nid)
->condition('ti.tid', $tid);
$weight = $weight
->execute()
->fetchField();
// Can't use empty() and isset() because weight could be 0.
if ($weight !== FALSE) {
$up = $direction === 'moveup';
if ($up) {
$operator = '<=';
$order_direction = 'DESC';
$direction = 'up';
}
else {
$operator = '>=';
$order_direction = 'ASC';
$direction = 'down';
}
$query = db_select('node', 'n');
$query
->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
$query
->fields('n', array(
'nid',
'vid',
));
$query
->fields('ti', array(
'weight',
));
$query
->condition('ti.tid', $tid);
$query
->condition('n.status', 1);
$query
->condition('ti.weight', $weight, $operator);
$query
->orderBy('ti.weight', $order_direction);
$query
->range(0, 2);
$query
->distinct();
$nodes = $query
->execute()
->fetchAll();
if (count($nodes) === 2) {
// Now we just need to swap the weights of the two nodes.
list($node1, $node2) = $nodes;
db_update('taxonomy_index')
->fields(array(
'weight' => $node1->weight,
))
->condition('nid', $node2->nid)
->condition('tid', $tid)
->execute();
db_update('taxonomy_index')
->fields(array(
'weight' => $node2->weight,
))
->condition('nid', $node1->nid)
->condition('tid', $tid)
->execute();
drupal_set_message(t('<em>%title</em> was moved %direction in %category.', array(
'%title' => $node->title,
'%direction' => $direction,
'%category' => $term->name,
)));
}
else {
drupal_set_message('There was a problem moving the node within its category.', 'error');
}
}
else {
drupal_set_message('There is no node in this term.', 'error');
}
}
// Now send user to the page they were on before or on front page.
$destination = isset($_GET['destination']) ? $_GET['destination'] : '<front>';
drupal_goto($destination);
}