privatemsg.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Privatemag module integration
  5. *
  6. * Limitations:
  7. * - No updating of messages.
  8. * - No threading
  9. * - Messages are marked as deleted and not actually deleted. Thats the
  10. * privatemsg API.
  11. * - All these limitations can be helped by http://drupal.org/node/1184984.
  12. */
  13. class MigrateDestinationPrivateMsg extends MigrateDestinationEntity {
  14. /**
  15. * An array with content ids of imported messages. Not yet used.
  16. */
  17. var $importedIds = array();
  18. static public function getKeySchema() {
  19. return array(
  20. 'mid' => array(
  21. 'type' => 'int',
  22. 'unsigned' => TRUE,
  23. 'not null' => TRUE,
  24. ),
  25. );
  26. }
  27. /**
  28. * Basic initialization
  29. *
  30. * @param array $options
  31. * Options applied to private messages.
  32. */
  33. public function __construct(array $options = array()) {
  34. parent::__construct('privatemsg_message', 'privatemsg_message', $options);
  35. }
  36. /**
  37. * Returns a list of fields available to be mapped/
  38. *
  39. * @return array
  40. * Keys: machine names of the fields (to be passed to addFieldMapping)
  41. * Values: Human-friendly descriptions of the fields.
  42. */
  43. public function fields() {
  44. $fields = array(
  45. // 'mid' => 'Message ID', // Updating not supported. See http://drupal.org/node/1184984.
  46. 'subject' => 'Subject',
  47. 'body' => 'Body',
  48. 'format' => 'Text format name for the Body',
  49. 'recipients' => 'User IDs of recipients',
  50. 'timestamp' => 'Timestamp',
  51. 'author' => 'User ID of author',
  52. 'is_new' => 'TRUE if unread by recipient, FALSE if read by recipient',
  53. );
  54. // Then add in anything provided by handlers
  55. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
  56. $fields += migrate_handler_invoke_all('PrivateMsg', 'fields', $this->entityType, $this->bundle);
  57. return $fields;
  58. }
  59. /**
  60. * Mark provided message as deleted.
  61. *
  62. * @param $id
  63. * IDs to be deleted.
  64. */
  65. public function rollback(array $id) {
  66. migrate_instrument_start(__METHOD__);
  67. // Delete recipients of the message.
  68. db_delete('pm_index')
  69. ->condition('mid', reset($id))
  70. ->execute();
  71. // Delete message itself.
  72. db_delete('pm_message')
  73. ->condition('mid', reset($id))
  74. ->execute();
  75. migrate_instrument_stop(__METHOD__);
  76. }
  77. /**
  78. * Import a single message.
  79. *
  80. * @param $entity
  81. * Object object to build. Prefilled with any fields mapped in the Migration.
  82. * @param $row
  83. * Raw source data object - passed through to prepare/complete handlers.
  84. * @return array
  85. * Array of key fields of the object that was saved if
  86. * successful. FALSE on failure.
  87. */
  88. public function import(stdClass $entity, stdClass $row) {
  89. $this->prepare($entity, $row);
  90. // The privatemsg API does not support updating. See http://drupal.org/node/1184984
  91. // $message['mid'] = $entity->mid;
  92. // The two user_load() calls here could by slow. If so, one could experiment
  93. // with entity cache module - http://drupal.org/project/entitycache.
  94. $options = array();
  95. if (isset($entity->timestamp)) $options['timestamp'] = Migration::timestamp($entity->timestamp);
  96. if (isset($entity->author)) $options['author'] = user_load($entity->author);
  97. if (isset($entity->format)) $options['format'] = $entity->format;
  98. if (!is_array($entity->recipients)) {
  99. $entity->recipients = array($entity->recipients);
  100. }
  101. foreach ($entity->recipients as $uid) {
  102. $entity->to[] = user_load($uid);
  103. }
  104. // FYI, API is at http://api.worldempire.ch/api/privatemsg/privatemsg.module/function/privatemsg_new_thread/7-2
  105. $return = privatemsg_new_thread($entity->to, $entity->subject, $entity->body, $options);
  106. if (!empty($return['success'])) {
  107. $this->complete((object)$return, $row);
  108. // Set the read status for the recipient (defaults to unread, so only need
  109. // to set if read)
  110. $mid = $return['message']->mid;
  111. if (isset($entity->is_new) && $entity->is_new == PRIVATEMSG_READ) {
  112. foreach ($entity->to as $account) {
  113. privatemsg_message_change_status($mid, $entity->is_new, $account);
  114. }
  115. }
  116. return array($mid);
  117. }
  118. else {
  119. $migration = Migration::currentMigration();
  120. $migration->saveMessage(reset($return['messages']['error']));
  121. return FALSE;
  122. }
  123. }
  124. }