]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_member.php
Merge branch 'case-sensitive-openid' into 'master'
[quix0rs-gnu-social.git] / classes / Group_member.php
1 <?php
2 /**
3  * Table Definition for group_member
4  */
5
6 class Group_member extends Managed_DataObject
7 {
8     ###START_AUTOCODE
9     /* the code below is auto generated do not remove the above tag */
10
11     public $__table = 'group_member';                    // table name
12     public $group_id;                        // int(4)  primary_key not_null
13     public $profile_id;                      // int(4)  primary_key not_null
14     public $is_admin;                        // tinyint(1)
15     public $uri;                             // varchar(191)   not 255 because utf8mb4 takes more space
16     public $created;                         // datetime()   not_null
17     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
18
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21
22     public static function schemaDef()
23     {
24         return array(
25             'fields' => array(
26                 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
27                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
28                 'is_admin' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this user an admin?'),
29                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
30                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
31                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
32             ),
33             'primary key' => array('group_id', 'profile_id'),
34             'unique keys' => array(
35                 'group_member_uri_key' => array('uri'),
36             ),
37             'foreign keys' => array(
38                 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
39                 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
40             ),
41             'indexes' => array(
42                 // @fixme probably we want a (profile_id, created) index here?
43                 'group_member_profile_id_idx' => array('profile_id'),
44                 'group_member_created_idx' => array('created'),
45                 'group_member_profile_id_created_idx' => array('profile_id', 'created'),
46                 'group_member_group_id_created_idx' => array('group_id', 'created'),
47             ),
48         );
49     }
50
51     /**
52      * Method to add a user to a group.
53      * In most cases, you should call Profile->joinGroup() instead.
54      *
55      * @param integer $group_id   Group to add to
56      * @param integer $profile_id Profile being added
57      * 
58      * @return Group_member new membership object
59      */
60
61     static function join($group_id, $profile_id)
62     {
63         $member = new Group_member();
64
65         $member->group_id   = $group_id;
66         $member->profile_id = $profile_id;
67         $member->created    = common_sql_now();
68         $member->uri        = self::newUri(Profile::getByID($profile_id),
69                                            User_group::getByID($group_id),
70                                            $member->created);
71
72         $result = $member->insert();
73
74         if (!$result) {
75             common_log_db_error($member, 'INSERT', __FILE__);
76             // TRANS: Exception thrown when joining a group fails.
77             throw new Exception(_("Group join failed."));
78         }
79
80         return $member;
81     }
82
83     static function leave($group_id, $profile_id)
84     {
85         $member = Group_member::pkeyGet(array('group_id' => $group_id,
86                                               'profile_id' => $profile_id));
87
88         if (empty($member)) {
89             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
90             throw new Exception(_("Not part of group."));
91         }
92
93         $result = $member->delete();
94
95         if (!$result) {
96             common_log_db_error($member, 'INSERT', __FILE__);
97             // TRANS: Exception thrown when trying to leave a group fails.
98             throw new Exception(_("Group leave failed."));
99         }
100
101         return true;
102     }
103
104     function getMember()
105     {
106         $member = Profile::getKV('id', $this->profile_id);
107
108         if (empty($member)) {
109             // TRANS: Exception thrown providing an invalid profile ID.
110             // TRANS: %s is the invalid profile ID.
111             throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
112         }
113
114         return $member;
115     }
116
117     function getGroup()
118     {
119         $group  = User_group::getKV('id', $this->group_id);
120
121         if (empty($group)) {
122             // TRANS: Exception thrown providing an invalid group ID.
123             // TRANS: %s is the invalid group ID.
124             throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
125         }
126
127         return $group;
128     }
129
130     /**
131      * Get stream of memberships by member
132      *
133      * @param integer $memberId profile ID of the member to fetch for
134      * @param integer $offset   offset from start of stream to get
135      * @param integer $limit    number of memberships to get
136      *
137      * @return Group_member stream of memberships, use fetch() to iterate
138      */
139
140     static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
141     {
142         $membership = new Group_member();
143
144         $membership->profile_id = $memberId;
145
146         $membership->orderBy('created DESC');
147
148         $membership->limit($offset, $limit);
149
150         $membership->find();
151
152         return $membership;
153     }
154
155     function asActivity()
156     {
157         $member = $this->getMember();
158
159         if (!$member) {
160             throw new Exception("No such member: " . $this->profile_id);
161         }
162
163         $group  = $this->getGroup();
164
165         if (!$group) {
166             throw new Exception("No such group: " . $this->group_id);
167         }
168
169         $act = new Activity();
170
171         $act->id = $this->getUri();
172
173         $act->actor     = $member->asActivityObject();
174         $act->verb      = ActivityVerb::JOIN;
175         $act->objects[] = ActivityObject::fromGroup($group);
176
177         $act->time  = strtotime($this->created);
178         // TRANS: Activity title.
179         $act->title = _("Join");
180
181         // TRANS: Success message for subscribe to group attempt through OStatus.
182         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
183         $act->content = sprintf(_('%1$s has joined group %2$s.'),
184                                 $member->getBestName(),
185                                 $group->getBestName());
186
187         $url = common_local_url('AtomPubShowMembership',
188                                 array('profile' => $member->id,
189                                       'group' => $group->id));
190
191         $act->selfLink = $url;
192         $act->editLink = $url;
193
194         return $act;
195     }
196
197     /**
198      * Send notifications via email etc to group administrators about
199      * this exciting new membership!
200      */
201     public function notify()
202     {
203         mail_notify_group_join($this->getGroup(), $this->getMember());
204     }
205
206     function getUri()
207     {
208         return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
209     }
210 }