]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_member.php
Merge branch '1.0.x' into testing
[quix0rs-gnu-social.git] / classes / Group_member.php
1 <?php
2 /**
3  * Table Definition for group_member
4  */
5
6 class Group_member extends Memcached_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 $created;                         // datetime()   not_null
16     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
17
18     /* Static get */
19     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_member',$k,$v); }
20
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23
24     function pkeyGet($kv)
25     {
26         return Memcached_DataObject::pkeyGet('Group_member', $kv);
27     }
28
29     /**
30      * Method to add a user to a group.
31      * In most cases, you should call Profile->joinGroup() instead.
32      *
33      * @param integer $group_id   Group to add to
34      * @param integer $profile_id Profile being added
35      * 
36      * @return Group_member new membership object
37      */
38
39     static function join($group_id, $profile_id)
40     {
41         $member = new Group_member();
42
43         $member->group_id   = $group_id;
44         $member->profile_id = $profile_id;
45         $member->created    = common_sql_now();
46
47         $result = $member->insert();
48
49         if (!$result) {
50             common_log_db_error($member, 'INSERT', __FILE__);
51             // TRANS: Exception thrown when joining a group fails.
52             throw new Exception(_("Group join failed."));
53         }
54
55         return $member;
56     }
57
58     static function leave($group_id, $profile_id)
59     {
60         $member = Group_member::pkeyGet(array('group_id' => $group_id,
61                                               'profile_id' => $profile_id));
62
63         if (empty($member)) {
64             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
65             throw new Exception(_("Not part of group."));
66         }
67
68         $result = $member->delete();
69
70         if (!$result) {
71             common_log_db_error($member, 'INSERT', __FILE__);
72             // TRANS: Exception thrown when trying to leave a group fails.
73             throw new Exception(_("Group leave failed."));
74         }
75
76         return true;
77     }
78
79     function getMember()
80     {
81         $member = Profile::staticGet('id', $this->profile_id);
82
83         if (empty($member)) {
84             // TRANS: Exception thrown providing an invalid profile ID.
85             // TRANS: %s is the invalid profile ID.
86             throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
87         }
88
89         return $member;
90     }
91
92     function getGroup()
93     {
94         $group  = User_group::staticGet('id', $this->group_id);
95
96         if (empty($group)) {
97             // TRANS: Exception thrown providing an invalid group ID.
98             // TRANS: %s is the invalid group ID.
99             throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
100         }
101
102         return $group;
103     }
104
105     /**
106      * Get stream of memberships by member
107      *
108      * @param integer $memberId profile ID of the member to fetch for
109      * @param integer $offset   offset from start of stream to get
110      * @param integer $limit    number of memberships to get
111      *
112      * @return Group_member stream of memberships, use fetch() to iterate
113      */
114
115     static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
116     {
117         $membership = new Group_member();
118
119         $membership->profile_id = $memberId;
120
121         $membership->orderBy('created DESC');
122
123         $membership->limit($offset, $limit);
124
125         $membership->find();
126
127         return $membership;
128     }
129
130     function asActivity()
131     {
132         $member = $this->getMember();
133         $group  = $this->getGroup();
134
135         $act = new Activity();
136
137         $act->id = TagURI::mint('join:%d:%d:%s',
138                                 $member->id,
139                                 $group->id,
140                                 common_date_iso8601($this->created));
141
142         $act->actor     = ActivityObject::fromProfile($member);
143         $act->verb      = ActivityVerb::JOIN;
144         $act->objects[] = ActivityObject::fromGroup($group);
145
146         $act->time  = strtotime($this->created);
147         // TRANS: Activity title.
148         $act->title = _("Join");
149
150         // TRANS: Success message for subscribe to group attempt through OStatus.
151         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
152         $act->content = sprintf(_('%1$s has joined group %2$s.'),
153                                 $member->getBestName(),
154                                 $group->getBestName());
155
156         $url = common_local_url('AtomPubShowMembership',
157                                 array('profile' => $member->id,
158                                       'group' => $group->id));
159
160         $act->selfLink = $url;
161         $act->editLink = $url;
162
163         return $act;
164     }
165
166     /**
167      * Send notifications via email etc to group administrators about
168      * this exciting new membership!
169      */
170     public function notify()
171     {
172         mail_notify_group_join($this->getGroup(), $this->getMember());
173     }
174 }