/[drupal]/contributions/modules/weight/weight.module
ViewVC logotype

Diff of /contributions/modules/weight/weight.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.23.2.6, Wed Feb 25 19:05:39 2009 UTC revision 1.23.2.7, Sat Apr 25 15:03:23 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: weight.module,v 1.23.2.5 2009/01/30 02:22:14 nancyw Exp $  // $Id: weight.module,v 1.23.2.6 2009/02/25 19:05:39 nancyw Exp $
3  /**  /**
4   * @file   * @file
5   * This module uses the sticky column of the node table   * This module uses the sticky column of the node table
# Line 25  function weight_help($path, $args) { Line 25  function weight_help($path, $args) {
25            '@node_admin' => url('admin/content/node')            '@node_admin' => url('admin/content/node')
26            )            )
27        );        );
28        case 'admin/content/node':
29          return t('<strong>Note:</strong> When the weight module is enabled, it is not possible to filter based on sticky status.');
30    }    }
31  }  }
32    
# Line 52  function weight_menu() { Line 54  function weight_menu() {
54      'description' => 'Add weight-based sorting to nodes.',      'description' => 'Add weight-based sorting to nodes.',
55      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
56      'page arguments' => array('weight_settings_form'),      'page arguments' => array('weight_settings_form'),
57        'file' => 'weight.admin.inc',
58      );      );
59    
60    return $items;    return $items;
# Line 102  function weight_form_alter(&$form, $form Line 105  function weight_form_alter(&$form, $form
105    // 'filter on sticky status' altogether when weight_module is enabled    // 'filter on sticky status' altogether when weight_module is enabled
106      unset($form['filters']['status']['status']['#options']['sticky-1']);      unset($form['filters']['status']['status']['#options']['sticky-1']);
107      unset($form['filters']['status']['status']['#options']['sticky-0']);      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.')  
     );  
108    
109      // I can't add a table header for weight, so instead I'm going to explain      // I can't add a table header for weight, so instead I'm going to explain
110      // the weight dropdown to the user. Also, to position my help text      // the weight dropdown to the user. Also, to position my help text
# Line 122  function weight_form_alter(&$form, $form Line 121  function weight_form_alter(&$form, $form
121          // only add weight selector if weight is enabled for this node type          // only add weight selector if weight is enabled for this node type
122          if (in_array($form['admin']['name'][$nid]['#value'], $weight_node_type_names) ) {          if (in_array($form['admin']['name'][$nid]['#value'], $weight_node_type_names) ) {
123            $selector = weight_node_selector($nid);            $selector = weight_node_selector($nid);
124            $form['admin']['operations'][$nid]['weight_selector']['#value'] =  $selector['selector'];            $form['admin']['operations'][$nid]['#value'] .= ' '. $selector['selector'];
125            $form['admin']['status'][$nid]['#value'] .=  $selector['status'];            $form['admin']['status'][$nid]['#value'] .=  $selector['status'];
126          }          }
127        }        }
# Line 170  function weight_form_alter(&$form, $form Line 169  function weight_form_alter(&$form, $form
169    }    }
170  }  }
171    
 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' => '<p>'. t('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' => '<p>'. t('If the node has not been weighted, should we use the menu item weight?') .'</p>',  
     );  
   
   $form['weight_position'] = array(  
     '#type' => 'weight',  
     '#delta' => 10,  
     '#title' => t('Weight selector position weight'),  
     '#default_value' => variable_get('weight_position', 0),  
     '#description' => '<p>'. t('This controls where the selection for node weight goes on the node edit form. If the position is 10 and the user has "administer nodes" permission, it will be added into the "Workflow options."') .'</p>',  
     );  
   
   $form['weight_node_types'] = array(  
     '#type' => 'checkboxes',  
     '#title' => t('Display On'),  
     '#default_value' => variable_get('weight_node_types', $types),  
     '#options' => $types,  
     '#description' => '<p>'. t('Select the content types to be weighted.  
       The selected content types will be mass updated to the default weight</p>  
       <p><i>Note:</i> Unselecting a node type after having changed weights  
       will result in the loss of those weights. You may want to check the  
       <a href="@posts_page">content page</a> before unsetting any node types.',  
       array( '@posts_page' => url('admin/content/node'))  
       ) .'</p>',  
       );  
   
   $form['weight_default'] = array(  
     '#type' => 'weight',  
     '#delta' => variable_get('weight_range', 20),  
     '#title' => t('Default weight'),  
     '#default_value' => variable_get('weight_default', 0),  
     '#description' => t('If a new content type is selected, this is the weight that will be assigned to those nodes. If you are also changing the range above, "Save" that change first.'),  
     );  
   
   $form['submit'] = array(  
     '#type' => 'submit',  
     '#value' => t('Save configuration'),  
     );  
   
   return $form;  
 }  
   
 function weight_settings_form_submit($form, &$form_state) {  
   variable_set('weight_range', $form_state['values']['weight_range']);  
   variable_set('weight_position', $form_state['values']['weight_position']);  
   variable_set('weight_default', $form_state['values']['weight_default']);  
   
   // Check for changes in the list.  
   $before = array_filter(variable_get('weight_node_types', array()));  
   $after = array_filter($form_state['values']['weight_node_types']);  
   $del = array_diff($before, $after);  
   $add = array_diff($after, $before);  
   // Add weighting to new types.  
   if ($add) {  
     weight_old_nodes($add);  
   }  
   // Remove weighting from types taken out of the list.  
   if ($del) {  
     weight_disable($del);  
   }  
   
   variable_set('weight_node_types', $after);  
   
   drupal_set_message(t('Settings updated.'));  
 }  
   
 /**  
  * Update the sticky value of existing nodes if they are enabled for weights.  
  * This ensures that they will sort correctly.  
  */  
 function weight_old_nodes($weight_node_types = array()) {  
   if ($weight_node_types) {  
     $temp = new stdClass();  
     $temp->node_weight = variable_get('weight_default', 0);  
     drupal_set_message(t('Enabling weight for: !types, default weight: !default',  
       array('!types' => implode(', ', $weight_node_types), '!default' => $temp->node_weight)));  
     // Get default for non-sticky nodes;  
     $temp->sticky = 0;  
     _weight_encode($temp);  
     $not_sticky = $temp->sticky;  
     // Get default for sticky nodes;  
     $temp->sticky = 1;  
     _weight_encode($temp);  
     $is_sticky = $temp->sticky;  
     $placeholders = db_placeholders($weight_node_types, 'text');  
   
     array_unshift($weight_node_types, $is_sticky);  
     db_query("UPDATE {node} SET sticky = %d WHERE sticky = 1 AND type IN ($placeholders)", $weight_node_types);  
     $count = db_affected_rows();  
     array_shift($weight_node_types);  
     array_unshift($weight_node_types, $not_sticky);  
     db_query("UPDATE {node} SET sticky = %d WHERE sticky = 0 AND type IN ($placeholders)", $weight_node_types);  
     $count += db_affected_rows();  
     drupal_set_message(t('@count nodes weight enabled.', array('@count' => $count)));  
   }  
 }  
   
 /**  
  * Set nodes back to normal sticky values if they are not enabled for weights.  
  */  
 function weight_disable($weight_node_types = array()) {  
   if ($weight_node_types) {  
     drupal_set_message(t('Disabling weight for: !types', array('!types' => implode(', ', $weight_node_types))));  
     $placeholders = db_placeholders($weight_node_types, 'text');  
     db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1 AND type IN ($placeholders)", $weight_node_types);  
     $count = db_affected_rows();  
     db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0 AND type IN ($placeholders)", $weight_node_types);  
     $count += db_affected_rows();  
   }  
   else {  
     db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1");  
     $count = db_affected_rows();  
     db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0");  
     $count += db_affected_rows();  
   }  
   drupal_set_message(t('@count nodes weight disabled.', array('@count' => $count)));  
 }  
   
