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