2009-01-13 14:35:41 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Table Definition for group_member
|
|
|
|
*/
|
|
|
|
|
2011-08-23 06:52:02 +09:00
|
|
|
class Group_member extends Managed_DataObject
|
2009-01-13 14:35:41 +09:00
|
|
|
{
|
|
|
|
###START_AUTOCODE
|
|
|
|
/* the code below is auto generated do not remove the above tag */
|
|
|
|
|
|
|
|
public $__table = 'group_member'; // table name
|
|
|
|
public $group_id; // int(4) primary_key not_null
|
|
|
|
public $profile_id; // int(4) primary_key not_null
|
|
|
|
public $is_admin; // tinyint(1)
|
2011-08-23 05:36:23 +09:00
|
|
|
public $uri; // varchar(255)
|
2009-01-13 14:35:41 +09:00
|
|
|
public $created; // datetime() not_null
|
|
|
|
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
|
|
|
|
|
|
|
|
/* the code above is auto generated do not remove the tag below */
|
|
|
|
###END_AUTOCODE
|
2009-01-22 11:52:33 +09:00
|
|
|
|
2011-08-23 07:02:29 +09:00
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'fields' => array(
|
|
|
|
'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
|
|
|
|
'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
|
|
|
|
'is_admin' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this user an admin?'),
|
2011-08-23 07:13:02 +09:00
|
|
|
'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'),
|
2011-08-23 07:02:29 +09:00
|
|
|
'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
|
|
|
|
'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
|
|
|
|
),
|
|
|
|
'primary key' => array('group_id', 'profile_id'),
|
2011-08-23 07:13:02 +09:00
|
|
|
'unique keys' => array(
|
|
|
|
'group_member_uri_key' => array('uri'),
|
|
|
|
),
|
2011-08-23 07:02:29 +09:00
|
|
|
'foreign keys' => array(
|
|
|
|
'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
|
|
|
|
'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
|
|
|
|
),
|
|
|
|
'indexes' => array(
|
|
|
|
// @fixme probably we want a (profile_id, created) index here?
|
|
|
|
'group_member_profile_id_idx' => array('profile_id'),
|
|
|
|
'group_member_created_idx' => array('created'),
|
2011-10-21 01:50:39 +09:00
|
|
|
'group_member_profile_id_created_idx' => array('profile_id', 'created'),
|
|
|
|
'group_member_group_id_created_idx' => array('group_id', 'created'),
|
2011-08-23 07:02:29 +09:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-01-07 07:28:40 +09:00
|
|
|
function pkeyGet($kv)
|
2009-01-22 11:52:33 +09:00
|
|
|
{
|
|
|
|
return Memcached_DataObject::pkeyGet('Group_member', $kv);
|
|
|
|
}
|
2010-01-13 19:16:13 +09:00
|
|
|
|
2010-12-14 03:50:39 +09:00
|
|
|
/**
|
|
|
|
* Method to add a user to a group.
|
2011-03-22 06:35:29 +09:00
|
|
|
* In most cases, you should call Profile->joinGroup() instead.
|
2010-12-14 03:50:39 +09:00
|
|
|
*
|
|
|
|
* @param integer $group_id Group to add to
|
|
|
|
* @param integer $profile_id Profile being added
|
|
|
|
*
|
|
|
|
* @return Group_member new membership object
|
|
|
|
*/
|
|
|
|
|
2010-01-13 19:16:13 +09:00
|
|
|
static function join($group_id, $profile_id)
|
|
|
|
{
|
|
|
|
$member = new Group_member();
|
|
|
|
|
|
|
|
$member->group_id = $group_id;
|
|
|
|
$member->profile_id = $profile_id;
|
|
|
|
$member->created = common_sql_now();
|
2011-08-23 05:36:23 +09:00
|
|
|
$member->uri = self::newURI($profile_id, $group_id, $member->created);
|
2010-01-13 19:16:13 +09:00
|
|
|
|
|
|
|
$result = $member->insert();
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
common_log_db_error($member, 'INSERT', __FILE__);
|
2010-07-29 20:01:04 +09:00
|
|
|
// TRANS: Exception thrown when joining a group fails.
|
2010-01-13 19:16:13 +09:00
|
|
|
throw new Exception(_("Group join failed."));
|
|
|
|
}
|
|
|
|
|
2010-12-14 03:50:39 +09:00
|
|
|
return $member;
|
2010-01-13 19:16:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static function leave($group_id, $profile_id)
|
|
|
|
{
|
|
|
|
$member = Group_member::pkeyGet(array('group_id' => $group_id,
|
|
|
|
'profile_id' => $profile_id));
|
|
|
|
|
|
|
|
if (empty($member)) {
|
2010-07-29 20:01:04 +09:00
|
|
|
// TRANS: Exception thrown when trying to leave a group the user is not a member of.
|
2010-01-13 19:16:13 +09:00
|
|
|
throw new Exception(_("Not part of group."));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $member->delete();
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
common_log_db_error($member, 'INSERT', __FILE__);
|
2010-07-29 20:01:04 +09:00
|
|
|
// TRANS: Exception thrown when trying to leave a group fails.
|
2010-01-13 19:16:13 +09:00
|
|
|
throw new Exception(_("Group leave failed."));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-15 00:01:29 +09:00
|
|
|
|
|
|
|
function getMember()
|
|
|
|
{
|
|
|
|
$member = Profile::staticGet('id', $this->profile_id);
|
|
|
|
|
|
|
|
if (empty($member)) {
|
2010-09-29 06:21:09 +09:00
|
|
|
// TRANS: Exception thrown providing an invalid profile ID.
|
|
|
|
// TRANS: %s is the invalid profile ID.
|
|
|
|
throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
|
2010-09-15 00:01:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGroup()
|
|
|
|
{
|
|
|
|
$group = User_group::staticGet('id', $this->group_id);
|
|
|
|
|
|
|
|
if (empty($group)) {
|
2010-09-29 06:21:09 +09:00
|
|
|
// TRANS: Exception thrown providing an invalid group ID.
|
|
|
|
// TRANS: %s is the invalid group ID.
|
|
|
|
throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
|
2010-09-15 00:01:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
2010-12-14 03:50:39 +09:00
|
|
|
/**
|
|
|
|
* Get stream of memberships by member
|
|
|
|
*
|
|
|
|
* @param integer $memberId profile ID of the member to fetch for
|
|
|
|
* @param integer $offset offset from start of stream to get
|
|
|
|
* @param integer $limit number of memberships to get
|
|
|
|
*
|
|
|
|
* @return Group_member stream of memberships, use fetch() to iterate
|
|
|
|
*/
|
|
|
|
|
|
|
|
static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
|
|
|
|
{
|
|
|
|
$membership = new Group_member();
|
|
|
|
|
|
|
|
$membership->profile_id = $memberId;
|
|
|
|
|
|
|
|
$membership->orderBy('created DESC');
|
|
|
|
|
|
|
|
$membership->limit($offset, $limit);
|
|
|
|
|
|
|
|
$membership->find();
|
|
|
|
|
|
|
|
return $membership;
|
|
|
|
}
|
|
|
|
|
2010-09-15 00:01:29 +09:00
|
|
|
function asActivity()
|
|
|
|
{
|
|
|
|
$member = $this->getMember();
|
2013-07-01 01:07:55 +09:00
|
|
|
|
|
|
|
if (!$member) {
|
|
|
|
throw new Exception("No such member: " . $this->profile_id);
|
|
|
|
}
|
|
|
|
|
2010-09-15 00:01:29 +09:00
|
|
|
$group = $this->getGroup();
|
|
|
|
|
2013-07-01 01:07:55 +09:00
|
|
|
if (!$group) {
|
|
|
|
throw new Exception("No such group: " . $this->group_id);
|
|
|
|
}
|
|
|
|
|
2010-09-15 00:01:29 +09:00
|
|
|
$act = new Activity();
|
|
|
|
|
2011-08-23 05:36:23 +09:00
|
|
|
$act->id = $this->getURI();
|
2010-09-15 00:01:29 +09:00
|
|
|
|
2010-09-15 20:11:24 +09:00
|
|
|
$act->actor = ActivityObject::fromProfile($member);
|
|
|
|
$act->verb = ActivityVerb::JOIN;
|
|
|
|
$act->objects[] = ActivityObject::fromGroup($group);
|
2010-09-15 00:01:29 +09:00
|
|
|
|
|
|
|
$act->time = strtotime($this->created);
|
2010-09-29 06:21:09 +09:00
|
|
|
// TRANS: Activity title.
|
2010-09-15 20:11:24 +09:00
|
|
|
$act->title = _("Join");
|
2010-09-15 00:01:29 +09:00
|
|
|
|
|
|
|
// TRANS: Success message for subscribe to group attempt through OStatus.
|
|
|
|
// TRANS: %1$s is the member name, %2$s is the subscribed group's name.
|
2010-09-15 20:11:24 +09:00
|
|
|
$act->content = sprintf(_('%1$s has joined group %2$s.'),
|
2010-09-15 00:01:29 +09:00
|
|
|
$member->getBestName(),
|
|
|
|
$group->getBestName());
|
|
|
|
|
2010-12-14 02:40:44 +09:00
|
|
|
$url = common_local_url('AtomPubShowMembership',
|
|
|
|
array('profile' => $member->id,
|
|
|
|
'group' => $group->id));
|
|
|
|
|
|
|
|
$act->selfLink = $url;
|
|
|
|
$act->editLink = $url;
|
|
|
|
|
2010-09-15 00:01:29 +09:00
|
|
|
return $act;
|
|
|
|
}
|
2011-03-23 08:26:26 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send notifications via email etc to group administrators about
|
|
|
|
* this exciting new membership!
|
|
|
|
*/
|
|
|
|
public function notify()
|
|
|
|
{
|
|
|
|
mail_notify_group_join($this->getGroup(), $this->getMember());
|
|
|
|
}
|
2011-08-23 05:36:23 +09:00
|
|
|
|
|
|
|
function getURI()
|
|
|
|
{
|
|
|
|
if (!empty($this->uri)) {
|
|
|
|
return $this->uri;
|
|
|
|
} else {
|
2011-09-13 02:36:12 +09:00
|
|
|
return self::newURI($this->profile_id, $this->group_id, $this->created);
|
2011-08-23 05:36:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 02:36:12 +09:00
|
|
|
static function newURI($profile_id, $group_id, $created)
|
2011-08-23 05:36:23 +09:00
|
|
|
{
|
|
|
|
return TagURI::mint('join:%d:%d:%s',
|
2011-09-13 02:36:12 +09:00
|
|
|
$profile_id,
|
2011-08-23 05:36:23 +09:00
|
|
|
$group_id,
|
|
|
|
common_date_iso8601($created));
|
|
|
|
}
|
2009-01-13 14:35:41 +09:00
|
|
|
}
|