date.migrate.inc 5.9 KB

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