1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Support for migration into Date fields.
|
5
|
*/
|
6
|
|
7
|
if (!class_exists('MigrateFieldHandler')) {
|
8
|
return;
|
9
|
}
|
10
|
|
11
|
/**
|
12
|
* Implements hook_migrate_api().
|
13
|
*/
|
14
|
function date_migrate_api() {
|
15
|
$api = array(
|
16
|
'api' => 2,
|
17
|
'field handlers' => array('DateMigrateFieldHandler'),
|
18
|
);
|
19
|
return $api;
|
20
|
}
|
21
|
|
22
|
class DateMigrateFieldHandler extends MigrateFieldHandler {
|
23
|
|
24
|
/**
|
25
|
* Declares the types of fields used.
|
26
|
*/
|
27
|
public function __construct() {
|
28
|
$this->registerTypes(array('date', 'datestamp', 'datetime'));
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Arguments for a date field migration.
|
33
|
*
|
34
|
* @param string $timezone
|
35
|
* Timezone (such as UTC, America/New_York, etc.) to apply.
|
36
|
* @param string $timezone_db
|
37
|
* Timezone_db value for the field.
|
38
|
* @param string $rrule
|
39
|
* Rule string for a repeating date field.
|
40
|
* @param string $language
|
41
|
* Language of the text (defaults to destination language)
|
42
|
*
|
43
|
* @return array
|
44
|
* An array of the defined variables in this scope.
|
45
|
*/
|
46
|
public static function arguments($timezone = 'UTC', $timezone_db = 'UTC', $rrule = NULL, $language = NULL) {
|
47
|
return get_defined_vars();
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Converts incoming data into the proper field arrays for Date fields.
|
52
|
*
|
53
|
* @param object $entity
|
54
|
* The destination entity which will hold the field arrays.
|
55
|
* @param array $field_info
|
56
|
* Metadata for the date field being populated.
|
57
|
* @param array $instance
|
58
|
* Metadata for this instance of the date field being populated.
|
59
|
* @param array $values
|
60
|
* Array of date values to be fielded.
|
61
|
*
|
62
|
* @return array|null
|
63
|
* An array of date fields.
|
64
|
*/
|
65
|
public function prepare($entity, array $field_info, array $instance, array $values) {
|
66
|
if (isset($values['arguments'])) {
|
67
|
$arguments = $values['arguments'];
|
68
|
unset($values['arguments']);
|
69
|
}
|
70
|
else {
|
71
|
$arguments = array();
|
72
|
}
|
73
|
|
74
|
$language = $this->getFieldLanguage($entity, $field_info, $arguments);
|
75
|
|
76
|
foreach ($values as $delta => $from) {
|
77
|
if (!empty($arguments['timezone'])) {
|
78
|
if (is_array($arguments['timezone'])) {
|
79
|
$timezone = $arguments['timezone'][$delta];
|
80
|
}
|
81
|
else {
|
82
|
$timezone = $arguments['timezone'];
|
83
|
}
|
84
|
}
|
85
|
else {
|
86
|
$timezone = 'UTC';
|
87
|
}
|
88
|
|
89
|
if (!empty($arguments['rrule'])) {
|
90
|
if (is_array($arguments['rrule'])) {
|
91
|
$rrule = $arguments['rrule'][$delta];
|
92
|
}
|
93
|
else {
|
94
|
$rrule = $arguments['rrule'];
|
95
|
}
|
96
|
}
|
97
|
else {
|
98
|
$rrule = NULL;
|
99
|
}
|
100
|
|
101
|
if (!empty($arguments['to'])) {
|
102
|
if (is_array($arguments['to'])) {
|
103
|
$to = $arguments['to'][$delta];
|
104
|
}
|
105
|
else {
|
106
|
$to = $arguments['to'];
|
107
|
}
|
108
|
}
|
109
|
else {
|
110
|
$to = NULL;
|
111
|
}
|
112
|
|
113
|
// Legacy support for JSON containing a set of properties - deprecated
|
114
|
// now that we have subfields.
|
115
|
if (!empty($from) && $from{0} == '{') {
|
116
|
$properties = drupal_json_decode($from);
|
117
|
$from = $properties['from'];
|
118
|
// Properties passed in with the date override any set via arguments.
|
119
|
if (!empty($properties['to'])) {
|
120
|
$to = $properties['to'];
|
121
|
}
|
122
|
if (!empty($properties['timezone'])) {
|
123
|
$timezone = $properties['timezone'];
|
124
|
}
|
125
|
if (!empty($properties['rrule'])) {
|
126
|
$rrule = $properties['rrule'];
|
127
|
}
|
128
|
}
|
129
|
|
130
|
// Missing data? Create an empty value and return;
|
131
|
// Don't try to turn the empty value into a bogus
|
132
|
// timestamp for 'now'.
|
133
|
if (empty($from)) {
|
134
|
$return[$language][$delta]['value'] = NULL;
|
135
|
$return[$language][$delta]['timezone'] = NULL;
|
136
|
if (!empty($field_info['settings']['todate'])) {
|
137
|
$return[$language][$delta]['value2'] = NULL;
|
138
|
}
|
139
|
return $return;
|
140
|
}
|
141
|
|
142
|
// If there is no 'to' date, just use the 'from' date.
|
143
|
if (!empty($field_info['settings']['todate']) && empty($to)) {
|
144
|
$to = $from;
|
145
|
}
|
146
|
|
147
|
// If we have a value, work from a timestamp.
|
148
|
$from = MigrationBase::timestamp($from);
|
149
|
if ($to) {
|
150
|
$to = MigrationBase::timestamp($to);
|
151
|
}
|
152
|
|
153
|
// What does the destination field expect?
|
154
|
switch ($field_info['type']) {
|
155
|
case 'datestamp':
|
156
|
// Already done.
|
157
|
break;
|
158
|
|
159
|
case 'datetime':
|
160
|
// YYYY-MM-DD HH:MM:SS.
|
161
|
$from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
|
162
|
if ($to) {
|
163
|
$to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
|
164
|
}
|
165
|
break;
|
166
|
|
167
|
case 'date':
|
168
|
// ISO date: YYYY-MM-DDTHH:MM:SS.
|
169
|
$from = format_date($from, 'custom', 'Y-m-d\TH:i:s', $timezone);
|
170
|
if ($to) {
|
171
|
$to = format_date($to, 'custom', 'Y-m-d\TH:i:s', $timezone);
|
172
|
}
|
173
|
break;
|
174
|
|
175
|
default:
|
176
|
break;
|
177
|
}
|
178
|
|
179
|
// Handle repeats, coming in as RRULEs. Many field instances may be
|
180
|
// created.
|
181
|
if (function_exists('date_repeat_build_dates') && !empty($field_info['settings']['repeat']) && $rrule) {
|
182
|
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
|
183
|
$item = array(
|
184
|
'value' => $from,
|
185
|
'value2' => $to,
|
186
|
'timezone' => $timezone,
|
187
|
);
|
188
|
// Can be de-uglified when http://drupal.org/node/1159404 is committed.
|
189
|
$return[$language] = date_repeat_build_dates(NULL, date_ical_parse_rrule($field_info, $rrule), $field_info, $item);
|
190
|
}
|
191
|
else {
|
192
|
$return[$language][$delta]['value'] = $from;
|
193
|
$return[$language][$delta]['timezone'] = $timezone;
|
194
|
if (!empty($to)) {
|
195
|
$return[$language][$delta]['value2'] = $to;
|
196
|
}
|
197
|
}
|
198
|
}
|
199
|
if (!isset($return)) {
|
200
|
$return = NULL;
|
201
|
}
|
202
|
return $return;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* {@inheritdoc}
|
207
|
*/
|
208
|
public function fields($migration = NULL) {
|
209
|
return array(
|
210
|
'timezone' => t('Timezone'),
|
211
|
'rrule' => t('Recurring event rule'),
|
212
|
'to' => t('End date date'),
|
213
|
);
|
214
|
}
|
215
|
}
|