Newer
Older
Karen Stevenson
committed
<?php
// $Id$
/**
* A form to change the type of date used in date fields.
*/
function date_tools_change_type_form() {
$form = array();
// This is broken, still needs to be adjusted
// for the D6->D7 changes.
drupal_set_message(t('This operation does not yet work for the Drupal 7 version.'), 'error');
Karen Stevenson
committed
return $form;
$fields = content_fields();
$date_options = array();
$type_options = array();
Karen Stevenson
committed
17
18
19
20
21
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
$labels = array();
foreach (date_field_info() as $type => $info) {
$type_options[$type] = $info['label'] .': '. $info['description'];
$labels[$type] = $info['label'];
}
// Get the available date fields.
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'date' || $field['type'] == 'datestamp' || $field['type'] == 'datetime') {
$date_options[$labels[$field['type']]][$field_name] = t('Field @label (@field_name)', array('@label' => $field['widget']['label'], '@field_name' => $field_name, '@type' => $labels[$field['type']]));
}
}
if (sizeof($date_options) < 1) {
drupal_set_message(t('There are no date fields in this database.'));
return $form;
}
$form['date_field'] = array(
'#type' => 'select',
'#options' => $date_options,
'#title' => t('Date field'),
'#default_value' => '',
'#description' => t('The date field which whose type should be changed.'),
);
$form['type'] = array(
'#type' => 'radios',
'#options' => $type_options,
'#default_value' => '',
'#required' => TRUE,
'#description' => t('The type of date to change the field to.'),
'#prefix' => '<strong>' . t('New type:') .'</strong>',
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Change'));
return $form;
}
function date_tools_change_type_form_validate($form, &$form_state) {
$field_name = $form_state['values']['date_field'];
$new_type = $form_state['values']['type'];
$field = content_fields($field_name);
$old_type = $field['type'];
if ($new_type == $old_type) {
form_set_error('type', t('The current type is the same as the chosen type. There is nothing to change.'));
}
}
function date_tools_change_type_form_submit($form, &$form_state) {
$field_name = $form_state['values']['date_field'];
$new_type = $form_state['values']['type'];
$field = content_fields($field_name);
$old_type = $field['type'];
if ($new_type == $old_type) {
return;
}
$db_info = content_database_info($field);
$table = $db_info['table'];
$columns = $db_info['columns'];
$labels = array();
foreach (date_field_info() as $type => $info) {
$labels[$type] = $info['label'];
}
Karen Stevenson
committed
// Is there any data in this field? If not, we can
// skip some steps.
$has_data = db_query("SELECT COUNT(*) FROM {". $table ."}")->fetchField();
Karen Stevenson
committed
// Create a backup copy of the original values.
// The values are going to get corrupted when we
// change the column type.
if ($has_data) {
$temp_table = $table .'_temp';
db_query("CREATE TABLE {". $temp_table ."} SELECT * FROM {". $table ."}");
}
Karen Stevenson
committed
// Change the field definition to the new type.
$field['type'] = $new_type;
require_once('./'. drupal_get_path('module', 'content') .'/includes/content.crud.inc');
content_field_instance_update($field, FALSE);
content_clear_type_cache();
// If there's no data to update, we're finished.
if (!$has_data) {
drupal_set_message(t('The field @field_name has been changed from @old_type to @new_type.', array(
'@field_name' => $field['widget']['label'], '@old_type' => $labels[$old_type], '@new_type' => $labels[$new_type])));
return;
}
Karen Stevenson
committed
// Replace old values with modified values, massaging the
// original values as necessary for the new type.
Karen Stevenson
committed
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
$date_handler = new date_sql_handler();
$date_handler->construct();
$date_handler->granularity = $field['granularity'];
$date_handler->date_type = $old_type;
Karen Stevenson
committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
$new_columns = array();
$old_columns = array('nid', 'vid');
$new_columns[] = $temp_table . '.nid AS nid';
$new_columns[] = $temp_table . '.vid AS vid';
if ($field->multiple) {
$new_columns[] = $temp_table . '.delta AS delta';
$old_columns[] = 'delta';
}
foreach ($columns as $column => $info) {
if ($column != 'value' && $column != 'value2') {
continue;
}
$old_columns[] = $info['column'];
$db_field = $date_handler->sql_field($temp_table . '.' . $info['column'], 0);
switch ($old_type) {
case 'date':
switch ($new_type) {
case 'datestamp':
$new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
break;
case 'datetime':
$new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
break;
}
break;
case 'datestamp':
switch ($new_type) {
case 'date':
$new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
break;
case 'datetime':
$new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
break;
}
break;
case 'datetime':
switch ($new_type) {
case 'date':
$new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
break;
case 'datestamp':
$new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
break;
}
break;
}
}
Karen Stevenson
committed
// Make sure database timezone is set to UTC.
$date_handler->set_db_timezone();
Karen Stevenson
committed
// Make the replacement.
$sql = 'REPLACE INTO {'. $table .'} ('. implode(', ', $old_columns) .') '.
' SELECT '. implode(', ', $new_columns) .' FROM {'. $temp_table .'}';
db_query($sql);
//dsm($sql);
db_query("DROP TABLE {". $temp_table ."}");
Karen Stevenson
committed
drupal_set_message(t('The field @field_name has been changed from @old_type to @new_type.', array(
'@field_name' => $field['widget']['label'], '@old_type' => $labels[$old_type], '@new_type' => $labels[$new_type])));