<?php
/*
This file has been generated by the Sabai scaffold script. Do not edit this file directly.
If you need to customize the class, use the following file:
plugins/User/Model/Role.php
*/
abstract class Plugg_User_Model_Base_Role extends Sabai_Model_Entity
{
    public function __construct(Sabai_Model $model)
    {
        parent::__construct('Role', $model);
        $this->_vars = array('role_id' => 0, 'role_created' => 0, 'role_updated' => 0, 'role_name' => null, 'role_permissions' => null, 'role_system' => 0, 'role_member_count' => 0, 'role_member_last' => 0, 'role_member_lasttime' => 0);
    }

    public function getId()
    {
        return $this->getVar('id');
    }

    public function setId($value)
    {
        $this->setVar('id', $value);
    }

    public function getTimeCreated()
    {
        return $this->getVar('created');
    }

    public function getTimeUpdated()
    {
        return $this->getVar('updated');
    }

    public function getLabel()
    {
        return $this->getVar('name');
    }


    public function getMemberCount()
    {
        return $this->getVar('member_count');
    }

    public function hasLastMember()
    {
        $last_id = $this->getVar('member_last');
        return !empty($last_id);
    }

    public function addMember(Sabai_Model_Entity $entity)
    {
        if ($entity->getName() != 'Member') return false;

        return $this->_addEntity($entity);
    }

    public function removeMember(Sabai_Model_Entity $entity)
    {
        if ($entity->getName() != 'Member') return;

        return $this->removeMemberById($entity->getId());
    }

    public function removeMemberById($id)
    {
        return $this->_removeEntityById('member_id', 'Member', $id);
    }

    public function createMember()
    {
        return $this->_createEntity('Member');
    }

    protected function _fetchMembers($limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchEntities('Member', $limit, $offset, $sort, $order);
    }

    protected function _fetchLastMember()
    {
        if (!isset($this->_objects['LastMember']) && $this->hasLastMember()) {
            $this->_objects['LastMember'] = $this->_fetchEntities('Member', 1, 0, 'member_created', 'DESC')->getNext();
        }
        return $this->_objects['LastMember'];
    }

    public function paginateMembers($perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateEntities('Member', $perpage, $sort, $order);
    }

    public function removeMembers()
    {
        return $this->_removeEntities('Member');
    }

    public function countMembers()
    {
        return $this->_countEntities('Member');
    }

    protected function _getVar($name)
    {
        return $this->_vars['role_' . $name];
    }

    protected function _setVar($name, $value)
    {
        switch ($name) {
        case 'id':
            $this->_vars['role_id'] = $value;
            break;
        case 'name':
            $this->_vars['role_name'] = trim($value);
            break;
        case 'permissions':
            $this->_vars['role_permissions'] = trim($value);
            break;
        case 'system':
            $this->_vars['role_system'] = $value;
            break;
        case 'member_lasttime':
            $this->_vars['role_member_lasttime'] = $value;
            break;
        default:
            trigger_error(sprintf('Error trying to set value for variable %s. This variable is either read-only or does not exist for this entity', $name), E_USER_WARNING);
            return false;
        }
        return true;
    }

    protected function _get($name, $sort, $order)
    {
        switch ($name) {
        case 'name':
            return $this->getVar('name');
        case 'permissions':
            return $this->getVar('permissions');
        case 'system':
            return $this->getVar('system');
        case 'Members':
            return $this->_fetchMembers(0, 0, $sort, $order);
        case 'LastMember':
            return $this->_fetchLastMember();
        }
    }

    public function __set($name, $value)
    {
        switch ($name) {
        case 'name':
            $this->setVar('name', $value);
            break;
        case 'permissions':
            $this->setVar('permissions', $value);
            break;
        case 'system':
            $this->setVar('system', $value);
            break;
        case 'Members':
            $this->removeMembers();
            foreach (array_keys($value) as $i) {
                $this->addMember($value[$i]);
            }
            break;
        }
    }

    public function initVar($name, $value)
    {
        switch ($name) {
        default:
            $this->_vars[$name] = $value;
            break;
        }
    }
}

abstract class Plugg_User_Model_Base_RoleRepository extends Sabai_Model_EntityRepository
{
    public function __construct(Sabai_Model $model)
    {
        parent::__construct('Role', $model);
    }

    protected function _getCollectionByRowset(Sabai_DB_Rowset $rs)
    {
        return new Plugg_User_Model_Base_RolesByRowset($rs, $this->_model->create('Role'), $this->_model);
    }

    public function createCollection(array $entities = array())
    {
        return new Plugg_User_Model_Base_Roles($this->_model, $entities);
    }
}

class Plugg_User_Model_Base_RolesByRowset extends Sabai_Model_EntityCollection_Rowset
{
    public function __construct(Sabai_DB_Rowset $rs, Sabai_Model_Entity $emptyEntity, Sabai_Model $model)
    {
        parent::__construct('Roles', $rs, $emptyEntity, $model);
    }

    protected function _loadRow(Sabai_Model_Entity $entity, array $row)
    {
        $entity->initVars($row);
    }
}

class Plugg_User_Model_Base_Roles extends Sabai_Model_EntityCollection_Array
{
    public function __construct(Sabai_Model $model, array $entities = array())
    {
        parent::__construct($model, 'Roles', $entities);
    }
}