Index: weight.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/weight/weight.module,v retrieving revision 1.25 diff -u -F^function -r1.25 weight.module --- weight.module 3 Oct 2008 02:20:53 -0000 1.25 +++ weight.module 14 Oct 2008 05:15:06 -0000 @@ -40,7 +40,7 @@ function weight_menu() { // Ajax callback for weight changer page. $items['admin/node/weight/_change_weight'] = array( - 'page callback' => '_change_weight', + 'page callback' => '_weight_change', 'access arguments' => array('administer nodes'), 'type' => MENU_CALLBACK ); @@ -50,21 +50,22 @@ function weight_menu() { 'title' => 'Weight', 'access arguments' => array('administer site configuration'), 'description' => 'Add weight-based sorting to nodes.', - 'page callback' => 'weight_settings_page' + 'page callback' => 'drupal_get_form', + 'page arguments' => array('weight_settings_form'), ); // 2nd level nav (tabs). $items['admin/settings/weight/settings'] = array( 'title' => 'Settings', 'access arguments' => array('administer site configuration'), - 'page callback' => 'weight_settings_page', 'type' => MENU_DEFAULT_LOCAL_TASK ); $items['admin/settings/weight/setup'] = array( 'title' => 'Db setup', 'access arguments' => array('administer site configuration'), - 'page callback' => 'weight_enable_page', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('weight_setup_form'), 'type' => MENU_LOCAL_TASK, 'weight' => 4 ); @@ -73,7 +74,6 @@ function weight_menu() { $items['admin/settings/weight/setup/enable'] = array( 'title' => 'Enable', 'access arguments' => array('administer site configuration'), - 'page callback' => 'weight_enable_page', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -2 ); @@ -81,7 +81,8 @@ function weight_menu() { $items['admin/settings/weight/setup/disable'] = array( 'title' => 'Disable', 'access arguments' => array('administer site configuration'), - 'page callback' => 'weight_disable_page', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('weight_disable_form'), 'type' => MENU_LOCAL_TASK, 'weight' => 2 ); @@ -103,10 +104,10 @@ function weight_nodeapi(&$node, $op) { // - stickiness is sorted DESC while weight is sorted ASC so we invert // the weight before saving... if sticky box is checked, subtract weight // from 100; unweighted sticky nodes will have a value of 100. - _weight2encoded_sticky($node); + _weight_encode($node); break; case 'load': - _encoded_sticky2weight($node); + _weight_decode($node); break; } } @@ -167,78 +168,83 @@ function weight_form_alter(&$form, $form if (user_access('assign node weight') || user_access('administer nodes')) { $node = $form['#node']; if (in_array($node->type, $weight_node_types)) { + // Add the node weight selector to the bottom of the Publishing options fieldset. $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( + $form['options']['node_weight'] = array( '#type' => 'weight', - '#title' => t('Node Weight'), + '#title' => t('Weight'), '#default_value' => (int)$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.'), + '#description' => t('In listings, content with a lower (lighter) weight value will appear above those with a higher (heavier) value.'), + '#weight' => 10, ); } } } } -function weight_enable_page() { - if ($_POST['op'] == t('Setup Database')) { - weight_old_nodes(); - drupal_goto('admin/settings/weight'); - } - $count = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node} WHERE sticky IN (0,1)')); - $output = t("
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.
-%count nodes need to be updated.
", - array('%count' => $count)); - $output .= drupal_get_form('weight_setup_form'); - return $output; -} - function weight_setup_form() { - $form[] = array( - '#type' => 'submit', - '#value' => t('Setup Database'), + $count = 0; + $weight_node_types = array_filter(variable_get('weight_node_types', array())); + if (!empty($weight_node_types)) { + $placeholders = db_placeholders($weight_node_types, 'text'); + $count = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.sticky IN (0,1) AND n.type IN ($placeholders)", $weight_node_types)); + } + + $form['information'] = array( + '#type' => 'markup', + '#value' => t("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.
@count nodes need to be updated.
", array('@count' => $count)) ); + + // Only provide the submit button if there are actual nodes needing the update. + if ($count) { + $form['submit'] = 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('Before disabling the weight module, you will want to click this button to change the database back to Drupal\'s conventional sticky system.
-NOTE: Clicking this button will erase any node weights that have been set.
'); - $output .= drupal_get_form('weight_disable_form'); - return $output; +function weight_setup_form_submit($form, &$form_state) { + weight_old_nodes(); + $form_state['redirect'] = 'admin/settings/weight'; } function weight_disable_form() { - $form[] = array( - '#type' => 'submit', - '#value' => t('Remove weights'), + $count = 0; + $weight_node_types = array_filter(variable_get('weight_node_types', array())); + if (empty($weight_node_types)) { + $count = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.sticky NOT IN (0,1)", $weight_node_types)); + } + else { + $placeholders = db_placeholders($weight_node_types, 'text'); + $count = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.sticky NOT IN (0,1) AND n.type NOT IN ($placeholders)", $weight_node_types)); + } + + $form['information'] = array( + '#type' => 'markup', + '#value' => t("Before disabling the weight module, you will want to click this button to change the database back to Drupal's conventional sticky system.
@count nodes need to be updated.
", array('@count' => $count)), + ); + + // Only provide the submit button if there are actual nodes needing the update. + if ($count) { + $form['warning'] = array( + '#type' => 'markup', + '#value' => t('NOTE: Clicking this button will erase any node weights that have been set.
'), + ); + $form['submit'] = 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']); - - variable_set('weight_range', $weight_range); - variable_set('weight_node_types', $weight_node_types); - drupal_set_message(t('Settings updated.')); - } - $output .= drupal_get_form('weight_settings_form'); - return $output; +function weight_disable_form_submit($form, &$form_state) { + weight_disable(); + $form_state['redirect'] = 'admin/settings/weight'; } function weight_settings_form() { @@ -267,42 +273,54 @@ function weight_settings_form() { ), ); - $form[] = array( + $form['submit'] = array( '#type' => 'submit', '#value' => t('Update'), ); return $form; } +function weight_settings_form_submit($form, &$form_state) { + variable_set('weight_range', $form_state['values']['weight_range']); + variable_set('weight_node_types', $form_state['values']['weight_node_types']); + drupal_set_message(t('Settings updated.')); +} + /** - * 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 - * + * Update the sticky value of existing nodes if they are enabled for weights. + * This ensures that they will sort correctly. */ function weight_old_nodes() { - // TODO: only do this for the types supported by this module. - $count = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node} WHERE sticky IN (0,1)')); - if ($count > 0) { - db_query('UPDATE {node} SET sticky = 100 WHERE sticky = 1'); - db_query('UPDATE {node} SET sticky = -100 WHERE sticky = 0'); - drupal_set_message($count . t(' nodes updated to support weight.module')); - } - else { - drupal_set_message(t('No nodes needed to be updated.')); + $weight_node_types = array_filter(variable_get('weight_node_types', array())); + if (!empty($weight_node_types)) { + $placeholders = db_placeholders($weight_node_types, 'text'); + db_query("UPDATE {node} n SET n.sticky = 100 WHERE n.sticky = 1 AND n.type IN ($placeholders)", $weight_node_types); + $count = db_affected_rows(); + db_query("UPDATE {node} n SET n.sticky = -100 WHERE n.sticky = 0 AND n.type IN ($placeholders)", $weight_node_types); + $count += db_affected_rows(); + drupal_set_message(t('@count nodes updated.', array('@count' => $count))); } } /** - * 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. + * Set nodes back to normal sticky values if they are not enabled for weights. */ function weight_disable() { - // TODO: only do this for the types supported by this module. - 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 the Weight module now.')); + $weight_node_types = array_filter(variable_get('weight_node_types', array())); + if (empty($weight_node_types)) { + db_query("UPDATE {node} n SET n.sticky = 1 WHERE n.sticky > 1"); + $count = db_affected_rows(); + db_query("UPDATE {node} n SET n.sticky = 0 WHERE n.sticky < 0"); + $count += db_affected_rows(); + } + else { + $placeholders = db_placeholders($weight_node_types, 'text'); + db_query("UPDATE {node} n SET n.sticky = 1 WHERE n.sticky > 1 AND n.type NOT IN ($placeholders)", $weight_node_types); + $count = db_affected_rows(); + db_query("UPDATE {node} n SET n.sticky = 0 WHERE n.sticky < 0 AND n.type NOT IN ($placeholders)", $weight_node_types); + $count += db_affected_rows(); + } + drupal_set_message(t('@count nodes updated.', array('@count' => $count))); } /** @@ -324,7 +342,7 @@ function weight_node_selector($nid) { // $sticky = db_result(db_query('SELECT sticky FROM {node} WHERE nid = %d', $nid)); $node = node_load($nid); // Convert to our weight range. -// _encoded_sticky2weight($node); +// _weight_decode($node); $weight = $node->node_weight; // ugly bit of javascript we use for each dropdown to submit weight changes // in the background. Relies on even uglier httpRequest.js file that comes @@ -345,7 +363,7 @@ function weight_node_selector($nid) { } // Ajax callback for weight manager page. -function _change_weight($nid, $weight) { +function _weight_change($nid, $weight) { // This is a lot of unnecessary overhead. TODO: just do DB calls. $node = node_load($nid); @@ -361,7 +379,7 @@ function _change_weight($nid, $weight) { } // Convert our weight to 'encoded' sticky value for DB. -function _weight2encoded_sticky(&$node) { +function _weight_encode(&$node) { if ($node->sticky) { $node->sticky = 100 - $node->node_weight; } @@ -372,7 +390,7 @@ function _weight2encoded_sticky(&$node) } // Convert our weight back out of sticky. -function _encoded_sticky2weight(&$node) { +function _weight_decode(&$node) { // $x = $node->sticky; if ($node->sticky > 0) { $node->node_weight = $node->sticky == 1 ? 0 : (100 - $node->sticky);