View source
<?php
if (!class_exists('MigrateFieldHandler')) {
return;
}
class DateMigrateFieldHandler extends MigrateFieldHandler {
public function __construct() {
$this
->registerTypes(array(
'date',
'datestamp',
'datetime',
));
}
public static function arguments($timezone = 'UTC', $timezone_db = 'UTC', $rrule = NULL, $language = NULL) {
return get_defined_vars();
}
public function prepare($entity, array $field_info, array $instance, array $values) {
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
else {
$arguments = array();
}
$language = $this
->getFieldLanguage($entity, $field_info, $arguments);
foreach ($values as $delta => $from) {
if (!empty($arguments['timezone'])) {
if (is_array($arguments['timezone'])) {
$timezone = $arguments['timezone'][$delta];
}
else {
$timezone = $arguments['timezone'];
}
}
else {
$timezone = 'UTC';
}
if (!empty($arguments['rrule'])) {
if (is_array($arguments['rrule'])) {
$rrule = $arguments['rrule'][$delta];
}
else {
$rrule = $arguments['rrule'];
}
}
else {
$rrule = NULL;
}
if (!empty($arguments['to'])) {
if (is_array($arguments['to'])) {
$to = $arguments['to'][$delta];
}
else {
$to = $arguments['to'];
}
}
else {
$to = NULL;
}
if (!empty($from[0]) && $from[0] == '{') {
$properties = drupal_json_decode($from);
$from = $properties['from'];
if (!empty($properties['to'])) {
$to = $properties['to'];
}
if (!empty($properties['timezone'])) {
$timezone = $properties['timezone'];
}
if (!empty($properties['rrule'])) {
$rrule = $properties['rrule'];
}
}
if (empty($from)) {
$return[$language][$delta]['value'] = NULL;
$return[$language][$delta]['timezone'] = NULL;
if (!empty($field_info['settings']['todate'])) {
$return[$language][$delta]['value2'] = NULL;
}
return $return;
}
if (!empty($field_info['settings']['todate']) && empty($to)) {
$to = $from;
}
$from = MigrationBase::timestamp($from);
if ($to) {
$to = MigrationBase::timestamp($to);
}
switch ($field_info['type']) {
case 'datestamp':
break;
case 'datetime':
$from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
if ($to) {
$to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
}
break;
case 'date':
$from = format_date($from, 'custom', 'Y-m-d\\TH:i:s', $timezone);
if ($to) {
$to = format_date($to, 'custom', 'Y-m-d\\TH:i:s', $timezone);
}
break;
}
if (function_exists('date_repeat_build_dates') && !empty($field_info['settings']['repeat']) && $rrule) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
$item = array(
'value' => $from,
'value2' => $to,
'timezone' => $timezone,
);
$return[$language] = date_repeat_build_dates(NULL, date_ical_parse_rrule($field_info, $rrule), $field_info, $item);
}
else {
$return[$language][$delta]['value'] = $from;
$return[$language][$delta]['timezone'] = $timezone;
if (!empty($to)) {
$return[$language][$delta]['value2'] = $to;
}
}
}
if (!isset($return)) {
$return = NULL;
}
return $return;
}
public function fields($migration = NULL) {
return array(
'timezone' => t('Timezone'),
'rrule' => t('Recurring event rule'),
'to' => t('End date date'),
);
}
}