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