1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Update scripts for the Date All Day module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Change old "all day" values.
|
10
|
*
|
11
|
* Old data was stored with values with the time set to "00:00:00" whereas new
|
12
|
* data is stored with the time set to "23:59:59" (the minutes value varies
|
13
|
* depending upon the field's "increment" value), while keeping the same date.
|
14
|
*
|
15
|
* @todo Run this through a sandbox.
|
16
|
*/
|
17
|
function date_all_day_update_7200() {
|
18
|
// Get all fields provided by the Data module. This is used instead of the
|
19
|
// usual field_read_fields() because we want to select three different field
|
20
|
// types, whereas the API only supports one at a time.
|
21
|
$field_names = db_select('field_config', 'f')
|
22
|
->fields('f', array('field_name'))
|
23
|
->condition('f.type', array('datetime', 'date', 'datestamp'), 'IN')
|
24
|
->execute()
|
25
|
->fetchCol();
|
26
|
|
27
|
// Keep track of the fields that were processed.
|
28
|
$fields_processed = array();
|
29
|
|
30
|
// Loop over each of the fields that was found.
|
31
|
foreach ($field_names as $field_name) {
|
32
|
// Get the full specs of this base field.
|
33
|
$field = field_read_field($field_name);
|
34
|
|
35
|
// Loop over each instance of this field.
|
36
|
$params = array(
|
37
|
'field_name' => $field_name,
|
38
|
);
|
39
|
foreach (field_read_instances($params) as $instance) {
|
40
|
if (!empty($instance['widget']['settings']['display_all_day'])) {
|
41
|
$fields_processed[] = $field_name;
|
42
|
|
43
|
// If this field has a "to" date, the column to change is the "value2"
|
44
|
// column, otherwise just change the primary column.
|
45
|
if (!empty($field['settings']['todate'])) {
|
46
|
$field_to_change = $field_name . '_value2';
|
47
|
}
|
48
|
else {
|
49
|
$field_to_change = $field_name . '_value';
|
50
|
}
|
51
|
|
52
|
// Manually fix each record.
|
53
|
$records = db_select('field_data_' . $field_name, 'f')
|
54
|
->fields('f')
|
55
|
->condition('f.entity_type', $instance['entity_type'])
|
56
|
->condition('f.bundle', $instance['bundle'])
|
57
|
->condition('f.' . $field_to_change, '%:00:00', 'LIKE')
|
58
|
->execute();
|
59
|
|
60
|
// The value that the widget is set to increment by. This is stored in
|
61
|
// minutes.
|
62
|
$increment = 1;
|
63
|
if (isset($instance['widget']['settings']['increment'])) {
|
64
|
$increment = $instance['widget']['settings']['increment'];
|
65
|
}
|
66
|
|
67
|
foreach ($records as $record) {
|
68
|
// Extract the value and convert it to a timestamp.
|
69
|
$end_date_value = $record->{$field_to_change};
|
70
|
$date = strtotime($end_date_value);
|
71
|
|
72
|
// The old time was stored with the value of midnight the day before
|
73
|
// the date that was intended, e.g. if the date was 2020-10-17 the
|
74
|
// time was stored as 0:00:00 thus the saved value "2020-10-17
|
75
|
// 00:00:00". This needs to be converted to 1 second before midnight
|
76
|
// on *that* day, i.e. "2020-10-17 23:59:59".
|
77
|
// @see hook_date_combo_validate_date_end_alter()
|
78
|
// @see date_all_day_date_combo_validate_date_start_alter()
|
79
|
$date += 23 * 60 * 60; // 23 hours.
|
80
|
$date += (60 - $increment) * 60; // 60 minutes - $increment, e.g. 59.
|
81
|
$date += 59; // 59 seconds.
|
82
|
|
83
|
// Reformat the date using an appropriate format for storing in the
|
84
|
// database.
|
85
|
$date = date(DATE_FORMAT_DATETIME, $date);
|
86
|
|
87
|
// Update the record.
|
88
|
db_update('field_data_' . $field_name)
|
89
|
->fields(array(
|
90
|
$field_to_change => $date,
|
91
|
))
|
92
|
->condition('entity_type', $record->entity_type)
|
93
|
->condition('bundle', $record->bundle)
|
94
|
->condition('deleted', $record->deleted)
|
95
|
->condition('revision_id', $record->revision_id)
|
96
|
->condition('language', $record->language)
|
97
|
->condition('delta', $record->delta)
|
98
|
->execute();
|
99
|
|
100
|
// Clear the caches for this entity.
|
101
|
entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
// If any data was converted clear the site's caches.
|
108
|
if ($fields_processed) {
|
109
|
drupal_flush_all_caches();
|
110
|
}
|
111
|
|
112
|
return t('Processed %fields.', array('%fields' => implode(', ', $fields_processed)));
|
113
|
}
|