diff options
Diffstat (limited to 'demos/time-tracker/protected/App_Code')
10 files changed, 664 insertions, 0 deletions
| diff --git a/demos/time-tracker/protected/App_Code/BaseDao.php b/demos/time-tracker/protected/App_Code/BaseDao.php new file mode 100644 index 00000000..63b91def --- /dev/null +++ b/demos/time-tracker/protected/App_Code/BaseDao.php @@ -0,0 +1,45 @@ +<?php
 +/**
 + * Base DAO class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * Base DAO class.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class BaseDao
 +{
 +	/**
 +	 * @var TSqlMapper sqlmap client.
 +	 */
 +	private $_connection;
 +	
 +	/**
 +	 * @param TSqlMapper sqlmap client.
 +	 */
 +	public function setConnection($connection)
 +	{
 +		$this->_connection = $connection;
 +	}
 +	
 +	/**
 +	 * @return TSqlMapper sqlmap client.
 +	 */
 +	protected function getConnection()
 +	{
 +		return $this->_connection;
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/DaoManager.php b/demos/time-tracker/protected/App_Code/DaoManager.php new file mode 100644 index 00000000..b8ac55af --- /dev/null +++ b/demos/time-tracker/protected/App_Code/DaoManager.php @@ -0,0 +1,126 @@ +<?php
 +/**
 + * DaoManager class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 + 
 +/**
 + * DaoManager class.
 + * 
 + * A Registry for Dao and an implementation of that type.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class DaoManager extends TModule
 +{
 +	/**
 +	 * @var TSqlMapper sqlmap client
 +	 */
 +	private $_connection;
 +	/**
 +	 * @var boolean if the module has been initialized
 +	 */
 +	private $_initialized=false;	
 +	/**
 +	 * @var array registered list of dao
 +	 */
 +	private $_dao=array();
 +	/**
 +	 * Initializes the module.
 +	 * This method is required by IModule and is invoked by application.
 +	 * It loads dao information from the module configuration.
 +	 * @param TXmlElement module configuration
 +	 */
 +	public function init($config)
 +	{ 	
 +		if($this->_connection === null)
 +			throw new TimeTrackerException('daomanager_connection_required');
 +		$app = $this->getApplication();
 +		if(is_string($this->_connection))
 +		{
 +			if(($conn=$app->getModule($this->_connection)->getClient())===null)
 +				throw new TimeTrackerException('daomanager_undefined_connection',$this->_connection);
 +			if(!($conn instanceof TSqlMapper))
 +				throw new TimeTrackerException('daomanager_invalid_connection',	$this->_connection);
 +			$this->_connection = $conn;
 +		}
 +		$this->includeDaoImplementation($config->getElementsByTagName('dao'));
 +		$this->_initialized = true;
 +	}
 +	
 +	/**
 +	 * Register the dao type and implementation class names.
 +	 * @param array list of TXmlDocument nodes.
 +	 */
 +	protected function includeDaoImplementation($nodes)
 +	{
 +		foreach($nodes as $node)
 +		{
 +			$id = $node->getAttribute('id');
 +			$class = $node->getAttribute('class');
 +			$this->_dao[$id] = array('class' => $class);
 +		}
 +	}
 +	
 +	/**
 +	 * @return array list of registered Daos
 +	 */
 +	public function getDaos()
 +	{
 +		return $this->_dao;
 +	}
 +	
 +	/**
 +	 * Returns an implementation of a Dao type, implements the Registery
 +	 * pattern. Multiple calls returns the same Dao instance.
 +	 * @param string Dao type to find.
 +	 * @return object instance of the Dao implementation.
 +	 */
 +	public function getDao($class)
 +	{
 +		if(isset($this->_dao[$class]))
 +		{
 +			if(!isset($this->_dao[$class]['instance']))
 +			{
 +				$dao = Prado::createComponent($this->_dao[$class]['class']);
 +				$dao->setConnection($this->getConnection());
 +				$this->_dao[$class]['instance'] = $dao;	
 +			}
 +			return $this->_dao[$class]['instance'];
 +		}
 +		else
 +			throw TimeTrackerException('daomanager_undefined_dao', $class);
 +	}
 +	
 +	/**
 +	 * @return TSqlMapper sqlmap client instance
 +	 */
 +	public function getConnection()
 +	{
 +		return $this->_connection;
 +	}
 +	
 +	/**
 +	 * Sets the connection for all Daos registered.
 +	 * @param string|TSqlMapper sqlmap client module id or TSqlMapper instance.
 +	 */
 +	public function setConnection($client)
 +	{
 +		if($this->_initialized)
 +			throw new TimeTrackerException('daomanager_unchangeable');
 +		if(!is_string($client) && !($client instanceof TSqlMapper))
 +			throw new TConfigurationException('daomanager_invalid_connection',$client);
 +		$this->_connection = $client;
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/Project.php b/demos/time-tracker/protected/App_Code/Project.php new file mode 100644 index 00000000..660fad04 --- /dev/null +++ b/demos/time-tracker/protected/App_Code/Project.php @@ -0,0 +1,35 @@ +<?php
 +/**
 + * Project class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * Time Tracker Project class.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class Project
 +{
 +	public $ActualDuration = 0;
 +	public $CreatorUserName = '';
 +	public $CompletionDate = 0;
 +	public $DateCreated = 0;
 +	public $Description = '';
 +	public $EstimateDuration = 0;
 +	public $ID = 0;
 +	public $ManagerUserName = '';
 +	public $Name = '';
 +}
 +
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/ProjectDao.php b/demos/time-tracker/protected/App_Code/ProjectDao.php new file mode 100644 index 00000000..81902e0c --- /dev/null +++ b/demos/time-tracker/protected/App_Code/ProjectDao.php @@ -0,0 +1,93 @@ +<?php
 +/**
 + * Project DAO class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * Project DAO class.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class ProjectDao extends BaseDao
 +{
 +/*	public function createNewProject($project)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$creator = $sqlmap->queryForObject('GetUserByName', $project->CreatorUserName);
 +		$manager = $sqlmap->queryForObject('GetUserByName', $project->ManagerUserName);
 +		$exists = $sqlmap->queryForObject('GetProjectByName', $project->Name);
 +		if($exists)
 +		{
 +			throw new TimeTrackerException(
 +				'project_exists', $project->Name);
 +		}
 +		else if(!$creator || !$manager)
 +		{
 +			throw new TimeTrackerException(
 +				'invalid_creator_and_manager',
 +				$project->Name, $project->CreatorUserName, 
 +				$project->ManagerUserName);	
 +		}
 +		else
 +		{
 +			$param['project'] = $project;
 +			$param['creator'] = $creator->ID;
 +			$param['manager'] = $manager->ID; 
 +			return $sqlmap->insert('CreateNewProject', $param);			
 +		}
 +	} 
 +	
 +	public function getProjectByID($projectID)
 +	{
 +		$sqlmap = $this->getConnection();
 +		return $sqlmap->queryForObject('GetProjectByID', $projectID);
 +	}
 +	
 +	public function addUserToProject($project, $user)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$project = $this->getProjectByID($project->ID);
 +		$user = $sqlmap->queryForObject('GetUserByName', $user->Name);
 +		$list = $sqlmap->queryForList('GetProjectMembers', $project);
 +		$userExists = false;
 +		foreach($list as $k)
 +		{
 +			if($k->ID == $user->ID)
 +				$userExists = true;
 +		}
 +		if(!$project)
 +		{
 +			throw new TimeTrackerException(
 +				'invalid_project', $project->Name);			
 +		}
 +		else if(!$user)
 +		{
 +			throw new TimeTrackerException(
 +				'invalid_user', $user->Name);	
 +		}
 +		else if($userExists)
 +		{
 +			throw new TimeTrackerException(
 +				'project_member_exists', $projet->Name, $user->Name);
 +		}
 +		else
 +		{
 +			$param['project'] = $project;
 +			$param['user'] = $user;
 +			return $sqlmap->insert('AddUserToProject', $param);
 +		}	
 +	}
 +*/
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/TimeTrackerException.php b/demos/time-tracker/protected/App_Code/TimeTrackerException.php new file mode 100644 index 00000000..64b11405 --- /dev/null +++ b/demos/time-tracker/protected/App_Code/TimeTrackerException.php @@ -0,0 +1,33 @@ +<?php
 +/**
 + * TimeTrackerException class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * Generic time tracker application exception. Exception messages are saved in
 + * "exceptions.txt"
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class TimeTrackerException extends TException
 +{
 +	/**
 +	 * @return string path to the error message file
 +	 */
 +	protected function getErrorMessageFile()
 +	{
 +		return dirname(__FILE__).'/exceptions.txt';
 +	}	
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/TimeTrackerUser.php b/demos/time-tracker/protected/App_Code/TimeTrackerUser.php new file mode 100644 index 00000000..99ac1209 --- /dev/null +++ b/demos/time-tracker/protected/App_Code/TimeTrackerUser.php @@ -0,0 +1,48 @@ +<?php
 +/**
 + * TimeTrackerUser class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * Import TUser and TUserManager
 + */
 +Prado::using('System.Security.TUser');
 +Prado::using('System.Security.TUserManager');
 +
 +/**
 + * User class for Time Tracker application.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class TimeTrackerUser extends TUser
 +{
 +	private $_emailAddress;
 +	
 +	/**
 +	 * @param string user email address
 +	 */
 +	public function setEmailAddress($value)
 +	{
 +		$this->_emailAddress = $value;
 +	}
 +	
 +	/**
 +	 * @return string user email address
 +	 */
 +	public function getEmailAddress()
 +	{
 +		return $this->_emailAddress;
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/TimeTrackerUserTypeHandler.php b/demos/time-tracker/protected/App_Code/TimeTrackerUserTypeHandler.php new file mode 100644 index 00000000..07c46acc --- /dev/null +++ b/demos/time-tracker/protected/App_Code/TimeTrackerUserTypeHandler.php @@ -0,0 +1,54 @@ +<?php
 +/**
 + * TimeTrackerUserTypeHandler class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * SQLMap type handler for TimeTrackerUser.
 + * The TimeTrackerUser requires an instance of IUserManager in constructor.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class TimeTrackerUserTypeHandler implements ITypeHandlerCallback
 +{
 +	/**
 +	 * Not implemented.
 +	 */
 +	public function getParameter($object)
 +	{
 +		throw new TimeTrackerException('Not implemented');
 +	}
 +
 +	/**
 +	 * Not implemented.
 +	 */
 +	public function getResult($string)
 +	{
 +		throw new TimeTrackerException('Not implemented');		
 +	}
 +
 +	/**
 +	 * Creates a new instance of TimeTrackerUser
 +	 * @param array result data
 +	 * @return TimeTrackerUser new user instance
 +	 */
 +	public function createNewInstance($row=null)
 +	{
 +		$manager = Prado::getApplication()->getModule('users');
 +		if(is_null($manager))
 +			$manager = new UserManager();
 +		return new TimeTrackerUser($manager);
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/UserDao.php b/demos/time-tracker/protected/App_Code/UserDao.php new file mode 100644 index 00000000..4dc39b2b --- /dev/null +++ b/demos/time-tracker/protected/App_Code/UserDao.php @@ -0,0 +1,155 @@ +<?php
 +/**
 + * User Dao class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * UserDao class list, create, find and delete users. 
 + * In addition, it can validate username and password, and update
 + * the user roles. Furthermore, a unique new token can be generated,
 + * this token can be used to perform persistent cookie login.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class UserDao extends BaseDao
 +{
 +	/**
 +	 * @param string username
 +	 * @return TimeTrackerUser find by user name, null if not found or disabled.
 +	 */
 +	public function getUserByName($username)
 +	{
 +		$sqlmap = $this->getConnection();
 +		return $sqlmap->queryForObject('GetUserByName', $username);	
 +	}
 +	
 +	/**
 +	 * @return array list of all enabled users.
 +	 */
 +	public function getAllUsers()
 +	{
 +		$sqlmap = $this->getConnection();
 +		return $sqlmap->queryForList('GetAllUsers');
 +	}
 +	
 +	/**
 +	 * @param TimeTrackerUser new user details.
 +	 * @param string new user password.
 +	 */
 +	public function addNewUser($user, $password)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$param['user'] = $user;
 +		$param['password'] = md5($password);
 +		$sqlmap->insert('AddNewUser', $param);
 +		if(count($user->getRoles()) > 0)
 +			$this->updateUserRoles($user);
 +	}
 +	
 +	/**
 +	 * @param string username to delete
 +	 */
 +	public function deleteUserByName($username)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$sqlmap->delete('DeleteUserByName', $username);
 +	}
 +
 +	/**
 +	 * Updates the user profile details, including user roles.
 +	 * @param TimeTrackerUser updated user details.
 +	 * @param string new user password, null to avoid updating password.
 +	 */
 +	public function updateUser($user,$password=null)
 +	{
 +		$sqlmap = $this->getConnection();
 +		if($password !== null)
 +		{
 +			$param['user'] = $user;
 +			$param['password'] = md5($password);
 +			$sqlmap->update('UpdateUserDetailsAndPassword', $param);
 +		}
 +		else
 +		{
 +			$sqlmap->update('UpdateUserDetails', $user);
 +		}
 +		$this->updateUserRoles($user);
 +	}
 +
 +	/**
 +	 * @param string username to be validated
 +	 * @param string matching password
 +	 * @return boolean true if the username and password matches.
 +	 */
 +	public function validateUser($username, $password)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$param['username'] = $username;
 +		$param['password'] = md5($password);
 +		return $sqlmap->queryForObject('ValidateUser', $param);
 +	}
 +		
 +	/**
 +	 * @param string unique persistent session token
 +	 * @return TimeTrackerUser user details if valid token, null otherwise.
 +	 */
 +	public function validateSignon($token)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$sqlmap->update('UpdateSignon', $token);
 +		return $sqlmap->queryForObject('ValidateAutoSignon', $token);
 +	}
 +	
 +	/**
 +	 * @param TimeTrackerUser user details to generate the token
 +	 * @return string unique persistent login token.
 +	 */
 +	public function createSignonToken($user)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$param['username'] = $user->getName();
 +		$param['token'] = md5(microtime().$param['username']);
 +		$sqlmap->insert('RegisterAutoSignon', $param);
 +		return $param['token'];
 +	}
 +	
 +	/**
 +	 * @param TimeTrackerUser deletes all signon token for given user, null to delete all
 +	 * tokens.
 +	 */
 +	public function clearSignonTokens($user=null)
 +	{
 +		$sqlmap = $this->getConnection();
 +		if($user !== null)
 +			$sqlmap->delete('DeleteAutoSignon', $user->getName());
 +		else
 +			$sqlmap->delete('DeleteAllSignon');
 +	}
 +	
 +	/**
 +	 * @param TimeTrackerUser user details for updating the assigned roles.
 +	 */
 +	public function updateUserRoles($user)
 +	{
 +		$sqlmap = $this->getConnection();
 +		$sqlmap->delete('DeleteUserRoles', $user);
 +		foreach($user->getRoles() as $role)
 +		{
 +			$param['username'] = $user->getName();
 +			$param['role'] = $role;
 +			$sqlmap->update('AddUserRole', $param);
 +		}
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/UserManager.php b/demos/time-tracker/protected/App_Code/UserManager.php new file mode 100644 index 00000000..1327dc3c --- /dev/null +++ b/demos/time-tracker/protected/App_Code/UserManager.php @@ -0,0 +1,68 @@ +<?php
 +/**
 + * UserManager class file.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @link http://www.pradosoft.com/
 + * @copyright Copyright © 2005-2006 PradoSoft
 + * @license http://www.pradosoft.com/license/
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + */
 +
 +/**
 + * User manager module class for time tracker application.
 + *
 + * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 + * @version $Revision: $  $16/07/2006: $
 + * @package Demos
 + * @since 3.1
 + */
 +class UserManager extends TModule implements IUserManager
 +{
 +	/**
 +	 * @return string name for a guest user.
 +	 */
 +	public function getGuestName()
 +	{
 +		return 'Guest';
 +	}
 +	
 +	/**
 +	 * Returns a user instance given the user name.
 +	 * @param string user name, null if it is a guest.
 +	 * @return TUser the user instance, null if the specified username is not in the user database.
 +	 */
 +	public function getUser($username=null)
 +	{
 +		if($username===null)
 +		{
 +			$user=new TUser($this);
 +			$user->setIsGuest(true);
 +			return $user;
 +		}
 +		else
 +		{
 +			$daos = $this->getApplication()->getModule('daos');
 +			$userDao = $daos->getDao('UserDao');
 +			$user = $userDao->getUserByName($username);
 +			$user->setIsGuest(false);
 +			return $user;
 +		}
 +	}
 +	
 +	/**
 +	 * Validates if the username and password are correct.
 +	 * @param string user name
 +	 * @param string password
 +	 * @return boolean true if validation is successful, false otherwise.
 +	 */
 +	public function validateUser($username,$password)
 +	{
 +		$daos = $this->getApplication()->getModule('daos');
 +		$userDao = $daos->getDao('UserDao');
 +		return $userDao->validateUser($username, $password);
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/demos/time-tracker/protected/App_Code/exceptions.txt b/demos/time-tracker/protected/App_Code/exceptions.txt new file mode 100644 index 00000000..6568cc72 --- /dev/null +++ b/demos/time-tracker/protected/App_Code/exceptions.txt @@ -0,0 +1,7 @@ +timetracker_user_readonly_id 	= Time tracker user ID is read-only.
 +invalid_creator_and_manager		= Unable to find time tracker usernames '{1}' and '{2}' for project '{0}'.
 +project_exists					= Project '{0}' already exists.
 +daomanager_connection_required	= An TSqlMapper connection is required by Dao Manager.
 +daomanager_undefined_connection	= Connection '{0}' for Dao Manager is undefined.
 +daomanager_invalid_connection	= Connection '{0}' does not appear to ba a TSqlMapper in Dao Manager.
 +daomanager_undefined_dao		= Dao class '{0}' is not registered.
\ No newline at end of file | 
