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'),
54 return Memcached_DataObject::pkeyGet('Group_member', $kv);
58 * Method to add a user to a group.
59 * In most cases, you should call Profile->joinGroup() instead.
61 * @param integer $group_id Group to add to
62 * @param integer $profile_id Profile being added
64 * @return Group_member new membership object
67 static function join($group_id, $profile_id)
69 $member = new Group_member();
71 $member->group_id = $group_id;
72 $member->profile_id = $profile_id;
73 $member->created = common_sql_now();
74 $member->uri = self::newURI($profile_id, $group_id, $member->created);
76 $result = $member->insert();
79 common_log_db_error($member, 'INSERT', __FILE__);
80 // TRANS: Exception thrown when joining a group fails.
81 throw new Exception(_("Group join failed."));
87 static function leave($group_id, $profile_id)
89 $member = Group_member::pkeyGet(array('group_id' => $group_id,
90 'profile_id' => $profile_id));
93 // TRANS: Exception thrown when trying to leave a group the user is not a member of.
94 throw new Exception(_("Not part of group."));
97 $result = $member->delete();
100 common_log_db_error($member, 'INSERT', __FILE__);
101 // TRANS: Exception thrown when trying to leave a group fails.
102 throw new Exception(_("Group leave failed."));
110 $member = Profile::staticGet('id', $this->profile_id);
112 if (empty($member)) {
113 // TRANS: Exception thrown providing an invalid profile ID.
114 // TRANS: %s is the invalid profile ID.
115 throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
123 $group = User_group::staticGet('id', $this->group_id);
126 // TRANS: Exception thrown providing an invalid group ID.
127 // TRANS: %s is the invalid group ID.
128 throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
135 * Get stream of memberships by member
137 * @param integer $memberId profile ID of the member to fetch for
138 * @param integer $offset offset from start of stream to get
139 * @param integer $limit number of memberships to get
141 * @return Group_member stream of memberships, use fetch() to iterate
144 static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
146 $membership = new Group_member();
148 $membership->profile_id = $memberId;
150 $membership->orderBy('created DESC');
152 $membership->limit($offset, $limit);
159 function asActivity()
161 $member = $this->getMember();
162 $group = $this->getGroup();
164 $act = new Activity();
166 $act->id = $this->getURI();
168 $act->actor = ActivityObject::fromProfile($member);
169 $act->verb = ActivityVerb::JOIN;
170 $act->objects[] = ActivityObject::fromGroup($group);
172 $act->time = strtotime($this->created);
173 // TRANS: Activity title.
174 $act->title = _("Join");
176 // TRANS: Success message for subscribe to group attempt through OStatus.
177 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
178 $act->content = sprintf(_('%1$s has joined group %2$s.'),
179 $member->getBestName(),
180 $group->getBestName());
182 $url = common_local_url('AtomPubShowMembership',
183 array('profile' => $member->id,
184 'group' => $group->id));
186 $act->selfLink = $url;
187 $act->editLink = $url;
193 * Send notifications via email etc to group administrators about
194 * this exciting new membership!
196 public function notify()
198 mail_notify_group_join($this->getGroup(), $this->getMember());
203 if (!empty($this->uri)) {
206 return self::newURI($this->member_id, $this->group_id, $this->created);
210 static function newURI($member_id, $group_id, $created)
212 return TagURI::mint('join:%d:%d:%s',
215 common_date_iso8601($created));