date.migrate.inc 5.9 KB

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