Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date.migrate.inc @ 1f142f4f

1
<?php
2

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

    
8
/**
9
 * Implements hook_migrate_api().
10
 */
11
function date_migrate_api() {
12
  $api = array(
13
    'api' => 2,
14
    'field handlers' => array('DateMigrateFieldHandler'),
15
  );
16
  return $api;
17
}
18

    
19
class DateMigrateFieldHandler extends MigrateFieldHandler {
20

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

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

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

    
71
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
72

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

    
86
      if (!empty($arguments['rrule'])) {
87
        if (is_array($arguments['rrule'])) {
88
          $rrule = $arguments['rrule'][$delta];
89
        }
90
        else {
91
          $rrule = $arguments['rrule'];
92
        }
93
      }
94
      else {
95
        $rrule = NULL;
96
      }
97

    
98
      if (!empty($arguments['to'])) {
99
        if (is_array($arguments['to'])) {
100
          $to = $arguments['to'][$delta];
101
        }
102
        else {
103
          $to = $arguments['to'];
104
        }
105
      }
106
      else {
107
        $to = NULL;
108
      }
109

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

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

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

    
144
      // If we have a value, work from a timestamp.
145
      $from = MigrationBase::timestamp($from);
146
      if ($to) {
147
        $to = MigrationBase::timestamp($to);
148
      }
149

    
150
      // What does the destination field expect?
151
      switch ($field_info['type']) {
152
        case 'datestamp':
153
          // Already done.
154
          break;
155

    
156
        case 'datetime':
157
          // YYYY-MM-DD HH:MM:SS.
158
          $from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
159
          if ($to) {
160
            $to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
161
          }
162
          break;
163

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

    
172
        default:
173
          break;
174
      }
175

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

    
202
  /**
203
   * {@inheritdoc}
204
   */
205
  public function fields($migration = NULL) {
206
    return array(
207
      'timezone' => t('Timezone'),
208
      'rrule' => t('Recurring event rule'),
209
      'to' => t('End date date'),
210
    );
211
  }
212
}