profile2.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @file
  4. * Support for profile2 destinations.
  5. */
  6. // TODO:
  7. // Make sure this works with updates, explicit destination keys
  8. /**
  9. * Destination class implementing migration into nodes.
  10. */
  11. class MigrateDestinationProfile2 extends MigrateDestinationEntity {
  12. var $entity_type = 'profile2';
  13. var $entity_info = NULL;
  14. var $entity_key = NULL;
  15. static public function getKeySchema() {
  16. $key = 'pid'; // Hard coded since $this->entity_key not in object context.
  17. return array(
  18. $key => array(
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => TRUE,
  22. ),
  23. );
  24. }
  25. /**
  26. * Return an options array for profile2 destinations.
  27. *
  28. * @param string $language
  29. * Default language for profiles created via this destination class.
  30. * @param string $text_format
  31. * Default text format for profiles created via this destination class.
  32. */
  33. static public function options($language, $text_format) {
  34. return compact('language', 'text_format');
  35. }
  36. /**
  37. * Basic initialization
  38. *
  39. * @param string $bundle
  40. * A.k.a. the profile type.
  41. * @param array $options
  42. * Options applied to profiles.
  43. */
  44. public function __construct($bundle, array $options = array()) {
  45. parent::__construct($this->entity_type, $bundle, $options);
  46. $this->entity_info = entity_get_info('profile2');
  47. $this->entity_key = $this->entity_info['entity keys']['id'];
  48. }
  49. /**
  50. * Returns a list of fields available to be mapped for the node type (bundle)
  51. *
  52. * @return array
  53. * Keys: machine names of the fields (to be passed to addFieldMapping)
  54. * Values: Human-friendly descriptions of the fields.
  55. */
  56. public function fields() {
  57. $fields = array();
  58. $type = ucfirst($this->entity_type) . ': ';
  59. // First the core (node table) properties
  60. $fields[$this->entity_key] = $type . t('Existing profile ID');
  61. $fields['uid'] = $type . t('Authored by (uid)');
  62. $fields['revision_uid'] = $type . t('Modified (uid)');
  63. //$fields['created'] = $type . t('Created timestamp');
  64. //$fields['changed'] = $type . t('Modified timestamp');
  65. //$fields['status'] = $type . t('Published');
  66. //$fields['revision'] = $type . t('Create new revision');
  67. $fields['language'] = $type . t('Language (fr, en, ...)');
  68. // Then add in anything provided by handlers
  69. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
  70. $fields += migrate_handler_invoke_all('Profile2', 'fields', $this->entityType, $this->bundle);
  71. return $fields;
  72. }
  73. /**
  74. * Delete a batch of profiles at once.
  75. *
  76. * @param $ids
  77. * Array of profile IDs to be deleted.
  78. */
  79. public function bulkRollback(array $ids) {
  80. migrate_instrument_start('profile2_delete_multiple');
  81. $this->prepareRollback($ids);
  82. entity_delete_multiple($this->entity_type, $ids);
  83. $this->completeRollback($ids);
  84. migrate_instrument_stop('profile2_delete_multiple');
  85. }
  86. /**
  87. * Import a single entity.
  88. *
  89. * @param $entity
  90. * Entity object to build. Prefilled with any fields mapped in the Migration.
  91. * @param $row
  92. * Raw source data object - passed through to prepare/complete handlers.
  93. * @return array
  94. * Array of key fields of the entity that was saved if
  95. * successful. FALSE on failure.
  96. */
  97. public function import(stdClass $entity, stdClass $row) {
  98. $migration = Migration::currentMigration();
  99. $type = $this->entity_info['entity keys']['bundle'];
  100. $entity->$type = $this->bundle;
  101. list($id, $vid, $bundle) = entity_extract_ids($this->entity_type, $entity);
  102. // Updating previously-migrated content?
  103. if (isset($row->migrate_map_destid1)) {
  104. // Make sure is_new is off
  105. $entity->is_new = FALSE;
  106. if (!empty($id)) {
  107. if ($id != $row->migrate_map_destid1) {
  108. throw new MigrateException(t("Incoming id !id and map destination id !destid1 don't match",
  109. array('!id' => $id, '!destid1' => $row->migrate_map_destid1)));
  110. }
  111. }
  112. else {
  113. $entity->{$this->entity_key} = $row->migrate_map_destid1;
  114. }
  115. }
  116. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  117. if (empty($id)) {
  118. throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
  119. }
  120. $old_entity = entity_load_single($this->entity_type, $id);
  121. if (!isset($entity->created)) {
  122. $entity->created = $old_entity->created;
  123. }
  124. if (!isset($entity->uid)) {
  125. $entity->uid = $old_entity->uid;
  126. }
  127. }
  128. // Invoke migration prepare handlers
  129. $this->prepare($entity, $row);
  130. // Trying to update an existing entity
  131. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  132. // Incoming data overrides existing data.
  133. foreach ($entity as $field => $value) {
  134. $old_entity->$field = $value;
  135. }
  136. // Use the loaded entity from now on.
  137. $entity = $old_entity;
  138. }
  139. else {
  140. // Create a full profile class.
  141. $entity = entity_create($this->entity_type, (array) $entity);
  142. }
  143. if (empty($id) && !(isset($entity->is_new) && $entity->is_new)) {
  144. $updating = TRUE;
  145. }
  146. else {
  147. $updating = FALSE;
  148. }
  149. migrate_instrument_start('entity_save');
  150. entity_save($this->entity_type, $entity);
  151. migrate_instrument_stop('entity_save');
  152. list($id, $vid, $bundle) = entity_extract_ids($this->entity_type, $entity);
  153. if (isset($id)) {
  154. if ($updating) {
  155. $this->numUpdated++;
  156. }
  157. else {
  158. $this->numCreated++;
  159. }
  160. $return = array($id);
  161. }
  162. else {
  163. $return = FALSE;
  164. }
  165. $this->complete($entity, $row);
  166. return $return;
  167. }
  168. }