From fbd06637eebb544d4980aef163a56f6c33d9e2af Mon Sep 17 00:00:00 2001
From: arknoll <arknoll@853506.no-reply.drupal.org>
Date: Fri, 1 Mar 2013 08:43:26 -0700
Subject: [PATCH 1/2] Issue #1781538 by arknoll: redhen contact migrate

---
 modules/redhen_contact/redhen_contact.info |    1 +
 1 file changed, 1 insertion(+)

diff --git a/modules/redhen_contact/redhen_contact.info b/modules/redhen_contact/redhen_contact.info
index 07edb2b..59c7e1c 100644
--- a/modules/redhen_contact/redhen_contact.info
+++ b/modules/redhen_contact/redhen_contact.info
@@ -13,6 +13,7 @@ files[] = lib/redhen_contact.metadata.inc
 files[] = lib/redhen_contact.views.inc
 files[] = lib/redhen_contact_type.entity.inc
 files[] = lib/redhen_contact_type.ui_controller.inc
+files[] = lib/redhen_contact.migrate.inc
 
 ; Tests
 ;files[] = tests/redhen_contact.test
-- 
1.7.10.2 (Apple Git-33)


From 6a76e32247b04285ba4186cc41a5c97a6008ba6b Mon Sep 17 00:00:00 2001
From: arknoll <arknoll@853506.no-reply.drupal.org>
Date: Fri, 1 Mar 2013 08:48:11 -0700
Subject: [PATCH 2/2] Issue #1781538 by arknoll: redhen contact migrate

---
 .../redhen_contact/lib/redhen_contact.migrate.inc  |  192 ++++++++++++++++++++
 1 file changed, 192 insertions(+)
 create mode 100644 modules/redhen_contact/lib/redhen_contact.migrate.inc

