You are here

function weight_update_7205 in Weight 7.2

Set default weights for enabled types.

File

./weight.install, line 294

Code

function weight_update_7205(&$sandbox) {
  if (!db_table_exists('weight_weights')) {
    $types = array();
    $defaults = array();
    $result = db_query('SELECT type, weight_default FROM {weight_settings} WHERE weight_enabled=1');
    foreach ($result as $row) {
      $types[] = $row->type;
      $defaults[$row->type] = $row->weight_default;
    }
    if (!empty($types)) {
      if (!isset($sandbox['progress'])) {
        if (!db_table_exists('weight_weights')) {
          db_create_table('weight_weights', drupal_get_schema_unprocessed('weight', 'weight_weights'));
        }
        $sandbox['progress'] = 0;
        $sandbox['last'] = 0;
        $sandbox['max'] = db_query("SELECT COUNT(nid) FROM {node} WHERE type IN (:types)", array(
          ':types' => $types,
        ))
          ->fetchField();
      }
      $query = db_select('node', 'n');
      $query
        ->fields('n', array(
        'nid',
        'type',
      ))
        ->condition('n.type', $types, 'IN')
        ->condition('n.nid', $sandbox['last'], '>')
        ->orderBy('n.nid')
        ->range(0, 200);
      $nodes = $query
        ->execute();
      foreach ($nodes as $node) {
        db_insert('weight_weights')
          ->fields(array(
          'entity_id' => $node->nid,
          'entity_type' => 'node',
          'weight' => $defaults[$node->type],
        ))
          ->execute();
        $sandbox['progress']++;
        $sandbox['last'] = $node->nid;
      }
      $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
    }
  }
}