<?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/Authdata.php
*/
abstract class Plugg_User_Model_Base_Authdata extends Sabai_Model_Entity
{
    public function __construct(Sabai_Model $model)
    {
        parent::__construct('Authdata', $model);
        $this->_vars = array('authdata_id' => 0, 'authdata_created' => 0, 'authdata_updated' => 0, 'authdata_claimed_id' => null, 'authdata_display_id' => null, 'authdata_lastused' => 0, 'authdata_auth_id' => null, 'authdata_userid' => null);
    }

    public function getUserId()
    {
        return $this->getVar('userid');
    }

    public function assignUser($user)
    {
        $this->_setVar('userid', $user->getId());
    }

    protected function _fetchUser($withData = false)
    {
        if (!isset($this->_objects['User'])) {
            $user_id = $this->getUserId();
            $identities = $this->_model->fetchUserIdentities(array($user_id), $withData);
            $this->_objects['User'] = $identities[$user_id];
        }
        return $this->_objects['User'];
    }

    public function isOwnedBy($user)
    {
        return $this->getUserId() == $user->getId();
    }

    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 assignAuth(Sabai_Model_Entity $entity)
    {
        if ($entity->getName() != 'Auth') return false;

        return $this->_assignEntity($entity, 'auth_id');
    }

    public function unassignAuth()
    {
        return $this->_unassignEntity('Auth', 'auth_id');
    }

    protected function _fetchAuth()
    {
        return $this->_fetchEntity('Auth', 'auth_id');
    }

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

    protected function _setVar($name, $value)
    {
        switch ($name) {
        case 'id':
            $this->_vars['authdata_id'] = $value;
            break;
        case 'claimed_id':
            $this->_vars['authdata_claimed_id'] = trim($value);
            break;
        case 'display_id':
            $this->_vars['authdata_display_id'] = trim($value);
            break;
        case 'lastused':
            $this->_vars['authdata_lastused'] = $value;
            break;
        case 'auth_id':
            $this->_vars['authdata_auth_id'] = $value;
            break;
        case 'userid':
            $this->_vars['authdata_userid'] = trim($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 'claimed_id':
            return $this->getVar('claimed_id');
        case 'display_id':
            return $this->getVar('display_id');
        case 'lastused':
            return $this->getVar('lastused');
        case 'Auth':
            return $this->_fetchAuth();
        case 'User':
            return $this->_fetchUser();
        case 'UserWithData':
            return $this->_fetchUser(true);
        }
    }

    public function __set($name, $value)
    {
        switch ($name) {
        case 'claimed_id':
            $this->setVar('claimed_id', $value);
            break;
        case 'display_id':
            $this->setVar('display_id', $value);
            break;
        case 'lastused':
            $this->setVar('lastused', $value);
            break;
        case 'Auth':
            $entity = is_array($value) ? $value[0] : $value;
            $this->assignAuth($entity);
            break;
        }
    }

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

abstract class Plugg_User_Model_Base_AuthdataRepository extends Sabai_Model_EntityRepository
{
    public function __construct(Sabai_Model $model)
    {
        parent::__construct('Authdata', $model);
    }
    public function fetchByUser($id, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeign('authdata_userid', $id, $limit, $offset, $sort, $order);
    }

    public function paginateByUser($id, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntity('User', $id, $perpage, $sort, $order);
    }

    public function countByUser($id)
    {
        return $this->_countByForeign('authdata_userid', $id);
    }

    public function fetchByUserAndCriteria($id, Sabai_Model_Criteria $criteria, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeignAndCriteria('authdata_userid', $id, $criteria, $limit, $offset, $sort, $order);
    }

    public function paginateByUserAndCriteria($id, Sabai_Model_Criteria $criteria, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntityAndCriteria('User', $id, $criteria, $perpage, $sort, $order);
    }

    public function countByUserAndCriteria($id, Sabai_Model_Criteria $criteria)
    {
        return $this->_countByForeignAndCriteria('authdata_userid', $id, $criteria);
    }

    public function fetchByAuth($id, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeign('authdata_auth_id', $id, $limit, $offset, $sort, $order);
    }

    public function paginateByAuth($id, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntity('Auth', $id, $perpage, $sort, $order);
    }

    public function countByAuth($id)
    {
        return $this->_countByForeign('authdata_auth_id', $id);
    }

    public function fetchByAuthAndCriteria($id, Sabai_Model_Criteria $criteria, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeignAndCriteria('authdata_auth_id', $id, $criteria, $limit, $offset, $sort, $order);
    }

    public function paginateByAuthAndCriteria($id, Sabai_Model_Criteria $criteria, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntityAndCriteria('Auth', $id, $criteria, $perpage, $sort, $order);
    }

    public function countByAuthAndCriteria($id, Sabai_Model_Criteria $criteria)
    {
        return $this->_countByForeignAndCriteria('authdata_auth_id', $id, $criteria);
    }

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

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

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

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

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