diff --git a/modules/redhen_contact/lib/redhen_contact.migrate.inc b/modules/redhen_contact/lib/redhen_contact.migrate.inc
new file mode 100644
index 0000000..cc54357
--- /dev/null
+++ b/modules/redhen_contact/lib/redhen_contact.migrate.inc
@@ -0,0 +1,192 @@
+<?php
+
+/**
+ * @file
+ * Support for the Migrate API.
+ *
+ * Your field collection migration should be run after the host entity
+ * migration. For example, if the collection is attached to nodes via a field
+ * named 'field_attached_data', and if the nodes are being imported by
+ * ArticleMigration, your collection migration class constructor should look
+ * like:
+ *
+ * @code
+ * class TestContactMigration extends DynamicMigration {
+ *   public function __construct() {
+ *   parent::__construct();
+ * 
+ *   $query = ...
+ * 
+ *   $this->source = new MigrateSourceSQL($query);
+ *   
+ *    // the "Machine name" of the redhen contact BUNDLE
+ *   $this->destination = new MigrateDestinationRedhenContact('master');
+ * 
+ *   $this->map = new MigrateSQLMap($this->machineName,
+ *       array('PK_in_old_db' => array(
+ *             'type' => 'int',
+ *             'not null' => TRUE,
+ *             'description' => 'Old PK.'
+ *            )
+ *       ),
+ *       MigrateDestinationRedhenContact::getKeySchema()
+ *   );
+ * 
+ * 
+ *   $this->addFieldMapping('first_name', 'Pers_Fname');
+ *   $this->addFieldMapping('last_name', 'Pers_Lname');
+ * 
+ *   public function prepare($entity, $row) {
+ *    $entity->redhen_contact_email['und'][0]['value'] = $email;
+ *    $entity->redhen_contact_email['und'][0]['options']['label_id'] = 0;
+ *    $entity->redhen_contact_email['und'][0]['options']['hold'] = 0;
+ *    $entity->redhen_contact_email['und'][0]['options']['bulk'] = 0;
+ *    $entity->redhen_contact_email['und'][0]['options']['default'] = ($i==0) ? 1 : 0;
+ *  }
+ * }
+ * @endcode
+ *
+ * @see http://drupal.org/node/1900640
+ */
+
+/**
+ * Destination class implementing migration into field_collection.
+ */
+class MigrateDestinationRedhenContact extends MigrateDestinationEntity {
+  /**
+   * The type of entity hosting this collection field (e.g., node).
+   *
+   * @var string
+   */
+  static public function getKeySchema() {
+    return array(
+      'contact_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => 'ID of contact',
+      ),
+    );
+  }
+
+  /**
+   * Basic initialization.
+   *
+   * @param array $options
+   *  Options applied to collections.
+   */
+  public function __construct($bundle, array $options = array()) {
+    parent::__construct('redhen_contact', $bundle, $options);
+    //$this->hostEntityType = $options['host_entity_type'];
+  }
+
+  /**
+   * Returns a list of fields available to be mapped for this collection
+   * (bundle).
+   *
+   * @return array
+   *  Keys: machine names of the fields (to be passed to addFieldMapping).
+   *  Values: Human-friendly descriptions of the fields.
+   */
+  public function fields() {
+    $fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
+    //$fields['host_entity_id'] = t('Field collection host ID');
+    return $fields;
+  }
+
+  /**
+   * Import a single contact item.
+   *
+   * @param $contact
+   *  Contact object to build. Pre-filled with any fields mapped in the
+   *  migration.
+   * @param $row
+   *  Raw source data object - passed through to prepare/complete handlers.
+   *
+   * @return array|false
+   *  Array of key fields (item_id only in this case) of the contact that was
+   *  saved or FALSE on failure.
+   */
+  public function import(stdClass $contact, stdClass $row) {
+    if (isset($row->migrate_map_destid1)) {
+      // We're updated an existing entity - start from the previous data.
+      // entity_load() returns an array, so we get the contact entity
+      // with array_shift().
+      $entity = array_shift(entity_load('redhen_contact', array($row->migrate_map_destid1), array(), TRUE));
+      $entity_old = clone $entity;
+      $updating = TRUE;
+    }
+    else {
+      // Skip the contact if it has no host.
+      $entity = entity_create('redhen_contact', array('type' => $this->bundle));
+      $updating = FALSE;
+    }
+
+    foreach ((array) $contact as $field => $value) {
+      $entity->{$field} = $value;
+    }
+
+    $this->prepare($entity, $row);
+
+    // Restore fields from original contact if updating
+    if ($updating) {
+      foreach ($entity as $field => $value) {
+        if ('field_' != substr($field, 0, 6)) {
+          continue;
+        }
+        elseif (isset($entity_old->$field) && !isset($contact->$field)) {
+          $entity->$field = $entity_old->$field;
+        }
+      }
+    }
+
+    migrate_instrument_start('redhen_contact_save');
+    $status = entity_save('redhen_contact', $entity);
+    migrate_instrument_stop('redhen_contact_save');
+
+    if ($status !== FALSE) {
+      $this->complete($entity, $row);
+      if ($updating) {
+        $this->numUpdated++;
+      }
+      else {
+        $this->numCreated++;
+      }
+      return array($entity->contact_id);
+    }
+    else {
+      return FALSE;
+    }
+  }
+
+  /**
+   * Delete a migrated contact.
+   *
+   * @param $key
+   *  Array of fields representing the key.
+   */
+  public function rollback(array $key) {
+    $item_id = reset($key);
+
+    $this->prepareRollback($item_id);
+    $contact = redhen_contact_load($item_id);
+    // If the contact wasn't imported then we can't roll it back, so check if
+    // the loaded object is an instance of the RedhenContact class.
+    if ($contact instanceof RedhenContact) {
+      $contact->delete();
+    }
+
+    $this->completeRollback($item_id);
+    return TRUE;
+  }
+}
+
+/**
+ * Implements migrate hook_migrate_api().
+ */
+function redhen_contact_migrate_api() {
+  $api = array(
+    'api' => 2,
+  );
+  return $api;
+}
\ No newline at end of file
-- 
1.7.10.2 (Apple Git-33)

