]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_join_queue.php
Merge commit '5c4a7a10' into nightly
[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      * @param User_group $group
81      */
82     function abort()
83     {
84         $profile = $this->getMember();
85         $group = $this->getGroup();
86         if ($request) {
87             if (Event::handle('StartCancelJoinGroup', array($profile, $group))) {
88                 $this->delete();
89                 Event::handle('EndCancelJoinGroup', array($profile, $group));
90             }
91         }
92     }
93
94     /**
95      * Complete a pending group join...
96      *
97      * @return Group_member object on success
98      */
99     function complete()
100     {
101         $join = null;
102         $profile = $this->getMember();
103         $group = $this->getGroup();
104         if (Event::handle('StartJoinGroup', array($profile, $group))) {
105             $join = Group_member::join($group->id, $profile->id);
106             $this->delete();
107             Event::handle('EndJoinGroup', array($profile, $group));
108         }
109         if (!$join) {
110             throw new Exception('Internal error: group join failed.');
111         }
112         $join->notify();
113         return $join;
114     }
115
116     /**
117      * Send notifications via email etc to group administrators about
118      * this exciting new pending moderation queue item!
119      */
120     public function notify()
121     {
122         $joiner = Profile::getKV('id', $this->profile_id);
123         $group = User_group::getKV('id', $this->group_id);
124         mail_notify_group_join_pending($group, $joiner);
125     }
126 }