<?php defined('SYSPATH') OR die('No direct access allowed.');

class Model_User extends Model_Auth_User {
	
	// No Problem
	protected $_belongs_to = array( 'university' => array('model' => 'university', 'foreign_key' => 'university_id'),
                                        'department' => array('model' => 'department', 'foreign_key' => 'department_id')
                                       );
	
	public function __toString()
	{
		return $this->username;
	}
	
	public function validate_create(& $array ) 
	{
		// Initialise the validation library and setup some rules		
		$array = Validate::factory($array)
                
		->rules('password', $this->_rules['password'])
		->rules('username', $this->_rules['username'])
		->rules('email', $this->_rules['email'])
		->rules('password_confirm', $this->_rules['password_confirm'])
		->filter('username', 'trim')
		->filter('email', 'trim')
		->filter('password', 'trim')
		->filter('password_confirm', 'trim');
		return $array;
	}
	
	public function userImageUpload($imageName, $me){
		
		$userRecord = ORM::factory('user') 	-> where('id', '=', $me)
							-> find();
		$userRecord->image = $imageName;
		$userRecord->save();
	}
	
	public function registerUser($register){
		
		$user 	= ORM::factory('user');
                //throw new Exception($register["university_id"]);
		$user->values($register);
		$user->save();
		
		#Add the login role to the user
		$login_role = new Model_Role(array('name' =>'login'));
		$user->add('roles',$login_role);
	}
}