123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- defined('GNUSOCIAL') || die();
- class Group_member extends Managed_DataObject
- {
-
-
- public $__table = 'group_member';
- public $group_id;
- public $profile_id;
- public $is_admin;
- public $uri;
- public $created;
- public $modified;
-
-
- 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' => 'bool', 'default' => false, 'description' => 'is this user an admin?'),
- 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
- 'created' => array('type' => 'datetime', '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(
- 'group_member_profile_id_created_group_id_idx' => array('profile_id', 'created', 'group_id'),
- 'group_member_group_id_created_profile_id_idx' => array('profile_id', 'created', 'group_id'),
- ),
- );
- }
-
- public 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();
- $member->uri = self::newUri(
- Profile::getByID($profile_id),
- User_group::getByID($group_id),
- $member->created
- );
- $result = $member->insert();
- if (!$result) {
- common_log_db_error($member, 'INSERT', __FILE__);
-
- throw new Exception(_("Group join failed."));
- }
- return $member;
- }
- public static function leave($group_id, $profile_id)
- {
- $member = Group_member::pkeyGet(array('group_id' => $group_id,
- 'profile_id' => $profile_id));
- if (empty($member)) {
-
- throw new Exception(_("Not part of group."));
- }
- $result = $member->delete();
- if (!$result) {
- common_log_db_error($member, 'INSERT', __FILE__);
-
- throw new Exception(_("Group leave failed."));
- }
- return true;
- }
- public function getMember()
- {
- $member = Profile::getKV('id', $this->profile_id);
- if (empty($member)) {
-
-
- throw new Exception(sprintf(_("Profile ID %s is invalid."), $this->profile_id));
- }
- return $member;
- }
- public function getGroup()
- {
- $group = User_group::getKV('id', $this->group_id);
- if (empty($group)) {
-
-
- throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id));
- }
- return $group;
- }
-
- public static function byMember($memberId, $offset = 0, $limit = GROUPS_PER_PAGE)
- {
- $membership = new Group_member();
- $membership->profile_id = $memberId;
- $membership->orderBy('created DESC, group_id DESC');
- $membership->limit($offset, $limit);
- $membership->find();
- return $membership;
- }
- public 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);
-
- $act->title = _("Join");
-
-
- $act->content = sprintf(
- _('%1$s has joined group %2$s.'),
- $member->getBestName(),
- $group->getBestName()
- );
- $url = common_local_url(
- 'AtomPubShowMembership',
- [
- 'profile' => $member->id,
- 'group' => $group->id,
- ]
- );
- $act->selfLink = $url;
- $act->editLink = $url;
- return $act;
- }
-
- public function notify()
- {
- mail_notify_group_join($this->getGroup(), $this->getMember());
- }
- public function getUri()
- {
- return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
- }
- }
|