You are here

class EntityListMigrateSource in CRM Core 7

Class EntityListMigrateSource which contains list of entities.

This class is wrapper around CiviCRM entities.

Hierarchy

Expanded class hierarchy of EntityListMigrateSource

File

modules/crm_core_data_import/plugins/source/CivicrmDataSourceHandler.inc, line 525

View source
class EntityListMigrateSource extends MigrateSource {
  protected $currentId;
  protected $entityList;
  protected $entityType;
  protected $bundle;

  /**
   * {@inheritdoc}
   */
  public function __construct($civicrm_entity_type, $civicrm_entity_bundle, $options = array()) {
    parent::__construct($options);
    $this->entityList = array();
    $this->entityType = $civicrm_entity_type;
    $this->bundle = $civicrm_entity_bundle;
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return t('Available %num entities', array(
      '%num' => $this
        ->computeCount(),
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function computeCount() {

    // @TODO should be improved.
    if ($this->entityType == 'Contact') {
      return crm_core_data_import_single_query("SELECT count(*) FROM civicrm_contact WHERE contact_type = '{$this->bundle}' AND is_deleted = 0");
    }
    if ($this->entityType == 'Activity') {
      $activity_type = crm_core_data_get_civicrm_activity_type($this->bundle);
      if (!empty($activity_type['value'])) {
        $activity_type_id = $activity_type['value'];
        return crm_core_data_import_single_query("SELECT count(*) FROM civicrm_activity WHERE activity_type_id = '{$activity_type_id}' AND is_deleted = 0");
      }
    }
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function performRewind() {
    $this->currentId = 0;

    // Set maximum items for get via CiviCRM API.
    $options = array(
      'rowCount' => PHP_INT_MAX,
    );
    $this
      ->attachBundleCriteria($options);

    // @TODO there we can use custom query to get all ids.
    // Extract ids.
    $entity_list = crm_core_data_import_civicrm_api($this->entityType, 'get', $options);
    $this->entityList = array_keys($entity_list);
    drupal_alter('crm_core_data_import_civicrm_source_entity_list', $this->entityList, $this->entityType, $this->bundle);
  }

  /**
   * {@inheritdoc}
   */
  public function getNextRow() {
    if (!empty($this->entityList[$this->currentId])) {
      $row = $this
        ->fetchItem();
      $this->currentId++;
      return $row;
    }
    else {
      return NULL;
    }
  }

  /**
   * Create object based on source data.
   */
  public function processObjectProperties($source_item) {
    $item = new stdClass();
    foreach ($source_item as $property_key => $value) {

      // Add field collection properties to source in expected format.
      // The format is property key with array of property values.
      if (isset($value['version'])) {
        list(, $field_name, ) = explode('.', $property_key);
        foreach ($value['values'] as $delta => $properties) {
          foreach ($properties as $property_key => $property_value) {
            $item->{$this->entityType . ':' . $this->bundle . ':' . $field_name . ':' . $property_key}[$delta] = $property_value;
          }
        }
      }
      else {
        $item->{$this->entityType . ':' . $this->bundle . ':' . $property_key} = $value;
      }
    }
    return $item;
  }

  /**
   * Attach filter by bundle to options.
   */
  public function attachBundleCriteria(&$options) {
    if ($this->entityType == 'Contact') {
      $options['contact_type'] = $this->bundle;
    }
    if ($this->entityType == 'Activity') {
      $activity_type = crm_core_data_get_civicrm_activity_type($this->bundle);
      $options['activity_type_id'] = $activity_type['value'];
    }
  }

  /**
   * Attach filter by bundle to options.
   */
  public function attachIdCriteria(&$options) {
    if ($this->entityType == 'Contact') {
      $options['contact_id'] = $this->entityList[$this->currentId];
    }
    if ($this->entityType == 'Activity') {
      $options['activity_id'] = $this->entityList[$this->currentId];
    }
  }

  /**
   * Fetch item from CiviCRM.
   */
  public function fetchItem() {
    $options = array();
    $this
      ->attachBundleCriteria($options);
    $this
      ->attachIdCriteria($options);
    if ($this->entityType == 'Contact') {
      foreach (_crm_core_data_import_contact_field_collection_fields() as $collection_item) {
        $options['api.' . $collection_item . '.get'] = 1;
      }
    }
    $custom_fields_options = $this
      ->attachCustomFields($options, $this->entityType);
    $core_fields = crm_core_data_import_civicrm_api($this->entityType, 'get', $options);
    $custom_fields = crm_core_data_import_civicrm_api($this->entityType, 'get', $custom_fields_options);

    // Need to check for empty data just in case to avoid exceptions.
    $current_id = $this->entityList[$this->currentId];
    $core_fields = !empty($core_fields[$current_id]) ? $core_fields[$current_id] : array();
    $custom_fields = !empty($custom_fields[$current_id]) ? $custom_fields[$current_id] : array();
    $item = array_merge($core_fields, $custom_fields);
    return $this
      ->processObjectProperties($item);
  }

  /**
   * Attach custom fields.
   */
  public function attachCustomFields($options, $civicrm_entity_type) {
    $fields = _crm_core_data_import_civicrm_get_custom_fields($civicrm_entity_type);
    $options['return'] = array_keys($fields);
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityListMigrateSource::$bundle protected property
EntityListMigrateSource::$currentId protected property
EntityListMigrateSource::$entityList protected property
EntityListMigrateSource::$entityType protected property
EntityListMigrateSource::attachBundleCriteria public function Attach filter by bundle to options.
EntityListMigrateSource::attachCustomFields public function Attach custom fields.
EntityListMigrateSource::attachIdCriteria public function Attach filter by bundle to options.
EntityListMigrateSource::computeCount public function
EntityListMigrateSource::fetchItem public function Fetch item from CiviCRM.
EntityListMigrateSource::fields public function Derived classes must implement fields(), returning a list of available source fields. Overrides MigrateSource::fields
EntityListMigrateSource::getNextRow public function
EntityListMigrateSource::performRewind public function
EntityListMigrateSource::processObjectProperties public function Create object based on source data.
EntityListMigrateSource::__construct public function Class constructor. Overrides MigrateSource::__construct
EntityListMigrateSource::__toString public function
MigrateSource::$activeMap protected property The MigrateMap class for the current migration.
MigrateSource::$activeMigration protected property The Migration class currently invoking us, during rewind() and next().
MigrateSource::$cacheCounts protected property Whether this instance should cache the source count.
MigrateSource::$cacheKey protected property Key to use for caching counts.
MigrateSource::$currentKey protected property The primary key of the current row
MigrateSource::$currentRow protected property The current row from the quey
MigrateSource::$highwaterField protected property Information on the highwater mark for the current migration, if any.
MigrateSource::$idList protected property List of source IDs to process.
MigrateSource::$mapRowAdded protected property By default, next() will directly read the map row and add it to the data row. A source plugin implementation may do this itself (in particular, the SQL source can incorporate the map table into the query) - if so, it should set this TRUE so we…
MigrateSource::$multikeySeparator protected property Used in the case of multiple key sources that need to use idlist.
MigrateSource::$numIgnored protected property Number of rows intentionally ignored (prepareRow() returned FALSE)
MigrateSource::$numProcessed protected property Number of rows we've at least looked at. 1
MigrateSource::$originalHighwater protected property The highwater mark at the beginning of the import operation.
MigrateSource::$skipCount protected property Whether this instance should not attempt to count the source.
MigrateSource::$trackChanges protected property If TRUE, we will maintain hashed source rows to determine whether incoming data has changed.
MigrateSource::count public function Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable.
MigrateSource::current public function Implementation of Iterator::current() - called when entering a loop iteration, returning the current row
MigrateSource::dataChanged protected function Determine whether this row has changed, and therefore whether it should be processed.
MigrateSource::getCurrentKey public function
MigrateSource::getIgnored public function
MigrateSource::getProcessed public function
MigrateSource::hash protected function Generate a hash of the source row. 3
MigrateSource::key public function Implementation of Iterator::key - called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentKey() is preferable.
MigrateSource::next public function Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process.
MigrateSource::prepareRow protected function Give the calling migration a shot at manipulating, and possibly rejecting, the source row.
MigrateSource::resetStats public function Reset numIgnored back to 0.
MigrateSource::rewind public function Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records.
MigrateSource::valid public function Implementation of Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it