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(191) not 255 because utf8mb4 takes more space
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' => 191, '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::getByID($profile_id),
69 User_group::getByID($group_id),
72 $result = $member->insert();
75 common_log_db_error($member, 'INSERT', __FILE__);
76 // TRANS: Exception thrown when joining a group fails.
77 throw new Exception(_("Group join failed."));
83 static function leave($group_id, $profile_id)
85 $member = Group_member::pkeyGet(array('group_id' => $group_id,
86 'profile_id' => $profile_id));
89 // TRANS: Exception thrown when trying to leave a group the user is not a member of.
90 throw new Exception(_("Not part of group."));
93 $result = $member->delete();
96 common_log_db_error($member, 'INSERT', __FILE__);
97 // TRANS: Exception thrown when trying to leave a group fails.
98 throw new Exception(_("Group leave failed."));
106 $member = Profile::getKV('id', $this->profile_id);
108 if (empty($member)) {
109 // TRANS: Exception thrown providing an invalid profile ID.
110 // TRANS: %s is the invalid profile ID.
111 throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
119 $group = User_group::getKV('id', $this->group_id);
122 // TRANS: Exception thrown providing an invalid group ID.
123 // TRANS: %s is the invalid group ID.
124 throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
131 * Get stream of memberships by member
133 * @param integer $memberId profile ID of the member to fetch for
134 * @param integer $offset offset from start of stream to get
135 * @param integer $limit number of memberships to get
137 * @return Group_member stream of memberships, use fetch() to iterate
140 static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
142 $membership = new Group_member();
144 $membership->profile_id = $memberId;
146 $membership->orderBy('created DESC');
148 $membership->limit($offset, $limit);
155 function asActivity()
157 $member = $this->getMember();
160 throw new Exception("No such member: " . $this->profile_id);
163 $group = $this->getGroup();
166 throw new Exception("No such group: " . $this->group_id);
169 $act = new Activity();
171 $act->id = $this->getUri();
173 $act->actor = $member->asActivityObject();
174 $act->verb = ActivityVerb::JOIN;
175 $act->objects[] = ActivityObject::fromGroup($group);
177 $act->time = strtotime($this->created);
178 // TRANS: Activity title.
179 $act->title = _("Join");
181 // TRANS: Success message for subscribe to group attempt through OStatus.
182 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
183 $act->content = sprintf(_('%1$s has joined group %2$s.'),
184 $member->getBestName(),
185 $group->getBestName());
187 $url = common_local_url('AtomPubShowMembership',
188 array('profile' => $member->id,
189 'group' => $group->id));
191 $act->selfLink = $url;
192 $act->editLink = $url;
198 * Send notifications via email etc to group administrators about
199 * this exciting new membership!
201 public function notify()
203 mail_notify_group_join($this->getGroup(), $this->getMember());
208 return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);