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] = '';
|
78
|
}
|
79
|
else {
|
80
|
if (!isset($field[$language][$delta]['timezone'])) {
|
81
|
$field[$language][$delta]['timezone'] = $value->getTimezone()->getName();
|
82
|
}
|
83
|
|
84
|
$value->setTimezone($db_tz);
|
85
|
|
86
|
$field[$language][$delta][$value_key] = $value->format($format, TRUE);
|
87
|
$field[$language][$delta][$offset_key] = $value->getOffset();
|
88
|
}
|
89
|
|
90
|
$delta++;
|
91
|
}
|
92
|
|
93
|
$entity->$target = $field;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Summary callback for date field targets.
|
98
|
*/
|
99
|
function date_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
100
|
$mapping += array('timezone' => 'UTC');
|
101
|
|
102
|
$options = _date_feeds_timezone_options();
|
103
|
|
104
|
return t('Default timezone: %zone', array('%zone' => $options[$mapping['timezone']]));
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Form callback for date field targets.
|
109
|
*/
|
110
|
function date_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
111
|
$mapping += array('timezone' => 'UTC');
|
112
|
|
113
|
return array(
|
114
|
'timezone' => array(
|
115
|
'#type' => 'select',
|
116
|
'#title' => t('Timezone handling'),
|
117
|
'#options' => _date_feeds_timezone_options(),
|
118
|
'#default_value' => $mapping['timezone'],
|
119
|
'#description' => t('This value will only be used if the timezone is mising.'),
|
120
|
),
|
121
|
);
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Returns the timezone options.
|
126
|
*
|
127
|
* @return array
|
128
|
* A map of timezone options.
|
129
|
*/
|
130
|
function _date_feeds_timezone_options() {
|
131
|
return array(
|
132
|
'__SITE__' => t('Site default'),
|
133
|
) + system_time_zones();
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Returns the timezone to be used as the default.
|
138
|
*
|
139
|
* @param array $mapping
|
140
|
* The mapping array.
|
141
|
*
|
142
|
* @return string
|
143
|
* The timezone to use as the default.
|
144
|
*/
|
145
|
function _date_feeds_get_default_timezone(array $mapping) {
|
146
|
$mapping += array('timezone' => 'UTC');
|
147
|
|
148
|
if ($mapping['timezone'] === '__SITE__') {
|
149
|
return variable_get('date_default_timezone', 'UTC');
|
150
|
}
|
151
|
|
152
|
return $mapping['timezone'];
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Converts a date string or object into a DateObject.
|
157
|
*
|
158
|
* @param DateTime|string|int $value
|
159
|
* The date value or object.
|
160
|
* @param DateTimeZone $default_tz
|
161
|
* The default timezone.
|
162
|
*
|
163
|
* @return DateObject
|
164
|
* The converted DateObject.
|
165
|
*/
|
166
|
function _date_feeds_get_date_object($value, DateTimeZone $default_tz) {
|
167
|
if ($value instanceof DateObject) {
|
168
|
return $value;
|
169
|
}
|
170
|
|
171
|
// Convert DateTime and FeedsDateTime.
|
172
|
if ($value instanceof DateTime) {
|
173
|
if (!$value->getTimezone() || !preg_match('/[a-zA-Z]/', $value->getTimezone()->getName())) {
|
174
|
$value->setTimezone($default_tz);
|
175
|
}
|
176
|
return new DateObject($value->format(DATE_FORMAT_ISO), $value->getTimezone());
|
177
|
}
|
178
|
|
179
|
if (is_string($value) || is_object($value) && method_exists($value, '__toString')) {
|
180
|
$value = trim($value);
|
181
|
}
|
182
|
|
183
|
// Filter out meaningless values.
|
184
|
if (empty($value) || !is_string($value) && !is_int($value)) {
|
185
|
return FALSE;
|
186
|
}
|
187
|
|
188
|
// Support year values.
|
189
|
if ((string) $value === (string) (int) $value) {
|
190
|
if ($value >= variable_get('date_min_year', 100) && $value <= variable_get('date_max_year', 4000)) {
|
191
|
return new DateObject('January ' . $value, $default_tz);
|
192
|
}
|
193
|
}
|
194
|
|
195
|
return new DateObject($value, $default_tz);
|
196
|
}
|