View source
<?php
if (module_exists('views')) {
include drupal_get_path('module', 'weight') . '/weight.views.inc';
}
function weight_help($section) {
switch ($section) {
case 'admin/setting/weight':
case 'admin/modules#description':
return t('Add weight-based sorting to nodes.');
case 'admin/help#weight':
return t('
<p><strong>Description:</strong> The weight module adds a weight option
to enabled node types. It uses the "sticky" field in the database to
store weights as well as sticky information (so that feature is not
lost). Nodes will be sorted first by stickiness, then by weight
(lightest to heaviest), then by creation date.</p>
<p><strong>Setup:</strong> To enable weight sorting on existing nodes,
visit the <a href="@setup">weight db setup page</a> and click
"Setup Database" to convert old sticky values to new weight-encoded values for
proper sorting.</p>
<p><strong>Permissions:</strong> Users with "administer nodes"
permission will always be able to adjust weight for enabled node types.
However, enabling "assign node weight" will allow non-node-admin users
to adjust weight on their own nodes. Find these settings <a
href="@access">here</a>.</p>
<p><strong>Bulk weight management:</strong> You may easily manage the
weight of multiple nodes simultaneously by using the <a href="@node_admin">
node admin page</a>.</p>
', array(
'@setup' => url('admin/settings/weight/setup'),
'@access' => url('admin/user/access'),
'@node_admin' => url('admin/content/node'),
));
}
}
function weight_perm() {
return array(
'assign node weight',
);
}
function weight_menu($may_cache) {
$items = array();
if ($may_cache) {
$access = user_access('administer site configuration');
$items[] = array(
'path' => 'admin/node/weight/_change_weight',
'callback' => '_change_weight',
'access' => user_access('administer nodes'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'title' => t('Weight'),
'path' => 'admin/settings/weight',
'access' => $access,
'description' => t('Add weight-based sorting to nodes.'),
'callback' => 'weight_settings_page',
);
$items[] = array(
'title' => t('settings'),
'path' => 'admin/settings/weight/settings',
'access' => $access,
'callback' => 'weight_settings_page',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'title' => t('db setup'),
'path' => 'admin/settings/weight/setup',
'access' => $access,
'callback' => 'weight_enable_page',
'type' => MENU_LOCAL_TASK,
'weight' => 4,
);
$items[] = array(
'title' => t('enable'),
'path' => 'admin/settings/weight/setup/enable',
'access' => $access,
'callback' => 'weight_enable_page',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -2,
);
$items[] = array(
'title' => t('disable'),
'path' => 'admin/settings/weight/setup/disable',
'access' => $access,
'callback' => 'weight_disable_page',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
}
return $items;
}
function weight_nodeapi(&$node, $op) {
switch ($op) {
case 'submit':
if (is_null($node->node_weight)) {
$node->node_weight = 0;
}
if (variable_get('weight_use_menu', FALSE)) {
$node->node_weight = isset($node->menu['title']) && !empty($node->menu['title']) ? $node->menu['weight'] : $node->node_weight;
}
_weight2encoded_sticky($node);
break;
case 'load':
_encoded_sticky2weight($node);
break;
}
}
function weight_form_alter($form_id, &$form) {
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names')));
$weight_node_type_names = array();
foreach ($weight_node_types as $type) {
$weight_node_type_names[] = node_get_types('name', $type);
}
if ($form_id == 'node_filter_form') {
unset($form['filters']['status']['status']['#options']['sticky-1']);
unset($form['filters']['status']['status']['#options']['sticky-0']);
$form['weight_help'] = array(
'#type' => 'markup',
'#value' => t('<strong>Note:</strong> When the weight module is enabled, it is not possible to filter based on sticky status.'),
);
}
if ($form_id == 'node_admin_nodes') {
$form['options']['#suffix'] .= t('<strong>Weight:</strong> To change the weight of a node, select a value from the corresponding dropdown box under <i>@operations</i>. Node weights are submitted immediately. Selectors are only available for node types configured on the <a href="@weight_admin">weight admin page</a>.', array(
'@weight_admin' => url('admin/settings/weight'),
'@operations' => t('Operations'),
));
if (!empty($form['operations'])) {
foreach ($form['operations'] as $nid => $title) {
if (in_array($form['name'][$nid]['#value'], $weight_node_type_names)) {
$selector = weight_node_selector($nid);
$form['operations'][$nid]['weight_selector']['#value'] = $selector['selector'];
$form['status'][$nid]['#value'] .= $selector['status'];
}
}
}
}
if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
if (user_access('assign node weight') || user_access('administer nodes')) {
$node = $form['#node'];
if (in_array($node->type, $weight_node_types)) {
$range = variable_get('weight_range', 20);
$form['weight_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Node Weight'),
'#collapsible' => TRUE,
'#collapsed' => $node->node_weight == 0,
'#weight' => 2,
);
$form['weight_fieldset']['node_weight'] = array(
'#type' => 'weight',
'#title' => t("Node Weight"),
'#default_value' => $node->node_weight,
'#delta' => $range,
'#description' => t('In a node list context (such as the front page or term pages), list items (e.g. "teasers") will be ordered by "stickiness" then by "node weight" then by "authored on" datestamp. Items with a lower (lighter) node weight value will appear above those with a higher (heavier) value.'),
);
if (variable_get('weight_use_menu', FALSE)) {
$form['weight_fieldset']['node_weight']['#description'] .= '<br /> ' . t('<strong>Note</strong>: If this node is used in a menu, then this weight will be ignored.');
}
}
}
}
}
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,
));
$output .= drupal_get_form('weight_setup_form');
return $output;
}
function weight_setup_form() {
$form[] = array(
'#type' => 'submit',
'#value' => t('Setup Database'),
);
return $form;
}
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>');
$output .= drupal_get_form('weight_disable_form');
return $output;
}
function weight_disable_form() {
$form[] = array(
'#type' => 'submit',
'#value' => t('Remove weights'),
);
return $form;
}
function weight_settings_page() {
if ($_POST['op'] == t('Update')) {
$weight_range = check_plain($_POST['weight_range']);
$weight_node_types = array_keys($_POST['weight_node_types']);
$weight_use_menu = $_POST['weight_use_menu'];
variable_set('weight_range', $weight_range);
variable_set('weight_node_types', $weight_node_types);
variable_set('weight_use_menu', $weight_use_menu);
drupal_set_message(t('Settings updated.'));
}
$output .= drupal_get_form('weight_settings_form');
return $output;
}
function weight_settings_form() {
$form = array();
$types = node_get_types('names');
$form['weight_range'] = array(
'#type' => 'select',
'#title' => t('Node Weight Range'),
'#default_value' => variable_get('weight_range', 20),
'#options' => array(
5 => 5,
10 => 10,
20 => 20,
30 => 30,
40 => 40,
50 => 50,
60 => 60,
70 => 70,
80 => 80,
90 => 90,
),
'#description' => t('<p>This will be the +/- range for node weight.</p>'),
);
$form['weight_use_menu'] = array(
'#type' => 'checkbox',
'#title' => t('Use Menu Weight'),
'#default_value' => variable_get('weight_use_menu', FALSE),
'#description' => t('<p>If the node has not been weighted, should we use the menu item weight?</p>'),
);
$form['weight_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Display On'),
'#default_value' => variable_get('weight_node_types', $types),
'#options' => $types,
'#description' => t('<p>Add node weighting to these content types.</p>
<p><i>Note:</i> Unselecting a node type after having changed weights
for nodes of that type will leave these nodes with the current weight.
You may want to check the <a href="@posts_page">Post page</a>. Before
unsetting any node types.', array(
'@posts_page' => url('admin/content/node'),
)),
);
$form[] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
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.'));
}
}
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_node_selector($nid) {
static $js_included;
if (!$js_included) {
$path = drupal_get_path('module', 'weight');
drupal_add_js($path . '/httpRequest.js', 'module', 'header', TRUE);
$js_included = TRUE;
drupal_add_css($path . '/weight.css');
}
$selector_template = "\n" . "<form><select style=\"margin: 0;\"\n onchange='httpRequest(\"GET\", \"" . base_path() . "admin/node/weight/_change_weight/\" + [NID] + \"/\" +\n this.options[this.selectedIndex].value,true)' >";
$node = node_load($nid);
$weight = $node->node_weight;
$weight_range = variable_get('weight_range', 20);
for ($i = 0 - $weight_range; $i <= $weight_range; $i++) {
$selector_template .= "<option value='{$i}'>{$i}</option>";
}
$selector_template .= '</select></form>';
$weight_selector = $selector_template;
$weight_selector = preg_replace("/(value='{$weight}')/", "\$1 selected='selected'", $weight_selector);
$weight_selector = preg_replace("/\\[NID\\]/", $nid, $weight_selector);
$weight_selector = '<div class="weight-selector">' . $weight_selector . '</div>';
$status = NULL;
$status .= $node->sticky ? '<br />' . t('sticky') : NULL;
$status .= $node->promote ? '<br />' . t('promoted') : NULL;
$status .= $node->translate ? '<br />' . t('translate') : NULL;
$status .= $node->moderate ? '<br />' . t('moderated') : NULL;
return array(
'selector' => $weight_selector,
'status' => $status,
);
}
function _change_weight($nid, $weight) {
$node = node_load($nid);
$node->node_weight = $weight;
node_submit($node);
node_save($node);
}
function _weight2encoded_sticky(&$node) {
if ($node->sticky) {
$node->sticky = 100 - $node->node_weight;
}
else {
$node->sticky = -($node->node_weight + 100);
}
}
function _encoded_sticky2weight(&$node) {
if ($node->sticky > 0) {
$node->node_weight = $node->sticky == 1 ? 0 : 100 - $node->sticky;
$node->sticky = 1;
}
else {
$node->node_weight = $node->sticky == 0 ? 0 : -($node->sticky + 100);
$node->sticky = 0;
}
}