3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class Member extends BaseUser implements ManageableMember, Registerable, Updateable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Destructor to always flush updates
40 public function __destruct () {
41 // Flush any updated entries to the database
42 $this->flushPendingUpdates();
44 // Call parent destructor
49 * Creates an instance of this user class by a provided username. This
50 * factory method will check if username is already taken and if not so it
51 * will throw an exception.
53 * @param $userName Username we need a class instance for
54 * @return $userInstance An instance of this user class
55 * @throws UsernameMissingException If the username does not exist
57 public final static function createMemberByUsername ($userName) {
59 $userInstance = new Member();
62 $userInstance->setUserName($userName);
64 // Check if username exists
65 if ($userInstance->ifUsernameExists() === false) {
66 // Throw an exception here
67 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
70 // Return the instance
75 * Creates an instance of this user class by a provided email address. This
76 * factory method will not check if email address is there.
78 * @param $email Email address of the user
79 * @return $userInstance An instance of this user class
81 public final static function createMemberByEmail ($email) {
83 $userInstance = new Member();
86 $userInstance->setEmail($email);
88 // Return the instance
93 * Creates a user by a given request instance
95 * @param $requestInstance An instance of a Requestable class
96 * @return $userInstance An instance of this user class
97 * @todo Add more ways over creating user classes
99 public final static function createMemberByRequest (Requestable $requestInstance) {
100 // Determine if by email or username
101 if (!is_null($requestInstance->getRequestElement('username'))) {
103 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
104 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
106 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
109 $userInstance = new Member();
110 $userInstance->partialStub("We need to add more ways of creating user classes here.");
111 $userInstance->debugBackTrace();
115 // Return the prepared instance
116 return $userInstance;
120 * Updates the last activity timestamp and last performed action in the
121 * database result. You should call flushPendingUpdates() to flush these updates
122 * to the database layer.
124 * @param $requestInstance A requestable class instance
127 public function updateLastActivity (Requestable $requestInstance) {
129 $lastAction = $requestInstance->getRequestElement('action');
131 // If there is no action use the default on
132 if (is_null($lastAction)) {
133 $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
136 // Get a critieria instance
137 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139 // Add search criteria
140 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
141 $searchInstance->setLimit(1);
143 // Now get another criteria
144 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
146 // And add our both entries
147 $updateInstance->addCriteria("last_activity", date("Y-m-d H:i:s", time()));
148 $updateInstance->addCriteria("last_action", $lastAction);
150 // Add the search criteria for searching for the right entry
151 $updateInstance->setSearchInstance($searchInstance);
153 // Remember the update in database result
154 $this->getResultInstance()->add2UpdateQueue($updateInstance);
158 * Flushs all pending updates to the database layer
162 public function flushPendingUpdates () {
163 // Do we have data to update?
164 if ($this->getResultInstance()->ifDataNeedsFlush()) {
165 // Get a database wrapper
166 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
168 // Yes, then send the whole result to the database layer
169 $wrapperInstance->doUpdateByResult($this->getResultInstance());