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