]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_member.php
The overloaded DB_DataObject function staticGet is now called getKV
[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(255)
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' => 255, '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     function pkeyGet($kv)
52     {
53         return Memcached_DataObject::pkeyGet('Group_member', $kv);
54     }
55
56     /**
57      * Method to add a user to a group.
58      * In most cases, you should call Profile->joinGroup() instead.
59      *
60      * @param integer $group_id   Group to add to
61      * @param integer $profile_id Profile being added
62      * 
63      * @return Group_member new membership object
64      */
65
66     static function join($group_id, $profile_id)
67     {
68         $member = new Group_member();
69
70         $member->group_id   = $group_id;
71         $member->profile_id = $profile_id;
72         $member->created    = common_sql_now();
73         $member->uri        = self::newURI($profile_id, $group_id, $member->created);
74
75         $result = $member->insert();
76
77         if (!$result) {
78             common_log_db_error($member, 'INSERT', __FILE__);
79             // TRANS: Exception thrown when joining a group fails.
80             throw new Exception(_("Group join failed."));
81         }
82
83         return $member;
84     }
85
86     static function leave($group_id, $profile_id)
87     {
88         $member = Group_member::pkeyGet(array('group_id' => $group_id,
89                                               'profile_id' => $profile_id));
90
91         if (empty($member)) {
92             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
93             throw new Exception(_("Not part of group."));
94         }
95
96         $result = $member->delete();
97
98         if (!$result) {
99             common_log_db_error($member, 'INSERT', __FILE__);
100             // TRANS: Exception thrown when trying to leave a group fails.
101             throw new Exception(_("Group leave failed."));
102         }
103
104         return true;
105     }
106
107     function getMember()
108     {
109         $member = Profile::getKV('id', $this->profile_id);
110
111         if (empty($member)) {
112             // TRANS: Exception thrown providing an invalid profile ID.
113             // TRANS: %s is the invalid profile ID.
114             throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
115         }
116
117         return $member;
118     }
119
120     function getGroup()
121     {
122         $group  = User_group::getKV('id', $this->group_id);
123
124         if (empty($group)) {
125             // TRANS: Exception thrown providing an invalid group ID.
126             // TRANS: %s is the invalid group ID.
127             throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
128         }
129
130         return $group;
131     }
132
133     /**
134      * Get stream of memberships by member
135      *
136      * @param integer $memberId profile ID of the member to fetch for
137      * @param integer $offset   offset from start of stream to get
138      * @param integer $limit    number of memberships to get
139      *
140      * @return Group_member stream of memberships, use fetch() to iterate
141      */
142
143     static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
144     {
145         $membership = new Group_member();
146
147         $membership->profile_id = $memberId;
148
149         $membership->orderBy('created DESC');
150
151         $membership->limit($offset, $limit);
152
153         $membership->find();
154
155         return $membership;
156     }
157
158     function asActivity()
159     {
160         $member = $this->getMember();
161
162         if (!$member) {
163             throw new Exception("No such member: " . $this->profile_id);
164         }
165
166         $group  = $this->getGroup();
167
168         if (!$group) {
169             throw new Exception("No such group: " . $this->group_id);
170         }
171
172         $act = new Activity();
173
174         $act->id = $this->getURI();
175
176         $act->actor     = ActivityObject::fromProfile($member);
177         $act->verb      = ActivityVerb::JOIN;
178         $act->objects[] = ActivityObject::fromGroup($group);
179
180         $act->time  = strtotime($this->created);
181         // TRANS: Activity title.
182         $act->title = _("Join");
183
184         // TRANS: Success message for subscribe to group attempt through OStatus.
185         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
186         $act->content = sprintf(_('%1$s has joined group %2$s.'),
187                                 $member->getBestName(),
188                                 $group->getBestName());
189
190         $url = common_local_url('AtomPubShowMembership',
191                                 array('profile' => $member->id,
192                                       'group' => $group->id));
193
194         $act->selfLink = $url;
195         $act->editLink = $url;
196
197         return $act;
198     }
199
200     /**
201      * Send notifications via email etc to group administrators about
202      * this exciting new membership!
203      */
204     public function notify()
205     {
206         mail_notify_group_join($this->getGroup(), $this->getMember());
207     }
208
209     function getURI()
210     {
211         if (!empty($this->uri)) {
212             return $this->uri;
213         } else {
214             return self::newURI($this->profile_id, $this->group_id, $this->created);
215         }
216     }
217
218     static function newURI($profile_id, $group_id, $created)
219     {
220         return TagURI::mint('join:%d:%d:%s',
221                             $profile_id,
222                             $group_id,
223                             common_date_iso8601($created));
224     }
225 }