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