user_relationships.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Import User Relationships.
  5. */
  6. /**
  7. * Destination class implementing migration into user_relationships table.
  8. */
  9. class MigrateDestinationUserRelationships extends MigrateDestination {
  10. protected $typeID;
  11. protected $typeName;
  12. /**
  13. * Construct a destination for the specified user relationship type. Interprets
  14. * the type as a type name - if that fails, assumes it's a type ID (rtid).
  15. *
  16. * @param mixed $type_name
  17. * Name of a user relationship type, or its rtid.
  18. */
  19. public function __construct($type_name) {
  20. parent::__construct();
  21. $type = user_relationships_type_load(array('name' => $type_name));
  22. if ($type) {
  23. $this->typeName = $type_name;
  24. $this->typeID = $type->rtid;
  25. }
  26. else {
  27. $type = user_relationships_type_load($type_name);
  28. $this->typeName = $type->name;
  29. $this->typeID = $type_name;
  30. }
  31. }
  32. static public function getKeySchema() {
  33. return array(
  34. 'rid' => array(
  35. 'type' => 'int',
  36. 'not null' => TRUE,
  37. 'description' => 'Relationship ID',
  38. ),
  39. );
  40. }
  41. /**
  42. * Delete a membership.
  43. *
  44. * @param $id
  45. * ID to be deleted.
  46. */
  47. public function rollback(array $id) {
  48. migrate_instrument_start(__METHOD__);
  49. if ($relationship = user_relationships_load(array('rid' => $id['destid1']))) {
  50. $account = new stdClass;
  51. user_relationships_delete_relationship(current($relationship), $account);
  52. }
  53. migrate_instrument_stop(__METHOD__);
  54. }
  55. /**
  56. * Import a single membership.
  57. *
  58. * @param $entity
  59. * Object object to build. Prefilled with any fields mapped in the Migration.
  60. * @param $row
  61. * Raw source data object - passed through to prepare/complete handlers.
  62. * @return array
  63. * Array of key fields of the object that was saved if
  64. * successful. FALSE on failure.
  65. */
  66. public function import(stdClass $entity, stdClass $row) {
  67. // Normalize to unix timestamps.
  68. foreach (array('created_at', 'updated_at') as $property) {
  69. if (isset($entity->$property)) {
  70. $entity->$property = Migration::timestamp($entity->$property);
  71. }
  72. }
  73. $entity->rtid = $this->typeID;
  74. $op = isset($entity->op) ? $entity->op : 'approve';
  75. if ($saved = user_relationships_save_relationship($entity, $op)) {
  76. return array($saved->rid);
  77. }
  78. }
  79. public function fields() {
  80. return array(
  81. 'rid' => 'Relationship ID',
  82. 'requester_id' => 'User ID of relationship requestor',
  83. 'requestee_id' => 'User ID of relationship requestee',
  84. 'approved' => 'Whether the requestee approved the relationship',
  85. 'created_at' => 'Timestamp when the relationship was created',
  86. 'updated_at' => 'Timestamp when the relationship was last updated',
  87. 'flags' => 'UR_OK (0) or UR_BANNED (1)',
  88. 'op' => 'Default value is \'approve\'. Sent as second param to user_relationships_save_relationship().'
  89. );
  90. }
  91. public function __toString() {
  92. return t('User relationship type: !type', array('!type' => $this->typeName));
  93. }
  94. }