summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/date/date_migrate/date.migrate.inc
blob: 637a626bbb8127a93f9699e51d128c2b673cf2c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 * @file
 * Support for migration into Date fields.
 */

class DateMigrateFieldHandler extends MigrateFieldHandler {

  /**
   * Declares the types of fields used.
   */
  public function __construct() {
    $this->registerTypes(array('date', 'datestamp', 'datetime'));
  }

  /**
   * Arguments for a date field migration.
   *
   * @param string $timezone
   *   Timezone (such as UTC, America/New_York, etc.) to apply.
   * @param string $timezone_db
   *   Timezone_db value for the field.
   * @param string $rrule
   *   Rule string for a repeating date field.
   * @param string $language
   *   Language of the text (defaults to destination language)
   *
   * @return array
   *   An array of the defined variables in this scope.
   */
  static function arguments($timezone = 'UTC', $timezone_db = 'UTC', $rrule = NULL, $language = NULL) {
    return get_defined_vars();
  }

  /**
   * Converts incoming data into the proper field arrays for Date fields.
   *
   * @param object $entity
   *   The destination entity which will hold the field arrays.
   * @param array $field_info
   *   Metadata for the date field being populated.
   * @param array $instance
   *   Metadata for this instance of the date field being populated.
   * @param array $values
   *   Array of date values to be fielded.
   *
   * @return array|null
   *   An array of date fields.
   */
  public function prepare($entity, array $field_info, array $instance, array $values) {
    if (isset($values['arguments'])) {
      $arguments = $values['arguments'];
      unset($values['arguments']);
    }
    else {
      $arguments = array();
    }

    if (isset($arguments['timezone'])) {
      $default_timezone = $arguments['timezone'];
    }
    else {
      $default_timezone = 'UTC';
    }
    if (isset($arguments['timezone_db'])) {
      $default_timezone_db = $arguments['timezone_db'];
    }
    else {
      $default_timezone_db = NULL;
    }
    if (isset($arguments['rrule'])) {
      $default_rrule = $arguments['rrule'];
    }
    else {
      $default_rrule = NULL;
    }
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);

    // Setup the standard Field API array for saving.
    $delta = 0;
    foreach ($values as $from) {
      // Set defaults.
      $to = NULL;
      $timezone = $default_timezone;
      $timezone_db = $default_timezone_db;
      $rrule = $default_rrule;

      // Is the value a straight datetime value, or JSON containing a set of
      // properties?
      if (!empty($from) && $from{0} == '{') {
        $properties = drupal_json_decode($from);
        $from = $properties['from'];
        // Properties passed in with the date override any set via arguments.
        if (!empty($properties['to'])) {
          $to = $properties['to'];
        }
        if (!empty($properties['timezone'])) {
          $timezone = $properties['timezone'];
        }
        if (!empty($properties['timezone_db'])) {
          $timezone_db = $properties['timezone_db'];
        }
        if (!empty($properties['rrule'])) {
          $rrule = $properties['rrule'];
        }
      }

      // Missing data? Create an empty value and return;
      // Don't try to turn the empty value into a bogus
      // timestamp for 'now'.
      if (empty($from)) {
        $return[$language][$delta]['value'] = NULL;
        if (!empty($field_info['settings']['todate'])) {
          $return[$language][$delta]['value2'] = NULL;
        }
        return $return;
      }

      // If there is no 'to' date, just use the 'from' date.
      if (!empty($field_info['settings']['todate']) && empty($to)) {
        $to = $from;
      }

      // If we have a value, work from a timestamp.
      $from = MigrationBase::timestamp($from);
      if ($to) {
        $to = MigrationBase::timestamp($to);
      }

      // What does the destination field expect?
      switch ($field_info['type']) {
        case 'datestamp':
          // Already done.
          break;
        case 'datetime':
          // YYYY-MM-DD HH:MM:SS.
          $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':
          // ISO date: YYYY-MM-DDTHH:MM:SS.
          $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;
        default:
          break;
      }

      // Handle repeats, coming in as RRULEs. Many field instances may be
      // created.
      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);
        // Can be de-uglified when http://drupal.org/node/1159404 is committed.
        $return[$language] = date_repeat_build_dates(NULL, date_ical_parse_rrule($field_info, $rrule), $field_info, $item);
      }
      else {
        $return[$language][$delta]['value'] = $from;
        if (!empty($to)) {
          $return[$language][$delta]['value2'] = $to;
        }
      }
      $delta++;
    }
    if (!isset($return)) {
      $return = NULL;
    }
    return $return;
  }
}