]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_member.php
Merge branch 'yammer' of gitorious.org:~brion/statusnet/brion-fixes 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     static function join($group_id, $profile_id)
30     {
31         $member = new Group_member();
32
33         $member->group_id   = $group_id;
34         $member->profile_id = $profile_id;
35         $member->created    = common_sql_now();
36
37         $result = $member->insert();
38
39         if (!$result) {
40             common_log_db_error($member, 'INSERT', __FILE__);
41             // TRANS: Exception thrown when joining a group fails.
42             throw new Exception(_("Group join failed."));
43         }
44
45         return true;
46     }
47
48     static function leave($group_id, $profile_id)
49     {
50         $member = Group_member::pkeyGet(array('group_id' => $group_id,
51                                               'profile_id' => $profile_id));
52
53         if (empty($member)) {
54             // TRANS: Exception thrown when trying to leave a group the user is not a member of.
55             throw new Exception(_("Not part of group."));
56         }
57
58         $result = $member->delete();
59
60         if (!$result) {
61             common_log_db_error($member, 'INSERT', __FILE__);
62             // TRANS: Exception thrown when trying to leave a group fails.
63             throw new Exception(_("Group leave failed."));
64         }
65
66         return true;
67     }
68
69     function getMember()
70     {
71         $member = Profile::staticGet('id', $this->profile_id);
72
73         if (empty($member)) {
74             throw new Exception("Profile ID {$this->profile_id} invalid.");
75         }
76
77         return $member;
78     }
79
80     function getGroup()
81     {
82         $group  = User_group::staticGet('id', $this->group_id);
83
84         if (empty($group)) {
85             throw new Exception("Group ID {$this->group_id} invalid.");
86         }
87
88         return $group;
89     }
90
91     function asActivity()
92     {
93         $member = $this->getMember();
94         $group  = $this->getGroup();
95
96         $act = new Activity();
97
98         $act->id = TagURI::mint('join:%d:%d:%s',
99                                 $member->id,
100                                 $group->id,
101                                 common_date_iso8601($this->created));
102
103         $act->actor     = ActivityObject::fromProfile($member);
104         $act->verb      = ActivityVerb::JOIN;
105         $act->objects[] = ActivityObject::fromGroup($group);
106
107         $act->time  = strtotime($this->created);
108         $act->title = _("Join");
109
110         // TRANS: Success message for subscribe to group attempt through OStatus.
111         // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
112         $act->content = sprintf(_('%1$s has joined group %2$s.'),
113                                 $member->getBestName(),
114                                 $group->getBestName());
115
116         return $act;
117     }
118 }