nodeorder.module in Node Order 6
Same filename and directory in other branches
Nodeorder module.
File
nodeorder.moduleView source
<?php
// vim: syntax=php
/**
* @file
* Nodeorder module.
*/
/**
* Implementation of hook_perm().
*/
function nodeorder_perm() {
return array(
'order nodes within categories',
'administer nodeorder',
);
}
/**
* Implementation of hook_theme()
*/
function nodeorder_theme() {
return array(
'nodeorder_admin_display_form' => array(
'template' => 'nodeorder-admin-display-form',
'arguments' => array(
'form' => NULL,
),
'file' => 'nodeorder.admin.inc',
),
);
}
/**
* Implementation of hook_form_alter().
*/
function nodeorder_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'taxonomy_form_vocabulary') {
$is_orderable = $form['module']['#value'] == 'nodeorder';
$form['settings']['orderable'] = array(
'#type' => 'checkbox',
'#title' => t('Orderable'),
'#description' => t('If enabled, nodes may be ordered within this vocabulary.'),
'#weight' => 0.0075,
// Try to have this show up after the 'Required' checkbox
'#default_value' => $is_orderable,
);
// Add a submit handler for saving the orderable settings
$form['#submit'][] = 'nodeorder_taxonomy_form_vocabulary_submit';
/*
* Why not implement hook_taxonomy?
* hook_taxonomy sometimes gets called after editing terms;
* in that case the orderable-value will not be available and thus the
* orderable-setting on the vocabulary will always be disabled
*/
}
}
function nodeorder_taxonomy_form_vocabulary_submit($form, &$form_state) {
$vid = $form_state['values']['vid'];
if ($form_state['values']['orderable']) {
if ($form_state['values']['module'] != 'nodeorder') {
// Switching from non-orderable to orderable...
cache_clear_all('nodeorder:', 'cache', TRUE);
// Set weight_in_tid to nid for all rows in term_node where
// the tid is in this vocabulary...
$tree = taxonomy_get_tree($vid);
$tids = array();
foreach ($tree as $term) {
$tids[] = $term->tid;
}
if (count($tids) > 0) {
$order = 'n.sticky DESC, tn0.weight_in_tid';
$sql_max = "SELECT MAX(weight_in_tid) FROM {term_node} WHERE tid = %d";
$sql_update = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
foreach ($tids as $i => $tid) {
//select *current* nodes for the current term
$result = nodeorder_select_nodes(array(
$tid,
), 'and', 0, FALSE, $order, 0);
while ($node = db_fetch_object($result)) {
db_lock_table('term_node');
$max = db_result(db_query($sql_max, $tid));
db_query($sql_update, $max + 1, $tid, $node->nid);
db_unlock_tables();
}
}
}
db_query("UPDATE {vocabulary} SET module = '%s' WHERE vid = %d", 'nodeorder', $vid);
drupal_set_message(t('You may now order nodes within this vocabulary.'));
}
}
else {
if ($form_state['values']['module'] == 'nodeorder') {
// Switching from orderable to non-orderable...
cache_clear_all('nodeorder:', 'cache', TRUE);
db_query("UPDATE {vocabulary} SET module = '%s' WHERE vid = %d", 'taxonomy', $vid);
// Set weight_in_tid to 0 for all rows in term_node where
// the tid is in this vocabulary...
$tree = taxonomy_get_tree($vid);
$tids = array();
foreach ($tree as $term) {
$tids[] = $term->tid;
}
if (count($tids) > 0) {
db_query("UPDATE {term_node} SET weight_in_tid = 0 WHERE tid IN (" . implode(',', $tids) . ")");
}
drupal_set_message(t('You may no longer order nodes within this vocabulary.'));
}
}
}
/**
* Implementation of hook_link().
*/
function nodeorder_link($type, $node = 0, $main = 0) {
$links = array();
if (user_access('order nodes within categories') && variable_get('nodeorder_show_links_on_node', 1) > 0) {
// If this node belongs to any vocabularies that are orderable,
// stick a couple links on per term to allow the user to move
// the node up or down within the term...
if ($type == 'node') {
if (array_key_exists('taxonomy', $node)) {
$vocabularies = taxonomy_get_vocabularies();
if (variable_get('nodeorder_show_links_on_node', 1) == 2 && ((arg(0) == 'taxonomy' || arg(0) == 'nodeorder') && arg(1) == 'term')) {
$term = taxonomy_get_term(arg(2));
nodeorder_add_link($links, $vocabularies, $node, $term);
}
else {
if (variable_get('nodeorder_show_links_on_node', 1) == 1) {
foreach ($node->taxonomy as $term) {
nodeorder_add_link($links, $vocabularies, $node, $term);
}
}
}
}
}
}
return $links;
}
function nodeorder_add_link(&$links, $vocabularies, $node, $term) {
$vocabulary = $vocabularies[$term->vid];
if ($vocabulary->module == 'nodeorder') {
$weights = nodeorder_get_term_min_max($term->tid, FALSE);
$weight = db_result(db_query("SELECT weight_in_tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $term->tid));
if ($weight > $weights["min"]) {
$links['nodeorder_move_up_' . $term->tid] = array(
'href' => "nodeorder/moveup/" . $node->nid . "/" . $term->tid,
'title' => t("move up in " . $term->name),
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t("Move this " . $node->type . " up in its category."),
),
);
}
if ($weight < $weights["max"]) {
$links['nodeorder_move_down_' . $term->tid] = array(
'href' => "nodeorder/movedown/" . $node->nid . "/" . $term->tid,
'title' => t("move down in " . $term->name),
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t("Move this " . $node->type . " down in its category."),
),
);
}
}
}
function nodeorder_get_term_min_max($tid, $reset) {
static $min_weights = array();
static $max_weights = array();
if ($reset) {
unset($min_weights[$tid]);
unset($max_weights[$tid]);
}
if (!$min_weights[$tid] || !$max_weights[$tid]) {
$result = db_query("SELECT tid, max(weight_in_tid) as max_weight, min(weight_in_tid) as min_weight FROM {term_node} WHERE tid = %d GROUP BY tid", $tid);
while ($row = db_fetch_object($result)) {
$min_weights[$row->tid] = $row->min_weight;
$max_weights[$row->tid] = $row->max_weight;
}
}
$weights["min"] = $min_weights[$tid];
$weights["max"] = $max_weights[$tid];
return $weights;
}
/**
* Implementation of hook_term_path() from Taxonomy.
*/
function nodeorder_term_path($term) {
if (variable_get('nodeorder_replace_taxonomy_link', 1) || arg(0) == 'nodeorder') {
//if nodeorder is being used to display term pages
return 'nodeorder/term/' . $term->tid;
}
else {
return FALSE;
//create regular taxonomy-links on taxonomy page
}
}
/**
* Implementation of hook_menu().
*/
function nodeorder_menu() {
$items = array();
$items['admin/settings/nodeorder'] = array(
'title' => t('Nodeorder'),
'description' => t('Allows the ordering of nodes within taxonomy terms.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'nodeorder_admin',
),
'access arguments' => array(
'administer nodeorder',
),
'type' => MENU_NORMAL_ITEM,
);
$items['nodeorder/term/%'] = array(
'title' => t('Nodeorder term'),
'page callback' => 'nodeorder_term_page',
// I want to call taxonomy_term_page but can't change the sort order...
'page arguments' => array(
2,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['nodeorder/order/%'] = array(
'title' => t('Order nodes'),
'page callback' => 'nodeorder_admin_display',
'page arguments' => array(
2,
),
'access arguments' => array(
'order nodes within categories',
),
'file' => 'nodeorder.admin.inc',
'type' => MENU_CALLBACK,
);
$items['taxonomy/term/%/view'] = array(
'title' => t('View'),
'page callback' => 'taxonomy_term_page',
'page arguments' => array(
2,
),
'access arguments' => array(
'access content',
),
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['taxonomy/term/%/order'] = array(
'title' => t('Order nodes'),
'page callback' => 'nodeorder_admin_display',
'page arguments' => array(
2,
),
'access callback' => 'nodeorder_order_access',
'access arguments' => array(
2,
),
'file' => 'nodeorder.admin.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
$items['nodeorder/term/%/view'] = array(
'title' => t('View'),
'page callback' => 'nodeorder_term_page',
'page arguments' => array(
2,
),
'access arguments' => array(
'access content',
),
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['nodeorder/term/%/order'] = array(
'title' => t('Order nodes'),
'page callback' => 'nodeorder_admin_display',
'page arguments' => array(
2,
),
'access callback' => 'nodeorder_order_access',
'access arguments' => array(
2,
),
'file' => 'nodeorder.admin.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
$items['nodeorder/moveup/%/%'] = array(
'title' => t('Move Up'),
'page callback' => 'nodeorder_move_in_category',
'page arguments' => array(
1,
2,
3,
),
'access arguments' => array(
'order nodes within categories',
),
'type' => MENU_CALLBACK,
);
$items['nodeorder/movedown/%/%'] = array(
'title' => t('Move Down'),
'page callback' => 'nodeorder_move_in_category',
'page arguments' => array(
1,
2,
3,
),
'access arguments' => array(
'order nodes within categories',
),
'type' => MENU_CALLBACK,
);
$items['admin/content/taxonomy/%/order'] = array(
'title' => t('Order nodes'),
'page callback' => 'nodeorder_overview_terms',
'page arguments' => array(
3,
),
'access callback' => 'nodeorder_taxonomy_order_access',
'access arguments' => array(
3,
),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Custom access function which determines whether or not the user is allowed to reorder nodes and if the link should be shown at all
*/
function nodeorder_order_access($tid) {
return user_access('order nodes within categories') && variable_get('nodeorder_link_to_ordering_page', 1) && nodeorder_term_can_be_ordered($tid);
}
/**
* Custom access function which determines whether or not the user is allowed to reorder nodes and if the vocabulary is orderable
*/
function nodeorder_taxonomy_order_access($vid) {
return user_access('order nodes within categories') && variable_get('nodeorder_link_to_ordering_page_taxonomy_admin', 1) && nodeorder_vocabulary_can_be_ordered($vid);
}
/**
* Menu callback; displays all nodes associated with a term.
*/
function nodeorder_term_page($str_tids = '', $depth = 0, $op = 'page') {
$terms = taxonomy_terms_parse_string($str_tids);
if ($terms['operator'] != 'and' && $terms['operator'] != 'or') {
drupal_not_found();
}
if ($terms['tids']) {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data} t WHERE t.tid IN (' . db_placeholders($terms['tids']) . ')', 't', 'tid'), $terms['tids']);
$tids = array();
// we rebuild the $tids-array so it only contains terms the user has access to.
$names = array();
while ($term = db_fetch_object($result)) {
$tids[] = $term->tid;
$names[] = $term->name;
}
if ($names) {
drupal_set_title($title = check_plain(implode(', ', $names)));
// Set the order that gets passed in to taxonomy_select_nodes.
// This probably breaks down when there's a query that spans
// multiple terms...
//
// First sort by sticky, then by weight_in_tid...
if ($terms['operator'] == 'or') {
$order = 'n.sticky DESC, tn.weight_in_tid';
}
else {
$order = 'n.sticky DESC, tn0.weight_in_tid';
}
switch ($op) {
case 'page':
// Build breadcrumb based on first hierarchy of first term:
$current->tid = $tids[0];
$breadcrumb = array();
while ($parents = taxonomy_get_parents($current->tid)) {
$current = array_shift($parents);
$breadcrumb[] = l($current->name, 'nodeorder/term/' . $current->tid);
}
$breadcrumb[] = l(t('Home'), NULL);
$breadcrumb = array_reverse($breadcrumb);
drupal_set_breadcrumb($breadcrumb);
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
//.inc files are not loaded automatically
$output = theme('taxonomy_term_page', $tids, nodeorder_select_nodes($tids, $terms['operator'], $depth, TRUE, $order));
drupal_add_feed(url('taxonomy/term/' . $str_tids . '/' . $depth . '/feed'), 'RSS - ' . $title);
return $output;
case 'feed':
$channel['link'] = url('nodeorder/term/' . $str_tids . '/' . $depth, array(
'absolute' => TRUE,
));
$channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $title;
// Only display the description if we have a single term, to avoid clutter and confusion.
if (count($tids) == 1) {
$term = taxonomy_get_term($tids[0]);
// HTML will be removed from feed description, so no need to filter here.
$channel['description'] = $term->description;
}
$result = taxonomy_select_nodes($tids, $terms['operator'], $depth, FALSE, $order);
$items = array();
while ($row = db_fetch_object($result)) {
$items[] = $row->nid;
}
node_feed($items, $channel);
break;
default:
drupal_not_found();
}
}
else {
drupal_not_found();
}
}
}
/**
* NOTE: This is nearly a direct copy of taxonomy_select_nodes() -- see
* http://drupal.org/node/25801 if you find this sort of copy and
* paste upsetting...
*
* Finds all nodes that match selected taxonomy conditions.
*
* @param $tids
* An array of term IDs to match.
* @param $operator
* How to interpret multiple IDs in the array. Can be "or" or "and".
* @param $depth
* How many levels deep to traverse the taxonomy tree. Can be a nonnegative
* integer or "all".
* @param $pager
* Whether the nodes are to be used with a pager (the case on most Drupal
* pages) or not (in an XML feed, for example).
* @param $order
* The order clause for the query that retrieve the nodes.
* @param $count
* If $pager is TRUE, the number of nodes per page, or -1 to use the
* backward-compatible 'default_nodes_main' variable setting. If $pager
* is FALSE, the total number of nodes to select; or -1 to use the
* backward-compatible 'feed_default_items' variable setting; or 0 to
* select all nodes.
* @return
* A resource identifier pointing to the query results.
*/
function nodeorder_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC', $count = -1) {
if (count($tids) > 0) {
// For each term ID, generate an array of descendant term IDs to the right depth.
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$term = taxonomy_get_term($tid);
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
$descendant_tids[] = array_merge(array(
$tid,
), array_map('_taxonomy_get_tid_from_term', $tree));
}
if ($operator == 'or') {
$args = call_user_func_array('array_merge', $descendant_tids);
$placeholders = db_placeholders($args, 'int');
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ') AND n.status = 1 ORDER BY ' . $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ') AND n.status = 1';
}
else {
$joins = '';
$wheres = '';
$args = array();
foreach ($descendant_tids as $index => $tids) {
$joins .= ' INNER JOIN {term_node} tn' . $index . ' ON n.vid = tn' . $index . '.vid';
$wheres .= ' AND tn' . $index . '.tid IN (' . db_placeholders($tids, 'int') . ')';
$args = array_merge($args, $tids);
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, tn0.weight_in_tid FROM {node} n ' . $joins . ' WHERE n.status = 1 ' . $wheres . ' ORDER BY ' . $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n ' . $joins . ' WHERE n.status = 1 ' . $wheres;
}
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
if ($count == -1) {
$count = variable_get('default_nodes_main', 10);
}
$result = pager_query($sql, $count, 0, $sql_count, $args);
}
else {
if ($count == -1) {
$count = variable_get('feed_default_items', 10);
}
if ($count == 0) {
$result = db_query($sql, $args);
}
else {
$result = db_query_range($sql, $args, 0, $count);
}
}
}
return $result;
}
/**
* Move a node up or down in its category...
*/
function nodeorder_move_in_category($direction, $nid, $tid) {
// Note that it would be nice to wrap this in a transaction...
$up = $direction == 'moveup';
$node = node_load($nid);
$destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : $_GET['q'];
// Get the current weight for the node
$weight = db_result(db_query("SELECT weight_in_tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $tid));
if ($up) {
$weights = nodeorder_get_term_min_max($tid, FALSE);
if ($weight == $weights["min"]) {
drupal_set_message(t('%title cannot be moved up as it already is at the top.', array(
'%title' => $node->title,
)), 'error');
drupal_goto($destination);
return;
}
$sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid <= %d ORDER BY tn.weight_in_tid DESC';
$direction = 'up';
}
else {
$weights = nodeorder_get_term_min_max($tid, FALSE);
if ($weight == $weights["max"]) {
drupal_set_message(t('%title cannot be moved down as it already is at the bottom.', array(
'%title' => $node->title,
)), 'error');
drupal_goto($destination);
return;
}
$sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid >= %d ORDER BY tn.weight_in_tid';
$direction = 'down';
}
$result = db_query_range($sql, $tid, $weight, 0, 2);
$node1 = db_fetch_object($result);
$node2 = db_fetch_object($result);
// Now we just need to swap the weights of the two nodes...
if (!$node1 || !$node2) {
drupal_set_message('There was a problem moving the node within its category.');
drupal_access_denied();
return;
}
$sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE nid = %d AND tid = %d";
db_query($sql, $node1->weight_in_tid, $node2->nid, $tid);
db_query($sql, $node2->weight_in_tid, $node1->nid, $tid);
$term = taxonomy_get_term($tid);
drupal_set_message(t("<em>%title</em> was moved {$direction} in %category...", array(
'%title' => $node->title,
'%category' => $term->name,
)));
// Now send user to the page they were on before...
drupal_goto($_GET['destination']);
}
/**
* Returns TRUE if the node has terms in any orderable vocabulary...
*/
function nodeorder_can_be_ordered($node) {
$cid = 'nodeorder:can_be_ordered:' . $node->type;
if (($cache = cache_get($cid)) && !empty($cache->data)) {
return $cache->data;
}
else {
$sql = "SELECT v.vid AS vid FROM {vocabulary_node_types} vnt JOIN {vocabulary} v ON vnt.vid = v.vid WHERE vnt.type = '%s' AND v.module = 'nodeorder'";
$result = db_query($sql, $node->type);
$can_be_ordered = FALSE;
if (db_fetch_object($result)) {
$can_be_ordered = TRUE;
}
//permanently cache the value for easy reuse
cache_set($cid, $can_be_ordered, 'cache');
return $can_be_ordered;
}
}
/**
* Returns an array of the node's tids that are in orderable vocabularies...
*/
function nodeorder_orderable_tids($node) {
$tids = array();
$orderable_tids = array();
$cid = 'nodeorder:orderable_tids:' . $node->type;
if (($cache = cache_get($cid)) && !empty($cache->data)) {
$orderable_tids = $cache->data;
}
else {
$sql = "SELECT v.vid AS vid FROM {vocabulary_node_types} vnt JOIN {vocabulary} v ON vnt.vid = v.vid WHERE vnt.type = '%s' AND v.module = 'nodeorder'";
$result = db_query($sql, $node->type);
while ($row = db_fetch_object($result)) {
$tree = taxonomy_get_tree($row->vid);
foreach ($tree as $term) {
$orderable_tids[] = $term->tid;
}
}
//permanently cache the value for easy reuse
cache_set($cid, $orderable_tids, 'cache');
}
// Now select only those tids which are actually assigned to this term
foreach ($node->taxonomy as $key => $value) {
$list_of_tids = nodeorder_get_tids($key, $value);
$tids = array_merge($tids, array_intersect($list_of_tids, $orderable_tids));
}
return $tids;
}
function nodeorder_get_tids($key, $value) {
$tids = array();
if (isset($value)) {
if ($key === "tags") {
foreach ($value as $vid => $names) {
$tids = array_merge($tids, nodeorder_get_tids($vid, $names));
}
}
else {
if (is_numeric($value)) {
$tids[] = $value;
}
else {
if (is_array($value)) {
foreach ($value as $tid) {
$tids[] = $tid;
}
}
else {
if (is_string($value)) {
$values = drupal_explode_tags($value);
$get_tid_sql = "SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d";
foreach ($values as $term_name) {
$tids[] = db_result(db_query($get_tid_sql, $term_name, $key));
}
}
}
}
}
}
return $tids;
}
/**
* Returns TRUE if the vocabulary is orderable...
*/
function nodeorder_vocabulary_can_be_ordered($vid) {
$sql = "SELECT * FROM {vocabulary} WHERE module = 'nodeorder' AND vid = %d";
$result = db_query($sql, $vid);
if (db_fetch_object($result)) {
return TRUE;
}
return FALSE;
}
/**
* Returns TRUE if the term is in an orderable vocabulary...
*/
function nodeorder_term_can_be_ordered($tid) {
$cid = 'nodeorder:term_can_be_ordered:' . $tid;
if (($cache = cache_get($cid)) && !empty($cache->data)) {
return $cache->data;
}
else {
$sql = "SELECT vid FROM {term_data} WHERE tid = %d";
$vid = db_result(db_query($sql, $tid));
$sql = "SELECT * FROM {vocabulary} WHERE module = 'nodeorder' AND vid = %d";
$result = db_query($sql, $vid);
$term_can_be_ordered = FALSE;
if (db_fetch_object($result)) {
$term_can_be_ordered = TRUE;
}
//permanently cache the value for easy reuse
cache_set($cid, $term_can_be_ordered, 'cache');
return $term_can_be_ordered;
}
}
/**
* Implementation of hook_nodeapi().
*/
function nodeorder_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'presave':
if (nodeorder_can_be_ordered($node)) {
if (!isset($node->nodeorder)) {
$node->nodeorder = array();
// When a node gets loaded, store an element called 'nodeorder' that contains
// an associative array of tid to weight_in_tid...
$result = db_query('SELECT tid, weight_in_tid FROM {term_node} WHERE nid = %d', $node->nid);
while ($term_node = db_fetch_object($result)) {
$node->nodeorder[$term_node->tid] = $term_node->weight_in_tid;
}
}
}
break;
case 'delete':
// make sure the weight cache is invalidated
if (nodeorder_can_be_ordered($node)) {
$tids = nodeorder_orderable_tids($node);
if (count($tids) > 0) {
foreach ($tids as $i => $tid) {
nodeorder_get_term_min_max($tid, TRUE);
// reinitialize the cache
}
}
}
break;
case 'insert':
// Set the initial weight_in_tid to max+1... This makes sure that the weight
// will be unique for each nid/tid combination
//
// NOTE - fall through to 'update' since we do mostly the same thing there.
case 'update':
// Set the weight_in_tid -- taxonomy probably stomped it because
// we added the weight_in_tid column to term_node, and taxonomy
// just wants to delete and re-insert rows when things change...
// Note that we only want to set the weight_in_tid for tids that
// are in orderable vocabularies...
if (nodeorder_can_be_ordered($node)) {
$tids = nodeorder_orderable_tids($node);
if (count($tids) > 0) {
$sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
foreach ($tids as $i => $tid) {
db_lock_table('term_node');
$weights = nodeorder_get_term_min_max($tid, FALSE);
// get the cached weights
db_query($sql, $weights["max"] + 1, $tid, $node->nid);
nodeorder_get_term_min_max($tid, TRUE);
// reinitialize the cache
db_unlock_tables();
}
}
// New nodes won't have any saved weight_in_tid values so this array will be empty...
if ($node->nodeorder) {
// Restore any saved weight_in_tid values...
$sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE nid = %d AND tid = %d";
foreach ($node->nodeorder as $tid => $weight_in_tid) {
// weight_in_tid cannot be 0
if ($weight_in_tid != 0) {
db_query($sql, $weight_in_tid, $node->nid, $tid);
}
}
}
}
break;
}
}
/**
* Form for Admin Settings
*/
function nodeorder_admin() {
$form['nodeorder_show_links'] = array(
'#type' => 'fieldset',
'#title' => t('Display ordering links'),
'#description' => t('Choose whether to show ordering links. Links can be shown for all categories associated to a node or for the currently active category. It is also possible to not show the ordering links at all.'),
);
$form['nodeorder_show_links']['nodeorder_show_links_on_node'] = array(
'#type' => 'radios',
'#title' => t('Choose how to display ordering links'),
'#default_value' => variable_get('nodeorder_show_links_on_node', 1),
'#description' => 'When displaying links based on the context, they will only be shown on taxonomy and nodeorder pages.',
'#options' => array(
t('Don\'t display ordering links'),
t('Display ordering links for all categories'),
t('Display ordering links based on the active category'),
),
);
$form['nodeorder_link_to_ordering_page'] = array(
'#type' => 'checkbox',
'#title' => t('Display link to the ordering page'),
'#description' => t('If enabled, a tab will appear on all <em>nodeorder/term/%</em> and <em>taxonomy/term/%</em> pages that quickly allows administrators to get to the node ordering administration page for the term.'),
'#default_value' => variable_get('nodeorder_link_to_ordering_page', 1),
);
$form['nodeorder_link_to_ordering_page_taxonomy_admin'] = array(
'#type' => 'checkbox',
'#title' => t('Display link to the ordering page on taxonomy administration page'),
'#description' => t('If enabled, a tab will appear on <em>admin/content/taxonomy/%</em> pages that quickly allows administrators to get to the node ordering administration page for the term.'),
'#default_value' => variable_get('nodeorder_link_to_ordering_page_taxonomy_admin', 1),
);
$form['nodeorder_replace_taxonomy_link'] = array(
'#type' => 'checkbox',
'#title' => t('Replace the link to <em>taxonomy/term/%</em> by <em>nodeorder/term/%</em>'),
'#description' => t('If enabled, links to regular <em>taxonomy/term/%</em> pages will always be replaced by links to their <em>nodeorder/term/%</em> counterpart. Otherwise, this will only happen on the <em>nodeorder/term/%</em> pages.'),
'#default_value' => variable_get('nodeorder_replace_taxonomy_link', 1),
);
return system_settings_form($form);
}
/**
* Display a tree of all the terms in a vocabulary, with options to
* order nodes within each one.
*
* This code was cut and pasted from taxonomy_overview_terms. If
* If we were able to add another operation onto each term on the
* admin/content/taxonomy/VID page then we wouldn't even need this duplicate
* function.
*
* TODO - put in a patch for a taxonomy hook that lets us add
* admin operation links per term... maybe it would call
* module_invoke_all('taxonomy', 'list', 'term', $term) and
* array_merge the results with each $row[]...
*/
function nodeorder_overview_terms($vid) {
if (!nodeorder_vocabulary_can_be_ordered($vid)) {
return t('This vocabulary is not orderable. If you would like it to be orderable, check the Orderable box ') . l(t('here'), 'admin/content/taxonomy/edit/vocabulary' . $vid) . '.';
}
$header = array(
t('Name'),
t('Operations'),
);
$vocabularies = taxonomy_get_vocabularies();
$vocabulary = $vocabularies[$vid];
if (!$vocabulary) {
return drupal_not_found();
}
drupal_set_title(t('Terms in %vocabulary', array(
'%vocabulary' => $vocabulary->name,
)));
$start_from = $_GET['page'] ? $_GET['page'] : 0;
$total_entries = 0;
// total count for pager
$page_increment = 25;
// number of tids per page
$displayed_count = 0;
// number of tids shown
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
$total_entries++;
// we're counting all-totals, not displayed
if ($start_from && $start_from * $page_increment >= $total_entries || $displayed_count == $page_increment) {
continue;
}
$rows[] = array(
(isset($term->depth) && $term->depth > 0 ? theme('indentation', $term->depth) : '') . l($term->name, "nodeorder/term/{$term->tid}"),
l(t('order nodes'), "nodeorder/term/{$term->tid}/order"),
);
$displayed_count++;
// we're counting tids displayed
}
if (!$rows) {
$rows[] = array(
array(
'data' => t('No terms available.'),
'colspan' => '2',
),
);
}
$GLOBALS['pager_page_array'][] = $start_from;
// FIXME
$GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1;
// FIXME
if ($total_entries >= $page_increment) {
$rows[] = array(
array(
'data' => theme('pager', NULL, $page_increment),
'colspan' => '2',
),
);
}
return theme('table', $header, $rows, array(
'id' => 'taxonomy',
));
}
/**
* Implementation of hook_views_data_alter().
*/
function nodeorder_views_data_alter(&$data) {
// taxonomy weight
$data['term_node']['weight_in_tid'] = array(
'title' => t('Weight in tid'),
'help' => t('The term weight in tid field'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
}
/**
* Implementation of hook_help().
*/
function nodeorder_help($path, $arg) {
switch ($path) {
case 'nodeorder/term/%/order':
case 'nodeorder/order/%':
$term = taxonomy_get_term($arg[2]);
$output = '<p>' . t('This page provides a drag-and-drop interface for ordering nodes. To change the order of a node, grab a drag-and-drop handle under the <em>Node title</em> column and drag the node to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save order</em> button at the bottom of the page.') . '</p>';
return $output;
case 'admin/content/taxonomy/%/order':
$vocabulary = taxonomy_vocabulary_load($arg[3]);
$output = '<p>' . t('%capital_name is an orderable vocabulary. You may order the nodes associated with a term within this vocabulary by clicking the <em>order nodes</em> link next to the term.', array(
'%capital_name' => drupal_ucfirst($vocabulary->name),
));
return $output;
}
}
Functions
Name | Description |
---|---|
nodeorder_add_link | |
nodeorder_admin | Form for Admin Settings |
nodeorder_can_be_ordered | Returns TRUE if the node has terms in any orderable vocabulary... |
nodeorder_form_alter | Implementation of hook_form_alter(). |
nodeorder_get_term_min_max | |
nodeorder_get_tids | |
nodeorder_help | Implementation of hook_help(). |
nodeorder_link | Implementation of hook_link(). |
nodeorder_menu | Implementation of hook_menu(). |
nodeorder_move_in_category | Move a node up or down in its category... |
nodeorder_nodeapi | Implementation of hook_nodeapi(). |
nodeorder_orderable_tids | Returns an array of the node's tids that are in orderable vocabularies... |
nodeorder_order_access | Custom access function which determines whether or not the user is allowed to reorder nodes and if the link should be shown at all |
nodeorder_overview_terms | Display a tree of all the terms in a vocabulary, with options to order nodes within each one. |
nodeorder_perm | Implementation of hook_perm(). |
nodeorder_select_nodes | NOTE: This is nearly a direct copy of taxonomy_select_nodes() -- see http://drupal.org/node/25801 if you find this sort of copy and paste upsetting... |
nodeorder_taxonomy_form_vocabulary_submit | |
nodeorder_taxonomy_order_access | Custom access function which determines whether or not the user is allowed to reorder nodes and if the vocabulary is orderable |
nodeorder_term_can_be_ordered | Returns TRUE if the term is in an orderable vocabulary... |
nodeorder_term_page | Menu callback; displays all nodes associated with a term. |
nodeorder_term_path | Implementation of hook_term_path() from Taxonomy. |
nodeorder_theme | Implementation of hook_theme() |
nodeorder_views_data_alter | Implementation of hook_views_data_alter(). |
nodeorder_vocabulary_can_be_ordered | Returns TRUE if the vocabulary is orderable... |