X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FGroup_member.php;h=14f417758da21890fecf21a944cdc77891c3beb8;hb=7220b3ddd4a32b947e851c946cb6ad6146bebf7d;hp=7b1760f767e489ee068f70e8ce9c3ffee310a424;hpb=430bd69312a27f9f97bda78566a78c9f7eec1f14;p=quix0rs-gnu-social.git diff --git a/classes/Group_member.php b/classes/Group_member.php index 7b1760f767..14f417758d 100644 --- a/classes/Group_member.php +++ b/classes/Group_member.php @@ -3,7 +3,7 @@ * Table Definition for group_member */ -class Group_member extends Memcached_DataObject +class Group_member extends Managed_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -12,20 +12,52 @@ class Group_member extends Memcached_DataObject public $group_id; // int(4) primary_key not_null public $profile_id; // int(4) primary_key not_null public $is_admin; // tinyint(1) + public $uri; // varchar(255) public $created; // datetime() not_null public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP - /* Static get */ - function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_member',$k,$v); } - /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE - function pkeyGet($kv) + public static function schemaDef() { - return Memcached_DataObject::pkeyGet('Group_member', $kv); + 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?'), + 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universal identifier'), + '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'), + 'unique keys' => array( + 'group_member_uri_key' => array('uri'), + ), + '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'), + 'group_member_profile_id_created_idx' => array('profile_id', 'created'), + 'group_member_group_id_created_idx' => array('group_id', 'created'), + ), + ); } + /** + * Method to add a user to a group. + * In most cases, you should call Profile->joinGroup() instead. + * + * @param integer $group_id Group to add to + * @param integer $profile_id Profile being added + * + * @return Group_member new membership object + */ + static function join($group_id, $profile_id) { $member = new Group_member(); @@ -33,15 +65,17 @@ class Group_member extends Memcached_DataObject $member->group_id = $group_id; $member->profile_id = $profile_id; $member->created = common_sql_now(); + $member->uri = self::newURI($profile_id, $group_id, $member->created); $result = $member->insert(); if (!$result) { common_log_db_error($member, 'INSERT', __FILE__); + // TRANS: Exception thrown when joining a group fails. throw new Exception(_("Group join failed.")); } - return true; + return $member; } static function leave($group_id, $profile_id) @@ -50,6 +84,7 @@ class Group_member extends Memcached_DataObject 'profile_id' => $profile_id)); if (empty($member)) { + // TRANS: Exception thrown when trying to leave a group the user is not a member of. throw new Exception(_("Not part of group.")); } @@ -57,9 +92,129 @@ class Group_member extends Memcached_DataObject if (!$result) { common_log_db_error($member, 'INSERT', __FILE__); + // TRANS: Exception thrown when trying to leave a group fails. throw new Exception(_("Group leave failed.")); } return true; } + + function getMember() + { + $member = Profile::getKV('id', $this->profile_id); + + if (empty($member)) { + // 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)); + } + + return $member; + } + + function getGroup() + { + $group = User_group::getKV('id', $this->group_id); + + if (empty($group)) { + // 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)); + } + + return $group; + } + + /** + * 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; + } + + function asActivity() + { + $member = $this->getMember(); + + if (!$member) { + throw new Exception("No such member: " . $this->profile_id); + } + + $group = $this->getGroup(); + + if (!$group) { + throw new Exception("No such group: " . $this->group_id); + } + + $act = new Activity(); + + $act->id = $this->getURI(); + + $act->actor = $member->asActivityObject(); + $act->verb = ActivityVerb::JOIN; + $act->objects[] = ActivityObject::fromGroup($group); + + $act->time = strtotime($this->created); + // TRANS: Activity title. + $act->title = _("Join"); + + // 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. + $act->content = sprintf(_('%1$s has joined group %2$s.'), + $member->getBestName(), + $group->getBestName()); + + $url = common_local_url('AtomPubShowMembership', + array('profile' => $member->id, + 'group' => $group->id)); + + $act->selfLink = $url; + $act->editLink = $url; + + return $act; + } + + /** + * Send notifications via email etc to group administrators about + * this exciting new membership! + */ + public function notify() + { + mail_notify_group_join($this->getGroup(), $this->getMember()); + } + + function getURI() + { + if (!empty($this->uri)) { + return $this->uri; + } else { + return self::newURI($this->profile_id, $this->group_id, $this->created); + } + } + + static function newURI($profile_id, $group_id, $created) + { + return TagURI::mint('join:%d:%d:%s', + $profile_id, + $group_id, + common_date_iso8601($created)); + } }