<?php
class Application_Model_Row_User extends Zend_Db_Table_Row_Abstract {
	/**
	 * zahashuje heslo pomoci soli
	 * @param string $password
	 */
	public function hashPassword($password) {
		return sha1($password . $this->salt);
	}
	
	/**
	 * Vygeneruje a nastavo novou cookie
	 * @return Application_Model_Row_User
	 */
	public function setCookie() {
		//inicializace generatoru naodnych cisel
		srand(time() + microtime(false));
		
		//vygenerovani zakladni cookie
		$cookie = time();
		
		//separace tabulky
		$tableUsers = $this->getTable();
		
		//generovani cookie, dokud neni nalezena unikatni
		do {
			//hash stareho retezce rozsireneho o nahodne cislo
			$cookie = sha1($cookie . rand(0, 1000));
			
			//kontorla jeslti cookie existuje
			$row = $tableUsers->fetchRow($tableUsers->select(false)
				->where("cookie like ?", $cookie));
		} while ($row);
		
		//nastaveni nove cookie
		$this->cookie = $cookie;
		
		return $this;
	}
	
	public function getRoles() {
		//nacteni roli
		$roleList = $this->findManyToManyRowset("Application_Model_UsersRoles", "Application_Model_UsersHasManyUsersRoles", "user", "role");
		
		$roles = array();
		
		foreach ($roleList as $role) {
			$roles[] = $role->role_name;
		}
		
		return $roles;
	}
}