Skip to content
weight.install 9.04 KiB
Newer Older
davisben's avatar
davisben committed
<?php

/**
 * Implements hook_schema().
 */
function weight_schema() {
  $schema['weight_settings'] = array(
    'description' => 'Table for storing Weight configuration',
    'export' => array(
      'key' => 'type',
      'key name' => 'Type',
      'primary key' => 'type',
      'identifier' => 'settings',
      'export type string' => 'export_type',
      'api' => array(
        'owner' => 'weight',
        'api' => 'weight',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
davisben's avatar
davisben committed
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a content type configuration',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight_enabled' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight_range' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 20,
      ),
      'menu_weight' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight_default' => array(
        'type' => 'int',
        'size' => 'tiny',
davisben's avatar
davisben committed
        'not null' => TRUE,
        'default' => 0,
      ),
      'sync_translations' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ),
davisben's avatar
davisben committed
    ),
    'primary key' => array('id'),
    'foreign keys' => array(
      'node_type' => array(
        'table' => 'node_type',
        'columns' => array('type' => 'type'),
      ),
    ),
  );

  $schema['weight_weights'] = array(
    'fields' => array(
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'weight' => array(
davisben's avatar
davisben committed
        'type' => 'int',
davisben's avatar
davisben committed
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('entity_id'),
davisben's avatar
davisben committed
  );

  return $schema;
davisben's avatar
davisben committed
/**
 * Insert existing settings into {weight_settings}.
 */
function weight_update_7200() {
  $types = node_type_get_names();
  $weight_types = variable_get('weight_node_types', array());
davisben's avatar
davisben committed
  $range = variable_get('weight_range', 20);
  $menu_weight = variable_get('weight_use_menu', 0);
  $default = variable_get('weight_default', 0);
Ben Davis's avatar
Ben Davis committed
  drupal_install_schema('weight');
davisben's avatar
davisben committed

  // If there are no content types, there is nothing to convert.
davisben's avatar
davisben committed
  if (empty($types)) {
    return;
  }

  foreach ($types as $type => $name) {
    if (in_array($type, $weight_types)) {
      $enabled = 1;
    }
    else {
      $enabled = 0;
    }

davisben's avatar
davisben committed
    $query = db_insert('weight_settings')
      ->fields(array(
        'type' => $type,
        'weight_enabled' => $enabled,
davisben's avatar
davisben committed
        'weight_range' => $range,
        'menu_weight' => $menu_weight,
        'weight_default' => $default,
      ))
      ->execute();
  }
}

/**
 * We don't need weight_update_7201 anymore.
davisben's avatar
davisben committed
 */

/**
 * Move existing node weights to weight_weights table and restore sticky values.
davisben's avatar
davisben committed
 */
function weight_update_7202(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
davisben's avatar
davisben committed
    $sandbox['last'] = 0;
    $sandbox['max'] = db_query("SELECT COUNT(nid) FROM {node}")->fetchField();
  $query = db_select('node', 'n');
  $query
    ->fields('n', array('nid', 'type', 'sticky'))
    ->condition('n.nid', $sandbox['last'], '>')
    ->orderBy('n.nid')
    ->range(0, 200);
  $nodes = $query->execute();
davisben's avatar
davisben committed

  foreach ($nodes as $node) {
    $values = _weight_update_7202_decode($node->sticky);
    if ($values['weight'] != NULL) {
      $query = db_insert('weight_weights')
        ->fields(array(
          'entity_id' => $node->nid,
          'entity_type' => 'node',
          'weight' => $values['weight'],
        ))
        ->execute();
    }
davisben's avatar
davisben committed

    db_update('node')
      ->fields(array(
        'sticky' => $values['sticky'],
      ))
      ->condition('nid', $node->nid)
      ->execute();
davisben's avatar
davisben committed

    db_update('node_revision')
      ->fields(array(
        'sticky' => $values['sticky'],
      ))
      ->condition('nid', $node->nid)
      ->execute();

    $sandbox['progress']++;
    $sandbox['last'] = $node->nid;
  }
davisben's avatar
davisben committed

  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
davisben's avatar
davisben committed
}

/**
 * Remove existing variables.
 */
function weight_update_7203() {
  variable_del('weight_node_types');
  variable_del('weight_range');
  variable_del('weight_use_menu');
  variable_del('weight_default');
  variable_del('weight_label');
  variable_del('weight_position');
}

/**
* Alter database to allow for negative default weights.
*/
function weight_update_7204() {
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => FALSE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_change_field('weight_settings', 'weight_default', 'weight_default', $spec);
}

/**
 * Set default weights for enabled types.
 */
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']);
    }
  }
}

/**
 * Move existing node weights to weight_weights from field_data_weight.
 */
function weight_update_7206(&$sandbox) {
  if (db_table_exists('field_data_weight')) {
    if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['last'] = 0;
      $sandbox['max'] = db_query("SELECT COUNT(entity_id) FROM {field_data_weight}
        WHERE language=:lang", array(':lang' => LANGUAGE_NONE))->fetchField();
    $query = db_select('field_data_weight', 'fw');
      ->fields('fw', array('entity_id', 'weight_value'))
      ->condition('fw.language', LANGUAGE_NONE)
      ->condition('fw.entity_id', $sandbox['last'], '>')
      ->orderBy('fw.entity_id')
      ->range(0, 200);
    $nodes = $query->execute();
    foreach ($nodes as $node) {
          'entity_id' => $node->entity_id,
          'entity_type' => 'node',
          'weight' => $node->weight_value,
        ->condition('entity_id', $node->entity_id)
      $sandbox['last'] = $node->entity_id;
    $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  }
/**
 * Remove unneeded weight field.
 */
function weight_update_7207() {
  field_delete_field('weight');
  field_sync_field_status();
  $limit = variable_get('field_purge_batch_size', 10);
  field_purge_batch($limit);
/**
 * Remove unused id column from weight_settings and add primary key to type.
 */
function weight_update_7208() {
  db_drop_field('weight_settings', 'id');
  db_add_primary_key('weight_settings', array('type'));
}

/**
 * Add sync_translations column to weight_settings.
 */
function weight_update_7209() {
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => FALSE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field('weight_settings', 'sync_translations', $spec);
}

davisben's avatar
davisben committed
/**
 * Decode weight from sticky column.
 */
function _weight_update_7202_decode($sticky) {
  $values = array();

  if ($sticky == 0 || $sticky == 1) {
    $values['weight'] = NULL;
    $values['sticky'] = $sticky;
  }
  elseif ($sticky > 0) {
    $values['weight'] = 100 - $sticky;
    $values['sticky'] = 1;
  }
  else {
    $values['weight'] = -($sticky + 100);
    $values['sticky'] = 0;
  }

  return $values;
}