You are here

class MigrateDestinationCRMCoreContact in CRM Core 7

CRM Contact destination handler.

Hierarchy

Expanded class hierarchy of MigrateDestinationCRMCoreContact

File

modules/crm_core_contact/includes/crm_core_contact.migrate.inc, line 11
Class MigrateDestinationCRMCoreContact.

View source
class MigrateDestinationCRMCoreContact extends MigrateDestinationEntity {

  /**
   * Provides key schema.
   *
   * @return array
   *   Key schema.
   */
  public static function getKeySchema() {
    return array(
      'contact_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'The primary identifier for a contact.',
      ),
    );
  }

  /**
   * Return an options array for contact destinations.
   *
   * @param string $language
   *   Default language for contacts created via this destination class.
   * @param string $text_format
   *   Default text format for contacts created via this destination class.
   *
   * @return array
   *   Options.
   */
  public static function options($language, $text_format) {
    return compact('language', 'text_format');
  }

  /**
   * Constructor.
   *
   * @param string $bundle
   *   Bundle.
   * @param array $options
   *   Options.
   */
  public function __construct($bundle, array $options = array()) {
    parent::__construct('crm_core_contact', $bundle, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function fields($migration = NULL) {
    $fields = array();

    // First the core (contact table) properties.
    $fields['contact_id'] = t('CRM Core Contact: Existing Contact ID');
    $fields['created'] = t('Created timestamp');
    $fields['changed'] = t('Modified timestamp');
    $fields['uid'] = t('Authored by (uid)');
    $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
    return $fields;
  }

  /**
   * Delete a batch of contacts at once.
   *
   * @param array $contact_ids
   *   Array of contact IDs to be deleted.
   */
  public function bulkRollback(array $contact_ids) {
    migrate_instrument_start('crm_core_contact_delete_multiple');
    $this
      ->prepareRollback($contact_ids);
    crm_core_contact_delete_multiple($contact_ids);
    $this
      ->completeRollback($contact_ids);
    migrate_instrument_stop('crm_core_contact_delete_multiple');
  }

  /**
   * Import a single contact.
   *
   * @param object $contact
   *   Contact object to import.
   *   Pre-filled with any fields mapped in the Migration.
   * @param object $row
   *   Raw source data object - passed through to prepare/complete handlers.
   *
   * @return array
   *   Array of key fields (contact_id only in this case) of the contact that
   *   was saved if successful. FALSE on failure.
   *
   * @throws MigrateException
   */
  public function import(stdClass $contact, stdClass $row) {
    $migration = Migration::currentMigration();
    if (isset($row->migrate_map_destid1)) {

      // This is an update to an existing contact.
      if (isset($contact->contact_id)) {
        if ($contact->contact_id != $row->migrate_map_destid1) {
          $params = array(
            '@contact_id' => $contact->contact_id,
            '@destid1' => $row->migrate_map_destid1,
          );
          $message = t('Incoming contact ID "@contact_id" and map destination contact ID "@destid1" do not match.', $params);
          throw new MigrateException($message);
        }
      }
      else {
        $contact->contact_id = $row->migrate_map_destid1;
      }
    }
    if ($migration
      ->getSystemOfRecord() == Migration::DESTINATION) {
      if (!isset($contact->contact_id)) {
        throw new MigrateException(t('System-of-record is DESTINATION, but no destination contact_id provided'));
      }
      $old_contact = crm_core_contact_load($contact->contact_id);
      if (empty($old_contact)) {
        $params = array(
          '@contact_id' => $contact->contact_id,
        );
        $message = t('System-of-record is DESTINATION, but contact with ID "@contact_id" does not exist.', $params);
        throw new MigrateException($message);
      }
    }
    if (!isset($contact->type)) {

      // Default the type to our designated destination bundle (by doing this
      // conditionally, we permit some flexibility in terms of implementing
      // migrations which can affect more than one type).
      $contact->type = $this->bundle;
    }

    // Set default properties.
    if ($migration
      ->getSystemOfRecord() == Migration::SOURCE) {
      if (!isset($contact->created)) {
        $contact->created = REQUEST_TIME;
      }
    }
    $this
      ->prepare($contact, $row);
    if (!empty($contact->contact_id)) {
      $updating = TRUE;
    }
    else {
      $updating = FALSE;
    }
    migrate_instrument_start('contact_save');
    crm_core_contact_save($contact);
    migrate_instrument_stop('contact_save');
    if (!empty($contact->contact_id)) {
      if ($updating) {
        $this->numUpdated++;
      }
      else {
        $this->numCreated++;
      }
      $return = array(
        $contact->contact_id,
      );
    }
    else {
      $return = FALSE;
    }
    $this
      ->complete($contact, $row);
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationCRMCoreContact::bulkRollback public function Delete a batch of contacts at once.
MigrateDestinationCRMCoreContact::fields public function Derived classes must implement fields(), returning a list of available destination fields. Overrides MigrateDestination::fields
MigrateDestinationCRMCoreContact::getKeySchema public static function Provides key schema.
MigrateDestinationCRMCoreContact::import public function Import a single contact. Overrides MigrateDestination::import
MigrateDestinationCRMCoreContact::options public static function Return an options array for contact destinations.
MigrateDestinationCRMCoreContact::__construct public function Constructor. Overrides MigrateDestinationEntity::__construct
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::array_flatten public static function Flattens an array of allowed values.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString