Group_join_queue.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Table Definition for request_queue
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Group_join_queue extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'group_join_queue'; // table name
  11. public $profile_id;
  12. public $group_id;
  13. public $created;
  14. /* the code above is auto generated do not remove the tag below */
  15. ###END_AUTOCODE
  16. public static function schemaDef()
  17. {
  18. return array(
  19. 'description' => 'Holder for group join requests awaiting moderation.',
  20. 'fields' => array(
  21. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
  22. 'group_id' => array('type' => 'int', 'description' => 'remote or local group to join, if any'),
  23. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  24. ),
  25. 'primary key' => array('profile_id', 'group_id'),
  26. 'indexes' => array(
  27. 'group_join_queue_profile_id_created_idx' => array('profile_id', 'created'),
  28. 'group_join_queue_group_id_created_idx' => array('group_id', 'created'),
  29. ),
  30. 'foreign keys' => array(
  31. 'group_join_queue_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  32. 'group_join_queue_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  33. )
  34. );
  35. }
  36. public static function saveNew(Profile $profile, User_group $group)
  37. {
  38. $rq = new Group_join_queue();
  39. $rq->profile_id = $profile->id;
  40. $rq->group_id = $group->id;
  41. $rq->created = common_sql_now();
  42. $rq->insert();
  43. return $rq;
  44. }
  45. function getMember()
  46. {
  47. $member = Profile::getKV('id', $this->profile_id);
  48. if (empty($member)) {
  49. // TRANS: Exception thrown providing an invalid profile ID.
  50. // TRANS: %s is the invalid profile ID.
  51. throw new Exception(sprintf(_('Profile ID %s is invalid.'),$this->profile_id));
  52. }
  53. return $member;
  54. }
  55. function getGroup()
  56. {
  57. $group = User_group::getKV('id', $this->group_id);
  58. if (empty($group)) {
  59. // TRANS: Exception thrown providing an invalid group ID.
  60. // TRANS: %s is the invalid group ID.
  61. throw new Exception(sprintf(_('Group ID %s is invalid.'),$this->group_id));
  62. }
  63. return $group;
  64. }
  65. /**
  66. * Abort the pending group join...
  67. */
  68. function abort()
  69. {
  70. $profile = $this->getMember();
  71. $group = $this->getGroup();
  72. if (Event::handle('StartCancelJoinGroup', array($profile, $group))) {
  73. $this->delete();
  74. Event::handle('EndCancelJoinGroup', array($profile, $group));
  75. }
  76. }
  77. /**
  78. * Complete a pending group join...
  79. *
  80. * @return Group_member object on success
  81. */
  82. function complete()
  83. {
  84. $join = null;
  85. $profile = $this->getMember();
  86. $group = $this->getGroup();
  87. if (Event::handle('StartJoinGroup', array($profile, $group))) {
  88. $join = Group_member::join($group->id, $profile->id);
  89. $this->delete();
  90. Event::handle('EndJoinGroup', array($profile, $group));
  91. }
  92. if (!$join) {
  93. throw new Exception('Internal error: group join failed.');
  94. }
  95. $join->notify();
  96. return $join;
  97. }
  98. /**
  99. * Send notifications via email etc to group administrators about
  100. * this exciting new pending moderation queue item!
  101. */
  102. public function notify()
  103. {
  104. $joiner = Profile::getKV('id', $this->profile_id);
  105. $group = User_group::getKV('id', $this->group_id);
  106. mail_notify_group_join_pending($group, $joiner);
  107. }
  108. }