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 $uri; // varchar(255)
16 public $created; // datetime() not_null
17 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
19 /* the code above is auto generated do not remove the tag below */
22 public static function schemaDef()
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'),
33 'primary key' => array('group_id', 'profile_id'),
34 'unique keys' => array(
35 'group_member_uri_key' => array('uri'),
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')),
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'),
52 * Method to add a user to a group.
53 * In most cases, you should call Profile->joinGroup() instead.
55 * @param integer $group_id Group to add to
56 * @param integer $profile_id Profile being added
58 * @return Group_member new membership object
61 static function join($group_id, $profile_id)
63 $member = new Group_member();
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);
70 $result = $member->insert();
73 common_log_db_error($member, 'INSERT', __FILE__);
74 // TRANS: Exception thrown when joining a group fails.
75 throw new Exception(_("Group join failed."));
81 static function leave($group_id, $profile_id)
83 $member = Group_member::pkeyGet(array('group_id' => $group_id,
84 'profile_id' => $profile_id));
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."));
91 $result = $member->delete();
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."));
104 $member = Profile::getKV('id', $this->profile_id);
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));
117 $group = User_group::getKV('id', $this->group_id);
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));
129 * Get stream of memberships by member
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
135 * @return Group_member stream of memberships, use fetch() to iterate
138 static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
140 $membership = new Group_member();
142 $membership->profile_id = $memberId;
144 $membership->orderBy('created DESC');
146 $membership->limit($offset, $limit);
153 function asActivity()
155 $member = $this->getMember();
158 throw new Exception("No such member: " . $this->profile_id);
161 $group = $this->getGroup();
164 throw new Exception("No such group: " . $this->group_id);
167 $act = new Activity();
169 $act->id = $this->getURI();
171 $act->actor = $member->asActivityObject();
172 $act->verb = ActivityVerb::JOIN;
173 $act->objects[] = ActivityObject::fromGroup($group);
175 $act->time = strtotime($this->created);
176 // TRANS: Activity title.
177 $act->title = _("Join");
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());
185 $url = common_local_url('AtomPubShowMembership',
186 array('profile' => $member->id,
187 'group' => $group->id));
189 $act->selfLink = $url;
190 $act->editLink = $url;
196 * Send notifications via email etc to group administrators about
197 * this exciting new membership!
199 public function notify()
201 mail_notify_group_join($this->getGroup(), $this->getMember());
206 if (!empty($this->uri)) {
209 return self::newURI($this->profile_id, $this->group_id, $this->created);
213 static function newURI($profile_id, $group_id, $created)
215 return TagURI::mint('join:%d:%d:%s',
218 common_date_iso8601($created));