3 * Table Definition for group_member
6 class Group_member extends Managed_DataObject
9 /* the code below is auto generated do not remove the above tag */
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
19 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_member',$k,$v); }
21 /* the code above is auto generated do not remove the tag below */
24 public static function schemaDef()
28 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
29 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
30 'is_admin' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this user an admin?'),
32 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
33 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
35 'primary key' => array('group_id', 'profile_id'),
36 'foreign keys' => array(
37 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
38 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
41 // @fixme probably we want a (profile_id, created) index here?
42 'group_member_profile_id_idx' => array('profile_id'),
43 'group_member_created_idx' => array('created'),
50 return Memcached_DataObject::pkeyGet('Group_member', $kv);
54 * Method to add a user to a group.
55 * In most cases, you should call Profile->joinGroup() instead.
57 * @param integer $group_id Group to add to
58 * @param integer $profile_id Profile being added
60 * @return Group_member new membership object
63 static function join($group_id, $profile_id)
65 $member = new Group_member();
67 $member->group_id = $group_id;
68 $member->profile_id = $profile_id;
69 $member->created = common_sql_now();
71 $result = $member->insert();
74 common_log_db_error($member, 'INSERT', __FILE__);
75 // TRANS: Exception thrown when joining a group fails.
76 throw new Exception(_("Group join failed."));
82 static function leave($group_id, $profile_id)
84 $member = Group_member::pkeyGet(array('group_id' => $group_id,
85 'profile_id' => $profile_id));
88 // TRANS: Exception thrown when trying to leave a group the user is not a member of.
89 throw new Exception(_("Not part of group."));
92 $result = $member->delete();
95 common_log_db_error($member, 'INSERT', __FILE__);
96 // TRANS: Exception thrown when trying to leave a group fails.
97 throw new Exception(_("Group leave failed."));
105 $member = Profile::staticGet('id', $this->profile_id);
107 if (empty($member)) {
108 // TRANS: Exception thrown providing an invalid profile ID.
109 // TRANS: %s is the invalid profile ID.
110 throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
118 $group = User_group::staticGet('id', $this->group_id);
121 // TRANS: Exception thrown providing an invalid group ID.
122 // TRANS: %s is the invalid group ID.
123 throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
130 * Get stream of memberships by member
132 * @param integer $memberId profile ID of the member to fetch for
133 * @param integer $offset offset from start of stream to get
134 * @param integer $limit number of memberships to get
136 * @return Group_member stream of memberships, use fetch() to iterate
139 static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
141 $membership = new Group_member();
143 $membership->profile_id = $memberId;
145 $membership->orderBy('created DESC');
147 $membership->limit($offset, $limit);
154 function asActivity()
156 $member = $this->getMember();
157 $group = $this->getGroup();
159 $act = new Activity();
161 $act->id = TagURI::mint('join:%d:%d:%s',
164 common_date_iso8601($this->created));
166 $act->actor = ActivityObject::fromProfile($member);
167 $act->verb = ActivityVerb::JOIN;
168 $act->objects[] = ActivityObject::fromGroup($group);
170 $act->time = strtotime($this->created);
171 // TRANS: Activity title.
172 $act->title = _("Join");
174 // TRANS: Success message for subscribe to group attempt through OStatus.
175 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
176 $act->content = sprintf(_('%1$s has joined group %2$s.'),
177 $member->getBestName(),
178 $group->getBestName());
180 $url = common_local_url('AtomPubShowMembership',
181 array('profile' => $member->id,
182 'group' => $group->id));
184 $act->selfLink = $url;
185 $act->editLink = $url;
191 * Send notifications via email etc to group administrators about
192 * this exciting new membership!
194 public function notify()
196 mail_notify_group_join($this->getGroup(), $this->getMember());