Projet

Général

Profil

Paste
Télécharger (6,47 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_tools / date_tools.change_type.inc @ 599a39cd

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
 * @todo This is broken, still needs to be adjusted for the D6->D7 changes.
15
 */
16
function date_tools_change_type_form() {
17
  $form = array();
18
  drupal_set_message(t('This operation does not yet work for the Drupal 7 version.'), 'error');
19
  return $form;
20
  $fields = content_fields();
21
  $date_options = array();
22
  $type_options = array();
23

    
24
  $labels = array();
25
  foreach (date_field_info() as $type => $info) {
26
    $type_options[$type] = $info['label'] . ': ' . $info['description'];
27
    $labels[$type] = $info['label'];
28
  }
29
  // Get the available date fields.
30
  foreach ($fields as $field_name => $field) {
31
    if ($field['type'] == 'date' || $field['type'] == 'datestamp' || $field['type'] == 'datetime') {
32
      $date_options[$labels[$field['type']]][$field_name] = t('Field @label (@field_name)', array(
33
        '@label' => $field['widget']['label'],
34
        '@field_name' => $field_name,
35
        '@type' => $labels[$field['type']],
36
      ));
37
    }
38
  }
39
  if (count($date_options) < 1) {
40
    drupal_set_message(t('There are no date fields in this database.'));
41
    return $form;
42
  }
43
  $form['date_field'] = array(
44
    '#type' => 'select',
45
    '#options' => $date_options,
46
    '#title' => t('Date field'),
47
    '#default_value' => '',
48
    '#description' => t('The date field which whose type should be changed.'),
49
  );
50
  $form['type'] = array(
51
    '#type' => 'radios',
52
    '#options' => $type_options,
53
    '#default_value' => '',
54
    '#required' => TRUE,
55
    '#description' => t('The type of date to change the field to.'),
56
    '#prefix' => '<strong>' . t('New type:') . '</strong>',
57
  );
58
  $form['submit'] = array('#type' => 'submit', '#value' => t('Change'));
59
  return $form;
60
}
61

    
62
/**
63
 * Form validation handler for date_tools_change_type_form().
64
 *
65
 * @see date_tools_change_type_form_submit()
66
 */
67
function date_tools_change_type_form_validate($form, &$form_state) {
68
  $field_name = $form_state['values']['date_field'];
69
  $new_type = $form_state['values']['type'];
70
  $field = content_fields($field_name);
71
  $old_type = $field['type'];
72
  if ($new_type == $old_type) {
73
    form_set_error('type', t('The current type is the same as the chosen type. There is nothing to change.'));
74
  }
75
}
76

    
77
/**
78
 * Form submission handler for date_tools_change_type_form().
79
 *
80
 * @see date_tools_change_type_form_validate()
81
 */
82
function date_tools_change_type_form_submit($form, &$form_state) {
83
  $field_name = $form_state['values']['date_field'];
84
  $new_type = $form_state['values']['type'];
85
  $field = content_fields($field_name);
86
  $old_type = $field['type'];
87
  if ($new_type == $old_type) {
88
    return;
89
  }
90
  $db_info = content_database_info($field);
91
  $table = $db_info['table'];
92
  $columns = $db_info['columns'];
93
  $labels = array();
94
  foreach (date_field_info() as $type => $info) {
95
    $labels[$type] = $info['label'];
96
  }
97

    
98
  // Is there any data in this field? If not, we can skip some steps.
99
  $has_data = db_query("SELECT COUNT(*) FROM {" . $table . "}")->fetchField();
100

    
101
  // Create a backup copy of the original values. The values are going to get
102
  // corrupted when we change the column type.
103
  if ($has_data) {
104
    $temp_table = $table . '_temp';
105
    db_query("CREATE TABLE {" . $temp_table . "} SELECT * FROM {" . $table . "}");
106
  }
107

    
108
  // Change the field definition to the new type.
109
  $field['type'] = $new_type;
110
  require_once './' . drupal_get_path('module', 'content') . '/includes/content.crud.inc';
111
  content_field_instance_update($field, FALSE);
112
  content_clear_type_cache();
113

    
114
  // If there's no data to update, we're finished.
115
  if (!$has_data) {
116
    drupal_set_message(t('The field @field_name has been changed from @old_type to @new_type.', array(
117
    '@field_name' => $field['widget']['label'],
118
    '@old_type' => $labels[$old_type],
119
    '@new_type' => $labels[$new_type])));
120
    return;
121
  }
122

    
123
  // Replace old values with modified values, massaging the original values as
124
  // necessary for the new type.
125
  require_once './' . drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
126
  $date_handler = new date_sql_handler();
127
  $date_handler->granularity = $field['granularity'];
128
  $date_handler->date_type = $old_type;
129

    
130
  $new_columns = array();
131
  $old_columns = array('nid', 'vid');
132
  $new_columns[] = $temp_table . '.nid AS nid';
133
  $new_columns[] = $temp_table . '.vid AS vid';
134
  if ($field->multiple) {
135
    $new_columns[] = $temp_table . '.delta AS delta';
136
    $old_columns[] = 'delta';
137
  }
138
  foreach ($columns as $column => $info) {
139
    if ($column != 'value' && $column != 'value2') {
140
      continue;
141
    }
142
    $old_columns[] = $info['column'];
143
    $db_field = $date_handler->sql_field($temp_table . '.' . $info['column'], 0);
144
    switch ($old_type) {
145
      case 'date':
146
        switch ($new_type) {
147
          case 'datestamp':
148
            $new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
149
            break;
150

    
151
          case 'datetime':
152
            $new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
153
            break;
154
        }
155
        break;
156

    
157
      case 'datestamp':
158
        switch ($new_type) {
159
          case 'date':
160
            $new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
161
            break;
162

    
163
          case 'datetime':
164
            $new_columns[] = $date_handler->sql_format('Y-m-d H:i:s', $db_field) . ' AS ' . $info['column'];
165
            break;
166
        }
167
        break;
168

    
169
      case 'datetime':
170
        switch ($new_type) {
171
          case 'date':
172
            $new_columns[] = $date_handler->sql_format('Y-m-d/TH:i:s', $db_field) . ' AS ' . $info['column'];
173
            break;
174

    
175
          case 'datestamp':
176
            $new_columns[] = $date_handler->sql_format('U', $db_field) . ' AS ' . $info['column'];
177
            break;
178
        }
179
        break;
180
    }
181
  }
182

    
183
  // Make sure database timezone is set to UTC.
184
  $date_handler->set_db_timezone();
185

    
186
  // Make the replacement.
187
  $sql = 'REPLACE INTO {' . $table . '} (' . implode(', ', $old_columns) . ') ' . ' SELECT ' . implode(', ', $new_columns) . ' FROM {' . $temp_table . '}';
188
  db_query($sql);
189
  db_query("DROP TABLE {" . $temp_table . "}");
190

    
191
  drupal_set_message(t('The field @field_name has been changed from @old_type to @new_type.', array(
192
    '@field_name' => $field['widget']['label'],
193
    '@old_type' => $labels[$old_type],
194
    '@new_type' => $labels[$new_type],
195
  )));
196
}