Newer
Older
<?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,
),
),
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'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',
'unsigned' => FALSE,
'sync_translations' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
),
),
'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(
/**
* Insert existing settings into {weight_settings}.
*/
function weight_update_7200() {
$types = node_type_get_names();
$weight_types = variable_get('weight_node_types', array());
$range = variable_get('weight_range', 20);
$menu_weight = variable_get('weight_use_menu', 0);
$default = variable_get('weight_default', 0);
// If there are no content types, there is nothing to convert.
foreach ($types as $type => $name) {
if (in_array($type, $weight_types)) {
$enabled = 1;
}
else {
$enabled = 0;
}
$query = db_insert('weight_settings')
->fields(array(
'type' => $type,
'weight_range' => $range,
'menu_weight' => $menu_weight,
'weight_default' => $default,
))
->execute();
}
}
/**
* We don't need weight_update_7201 anymore.
* Move existing node weights to weight_weights table and restore sticky values.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 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();
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();
}
db_update('node')
->fields(array(
'sticky' => $values['sticky'],
))
->condition('nid', $node->nid)
->execute();
db_update('node_revision')
->fields(array(
'sticky' => $values['sticky'],
))
->condition('nid', $node->nid)
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}
/**
* 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;
}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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) {
db_update('weight_weights')
->fields(array(
'entity_id' => $node->entity_id,
'entity_type' => 'node',
'weight' => $node->weight_value,
->condition('entity_id', $node->entity_id)
$sandbox['progress']++;
$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);
}
/**
* 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;
}