172  /**  /**
173   * Generate JS code for selecting individual node weights on admin page   * Generate JS code for selecting individual node weights on admin page
174   */   */
# Line 316  function weight_node_selector($nid) { Line 182  function weight_node_selector($nid) {
182      drupal_add_css($path .'/weight.css');      drupal_add_css($path .'/weight.css');
183    }    }
184    
185    $selector_template = "\n"."<form><select style=\"margin: 0;\"    $selector_template = "\n"."<select style=\"margin: 0;\"
186      onchange='httpRequest(\"GET\", \"" . base_path() .      onchange='httpRequest(\"GET\", \"" . base_path() .
187      "admin/node/weight/_weight_change/\" + [NID] + \"/\" +      "?q=admin/node/weight/_weight_change/\" + [NID] + \"/\" +
188      this.options[this.selectedIndex].value,true)' >";      this.options[this.selectedIndex].value,true)' >";
189    
190    $node = node_load($nid);    $node = node_load($nid);
191    
192    // Convert to our weight range.    // Convert to our weight range.
193    $weight = $node->node_weight;    $weight = $node->node_weight;
194    
195    // ugly bit of javascript we use for each dropdown to submit weight changes    // ugly bit of javascript we use for each dropdown to submit weight changes
196    // in the background. Relies on even uglier httpRequest.js file that comes    // in the background. Relies on even uglier httpRequest.js file that comes
197    // with this module. Ironically, Ajax makes me feel dirty.    // with this module. Ironically, Ajax makes me feel dirty.
# Line 332  function weight_node_selector($nid) { Line 200  function weight_node_selector($nid) {
200    for ($i = 0 - $weight_range; $i <= $weight_range; ++$i) {    for ($i = 0 - $weight_range; $i <= $weight_range; ++$i) {
201      $selector_template .= "<option value='$i'>$i</option>";      $selector_template .= "<option value='$i'>$i</option>";
202    }    }
203    $selector_template .= '</select></form>';    $selector_template .= '</select>';
204    $weight_selector = $selector_template;  
205    // TODO: need node weight to be able to choose this!    $weight_selector = preg_replace("/(value='$weight')/", "$1 selected='selected'", $selector_template);
   $weight_selector = preg_replace("/(value='$weight')/", "$1 selected='selected'", $weight_selector);  
206    $weight_selector = preg_replace("/\[NID\]/", $nid, $weight_selector);    $weight_selector = preg_replace("/\[NID\]/", $nid, $weight_selector);
207    $weight_selector = '<div class="weight_selector">'. $weight_selector .'</div>';  
208    $status = NULL;    $status = NULL;
209    $status .= $node->sticky ? '<br />'. t('sticky') : NULL;    $status .= $node->sticky ? '<br />'. t('sticky') : NULL;
210    $status .= $node->promote ? '<br />'. t('promoted') : NULL;    $status .= $node->promote ? '<br />'. t('promoted') : NULL;
211    $status .= $node->translate ? '<br />'. t('translate') : NULL;    $status .= $node->translate ? '<br />'. t('translate') : NULL;
212    $status .= $node->moderate ? '<br />'. t('moderated') : NULL;    $status .= $node->moderate ? '<br />'. t('moderated') : NULL;
213    return array('selector' => $weight_selector, 'status' => $status);  
214      return array(
215        'selector' => '<div class="weight-selector">'. $weight_selector .'</div>',
216        'status' => $status,
217        );
218  }  }
219    
220  /**  /**

Legend:
Removed from v.1.23.2.6  
changed lines
  Added in v.1.23.2.7

  ViewVC Help
Powered by ViewVC 1.1.2