You are here

class MigrateDestinationVotingapi in Migrate Extras 6.2

Same name and namespace in other branches
  1. 7.2 votingapi.inc \MigrateDestinationVotingapi

Destination class for the votingapi_vote table.

Hierarchy

Expanded class hierarchy of MigrateDestinationVotingapi

File

./votingapi.inc, line 12
VotingAPI module integration

View source
class MigrateDestinationVotingapi extends MigrateDestination {
  public function __toString() {
    return t('votingapi');
  }

  /**
   * An array with content ids of imported votes. Used for recalculating results.
   */
  var $importedIds = array();
  public static function getKeySchema() {
    return array(
      'vote_id' => array(
        'type' => 'int',
        'not null' => TRUE,
      ),
    );
  }

  /**
   * Delete the provided votes and recalculate the results.
   *
   * @param $id
   *  IDs to be deleted.
   */
  public function bulkRollback(array $ids) {
    migrate_instrument_start(__METHOD__);
    $votes = db_select('votingapi_vote', 'vv')
      ->fields('vv', array(
      'content_id',
      'content_type',
    ))
      ->condition('vote_id', $ids, 'IN')
      ->execute();
    db_delete('votingapi_vote')
      ->condition('vote_id', $ids, 'IN') > execute();

    // foreach($votes as $vote) {
    //       votingapi_recalculate_results($vote['content_type'], $vote['content_id'], TRUE);
    //     }
    migrate_instrument_stop(__METHOD__);
  }

  /**
   * Import a single vote.
   *
   * @param $entity
   *  Object 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 of the object that was saved if
   *  successful. FALSE on failure.
   */
  public function import(stdClass $entity, stdClass $row) {
    $this
      ->prepare($entity, $row);
    if (isset($entity->timestamp)) {
      $entity->timestamp = Migration::timestamp($entity->timestamp);
    }

    // Just converting $entity to an array doesn't work...
    $vote = array();
    $vote['content_type'] = $entity->content_type;
    $vote['content_id'] = $entity->content_id;
    $vote['value_type'] = $entity->value_type;
    $vote['value'] = $entity->value;
    $vote['uid'] = $entity->uid;
    $vote['timestamp'] = $entity->timestamp;
    $vote['vote_source'] = $entity->vote_source;
    votingapi_add_votes($vote);
    if (isset($vote[0]['vote_id'])) {
      $entity->vote_id = $vote[0]['vote_id'];
      $this
        ->complete($entity, $row);
      $this->importedIds[$entity->content_type][] = $entity->content_id;
      return array(
        $entity->vote_id,
      );
    }
    else {
      return FALSE;
    }
  }

  /**
   * We're done with importing votes, recalculate the results.
   */
  function postImport() {
    $this->importedIds = array_unique($this->importedIds);
    foreach ($this->importedIds as $content_type => $content_ids) {
      foreach ($content_ids as $content_id) {
        votingapi_recalculate_results($content_type, $content_id, TRUE);
      }
    }
  }

  /**
   * Returns a list of fields available to be mapped/
   *
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields() {
    return array(
      'vote_id' => 'Vote ID',
      'content_type' => "Content Type (defaults to 'node')",
      'content_id' => 'Content ID',
      'value' => 'Numeric vote value',
      'value_type' => "Value type (percent/points, defaults to 'percent')",
      'tag' => "Tag (defaults to 'vote')",
      'uid' => 'User ID',
      'timestamp' => 'Timestamp',
      'vote_source' => 'Vote Source IP Address',
    );
  }

  /**
   * Give handlers a shot at modifying the object before saving it.
   *
   * @param $entity
   *  Entity object to build. Prefilled with any fields mapped in the Migration.
   * @param $source_row
   *  Raw source data object - passed through to prepare handlers.
   */
  public function prepare(stdClass $entity, stdClass $source_row) {
    $migration = Migration::currentMigration();
    $entity->migrate = array(
      'machineName' => $migration
        ->getMachineName(),
    );

    // Call any prepare handler for this specific Migration.
    if (method_exists($migration, 'prepare')) {
      $migration
        ->prepare($entity, $source_row);
    }
  }

  /**
   * Give handlers a shot at modifying the object (or taking additional action)
   * after saving it.
   *
   * @param $object
   *  Entity object to build. This is the complete object after saving.
   * @param $source_row
   *  Raw source data object - passed through to complete handlers.
   */
  public function complete(stdClass $entity, stdClass $source_row) {
    $migration = Migration::currentMigration();

    // Call any complete handler for this specific Migration.
    if (method_exists($migration, 'complete')) {
      $migration
        ->complete($entity, $source_row);
    }
  }

}

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.
MigrateDestination::__construct public function Null constructor 3
MigrateDestinationVotingapi::$importedIds property An array with content ids of imported votes. Used for recalculating results.
MigrateDestinationVotingapi::bulkRollback public function Delete the provided votes and recalculate the results.
MigrateDestinationVotingapi::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationVotingapi::fields public function Returns a list of fields available to be mapped/ Overrides MigrateDestination::fields
MigrateDestinationVotingapi::getKeySchema public static function
MigrateDestinationVotingapi::import public function Import a single vote. Overrides MigrateDestination::import
MigrateDestinationVotingapi::postImport function We're done with importing votes, recalculate the results.
MigrateDestinationVotingapi::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationVotingapi::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString