| 1 |
<?php |
<?php |
| 2 |
// $Id: weight.module,v 1.23.2.4 2009/01/27 16:44:38 nancyw Exp $ |
// $Id: weight.module,v 1.23.2.5 2009/01/30 02:22:14 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 |
| 202 |
'#title' => t('Display On'), |
'#title' => t('Display On'), |
| 203 |
'#default_value' => variable_get('weight_node_types', $types), |
'#default_value' => variable_get('weight_node_types', $types), |
| 204 |
'#options' => $types, |
'#options' => $types, |
| 205 |
'#description' => '<p>'. t('Add node weighting to these content types.</p> |
'#description' => '<p>'. t('Select the content types to be weighted. |
| 206 |
|
The selected content types will be mass updated to the default weight</p> |
| 207 |
<p><i>Note:</i> Unselecting a node type after having changed weights |
<p><i>Note:</i> Unselecting a node type after having changed weights |
| 208 |
for nodes of that type will leave these nodes with the current weight. |
will result in the loss of those weights. You may want to check the |
| 209 |
You may want to check the <a href="@posts_page">content page</a> before |
<a href="@posts_page">content page</a> before unsetting any node types.', |
| 210 |
unsetting any node types.', array( '@posts_page' => url('admin/content/node')) |
array( '@posts_page' => url('admin/content/node')) |
| 211 |
) .'</p>', |
) .'</p>', |
| 212 |
); |
); |
| 213 |
|
|
| 214 |
|
$form['weight_default'] = array( |
| 215 |
|
'#type' => 'weight', |
| 216 |
|
'#delta' => variable_get('weight_range', 20), |
| 217 |
|
'#title' => t('Default weight'), |
| 218 |
|
'#default_value' => variable_get('weight_default', 0), |
| 219 |
|
'#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.'), |
| 220 |
|
); |
| 221 |
|
|
| 222 |
$form['submit'] = array( |
$form['submit'] = array( |
| 223 |
'#type' => 'submit', |
'#type' => 'submit', |
| 224 |
'#value' => t('Save configuration'), |
'#value' => t('Save configuration'), |
| 225 |
); |
); |
| 226 |
|
|
| 227 |
return $form; |
return $form; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
function weight_settings_form_submit($form, &$form_state) { |
function weight_settings_form_submit($form, &$form_state) { |
| 231 |
|
variable_set('weight_range', $form_state['values']['weight_range']); |
| 232 |
|
variable_set('weight_position', $form_state['values']['weight_position']); |
| 233 |
|
variable_set('weight_default', $form_state['values']['weight_default']); |
| 234 |
|
|
| 235 |
|
// Check for changes in the list. |
| 236 |
$before = array_filter(variable_get('weight_node_types', array())); |
$before = array_filter(variable_get('weight_node_types', array())); |
| 237 |
$after = array_filter($form_state['values']['weight_node_types']); |
$after = array_filter($form_state['values']['weight_node_types']); |
| 238 |
$del = array_diff($before, $after); |
$del = array_diff($before, $after); |
| 239 |
$add = array_diff($after, $before); |
$add = array_diff($after, $before); |
| 240 |
|
// Add weighting to new types. |
| 241 |
if ($add) { |
if ($add) { |
| 242 |
weight_old_nodes($add); |
weight_old_nodes($add); |
| 243 |
} |
} |
| 244 |
|
// Remove weighting from types taken out of the list. |
| 245 |
if ($del) { |
if ($del) { |
| 246 |
weight_disable($del); |
weight_disable($del); |
| 247 |
} |
} |
| 248 |
variable_set('weight_range', $form_state['values']['weight_range']); |
|
| 249 |
variable_set('weight_node_types', $after); |
variable_set('weight_node_types', $after); |
| 250 |
variable_set('weight_position', $form_state['values']['weight_position']); |
|
| 251 |
drupal_set_message(t('Settings updated.')); |
drupal_set_message(t('Settings updated.')); |
| 252 |
} |
} |
| 253 |
|
|
| 257 |
*/ |
*/ |
| 258 |
function weight_old_nodes($weight_node_types = array()) { |
function weight_old_nodes($weight_node_types = array()) { |
| 259 |
if ($weight_node_types) { |
if ($weight_node_types) { |
| 260 |
drupal_set_message(t('Enabling weight for: !types', array('!types' => implode(', ', $weight_node_types)))); |
$temp = new stdClass(); |
| 261 |
|
$temp->node_weight = variable_get('weight_default', 0); |
| 262 |
|
drupal_set_message(t('Enabling weight for: !types, default weight: !default', |
| 263 |
|
array('!types' => implode(', ', $weight_node_types), '!default' => $temp->node_weight))); |
| 264 |
|
// Get default for non-sticky nodes; |
| 265 |
|
$temp->sticky = 0; |
| 266 |
|
_weight_encode($temp); |
| 267 |
|
$not_sticky = $temp->sticky; |
| 268 |
|
// Get default for sticky nodes; |
| 269 |
|
$temp->sticky = 1; |
| 270 |
|
_weight_encode($temp); |
| 271 |
|
$is_sticky = $temp->sticky; |
| 272 |
$placeholders = db_placeholders($weight_node_types, 'text'); |
$placeholders = db_placeholders($weight_node_types, 'text'); |
| 273 |
db_query("UPDATE {node} SET sticky = 100 WHERE sticky = 1 AND type IN ($placeholders)", $weight_node_types); |
|
| 274 |
|
array_unshift($weight_node_types, $is_sticky); |
| 275 |
|
db_query("UPDATE {node} SET sticky = %d WHERE sticky = 1 AND type IN ($placeholders)", $weight_node_types); |
| 276 |
$count = db_affected_rows(); |
$count = db_affected_rows(); |
| 277 |
db_query("UPDATE {node} SET sticky = -100 WHERE sticky = 0 AND type IN ($placeholders)", $weight_node_types); |
array_shift($weight_node_types); |
| 278 |
|
array_unshift($weight_node_types, $not_sticky); |
| 279 |
|
db_query("UPDATE {node} SET sticky = %d WHERE sticky = 0 AND type IN ($placeholders)", $weight_node_types); |
| 280 |
$count += db_affected_rows(); |
$count += db_affected_rows(); |
| 281 |
drupal_set_message(t('@count nodes weight enabled.', array('@count' => $count))); |
drupal_set_message(t('@count nodes weight enabled.', array('@count' => $count))); |
| 282 |
} |
} |