Group_member.php 7.21 KB
Newer Older
1 2 3 4 5
<?php
/**
 * Table Definition for group_member
 */

6
class Group_member extends Managed_DataObject
7 8 9 10 11 12 13 14
{
    ###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)
15
    public $uri;                             // varchar(191)   not 255 because utf8mb4 takes more space
16 17 18 19 20
    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
21

22 23 24 25 26 27 28
    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?'),
29
                'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
30 31 32 33
                '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'),
34 35 36
            'unique keys' => array(
                'group_member_uri_key' => array('uri'),
            ),
37 38 39 40 41 42 43 44
            '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'),
45 46
                'group_member_profile_id_created_idx' => array('profile_id', 'created'),
                'group_member_group_id_created_idx' => array('group_id', 'created'),
47 48 49 50
            ),
        );
    }

51 52
    /**
     * Method to add a user to a group.
53
     * In most cases, you should call Profile->joinGroup() instead.
54 55 56 57 58 59 60
     *
     * @param integer $group_id   Group to add to
     * @param integer $profile_id Profile being added
     * 
     * @return Group_member new membership object
     */

61 62 63 64 65 66 67
    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();
mmn's avatar
mmn committed
68 69 70
        $member->uri        = self::newUri(Profile::getByID($profile_id),
                                           User_group::getByID($group_id),
                                           $member->created);
71 72 73 74 75

        $result = $member->insert();

        if (!$result) {
            common_log_db_error($member, 'INSERT', __FILE__);
76
            // TRANS: Exception thrown when joining a group fails.
77 78 79
            throw new Exception(_("Group join failed."));
        }

80
        return $member;
81 82 83 84 85 86 87 88
    }

    static function leave($group_id, $profile_id)
    {
        $member = Group_member::pkeyGet(array('group_id' => $group_id,
                                              'profile_id' => $profile_id));

        if (empty($member)) {
89
            // TRANS: Exception thrown when trying to leave a group the user is not a member of.
90 91 92 93 94 95 96
            throw new Exception(_("Not part of group."));
        }

        $result = $member->delete();

        if (!$result) {
            common_log_db_error($member, 'INSERT', __FILE__);
97
            // TRANS: Exception thrown when trying to leave a group fails.
98 99 100 101 102
            throw new Exception(_("Group leave failed."));
        }

        return true;
    }
103 104 105

    function getMember()
    {
106
        $member = Profile::getKV('id', $this->profile_id);
107 108

        if (empty($member)) {
109 110 111
            // 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));
112 113 114 115 116 117 118
        }

        return $member;
    }

    function getGroup()
    {
119
        $group  = User_group::getKV('id', $this->group_id);
120 121

        if (empty($group)) {
122 123 124
            // 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));
125 126 127 128 129
        }

        return $group;
    }

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /**
     * 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;
    }

155 156 157
    function asActivity()
    {
        $member = $this->getMember();
158 159 160 161 162

        if (!$member) {
            throw new Exception("No such member: " . $this->profile_id);
        }

163 164
        $group  = $this->getGroup();

165 166 167 168
        if (!$group) {
            throw new Exception("No such group: " . $this->group_id);
        }

169 170
        $act = new Activity();

mmn's avatar
mmn committed
171
        $act->id = $this->getUri();
172

173
        $act->actor     = $member->asActivityObject();
174 175
        $act->verb      = ActivityVerb::JOIN;
        $act->objects[] = ActivityObject::fromGroup($group);
176 177

        $act->time  = strtotime($this->created);
178
        // TRANS: Activity title.
179
        $act->title = _("Join");
180 181 182

        // 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.
183
        $act->content = sprintf(_('%1$s has joined group %2$s.'),
184 185 186
                                $member->getBestName(),
                                $group->getBestName());

187 188 189 190 191 192 193
        $url = common_local_url('AtomPubShowMembership',
                                array('profile' => $member->id,
                                      'group' => $group->id));

        $act->selfLink = $url;
        $act->editLink = $url;

194 195
        return $act;
    }
196 197 198 199 200 201 202 203 204

    /**
     * Send notifications via email etc to group administrators about
     * this exciting new membership!
     */
    public function notify()
    {
        mail_notify_group_join($this->getGroup(), $this->getMember());
    }
205

mmn's avatar
mmn committed
206
    function getUri()
207
    {
mmn's avatar
mmn committed
208
        return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
209
    }
210
}