Projet

Général

Profil

Paste
Télécharger (4,76 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Examples and test folder for migration into date fields.
6
 */
7

    
8
/**
9
 * Migration class to test import of various date fields.
10
 */
11
class DateExampleMigration extends XMLMigration {
12

    
13
  /**
14
   * Sets up the migration.
15
   */
16
  public function __construct() {
17
    parent::__construct();
18
    $this->description = t('Example migration into date fields');
19

    
20
    $this->map = new MigrateSQLMap($this->machineName,
21
      array(
22
        'id' => array(
23
          'type' => 'int',
24
          'unsigned' => TRUE,
25
          'not null' => TRUE,
26
          'description' => 'Date ID',
27
        ),
28
      ),
29
      MigrateDestinationNode::getKeySchema()
30
    );
31

    
32
    // Source fields available in the XML file.
33
    $fields = array(
34
      'id' => t('Source id'),
35
      'title' => t('Title'),
36
      'body' => t('Description'),
37
      'date' => t('A simple date'),
38
      'date_range_from' => t('Start value for a date range'),
39
      'datestamp' => t('Simple datestamp'),
40
      'datestamp_range_from' => t('Start value for a datestamp range'),
41
      'datetime' => t('Simple datetime'),
42
      'datetime_range_from' => t('Start value for a datetime range'),
43
      'date_repeat' => t('Sample of a repeating date field'),
44
    );
45

    
46
    // Our test data is in an XML file.
47
    $xml_folder = drupal_get_path('module', 'date_migrate_test');
48
    $items_url = $xml_folder . '/example_data.xml';
49
    $item_xpath = '/source_data/item';
50
    $item_id_xpath = 'id';
51
    $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_id_xpath);
52
    $this->source = new MigrateSourceMultiItems($items_class, $fields);
53
    $this->destination = new MigrateDestinationNode('date_test_feature');
54

    
55
    // Basic fields.
56
    $this->addFieldMapping('title', 'title')
57
      ->xpath('title');
58
    $this->addFieldMapping('uid')
59
      ->defaultValue(1);
60
    $this->addFieldMapping('body', 'body')
61
      ->xpath('body');
62

    
63
    // For simple date fields, we just need the xpath.
64
    $this->addFieldMapping('field_date', 'date')
65
      ->xpath('date');
66

    
67
    // For date ranges, we add the "end" value in prepareRow() below.
68
    $this->addFieldMapping('field_date_range', 'date_range_from');
69
    $this->addFieldMapping('field_date_range:to', 'date_range_to');
70

    
71
    // RRULEs on repeat fields are also done in prepareRow().
72
    $this->addFieldMapping('field_date_repeat', 'date_repeat');
73
    $this->addFieldMapping('field_date_repeat:rrule', 'date_repeat_rrule');
74

    
75
    $this->addFieldMapping('field_datestamp', 'datestamp')
76
      ->xpath('datestamp');
77
    $this->addFieldMapping('field_datestamp_range', 'datestamp_range_from');
78
    $this->addFieldMapping('field_datestamp_range:to', 'datestamp_range_to');
79

    
80
    // You can specify a timezone to be applied to all values going into the
81
    // field (Tokyo is UTC+9, no DST).
82
    $this->addFieldMapping('field_datetime', 'datetime')
83
      ->xpath('datetime');
84
    $this->addFieldMapping('field_datetime:timezone')
85
      ->defaultValue('Asia/Tokyo');
86

    
87
    // You can also get the timezone from the source data - it can be different
88
    // for each instance of the field. Like To and RRULE values, it is added
89
    // in prepareRow().
90
    $this->addFieldMapping('field_datetime_range', 'datetime_range_from');
91
    $this->addFieldMapping('field_datetime_range:to', 'datetime_range_to');
92
    $this->addFieldMapping('field_datetime_range:timezone', 'datetime_range_timezone');
93

    
94
    // Unmapped destination fields.
95
    $this->addUnmigratedDestinations(array('is_new', 'status', 'promote',
96
      'revision', 'language', 'sticky', 'created', 'changed', 'revision_uid'));
97
  }
98

    
99
  /**
100
   * Transforms the raw migration data into the expected date formats.
101
   *
102
   * An advanced feature of the date field handler is that in addition to the
103
   * basic (Start) date itself, we can add additional properties like timezone,
104
   * encapsulating them as JSON.
105
   */
106
  public function prepareRow($current_row) {
107
    // The date range field can have multiple values.
108
    $current_row->date_range_from = array();
109
    foreach ($current_row->xml->date_range as $range) {
110
      $current_row->date_range_from[] = (string) $range->from[0];
111
      $current_row->date_range_to[] = (string) $range->to[0];
112
    }
113

    
114
    $current_row->datestamp_range_from
115
      = (string) $current_row->xml->datestamp_range->from[0];
116
    $current_row->datestamp_range_to
117
      = (string) $current_row->xml->datestamp_range->to[0];
118

    
119
    $current_row->datetime_range_from
120
      = (string) $current_row->xml->datetime_range->from[0];
121
    $current_row->datetime_range_to
122
      = (string) $current_row->xml->datetime_range->to[0];
123
    $current_row->datetime_range_timezone
124
      = (string) $current_row->xml->datetime_range->timezone[0];
125

    
126
    $current_row->date_repeat
127
      = (string) $current_row->xml->date_repeat->date[0];
128
    $current_row->date_repeat_rrule
129
      = (string) $current_row->xml->date_repeat->rule[0];
130
  }
131

    
132
}