Commit faa42db8 authored by Jeff Robbins's avatar Jeff Robbins

Complete rewrite of weight.module - allows weighting of nodes within lists

parent 27c57035
This module adds a weight option to all nodes. It does this by modifying the status variable in the table (drupal automatically sorts by status) to put lighter weights on the top.
*******************************************************
README.txt for weight.module for Drupal
by Jeff Robbins ::: jeff /@t\ jjeff /d0t\ com
*******************************************************
This module adds a weight option to enabled node types.
It uses the node table's 'sticky' column to store
weights as well as sticky information (so that feature
is not lost). Nodes with lower weight will float to the
top of lists, while heavier items will sink.
This is useful for sorting non-cronological nodes like
bios or ecommerce products.
Nodes will be sorted first by stickiness, then by
weight, then by creation date.
\ No newline at end of file
<?php
// $Id$
function weight_system($field) {
$info["name"] = "weight";
$info["description"] = "add weight capability to all nodes";
/**
* @abstract
* This module uses the sticky column of the node table
* to add weighting to nodes
*/
return $info[$field];
/**
* @todo
* - Make sure that there is a way to filter by sticky nodes on admin/node
*/
/**
* Implementation of hook_help()
*
*/
function weight_help($section) {
switch($section) {
case 'admin/modules#description':
return t('Add weight-based sorting to nodes.');
}
}
function weight_menu($may_cache){
$items = array();
if ($may_cache){
// top level settings
$items[] = array('title' => t('weight'), 'path' => 'admin/settings/weight', 'access' => 'administer nodes', 'callback'=>'weight_settings_page');
// 2nd level nav (tabs)
$items[] = array('title' => t('settings'), 'path' => 'admin/settings/weight/settings', 'access' => 'administer nodes', 'callback' => 'weight_settings_page', 'type' => MENU_DEFAULT_LOCAL_TASK);
$items[] = array('title' => t('db setup'), 'path' => 'admin/settings/weight/setup', 'access' => 'administer nodes', 'callback' => 'weight_enable_page', 'type' => MENU_LOCAL_TASK, 'weight' => 4);
// 3rd level
$items[] = array('title' => t('enable'), 'path' => 'admin/settings/weight/setup/enable', 'access' => 'administer nodes', 'callback' => 'weight_enable_page', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -2);
$items[] = array('title' => t('disable'), 'path' => 'admin/settings/weight/setup/disable', 'access' => 'administer nodes', 'callback' => 'weight_disable_page', 'type' => MENU_LOCAL_TASK, 'weight' => 2);
}
return $items;
}
function weight_nodeapi(&$node, $op, $arg = 0) {
switch($op) {
case 'form pre':
$x = $node->sticky;
$node->sticky = 0;
if($x & 0x80) {
$node->sticky = 1;
}
$x = $x & 0x7F; /* AND $x with 0111 1111 */
if($x) {
$x = 22 - $x - 10;
}
$node->weight = $x;
return form_weight("Weight", "weight", $node->weight, 10);
break;
$range = variable_get('weight_range', 20);
if (in_array($node->type, variable_get('weight_node_types', array_flip(node_get_types())))) {
return form_weight(t("Weight"), "weight", $node->weight, $range, t('This node can be weighted within lists. Lower values appear at the beginning and higher sink to the bottom.'));
}
break;
case 'validate':
if(isset($node->weight)) {
$x = $node->sticky;
$node->sticky = 0;
if($x & 0x80) {
$x = 1;
}
if($x == 1) {
$node->sticky = 0x80;
}
$node->sticky += (22 - ($node->weight + 10));
}
break;
}
}
// vim: ft=php
?>
if ($_POST['edit']['op'] == t('Submit')) {
// here we're 'encoding' weight into the sticky value for the database
// stickiness is the inverse of weight
// - stickiness is sorted DESC while weight is sorted ASC
// so we invert the weight before saving...
// if sticky box is checked, add 100 to weight
// unweighted sticky nodes will have a value of 100
if($node->sticky) {
$node->sticky = (-1*$node->weight) + 100;
}
// unweighted non-sticky nodes will have a value of -100
else {
$node->sticky = (-1*$node->weight) - 100;
}
}
break;
case 'load':
if ($node->sticky > 0) {
$weight = ($node->sticky - 100)*-1;
$node->sticky = 1;
}
else {
$node->weight = (($node->sticky) + 100)*-1;
$node->sticky = 0;
}
}
}
function weight_enable_page(){
if ($_POST['op'] == t('Setup Database')) {
weight_old_nodes();
drupal_goto('admin/settings/weight');
}
$count = db_num_rows(db_query('SELECT DISTINCT nid FROM {node} WHERE sticky IN (0,1)'));
$output = t('<p>The weight module uses the node table\'s "sticky" column to store weight information for each node. New and updated nodes will automatically have their sticky and weight information remapped. However if you have pre-existing nodes, you will need to update your database so that these nodes sort correctly with new nodes.</p>
<p>%count nodes need to be updated.</p>', array('%count' => $count));
$form = form_submit(t('Setup Database'));
$output .= form($form);
return $output;
}
function weight_disable_page(){
if ($_POST['op'] == t('Remove weights')) {
weight_disable();
drupal_goto('admin/modules');
}
$output = t('<p>Before disabling the weight module, you will want to click this button to change the database back to Drupal\'s conventional sticky system.</p>
<p><strong>NOTE: Clicking this button will erase any node weights that have been set.</strong></p>');
$form = form_submit(t('Remove weights'));
$output .= form($form);
return $output;
}
/**
* Update "old" nodes where sticky is 1 or 0
* And resave them with new values so that they will sort correctly
* with new and updated nodes
*
*/
function weight_old_nodes(){
$count = db_num_rows(db_query('SELECT DISTINCT nid FROM {node} WHERE sticky IN (0,1)'));
db_query('UPDATE {node} SET sticky = 100 WHERE sticky = 1');
db_query('UPDATE {node} SET sticky = -100 WHERE sticky = 0');
if ($count > 0) {
drupal_set_message($count . t(' nodes updated to support weight.module'));
}
else {
drupal_set_message(t('No nodes needed to be updated.'));
}
}
/**
* This function is not called from the module
* It is here to show how to set the database back to "normal"
* when deactivating the weight.module
*
*/
function weight_disable(){
db_query('UPDATE {node} SET sticky = 1 WHERE sticky > 0');
db_query('UPDATE {node} SET sticky = 0 WHERE sticky <= 0');
drupal_set_message(t('All node weights have been removed. Please deactivate weight module now.'));
}
function weight_settings_page() {
if ($_POST['edit']['op'] == t('Update')) {
$edit = $_POST['edit'];
foreach($edit as $key => $item) {
variable_set($key, $item);
}
drupal_set_message(t('Settings updated.'));
}
$form = form_select(t('Weight Range'), 'weight_range', variable_get('weight_range', 20), array(10=>10, 20=>20, 30=>30, 40=>40), t('This will be the +/- range for node weight.'));
$types = node_get_types();
$form .= form_checkboxes(t('Display On'), 'weight_node_types', variable_get('weight_node_types', $types), $types, t("Add node weighting to these content types."));
$form .= form_submit(t('Update'));
$output .= form($form);
$output .= str_replace(array('$RCSf'.'ile:', ',v', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile$ version: <b>$Revision$</b>, $Date$</p>');
return $output;
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment