date.migrate.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Support for migration into Date fields.
  5. */
  6. class DateMigrateFieldHandler extends MigrateFieldHandler {
  7. /**
  8. * Declares the types of fields used.
  9. */
  10. public function __construct() {
  11. $this->registerTypes(array('date', 'datestamp', 'datetime'));
  12. }
  13. /**
  14. * Arguments for a date field migration.
  15. *
  16. * @param string $timezone
  17. * Timezone (such as UTC, America/New_York, etc.) to apply.
  18. * @param string $timezone_db
  19. * Timezone_db value for the field.
  20. * @param string $rrule
  21. * Rule string for a repeating date field.
  22. * @param string $language
  23. * Language of the text (defaults to destination language)
  24. *
  25. * @return array
  26. * An array of the defined variables in this scope.
  27. */
  28. static function arguments($timezone = 'UTC', $timezone_db = 'UTC', $rrule = NULL, $language = NULL) {
  29. return get_defined_vars();
  30. }
  31. /**
  32. * Converts incoming data into the proper field arrays for Date fields.
  33. *
  34. * @param object $entity
  35. * The destination entity which will hold the field arrays.
  36. * @param array $field_info
  37. * Metadata for the date field being populated.
  38. * @param array $instance
  39. * Metadata for this instance of the date field being populated.
  40. * @param array $values
  41. * Array of date values to be fielded.
  42. *
  43. * @return array|null
  44. * An array of date fields.
  45. */
  46. public function prepare($entity, array $field_info, array $instance, array $values) {
  47. if (isset($values['arguments'])) {
  48. $arguments = $values['arguments'];
  49. unset($values['arguments']);
  50. }
  51. else {
  52. $arguments = array();
  53. }
  54. if (isset($arguments['timezone'])) {
  55. $default_timezone = $arguments['timezone'];
  56. }
  57. else {
  58. $default_timezone = 'UTC';
  59. }
  60. if (isset($arguments['timezone_db'])) {
  61. $default_timezone_db = $arguments['timezone_db'];
  62. }
  63. else {
  64. $default_timezone_db = NULL;
  65. }
  66. if (isset($arguments['rrule'])) {
  67. $default_rrule = $arguments['rrule'];
  68. }
  69. else {
  70. $default_rrule = NULL;
  71. }
  72. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  73. // Setup the standard Field API array for saving.
  74. $delta = 0;
  75. foreach ($values as $from) {
  76. // Set defaults.
  77. $to = NULL;
  78. $timezone = $default_timezone;
  79. $timezone_db = $default_timezone_db;
  80. $rrule = $default_rrule;
  81. // Is the value a straight datetime value, or JSON containing a set of
  82. // properties?
  83. if (!empty($from) && $from{0} == '{') {
  84. $properties = drupal_json_decode($from);
  85. $from = $properties['from'];
  86. // Properties passed in with the date override any set via arguments.
  87. if (!empty($properties['to'])) {
  88. $to = $properties['to'];
  89. }
  90. if (!empty($properties['timezone'])) {
  91. $timezone = $properties['timezone'];
  92. }
  93. if (!empty($properties['timezone_db'])) {
  94. $timezone_db = $properties['timezone_db'];
  95. }
  96. if (!empty($properties['rrule'])) {
  97. $rrule = $properties['rrule'];
  98. }
  99. }
  100. // Missing data? Create an empty value and return;
  101. // Don't try to turn the empty value into a bogus
  102. // timestamp for 'now'.
  103. if (empty($from)) {
  104. $return[$language][$delta]['value'] = NULL;
  105. if (!empty($field_info['settings']['todate'])) {
  106. $return[$language][$delta]['value2'] = NULL;
  107. }
  108. return $return;
  109. }
  110. // If there is no 'to' date, just use the 'from' date.
  111. if (!empty($field_info['settings']['todate']) && empty($to)) {
  112. $to = $from;
  113. }
  114. // If we have a value, work from a timestamp.
  115. $from = MigrationBase::timestamp($from);
  116. if ($to) {
  117. $to = MigrationBase::timestamp($to);
  118. }
  119. // What does the destination field expect?
  120. switch ($field_info['type']) {
  121. case 'datestamp':
  122. // Already done.
  123. break;
  124. case 'datetime':
  125. // YYYY-MM-DD HH:MM:SS.
  126. $from = format_date($from, 'custom', 'Y-m-d H:i:s', $timezone);
  127. if ($to) {
  128. $to = format_date($to, 'custom', 'Y-m-d H:i:s', $timezone);
  129. }
  130. break;
  131. case 'date':
  132. // ISO date: YYYY-MM-DDTHH:MM:SS.
  133. $from = format_date($from, 'custom', 'Y-m-d\TH:i:s', $timezone);
  134. if ($to) {
  135. $to = format_date($to, 'custom', 'Y-m-d\TH:i:s', $timezone);
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. // Handle repeats, coming in as RRULEs. Many field instances may be
  142. // created.
  143. if (function_exists('date_repeat_build_dates') && !empty($field_info['settings']['repeat']) && $rrule) {
  144. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  145. $item = array('value' => $from, 'value2' => $to, 'timezone' => $timezone);
  146. // Can be de-uglified when http://drupal.org/node/1159404 is committed.
  147. $return[$language] = date_repeat_build_dates(NULL, date_ical_parse_rrule($field_info, $rrule), $field_info, $item);
  148. }
  149. else {
  150. $return[$language][$delta]['value'] = $from;
  151. if (!empty($to)) {
  152. $return[$language][$delta]['value2'] = $to;
  153. }
  154. }
  155. $delta++;
  156. }
  157. if (!isset($return)) {
  158. $return = NULL;
  159. }
  160. return $return;
  161. }
  162. }