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