Group_join_queue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @param User_group $group
  69. */
  70. function abort()
  71. {
  72. $profile = $this->getMember();
  73. $group = $this->getGroup();
  74. if ($request) {
  75. if (Event::handle('StartCancelJoinGroup', array($profile, $group))) {
  76. $this->delete();
  77. Event::handle('EndCancelJoinGroup', array($profile, $group));
  78. }
  79. }
  80. }
  81. /**
  82. * Complete a pending group join...
  83. *
  84. * @return Group_member object on success
  85. */
  86. function complete()
  87. {
  88. $join = null;
  89. $profile = $this->getMember();
  90. $group = $this->getGroup();
  91. if (Event::handle('StartJoinGroup', array($profile, $group))) {
  92. $join = Group_member::join($group->id, $profile->id);
  93. $this->delete();
  94. Event::handle('EndJoinGroup', array($profile, $group));
  95. }
  96. if (!$join) {
  97. throw new Exception('Internal error: group join failed.');
  98. }
  99. $join->notify();
  100. return $join;
  101. }
  102. /**
  103. * Send notifications via email etc to group administrators about
  104. * this exciting new pending moderation queue item!
  105. */
  106. public function notify()
  107. {
  108. $joiner = Profile::getKV('id', $this->profile_id);
  109. $group = User_group::getKV('id', $this->group_id);
  110. mail_notify_group_join_pending($group, $joiner);
  111. }
  112. }