<?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/Auth.php
*/
abstract class Plugg_User_Model_Base_Auth extends Sabai_Model_Entity
{
    public function __construct(Sabai_Model $model)
    {
        parent::__construct('Auth', $model);
        $this->_vars = array('auth_id' => 0, 'auth_created' => 0, 'auth_updated' => 0, 'auth_name' => null, 'auth_title' => null, 'auth_plugin' => null, 'auth_order' => 0, 'auth_active' => 0, 'auth_authdata_count' => 0, 'auth_authdata_last' => 0, 'auth_authdata_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('title');
    }


    public function getAuthdataCount()
    {
        return $this->getVar('authdata_count');
    }

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

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

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

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

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

    public function removeAuthdataById($id)
    {
        return $this->_removeEntityById('authdata_id', 'Authdata', $id);
    }

    public function createAuthdata()
    {
        return $this->_createEntity('Authdata');
    }

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

    protected function _fetchLastAuthdata()
    {
        if (!isset($this->_objects['LastAuthdata']) && $this->hasLastAuthdata()) {
            $this->_objects['LastAuthdata'] = $this->_fetchEntities('Authdata', 1, 0, 'authdata_created', 'DESC')->getNext();
        }
        return $this->_objects['LastAuthdata'];
    }

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

    public function removeAuthdatas()
    {
        return $this->_removeEntities('Authdata');
    }

    public function countAuthdatas()
    {
        return $this->_countEntities('Authdata');
    }

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

    protected function _setVar($name, $value)
    {
        switch ($name) {
        case 'id':
            $this->_vars['auth_id'] = $value;
            break;
        case 'name':
            $this->_vars['auth_name'] = trim($value);
            break;
        case 'title':
            $this->_vars['auth_title'] = trim($value);
            break;
        case 'plugin':
            $this->_vars['auth_plugin'] = trim($value);
            break;
        case 'order':
            $this->_vars['auth_order'] = $value;
            break;
        case 'active':
            $this->_vars['auth_active'] = $value;
            break;
        case 'authdata_lasttime':
            $this->_vars['auth_authdata_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 'title':
            return $this->getVar('title');
        case 'plugin':
            return $this->getVar('plugin');
        case 'order':
            return $this->getVar('order');
        case 'active':
            return $this->getVar('active');
        case 'Authdatas':
            return $this->_fetchAuthdatas(0, 0, $sort, $order);
        case 'LastAuthdata':
            return $this->_fetchLastAuthdata();
        }
    }

    public function __set($name, $value)
    {
        switch ($name) {
        case 'name':
            $this->setVar('name', $value);
            break;
        case 'title':
            $this->setVar('title', $value);
            break;
        case 'plugin':
            $this->setVar('plugin', $value);
            break;
        case 'order':
            $this->setVar('order', $value);
            break;
        case 'active':
            $this->setVar('active', $value);
            break;
        case 'Authdatas':
            $this->removeAuthdatas();
            foreach (array_keys($value) as $i) {
                $this->addAuthdata($value[$i]);
            }
            break;
        }
    }

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

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

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

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

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

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

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