You are here

class MigrateDestinationCRMCoreActivity in CRM Core 7

Hierarchy

Expanded class hierarchy of MigrateDestinationCRMCoreActivity

File

modules/crm_core_activity/includes/crm_core_activity.migrate.inc, line 3

View source
class MigrateDestinationCRMCoreActivity extends MigrateDestinationEntity {
  public static function getKeySchema() {
    return array(
      'activity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'The primary identifer for an activity.',
      ),
    );
  }

  /**
   * Return an options array for activity destinations.
   *
   * @param string $language
   *  Default language for activities created via this destination class.
   * @param string $text_format
   *  Default text format for activities created via this destination class.
   */
  public static function options($language, $text_format) {
    return compact('language', 'text_format');
  }
  public function __construct($bundle, array $options = array()) {
    parent::__construct('crm_core_activity', $bundle, $options);
  }

  /**
   * Returns a list of fields available to be mapped for the activity type (bundle)
   *
   * @param Migration $migration
   *  Optionally, the migration containing this destination.
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields($migration = NULL) {
    $fields = array();

    // First the core (activity table) properties
    $fields['activity_id'] = t('CRM Core Activity: Existing Activity 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 activities at once.
   *
   * @param $activity_ids
   *  Array of activity IDs to be deleted.
   */
  public function bulkRollback(array $activity_ids) {
    migrate_instrument_start('crm_core_activity_delete_multiple');
    $this
      ->prepareRollback($activity_ids);
    crm_core_activity_delete_multiple($activity_ids);
    $this
      ->completeRollback($activity_ids);
    migrate_instrument_stop('crm_core_activity_delete_multiple');
  }

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

      // This is an update to an existing contact.
      if (isset($activity->activity_id)) {
        if ($activity->activity_id != $row->migrate_map_destid1) {
          $string = "Incoming activity_id !activity_id and map destination activity_id !destid1 don't match";
          $params = array(
            '!activity_id' => $activity->activity_id,
            '!destid1' => $row->migrate_map_destid1,
          );
          throw new MigrateException(t($string), $params);
        }
      }
      else {
        $activity->activity_id = $row->migrate_map_destid1;
      }
    }
    if ($migration
      ->getSystemOfRecord() == Migration::DESTINATION) {
      if (!isset($activity->activity_id)) {
        throw new MigrateException(t('System-of-record is DESTINATION, but no destination activity_id provided'));
      }
      $old_activity = crm_core_activity_load($activity->activity_id);
      if (empty($old_activity)) {
        $string = 'System-of-record is DESTINATION, but activity !activity_id does not exist';
        $params = array(
          '!activity_id' => $activity->activity_id,
        );
        throw new MigrateException(t($string), $params);
      }
    }
    if (!isset($activity->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).
      $activity->type = $this->bundle;
    }

    // Set default properties.
    if ($migration
      ->getSystemOfRecord() == Migration::SOURCE) {
      if (!isset($activity->created)) {
        $activity->created = REQUEST_TIME;
      }
    }
    $this
      ->prepare($activity, $row);
    if (isset($activity->activity_id)) {
      $updating = TRUE;
    }
    else {
      $updating = FALSE;
    }
    migrate_instrument_start('activity_save');
    crm_core_activity_save($activity);
    migrate_instrument_stop('activity_save');
    if (isset($activity->activity_id)) {
      if ($updating) {
        $this->numUpdated++;
      }
      else {
        $this->numCreated++;
      }
      $return = array(
        $activity->activity_id,
      );
    }
    else {
      $return = FALSE;
    }
    $this
      ->complete($activity, $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.
MigrateDestinationCRMCoreActivity::bulkRollback public function Delete a batch of activities at once.
MigrateDestinationCRMCoreActivity::fields public function Returns a list of fields available to be mapped for the activity type (bundle) Overrides MigrateDestination::fields
MigrateDestinationCRMCoreActivity::getKeySchema public static function
MigrateDestinationCRMCoreActivity::import public function Import a single activity. Overrides MigrateDestination::import
MigrateDestinationCRMCoreActivity::options public static function Return an options array for activity destinations.
MigrateDestinationCRMCoreActivity::__construct public function Simply save the key schema. 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