votingapi.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * VotingAPI module integration
  6. */
  7. /**
  8. * Destination class for the votingapi_vote table.
  9. */
  10. class MigrateDestinationVotingapi extends MigrateDestination {
  11. public function __toString() {
  12. return t('votingapi');
  13. }
  14. /**
  15. * An array with content ids of imported votes. Used for recalculating results.
  16. */
  17. var $importedIds = array();
  18. static public function getKeySchema() {
  19. return array(
  20. 'vote_id' => array(
  21. 'type' => 'int',
  22. 'not null' => TRUE,
  23. ),
  24. );
  25. }
  26. /**
  27. * Delete the provided votes and recalculate the results.
  28. *
  29. * @param $id
  30. * IDs to be deleted.
  31. */
  32. public function bulkRollback(array $ids) {
  33. migrate_instrument_start(__METHOD__);
  34. foreach ($ids as $id) {
  35. $votes[]['vote_id'] = $id;
  36. }
  37. votingapi_delete_votes($votes);
  38. // foreach($votes as $vote) {
  39. // votingapi_recalculate_results($vote['content_type'], $vote['content_id'], TRUE);
  40. // }
  41. migrate_instrument_stop(__METHOD__);
  42. }
  43. /**
  44. * Import a single vote.
  45. *
  46. * @param $entity
  47. * Object object to build. Prefilled with any fields mapped in the Migration.
  48. * @param $row
  49. * Raw source data object - passed through to prepare/complete handlers.
  50. * @return array
  51. * Array of key fields of the object that was saved if
  52. * successful. FALSE on failure.
  53. */
  54. public function import(stdClass $entity, stdClass $row) {
  55. $this->prepare($entity, $row);
  56. // Votes have to be assigned to an entity.
  57. if (empty($entity->entity_id)) {
  58. watchdog('VotingAPI Import', 'Skipped import due to empty entity_id for vote import: @serial', array('@serial' => json_encode($row)), WATCHDOG_ERROR);
  59. return FALSE;
  60. }
  61. if (isset($entity->timestamp)) {
  62. $entity->timestamp = Migration::timestamp($entity->timestamp);
  63. }
  64. // Just converting $entity to an array doesn't work...
  65. $vote = array();
  66. $vote['entity_type'] = $entity->entity_type;
  67. $vote['entity_id'] = $entity->entity_id;
  68. $vote['value_type'] = $entity->value_type;
  69. $vote['value'] = $entity->value;
  70. $vote['uid'] = $entity->uid;
  71. $vote['tag'] = $entity->tag;
  72. $vote['timestamp'] = $entity->timestamp;
  73. $vote['vote_source'] = $entity->vote_source;
  74. votingapi_add_votes($vote);
  75. if (isset($vote[0]['vote_id'])) {
  76. $entity->vote_id = $vote[0]['vote_id'];
  77. $this->complete($entity, $row);
  78. $this->importedIds[$entity->entity_type][] = $entity->entity_id;
  79. return array($entity->vote_id);
  80. }
  81. else {
  82. return FALSE;
  83. }
  84. }
  85. /**
  86. * We're done with importing votes, recalculate the results.
  87. */
  88. function postImport() {
  89. foreach ($this->importedIds as $entity_type => $entity_ids) {
  90. $this->importedIds = array_unique($this->importedIds);
  91. foreach ($entity_ids as $entity_id) {
  92. votingapi_recalculate_results($entity_type, $entity_id, TRUE);
  93. }
  94. }
  95. }
  96. /**
  97. * Returns a list of fields available to be mapped/
  98. *
  99. * @return array
  100. * Keys: machine names of the fields (to be passed to addFieldMapping)
  101. * Values: Human-friendly descriptions of the fields.
  102. */
  103. public function fields() {
  104. return array(
  105. 'vote_id' => 'Vote ID',
  106. 'entity_type' => "Entity Type (defaults to 'node')",
  107. 'entity_id' => 'Entity ID',
  108. 'value' => 'Numeric vote value',
  109. 'value_type' => "Value type (percent/points, defaults to 'percent')",
  110. 'tag' => "Tag (defaults to 'vote')",
  111. 'uid' => 'User ID',
  112. 'timestamp' => 'Timestamp',
  113. 'vote_source' => 'Vote Source IP Address',
  114. );
  115. }
  116. /**
  117. * Give handlers a shot at modifying the object before saving it.
  118. *
  119. * @param $entity
  120. * Entity object to build. Prefilled with any fields mapped in the Migration.
  121. * @param $source_row
  122. * Raw source data object - passed through to prepare handlers.
  123. */
  124. public function prepare(stdClass $entity, stdClass $source_row) {
  125. $migration = Migration::currentMigration();
  126. $entity->migrate = array(
  127. 'machineName' => $migration->getMachineName(),
  128. );
  129. // Call any prepare handler for this specific Migration.
  130. if (method_exists($migration, 'prepare')) {
  131. $migration->prepare($entity, $source_row);
  132. }
  133. }
  134. /**
  135. * Give handlers a shot at modifying the object (or taking additional action)
  136. * after saving it.
  137. *
  138. * @param $object
  139. * Entity object to build. This is the complete object after saving.
  140. * @param $source_row
  141. * Raw source data object - passed through to complete handlers.
  142. */
  143. public function complete(stdClass $entity, stdClass $source_row) {
  144. $migration = Migration::currentMigration();
  145. // Call any complete handler for this specific Migration.
  146. if (method_exists($migration, 'complete')) {
  147. $migration->complete($entity, $source_row);
  148. }
  149. }
  150. }