date.migrate.inc 5.8 KB

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