1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* A form to change the type of date used in date fields.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Form constructor for the date type change form.
|
10
|
*
|
11
|
* @see date_tools_change_type_form_validate()
|
12
|
* @see date_tools_change_type_form_submit()
|
13
|
*/
|
14
|
function date_tools_change_type_form() {
|
15
|
$form = array();
|
16
|
// This is broken, still needs to be adjusted for the D6->D7 changes.
|
17
|
drupal_set_message(t('This operation does not yet work for the Drupal 7 version.'), 'error');
|
18
|
return $form;
|
19
|
$fields = content_fields();
|
20
|
$date_options = array();
|
21
|
$type_options = array();
|
22
|
|
23
|
$labels = array();
|
24
|
foreach (date_field_info() as $type => $info) {
|
25
|
$type_options[$type] = $info['label'] . ': ' . $info['description'];
|
26
|
$labels[$type] = $info['label'];
|
27
|
}
|
28
|
// Get the available date fields.
|
29
|
foreach ($fields as $field_name => $field) {
|
30
|
if ($field['type'] == 'date' || $field['type'] == 'datestamp' || $field['type'] == 'datetime') {
|
31
|
$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']]));
|
32
|
}
|
33
|
}
|
34
|
if (sizeof($date_options) < 1) {
|
35
|
drupal_set_message(t('There are no date fields in this database.'));
|
36
|
return $form;
|
37
|
}
|
38
|
$form['date_field'] = array(
|
39
|
'#type' => 'select',
|
40
|
'#options' => $date_options,
|
41
|
'#title' => t('Date field'),
|
42
|
'#default_value' => '',
|
43
|
'#description' => t('The date field which whose type should be changed.'),
|
44
|
);
|
45
|
$form['type'] = array(
|
46
|
'#type' => 'radios',
|
47
|
'#options' => $type_options,
|
48
|
'#default_value' => '',
|
49
|
'#required' => TRUE,
|
50
|
'#description' => t('The type of date to change the field to.'),
|
51
|
'#prefix' => '<strong>' . t('New type:') . '</strong>',
|
52
|
);
|
53
|
$form['submit'] = array('#type' => 'submit', '#value' => t('Change'));
|
54
|
return $form;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Form validation handler for date_tools_change_type_form().
|
59
|
*
|
60
|
* @see date_tools_change_type_form_submit()
|
61
|
*/
|
62
|
function date_tools_change_type_form_validate($form, &$form_state) {
|
63
|
$field_name = $form_state['values']['date_field'];
|
64
|
$new_type = $form_state['values']['type'];
|
65
|
$field = content_fields($field_name);
|
66
|
$old_type = $field['type'];
|
67
|
if ($new_type == $old_type) {
|
68
|
form_set_error('type', t('The current type is the same as the chosen type. There is nothing to change.'));
|
69
|
}
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Form submission handler for date_tools_change_type_form().
|
74
|
*
|
75
|
* @see date_tools_change_type_form_validate()
|
76
|
*/
|
77
|
function date_tools_change_type_form_submit($form, &$form_state) {
|
78
|
$field_name = $form_state['values']['date_field'];
|
79
|
$new_type = $form_state['values']['type'];
|
80
|
$field = content_fields($field_name);
|
81
|
$old_type = $field['type'];
|
82
|
if ($new_type == $old_type) {
|
83
|
return;
|
84
|
}
|
85
|
$db_info = content_database_info($field);
|
86
|
$table = $db_info['table'];
|
87
|
$columns = $db_info['columns'];
|
88
|
$labels = array();
|
89
|
foreach (date_field_info() as $type => $info) {
|
90
|
$labels[$type] = $info['label'];
|
91
|
}
|
92
|
|
93
|
// Is there any data in this field? If not, we can
|
94
|
// skip some steps.
|
95
|
$has_data = db_query("SELECT COUNT(*) FROM {" . $table . "}")->fetchField();
|
96
|
|
97
|
// Create a backup copy of the original values.
|
98
|
// The values are going to get corrupted when we
|
99
|
// change the column type.
|
100
|
if ($has_data) {
|
101
|
$temp_table = $table . '_temp';
|
102
|
db_query("CREATE TABLE {" . $temp_table . "} SELECT * FROM {" . $table . "}");
|
103
|
}
|
104
|
|
105
|
// Change the field definition to the new type.
|
106
|
$field['type'] = $new_type;
|
107
|
require_once './' . drupal_get_path('module', 'content') . '/includes/content.crud.inc';
|
108
|
content_field_instance_update($field, FALSE);
|
109
|
content_clear_type_cache();
|
110
|
|
111
|
// If there's no data to update, we're finished.
|
112
|
if (!$has_data) {
|
113
|
drupal_set_message(t('The field @field_name has been changed from @old_type to @new_type.', array(
|
114
|
'@field_name' => $field['widget']['label'], '@old_type' => $labels[$old_type], '@new_type' => $labels[$new_type])));
|
115
|
return;
|
116
|
}
|
117
|
|
118
|
// Replace old values with modified values, massaging the original values as
|
119
|
// necessary for the new type.
|
120
|
require_once './' . drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
|
121
|
$date_handler = new date_sql_handler();
|
122
|
$date_handler->granularity = $field['granularity'];
|
123
|
$date_handler->date_type = $old_type;
|
124
|
|
125
|
$new_columns = array();
|
126
|
$old_columns = array('nid', 'vid');
|
127
|
$new_columns[] = $temp_table . '.nid AS nid';
|
128
|
$new_columns[] = $temp_table . '.vid AS vid';
|
129
|
if ($field->multiple) {
|
130
|
$new_columns[] = $temp_table . '.delta AS delta';
|
131
|
$old_columns[] = 'delta';
|
132
|
}
|
133
|
foreach ($columns as $column => $info) {
|
134
|
if ($column != 'value' && $column != 'value2') {
|
135
|
continue;
|
136
|
}
|
137
|
$old_columns[] = $info['column'];
|
138
|
$db_field = $date_handler->sql_field($temp_table . '.' . $info['column'], 0);
|
139
|
switch ($old_type) {
|
140
|
case 'date':
|
141
|
switch ($new_type) {
|
142
|
case 'datestamp':
|
143
|
$new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
|
144
|
break;
|
145
|
case 'datetime':
|
146
|
$new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
|
147
|
break;
|
148
|
}
|
149
|
break;
|
150
|
case 'datestamp':
|
151
|
switch ($new_type) {
|
152
|
case 'date':
|
153
|
$new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
|
154
|
break;
|
155
|
case 'datetime':
|
156
|
$new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
|
157
|
break;
|
158
|
}
|
159
|
break;
|
160
|
case 'datetime':
|
161
|
switch ($new_type) {
|
162
|
case 'date':
|
163
|
$new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
|
164
|
break;
|
165
|
case 'datestamp':
|
166
|
$new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
|
167
|
break;
|
168
|
}
|
169
|
break;
|
170
|
}
|
171
|
}
|
172
|
|
173
|
// Make sure database timezone is set to UTC.
|
174
|
$date_handler->set_db_timezone();
|
175
|
|
176
|
// Make the replacement.
|
177
|
$sql = 'REPLACE INTO {' . $table . '} (' . implode(', ', $old_columns) . ') ' . ' SELECT ' . implode(', ', $new_columns) . ' FROM {' . $temp_table . '}';
|
178
|
db_query($sql);
|
179
|
db_query("DROP TABLE {" . $temp_table . "}");
|
180
|
|
181
|
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])));
|
182
|
}
|