UserRoleModel.php 3.96 KB
<?php
/**
 *
 * @author Adhidarma <adhisimon@gmail.com>
 */
class UserRoleModel extends Model {
    protected $table_name = 'user_roles';
    protected $view_name = 'user_roles_combined';

    protected $table_fields = array(
        'user_id' => array(
            'type' => \DB\SQL\Schema::DT_INT,
            'nullable' => false,
            'index' => false,
            'unique' => false,
        ),

        'role_id' => array(
            'type' => \DB\SQL\Schema::DT_INT,
            'nullable' => false,
            'index' => false,
            'unique' => false,
        ),
    );

    protected $table_indexes = array(
        array(
            'columns' => array('user_id', 'role_id'),
            'unique' => true,
        ),

        array(
            'columns' => array('role_id', 'user_id'),
            'unique' => true,
        ),
    );

    protected $initial_rows = array(
        array(
            'user_id' => 1,
            'role_id' => 1
        ),
    );

    public function getMapperFromView() {
        return new DB\SQL\Mapper($this->getDb(), $this->getViewName());
    }

    public function getRoleIdsByUserId($user_id) {
        $items = $this->getByUserId($user_id);

        $retval = array();
        foreach ($items as $item) {
            $retval[] = $item['role_id'];
        }

        return $retval;
    }

    public function getRoleTitlesByUserId($user_id) {
        $items = $this->getByUserId($user_id);

        $retval = array();
        foreach ($items as $item) {
            $retval[] = $item['role_title'];
        }

        return $retval;
    }

    /**
     * Role-role yang dimiliki seorang user dengan user_id tertentu.
     */
    function getByUserId($user_id) {
        $mapper = $this->getMapperFromView();
        $_roles = $mapper->find(array('user_id=?', $user_id));

        $roles = array();
        foreach ($_roles as $role) {
            $roles[] = $role->cast();
        }

        return $roles;
    }

    /**
     * Apakah sebuah role id dimiliki oleh sebuah user id.
     */
    public function doesRoleIdBelongsToUserId($role_id, $user_id) {
        $role_ids = $this->getRoleIdsByUserId($user_id);
        return in_array($role_id, $role_ids);
    }

    /**
     * Apakah sebuah user id memiliki sebuah role id.
     *
     * Alias dari doesRoleIdBelongsToUserId($role_id, $user_id).
     *
     * @see doesRoleIdBelongsToUserId
     */
    public function doesUserIdHasRoleId($user_id, $role_id) {
        return $this->doesRoleIdBelongsToUserId($role_id, $user_id);
    }

    public function delete($user_id, $role_id) {
        $item = $this->getMapper();
        $item->load(array('user_id=? AND role_id=?', $user_id, $role_id));
        $item->erase();
    }

    final protected function getViewName() {
        return $this->view_name;
    }

    protected function createView() {
        $query = "
            CREATE VIEW " . $this->getViewName() . " AS
            SELECT
                " . $this->getTableName() . ".user_id AS user_id,
                users.email AS user_email,
                " . $this->getTableName() . ".role_id AS role_id,
                roles.title AS role_title
            FROM " . $this->getTableName() . "
            LEFT JOIN users ON
                " . $this->getTableName() . ".user_id=users.id
            LEFT JOIN roles ON
                " . $this->getTableName() . ".role_id=roles.id
        ";

        $db = $this->getDb();
        $retval = $db->exec($query);

        return true;
    }

    public function install() {
        parent::install();

        $test = new Test();

        if ($this->isTableExists($this->getViewName())) {
            $test->expect(
                true,
                'Table ' . $this->getViewName() . ' already exists, skipping it'
            );

            $this->dumpTestResult($test);
            return;
        }

        $test->expect(
            $this->createView(),
            "Create view " . $this->getViewName()
        );

        $this->dumpTestResult($test);
    }
}