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
20 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_member',$k,$v); }
22 /* the code above is auto generated do not remove the tag below */
25 public static function schemaDef()
29 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
30 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31 'is_admin' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this user an admin?'),
32 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'),
33 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
34 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
36 'primary key' => array('group_id', 'profile_id'),
37 'unique keys' => array(
38 'group_member_uri_key' => array('uri'),
40 'foreign keys' => array(
41 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
42 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
45 // @fixme probably we want a (profile_id, created) index here?
46 'group_member_profile_id_idx' => array('profile_id'),
47 'group_member_created_idx' => array('created'),
48 'group_member_profile_id_created_idx' => array('profile_id', 'created'),
49 'group_member_group_id_created_idx' => array('group_id', 'created'),
56 return Memcached_DataObject::pkeyGet('Group_member', $kv);
60 * Method to add a user to a group.
61 * In most cases, you should call Profile->joinGroup() instead.
63 * @param integer $group_id Group to add to
64 * @param integer $profile_id Profile being added
66 * @return Group_member new membership object
69 static function join($group_id, $profile_id)
71 $member = new Group_member();
73 $member->group_id = $group_id;
74 $member->profile_id = $profile_id;
75 $member->created = common_sql_now();
76 $member->uri = self::newURI($profile_id, $group_id, $member->created);
78 $result = $member->insert();
81 common_log_db_error($member, 'INSERT', __FILE__);
82 // TRANS: Exception thrown when joining a group fails.
83 throw new Exception(_("Group join failed."));
89 static function leave($group_id, $profile_id)
91 $member = Group_member::pkeyGet(array('group_id' => $group_id,
92 'profile_id' => $profile_id));
95 // TRANS: Exception thrown when trying to leave a group the user is not a member of.
96 throw new Exception(_("Not part of group."));
99 $result = $member->delete();
102 common_log_db_error($member, 'INSERT', __FILE__);
103 // TRANS: Exception thrown when trying to leave a group fails.
104 throw new Exception(_("Group leave failed."));
112 $member = Profile::staticGet('id', $this->profile_id);
114 if (empty($member)) {
115 // TRANS: Exception thrown providing an invalid profile ID.
116 // TRANS: %s is the invalid profile ID.
117 throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
125 $group = User_group::staticGet('id', $this->group_id);
128 // TRANS: Exception thrown providing an invalid group ID.
129 // TRANS: %s is the invalid group ID.
130 throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
137 * Get stream of memberships by member
139 * @param integer $memberId profile ID of the member to fetch for
140 * @param integer $offset offset from start of stream to get
141 * @param integer $limit number of memberships to get
143 * @return Group_member stream of memberships, use fetch() to iterate
146 static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
148 $membership = new Group_member();
150 $membership->profile_id = $memberId;
152 $membership->orderBy('created DESC');
154 $membership->limit($offset, $limit);
161 function asActivity()
163 $member = $this->getMember();
164 $group = $this->getGroup();
166 $act = new Activity();
168 $act->id = $this->getURI();
170 $act->actor = ActivityObject::fromProfile($member);
171 $act->verb = ActivityVerb::JOIN;
172 $act->objects[] = ActivityObject::fromGroup($group);
174 $act->time = strtotime($this->created);
175 // TRANS: Activity title.
176 $act->title = _("Join");
178 // TRANS: Success message for subscribe to group attempt through OStatus.
179 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
180 $act->content = sprintf(_('%1$s has joined group %2$s.'),
181 $member->getBestName(),
182 $group->getBestName());
184 $url = common_local_url('AtomPubShowMembership',
185 array('profile' => $member->id,
186 'group' => $group->id));
188 $act->selfLink = $url;
189 $act->editLink = $url;
195 * Send notifications via email etc to group administrators about
196 * this exciting new membership!
198 public function notify()
200 mail_notify_group_join($this->getGroup(), $this->getMember());
205 if (!empty($this->uri)) {
208 return self::newURI($this->profile_id, $this->group_id, $this->created);
212 static function newURI($profile_id, $group_id, $created)
214 return TagURI::mint('join:%d:%d:%s',
217 common_date_iso8601($created));