date_tools.change_type.inc 6.4 KB

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