Group_join_queue.php 4.7 KB

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