1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for date.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets().
|
10
|
*/
|
11
|
function date_feeds_processor_targets($entity_type, $bundle_name) {
|
12
|
$targets = array();
|
13
|
|
14
|
$field_types = array(
|
15
|
'date' => TRUE,
|
16
|
'datestamp' => TRUE,
|
17
|
'datetime' => TRUE,
|
18
|
);
|
19
|
|
20
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
21
|
$info = field_info_field($name);
|
22
|
|
23
|
if (!isset($field_types[$info['type']])) {
|
24
|
continue;
|
25
|
}
|
26
|
|
27
|
$targets[$name . ':start'] = array(
|
28
|
'name' => check_plain($instance['label']),
|
29
|
'callback' => 'date_feeds_set_target',
|
30
|
'description' => t('The start date for the @name field. Also use if mapping both start and end.', array('@name' => $instance['label'])),
|
31
|
'real_target' => $name,
|
32
|
'summary_callbacks' => array('date_feeds_summary_callback'),
|
33
|
'form_callbacks' => array('date_feeds_form_callback'),
|
34
|
);
|
35
|
|
36
|
if (!empty($info['settings']['todate'])) {
|
37
|
// Change the label for the start date.
|
38
|
$targets[$name . ':start']['name'] = t('@name: Start', array('@name' => $instance['label']));
|
39
|
|
40
|
$targets[$name . ':end'] = array(
|
41
|
'name' => t('@name: End', array('@name' => $instance['label'])),
|
42
|
'callback' => 'date_feeds_set_target',
|
43
|
'description' => t('The end date for the @name field.', array('@name' => $instance['label'])),
|
44
|
'real_target' => $name,
|
45
|
'summary_callbacks' => array('date_feeds_summary_callback'),
|
46
|
'form_callbacks' => array('date_feeds_form_callback'),
|
47
|
);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
return $targets;
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Callback for setting date values.
|
56
|
*/
|
57
|
function date_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
|
58
|
$language = $mapping['language'];
|
59
|
list($target, $sub_field) = explode(':', $target, 2);
|
60
|
|
61
|
$value_key = $sub_field === 'start' ? 'value' : 'value2';
|
62
|
$offset_key = $sub_field === 'start' ? 'offset' : 'offset2';
|
63
|
|
64
|
$field = isset($entity->$target) ? $entity->$target : array($language => array());
|
65
|
|
66
|
$info = field_info_field($target);
|
67
|
$format = date_type_format($info['type']);
|
68
|
|
69
|
$db_tz = new DateTimeZone(date_get_timezone_db($info['settings']['tz_handling']));
|
70
|
$default_tz = new DateTimeZone(_date_feeds_get_default_timezone($mapping));
|
71
|
|
72
|
$delta = 0;
|
73
|
foreach ($values as $value) {
|
74
|
$value = _date_feeds_get_date_object($value, $default_tz);
|
75
|
|
76
|
if (!$value || !empty($value->errors)) {
|
77
|
$field[$language][$delta][$value_key] = NULL;
|
78
|
}
|
79
|
else {
|
80
|
if (!isset($field[$language][$delta]['timezone'])) {
|
81
|
$timezone = $value->getTimezone()->getName();
|
82
|
if ($timezone == 'Z') {
|
83
|
// PHP < 5.5.10 doesn't know about the Zulu time zone (which equals
|
84
|
// the UTC time zone). Set timezone to "UTC".
|
85
|
$timezone = 'UTC';
|
86
|
}
|
87
|
$field[$language][$delta]['timezone'] = $timezone;
|
88
|
}
|
89
|
|
90
|
$value->setTimezone($db_tz);
|
91
|
|
92
|
$field[$language][$delta][$value_key] = $value->format($format, TRUE);
|
93
|
$field[$language][$delta][$offset_key] = $value->getOffset();
|
94
|
|
95
|
// Ensure that both value keys always exist to prevent php notices in
|
96
|
// date_field_validate().
|
97
|
if (!array_key_exists('value', $field[$language][$delta])) {
|
98
|
$field[$language][$delta]['value'] = NULL;
|
99
|
}
|
100
|
if (!array_key_exists('value2', $field[$language][$delta])) {
|
101
|
$field[$language][$delta]['value2'] = NULL;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
$delta++;
|
106
|
}
|
107
|
|
108
|
$entity->$target = $field;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Summary callback for date field targets.
|
113
|
*/
|
114
|
function date_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
115
|
$mapping += array('timezone' => 'UTC');
|
116
|
|
117
|
$options = _date_feeds_timezone_options();
|
118
|
|
119
|
return t('Default timezone: %zone', array('%zone' => $options[$mapping['timezone']]));
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Form callback for date field targets.
|
124
|
*/
|
125
|
function date_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
126
|
$mapping += array('timezone' => 'UTC');
|
127
|
|
128
|
return array(
|
129
|
'timezone' => array(
|
130
|
'#type' => 'select',
|
131
|
'#title' => t('Timezone handling'),
|
132
|
'#options' => _date_feeds_timezone_options(),
|
133
|
'#default_value' => $mapping['timezone'],
|
134
|
'#description' => t('This value will only be used if the timezone is mising.'),
|
135
|
),
|
136
|
);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Returns the timezone options.
|
141
|
*
|
142
|
* @return array
|
143
|
* A map of timezone options.
|
144
|
*/
|
145
|
function _date_feeds_timezone_options() {
|
146
|
return array(
|
147
|
'__SITE__' => t('Site default'),
|
148
|
) + system_time_zones();
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Returns the timezone to be used as the default.
|
153
|
*
|
154
|
* @param array $mapping
|
155
|
* The mapping array.
|
156
|
*
|
157
|
* @return string
|
158
|
* The timezone to use as the default.
|
159
|
*/
|
160
|
function _date_feeds_get_default_timezone(array $mapping) {
|
161
|
$mapping += array('timezone' => 'UTC');
|
162
|
|
163
|
if ($mapping['timezone'] === '__SITE__') {
|
164
|
return variable_get('date_default_timezone', 'UTC');
|
165
|
}
|
166
|
|
167
|
return $mapping['timezone'];
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Converts a date string or object into a DateObject.
|
172
|
*
|
173
|
* @param DateTime|string|int $value
|
174
|
* The date value or object.
|
175
|
* @param DateTimeZone $default_tz
|
176
|
* The default timezone.
|
177
|
*
|
178
|
* @return DateObject
|
179
|
* The converted DateObject.
|
180
|
*/
|
181
|
function _date_feeds_get_date_object($value, DateTimeZone $default_tz) {
|
182
|
if ($value instanceof DateObject) {
|
183
|
return $value;
|
184
|
}
|
185
|
|
186
|
// Convert DateTime and FeedsDateTime.
|
187
|
if ($value instanceof DateTime) {
|
188
|
if (!$value->getTimezone() || !preg_match('/[a-zA-Z]/', $value->getTimezone()->getName())) {
|
189
|
$value->setTimezone($default_tz);
|
190
|
}
|
191
|
return new DateObject($value->format(DATE_FORMAT_ISO), $value->getTimezone());
|
192
|
}
|
193
|
|
194
|
if (is_string($value) || is_object($value) && method_exists($value, '__toString')) {
|
195
|
$value = trim($value);
|
196
|
}
|
197
|
|
198
|
// Filter out meaningless values.
|
199
|
if (empty($value) || !is_string($value) && !is_int($value)) {
|
200
|
return FALSE;
|
201
|
}
|
202
|
|
203
|
// Support year values.
|
204
|
if ((string) $value === (string) (int) $value) {
|
205
|
if ($value >= variable_get('date_min_year', 100) && $value <= variable_get('date_max_year', 4000)) {
|
206
|
return new DateObject('January ' . $value, $default_tz);
|
207
|
}
|
208
|
}
|
209
|
|
210
|
return new DateObject($value, $default_tz);
|
211
|
}
|