/[drupal]/contributions/modules/weight/weight.admin.inc
ViewVC logotype

Diff of /contributions/modules/weight/weight.admin.inc

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

revision 1.1, Sat Apr 25 15:03:23 2009 UTC revision 1.1.2.1, Sat Apr 25 15:03:23 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: weight.module,v 1.23.2.6 2009/02/25 19:05:39 nancyw Exp $
3    /**
4     * @file
5     * This module uses the sticky column of the node table
6     * to add weighting to nodes.
7     */
8    
9    function weight_settings_form() {
10      $form = array();
11      $types = node_get_types('names');
12    
13      $form['weight_range'] = array(
14        '#type' => 'select',
15        '#title' => t('Node Weight Range'),
16        '#default_value' => variable_get('weight_range', 20),
17        '#options' => array(5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90),
18        '#description' => '<p>'. t('This will be the +/- range for node weight.') .'</p>',
19        );
20    
21      $form['weight_use_menu'] = array(
22        '#type' => 'checkbox',
23        '#title' => t('Use Menu Weight'),
24        '#default_value' => variable_get('weight_use_menu', FALSE),
25        '#description' => '<p>'. t('If the node has not been weighted, should we use the menu item weight?') .'</p>',
26        );
27    
28      $form['weight_position'] = array(
29        '#type' => 'weight',
30        '#delta' => 10,
31        '#title' => t('Weight selector position weight'),
32        '#default_value' => variable_get('weight_position', 0),
33        '#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>',
34        );
35    
36      $form['weight_node_types'] = array(
37        '#type' => 'checkboxes',
38        '#title' => t('Display On'),
39        '#default_value' => variable_get('weight_node_types', $types),
40        '#options' => $types,
41        '#description' => '<p>'. t('Select the content types to be weighted.
42          The selected content types will be mass updated to the default weight</p>
43          <p><i>Note:</i> Unselecting a node type after having changed weights
44          will result in the loss of those weights. You may want to check the
45          <a href="@posts_page">content page</a> before unsetting any node types.',
46          array('@posts_page' => url('admin/content/node'))
47          ) .'</p>',
48        );
49    
50      $form['weight_default'] = array(
51        '#type' => 'weight',
52        '#delta' => variable_get('weight_range', 20),
53        '#title' => t('Default weight'),
54        '#default_value' => variable_get('weight_default', 0),
55        '#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.'),
56        );
57    
58      $form['submit'] = array(
59        '#type' => 'submit',
60        '#value' => t('Save configuration'),
61        );
62    
63      return $form;
64    }
65    
66    function weight_settings_form_submit($form, &$form_state) {
67      variable_set('weight_range', $form_state['values']['weight_range']);
68      variable_set('weight_position', $form_state['values']['weight_position']);
69      variable_set('weight_default', $form_state['values']['weight_default']);
70    
71      // Check for changes in the list.
72      $before = array_filter(variable_get('weight_node_types', array()));
73      $after = array_filter($form_state['values']['weight_node_types']);
74      $del = array_diff($before, $after);
75      $add = array_diff($after, $before);
76      // Add weighting to new types.
77      if ($add) {
78        weight_old_nodes($add);
79      }
80      // Remove weighting from types taken out of the list.
81      if ($del) {
82        weight_disable($del);
83      }
84    
85      variable_set('weight_node_types', $after);
86    
87      drupal_set_message(t('Settings updated.'));
88    }
89    
90    /**
91     * Update the sticky value of existing nodes if they are enabled for weights.
92     * This ensures that they will sort correctly.
93     */
94    function weight_old_nodes($weight_node_types = array()) {
95      if ($weight_node_types) {
96        $temp = new stdClass();
97        $temp->node_weight = variable_get('weight_default', 0);
98        drupal_set_message(t('Enabling weight for: !types, default weight: !default',
99          array('!types' => implode(', ', $weight_node_types), '!default' => $temp->node_weight)));
100        // Get default for non-sticky nodes;
101        $temp->sticky = 0;
102        _weight_encode($temp);
103        $not_sticky = $temp->sticky;
104        // Get default for sticky nodes;
105        $temp->sticky = 1;
106        _weight_encode($temp);
107        $is_sticky = $temp->sticky;
108        $placeholders = db_placeholders($weight_node_types, 'text');
109    
110        array_unshift($weight_node_types, $is_sticky);
111        db_query("UPDATE {node} SET sticky = %d WHERE sticky = 1 AND type IN ($placeholders)", $weight_node_types);
112        $count = db_affected_rows();
113        array_shift($weight_node_types);
114        array_unshift($weight_node_types, $not_sticky);
115        db_query("UPDATE {node} SET sticky = %d WHERE sticky = 0 AND type IN ($placeholders)", $weight_node_types);
116        $count += db_affected_rows();
117        drupal_set_message(t('@count nodes weight enabled.', array('@count' => $count)));
118      }
119    }
120    
121    /**
122     * Set nodes back to normal sticky values if they are not enabled for weights.
123     */
124    function weight_disable($weight_node_types = array()) {
125      if ($weight_node_types) {
126        drupal_set_message(t('Disabling weight for: !types', array('!types' => implode(', ', $weight_node_types))));
127        $placeholders = db_placeholders($weight_node_types, 'text');
128        db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1 AND type IN ($placeholders)", $weight_node_types);
129        $count = db_affected_rows();
130        db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0 AND type IN ($placeholders)", $weight_node_types);
131        $count += db_affected_rows();
132      }
133      else {
134        db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1");
135        $count = db_affected_rows();
136        db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0");
137        $count += db_affected_rows();
138      }
139      drupal_set_message(t('@count nodes weight disabled.', array('@count' => $count)));
140    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2