]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_member.php
Merge branch 'master' of gitorious.org:statusnet/mainline into 0.9.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 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      *
32      * @param integer $group_id   Group to add to
33      * @param integer $profile_id Profile being added
34      * 
35      * @return Group_member new membership object
36      */
37
38     static function join($group_id, $profile_id)
39     {
40         $member = new Group_member();
41
42         $member->group_id   = $group_id;
43         $member->profile_id = $profile_id;
44         $member->created    = common_sql_now();
45
46         $result = $member->insert();
47
48         if (!$result) {
49             common_log_db_error($member, 'INSERT', __FILE__);
50             // TRANS: Exception thrown when joining a group fails.
51             throw new Exception(_("Group join failed."));
52         }
53
54         return $member;
55     }
56
57     static function leave($group_id, $profile_id)
58     {
59         $member = Group_member::pkeyGet(array('group_id' => $group_id,
60                                               'profile_id' => $profile_id));
61
62         if (empty($member)) {
63             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
64             throw new Exception(_("Not part of group."));
65         }
66
67         $result = $member->delete();
68
69         if (!$result) {
70             common_log_db_error($member, 'INSERT', __FILE__);
71             // TRANS: Exception thrown when trying to leave a group fails.
72             throw new Exception(_("Group leave failed."));
73         }
74
75         return true;
76     }
77
78     function getMember()
79     {
80         $member = Profile::staticGet('id', $this->profile_id);
81
82         if (empty($member)) {
83             // TRANS: Exception thrown providing an invalid profile ID.
84             // TRANS: %s is the invalid profile ID.
85             throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
86         }
87
88         return $member;
89     }
90
91     function getGroup()
92     {
93         $group  = User_group::staticGet('id', $this->group_id);
94
95         if (empty($group)) {
96             // TRANS: Exception thrown providing an invalid group ID.
97             // TRANS: %s is the invalid group ID.
98             throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
99         }
100
101         return $group;
102     }
103
104     /**
105      * Get stream of memberships by member
106      *
107      * @param integer $memberId profile ID of the member to fetch for
108      * @param integer $offset   offset from start of stream to get
109      * @param integer $limit    number of memberships to get
110      *
111      * @return Group_member stream of memberships, use fetch() to iterate
112      */
113
114     static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
115     {
116         $membership = new Group_member();
117
118         $membership->profile_id = $memberId;
119
120         $membership->orderBy('created DESC');
121
122         $membership->limit($offset, $limit);
123
124         $membership->find();
125
126         return $membership;
127     }
128
129     function asActivity()
130     {
131         $member = $this->getMember();
132         $group  = $this->getGroup();
133
134         $act = new Activity();
135
136         $act->id = TagURI::mint('join:%d:%d:%s',
137                                 $member->id,
138                                 $group->id,
139                                 common_date_iso8601($this->created));
140
141         $act->actor     = ActivityObject::fromProfile($member);
142         $act->verb      = ActivityVerb::JOIN;
143         $act->objects[] = ActivityObject::fromGroup($group);
144
145         $act->time  = strtotime($this->created);
146         // TRANS: Activity title.
147         $act->title = _("Join");
148
149         // TRANS: Success message for subscribe to group attempt through OStatus.
150         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
151         $act->content = sprintf(_('%1$s has joined group %2$s.'),
152                                 $member->getBestName(),
153                                 $group->getBestName());
154
155         $url = common_local_url('AtomPubShowMembership',
156                                 array('profile' => $member->id,
157                                       'group' => $group->id));
158
159         $act->selfLink = $url;
160         $act->editLink = $url;
161
162         return $act;
163     }
164 }