GitHost.io will be shut down on June 1, 2019. At that point this instance will be unreachable and all data will be irrevocably deleted. More details at https://about.gitlab.com/gitlab-hosted/#githost-is-shutting-down-on-june-1st-2019

Group_join_queue.php 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
 * Table Definition for request_queue
 */
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';

class Group_join_queue extends Managed_DataObject
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    public $__table = 'group_join_queue';       // table name
    public $profile_id;
    public $group_id;
    public $created;

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE

    public static function schemaDef()
    {
        return array(
            'description' => 'Holder for group join requests awaiting moderation.',
            'fields' => array(
                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
                'group_id' => array('type' => 'int', 'description' => 'remote or local group to join, if any'),
                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
            ),
            'primary key' => array('profile_id', 'group_id'),
            'indexes' => array(
                'group_join_queue_profile_id_created_idx' => array('profile_id', 'created'),
                'group_join_queue_group_id_created_idx' => array('group_id', 'created'),
            ),
            'foreign keys' => array(
                'group_join_queue_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
                'group_join_queue_group_id_fkey' => array('user_group', array('group_id' => 'id')),
            )
        );
    }

    public static function saveNew(Profile $profile, User_group $group)
    {
        $rq = new Group_join_queue();
        $rq->profile_id = $profile->id;
        $rq->group_id = $group->id;
        $rq->created = common_sql_now();
        $rq->insert();
        return $rq;
    }
50

51 52
    function getMember()
    {
53
        $member = Profile::getKV('id', $this->profile_id);
54 55 56 57

        if (empty($member)) {
            // TRANS: Exception thrown providing an invalid profile ID.
            // TRANS: %s is the invalid profile ID.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
58
            throw new Exception(sprintf(_('Profile ID %s is invalid.'),$this->profile_id));
59 60 61 62 63 64 65
        }

        return $member;
    }

    function getGroup()
    {
66
        $group  = User_group::getKV('id', $this->group_id);
67 68 69 70

        if (empty($group)) {
            // TRANS: Exception thrown providing an invalid group ID.
            // TRANS: %s is the invalid group ID.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
71
            throw new Exception(sprintf(_('Group ID %s is invalid.'),$this->group_id));
72 73 74 75 76 77 78 79 80 81 82 83
        }

        return $group;
    }

    /**
     * Abort the pending group join...
     */
    function abort()
    {
        $profile = $this->getMember();
        $group = $this->getGroup();
84 85 86 87

        if (Event::handle('StartCancelJoinGroup', array($profile, $group))) {
            $this->delete();
            Event::handle('EndCancelJoinGroup', array($profile, $group));
88 89 90 91 92 93 94 95
        }
    }

    /**
     * Complete a pending group join...
     *
     * @return Group_member object on success
     */
96
    function complete()
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    {
        $join = null;
        $profile = $this->getMember();
        $group = $this->getGroup();
        if (Event::handle('StartJoinGroup', array($profile, $group))) {
            $join = Group_member::join($group->id, $profile->id);
            $this->delete();
            Event::handle('EndJoinGroup', array($profile, $group));
        }
        if (!$join) {
            throw new Exception('Internal error: group join failed.');
        }
        $join->notify();
        return $join;
    }

113 114 115 116 117 118
    /**
     * Send notifications via email etc to group administrators about
     * this exciting new pending moderation queue item!
     */
    public function notify()
    {
119 120
        $joiner = Profile::getKV('id', $this->profile_id);
        $group = User_group::getKV('id', $this->group_id);
121 122
        mail_notify_group_join_pending($group, $joiner);
    }
123
}