Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date.migrate.inc @ 87dbc3bf

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
  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
        if (!empty($field_info['settings']['todate'])) {
133
          $return[$language][$delta]['value2'] = NULL;
134
        }
135
        return $return;
136
      }
137

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

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

    
149
      // What does the destination field expect?
150
      switch ($field_info['type']) {
151
        case 'datestamp':
152
          // Already done.
153
          break;
154
        case 'datetime':
155
          // YYYY-MM-DD HH:MM:SS.
156
          $from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
157
          if ($to) {
158
            $to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
159
          }
160
          break;
161
        case 'date':
162
          // ISO date: YYYY-MM-DDTHH:MM:SS.
163
          $from = format_date($from, 'custom', 'Y-m-d\TH:i:s', $timezone);
164
          if ($to) {
165
            $to = format_date($to, 'custom', 'Y-m-d\TH:i:s', $timezone);
166
          }
167
          break;
168
        default:
169
          break;
170
      }
171

    
172
      // Handle repeats, coming in as RRULEs. Many field instances may be
173
      // created.
174
      if (function_exists('date_repeat_build_dates') && !empty($field_info['settings']['repeat']) && $rrule) {
175
        include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
176
        $item = array('value' => $from, 'value2' => $to, 'timezone' => $timezone);
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
        if (!empty($to)) {
183
          $return[$language][$delta]['value2'] = $to;
184
        }
185
      }
186
    }
187
    if (!isset($return)) {
188
      $return = NULL;
189
    }
190
    return $return;
191
  }
192

    
193
  public function fields($migration = NULL) {
194
    return array(
195
      'timezone' => t('Timezone'),
196
      'rrule' => t('Recurring event rule'),
197
      'to' => t('End date date'),
198
    );
199
  }
200
}