Projet

Général

Profil

Paste
Télécharger (5,75 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.migrate.inc @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Support for migration into Date fields.
6
 */
7

    
8
if (!class_exists('MigrateFieldHandler')) {
9
  return;
10
}
11

    
12
/**
13
 * Support for migration into Date fields.
14
 */
15
class DateMigrateFieldHandler extends MigrateFieldHandler {
16

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

    
24
  /**
25
   * Arguments for a date field migration.
26
   *
27
   * @param string $timezone
28
   *   Timezone (such as UTC, America/New_York, etc.) to apply.
29
   * @param string $timezone_db
30
   *   Timezone_db value for the field.
31
   * @param string $rrule
32
   *   Rule string for a repeating date field.
33
   * @param string $language
34
   *   Language of the text (defaults to destination language)
35
   *
36
   * @return array
37
   *   An array of the defined variables in this scope.
38
   */
39
  public static function arguments($timezone = 'UTC', $timezone_db = 'UTC', $rrule = NULL, $language = NULL) {
40
    return get_defined_vars();
41
  }
42

    
43
  /**
44
   * Converts incoming data into the proper field arrays for Date fields.
45
   *
46
   * @param object $entity
47
   *   The destination entity which will hold the field arrays.
48
   * @param array $field_info
49
   *   Metadata for the date field being populated.
50
   * @param array $instance
51
   *   Metadata for this instance of the date field being populated.
52
   * @param array $values
53
   *   Array of date values to be fielded.
54
   *
55
   * @return array|null
56
   *   An array of date fields.
57
   */
58
  public function prepare($entity, array $field_info, array $instance, array $values) {
59
    if (isset($values['arguments'])) {
60
      $arguments = $values['arguments'];
61
      unset($values['arguments']);
62
    }
63
    else {
64
      $arguments = array();
65
    }
66

    
67
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
68

    
69
    foreach ($values as $delta => $from) {
70
      if (!empty($arguments['timezone'])) {
71
        if (is_array($arguments['timezone'])) {
72
          $timezone = $arguments['timezone'][$delta];
73
        }
74
        else {
75
          $timezone = $arguments['timezone'];
76
        }
77
      }
78
      else {
79
        $timezone = 'UTC';
80
      }
81

    
82
      if (!empty($arguments['rrule'])) {
83
        if (is_array($arguments['rrule'])) {
84
          $rrule = $arguments['rrule'][$delta];
85
        }
86
        else {
87
          $rrule = $arguments['rrule'];
88
        }
89
      }
90
      else {
91
        $rrule = NULL;
92
      }
93

    
94
      if (!empty($arguments['to'])) {
95
        if (is_array($arguments['to'])) {
96
          $to = $arguments['to'][$delta];
97
        }
98
        else {
99
          $to = $arguments['to'];
100
        }
101
      }
102
      else {
103
        $to = NULL;
104
      }
105

    
106
      // Legacy support for JSON containing a set of properties - deprecated
107
      // now that we have subfields.
108
      if (!empty($from) && $from[0] == '{') {
109
        $properties = drupal_json_decode($from);
110
        $from = $properties['from'];
111
        // Properties passed in with the date override any set via arguments.
112
        if (!empty($properties['to'])) {
113
          $to = $properties['to'];
114
        }
115
        if (!empty($properties['timezone'])) {
116
          $timezone = $properties['timezone'];
117
        }
118
        if (!empty($properties['rrule'])) {
119
          $rrule = $properties['rrule'];
120
        }
121
      }
122

    
123
      // Missing data? Create an empty value and return; Don't try to turn the
124
      // empty value into a bogus timestamp for 'now'.
125
      if (empty($from)) {
126
        $return[$language][$delta]['value'] = NULL;
127
        $return[$language][$delta]['timezone'] = NULL;
128
        if (!empty($field_info['settings']['todate'])) {
129
          $return[$language][$delta]['value2'] = NULL;
130
        }
131
        return $return;
132
      }
133

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

    
139
      // If we have a value, work from a timestamp.
140
      $from = MigrationBase::timestamp($from);
141
      if ($to) {
142
        $to = MigrationBase::timestamp($to);
143
      }
144

    
145
      // What does the destination field expect?
146
      switch ($field_info['type']) {
147
        case 'datestamp':
148
          // Already done.
149
          break;
150

    
151
        case 'datetime':
152
          // YYYY-MM-DD HH:MM:SS.
153
          $from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
154
          if ($to) {
155
            $to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
156
          }
157
          break;
158

    
159
        case 'date':
160
          // ISO date: YYYY-MM-DDTHH:MM:SS.
161
          $from = format_date($from, 'custom', 'Y-m-d\TH:i:s', $timezone);
162
          if ($to) {
163
            $to = format_date($to, 'custom', 'Y-m-d\TH:i:s', $timezone);
164
          }
165
          break;
166
      }
167

    
168
      // Handle repeats, coming in as RRULEs. Many field instances may be
169
      // created.
170
      if (function_exists('date_repeat_build_dates') && !empty($field_info['settings']['repeat']) && $rrule) {
171
        include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
172
        $item = array(
173
          'value' => $from,
174
          'value2' => $to,
175
          'timezone' => $timezone,
176
        );
177
        // Can be de-uglified when http://drupal.org/node/1159404 is committed.
178
        $return[$language] = date_repeat_build_dates(NULL, date_ical_parse_rrule($field_info, $rrule), $field_info, $item);
179
      }
180
      else {
181
        $return[$language][$delta]['value'] = $from;
182
        $return[$language][$delta]['timezone'] = $timezone;
183
        if (!empty($to)) {
184
          $return[$language][$delta]['value2'] = $to;
185
        }
186
      }
187
    }
188
    if (!isset($return)) {
189
      $return = NULL;
190
    }
191
    return $return;
192
  }
193

    
194
  /**
195
   * {@inheritdoc}
196
   */
197
  public function fields($migration = NULL) {
198
    return array(
199
      'timezone' => t('Timezone'),
200
      'rrule' => t('Recurring event rule'),
201
      'to' => t('End date date'),
202
    );
203
  }
204

    
205
}