Group_join_queue.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for request_queue
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Group_join_queue extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'group_join_queue'; // table name
  25. public $profile_id;
  26. public $group_id;
  27. public $created;
  28. /* the code above is auto generated do not remove the tag below */
  29. ###END_AUTOCODE
  30. public static function schemaDef()
  31. {
  32. return array(
  33. 'description' => 'Holder for group join requests awaiting moderation.',
  34. 'fields' => array(
  35. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
  36. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local group to join, if any'),
  37. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  38. ),
  39. 'primary key' => array('profile_id', 'group_id'),
  40. 'indexes' => array(
  41. 'group_join_queue_profile_id_created_idx' => array('profile_id', 'created'),
  42. 'group_join_queue_group_id_created_idx' => array('group_id', 'created'),
  43. ),
  44. 'foreign keys' => array(
  45. 'group_join_queue_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  46. 'group_join_queue_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  47. )
  48. );
  49. }
  50. public static function saveNew(Profile $profile, User_group $group)
  51. {
  52. $rq = new Group_join_queue();
  53. $rq->profile_id = $profile->id;
  54. $rq->group_id = $group->id;
  55. $rq->created = common_sql_now();
  56. $rq->insert();
  57. return $rq;
  58. }
  59. public function getMember()
  60. {
  61. $member = Profile::getKV('id', $this->profile_id);
  62. if (empty($member)) {
  63. // TRANS: Exception thrown providing an invalid profile ID.
  64. // TRANS: %s is the invalid profile ID.
  65. throw new Exception(sprintf(_('Profile ID %s is invalid.'), $this->profile_id));
  66. }
  67. return $member;
  68. }
  69. public function getGroup()
  70. {
  71. $group = User_group::getKV('id', $this->group_id);
  72. if (empty($group)) {
  73. // TRANS: Exception thrown providing an invalid group ID.
  74. // TRANS: %s is the invalid group ID.
  75. throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id));
  76. }
  77. return $group;
  78. }
  79. /**
  80. * Abort the pending group join...
  81. */
  82. public function abort()
  83. {
  84. $profile = $this->getMember();
  85. $group = $this->getGroup();
  86. if (Event::handle('StartCancelJoinGroup', array($profile, $group))) {
  87. $this->delete();
  88. Event::handle('EndCancelJoinGroup', array($profile, $group));
  89. }
  90. }
  91. /**
  92. * Complete a pending group join...
  93. *
  94. * @return Group_member object on success
  95. */
  96. public function complete()
  97. {
  98. $join = null;
  99. $profile = $this->getMember();
  100. $group = $this->getGroup();
  101. if (Event::handle('StartJoinGroup', array($profile, $group))) {
  102. $join = Group_member::join($group->id, $profile->id);
  103. $this->delete();
  104. Event::handle('EndJoinGroup', array($profile, $group));
  105. }
  106. if (!$join) {
  107. throw new Exception('Internal error: group join failed.');
  108. }
  109. $join->notify();
  110. return $join;
  111. }
  112. /**
  113. * Send notifications via email etc to group administrators about
  114. * this exciting new pending moderation queue item!
  115. */
  116. public function notify()
  117. {
  118. $joiner = Profile::getKV('id', $this->profile_id);
  119. $group = User_group::getKV('id', $this->group_id);
  120. mail_notify_group_join_pending($group, $joiner);
  121. }
  122. }