You are here

function weight_old_nodes in Weight 6

Same name and namespace in other branches
  1. 5 weight.module \weight_old_nodes()
  2. 7 weight.admin.inc \weight_old_nodes()

Update the sticky value of existing nodes if they are enabled for weights. This ensures that they will sort correctly.

1 call to weight_old_nodes()
weight_settings_form_submit in ./weight.admin.inc

File

./weight.admin.inc, line 127
This module uses the sticky column of the node table to add weighting to nodes.

Code

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,
    )));
    $placeholders = db_placeholders($weight_node_types, 'text');
    if (variable_get('weight_use_menu', 0)) {
      $result = db_query("SELECT nid FROM {node} WHERE sticky = 1 AND type IN ({$placeholders})", $weight_node_types);
      $count = db_affected_rows();
      while ($row = db_fetch_object($result)) {
        $weight = db_result(db_query("SELECT weight FROM {menu_links} WHERE link_path = '%s'", 'node/' . $row->nid));
        $temp->node_weight = $weight;
        $temp->sticky = 1;
        _weight_encode($temp);
        db_query("UPDATE {node} SET sticky = %d WHERE sticky = 1 AND nid = %d", array(
          $temp->sticky,
          $row->nid,
        ));
      }
      $result = db_query("SELECT nid FROM {node} WHERE sticky = 0 AND type IN ({$placeholders})", $weight_node_types);
      $count = db_affected_rows();
      while ($row = db_fetch_object($result)) {
        $weight = db_result(db_query("SELECT weight FROM {menu_links} WHERE link_path = '%s'", 'node/' . $row->nid));
        $temp->node_weight = $weight;
        $temp->sticky = 0;
        _weight_encode($temp);
        dpm($temp->sticky);
        db_query("UPDATE {node} SET sticky = %d WHERE sticky = 0 AND nid = %d", array(
          $temp->sticky,
          $row->nid,
        ));
      }
    }
    else {

      // 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;
      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,
      )));
    }
  }
}