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
28 * @param $className Name of the class
31 protected function __construct ($className = "") {
32 // Is the class name empty? Then this is not a specialized user class
33 if (empty($className)) $className = __CLASS__;
35 // Call parent constructor
36 parent::__construct($className);
38 // Set part description
39 $this->setObjectDescription("Generic user class");
41 // Create unique ID number
42 $this->generateUniqueId();
46 * Destructor to always flush updates
50 public function __destruct () {
51 // Flush any updated entries to the database
52 $this->flushPendingUpdates();
54 // Call parent destructor
59 * Creates an instance of this user class by a provided username. This
60 * factory method will check if the username is already taken and if not
61 * so it will throw an exception.
63 * @param $userName Username we need a class instance for
64 * @return $userInstance An instance of this user class
65 * @throws UsernameMissingException If the username does not exist
67 public final static function createMemberByUsername ($userName) {
69 $userInstance = new Member();
72 $userInstance->setUserName($userName);
74 // Check if the username exists
75 if (!$userInstance->ifUsernameExists()) {
76 // Throw an exception here
77 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
80 // Return the instance
85 * Creates an instance of this user class by a provided email address. This
86 * factory method will not check if the email address is there.
88 * @param $email Email address of the user
89 * @return $userInstance An instance of this user class
91 public final static function createMemberByEmail ($email) {
93 $userInstance = new Member();
96 $userInstance->setEmail($email);
98 // Return the instance
103 * Creates a user by a given request instance
105 * @param $requestInstance An instance of a Requestable class
106 * @return $userInstance An instance of this user class
108 public final static function createMemberByRequest (Requestable $requestInstance) {
109 // Determine if by email or username
110 if (!is_null($requestInstance->getRequestElement('username'))) {
112 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
113 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
115 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
118 $userInstance = new Member();
119 $userInstance->partialStub("We need to add more ways of creating user classes here.");
120 $userInstance->debugBackTrace();
124 // Return the prepared instance
125 return $userInstance;
129 * Adds data for later complete update
131 * @param $column Column we want to update
132 * @param $value New value to store in database
136 public function addUpdateData ($column, $value) {
137 $this->deprecatedMethod("Please use updateDatabaseField() instead!");
138 $this->updateDatabaseField($column, $value);
142 * Updates the last activity timestamp and last performed action in the
143 * database result. You should call flushPendingUpdates() to flush these updates
144 * to the database layer.
146 * @param $requestInstance A requestable class instance
149 public function updateLastActivity (Requestable $requestInstance) {
151 $lastAction = $requestInstance->getRequestElement('action');
153 // If there is no action use the default on
154 if (is_null($lastAction)) {
155 $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
158 // Get a critieria instance
159 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
161 // Add search criteria
162 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
163 $searchInstance->setLimit(1);
165 // Now get another criteria
166 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
168 // And add our both entries
169 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
170 $updateInstance->addCriteria('last_action', $lastAction);
172 // Add the search criteria for searching for the right entry
173 $updateInstance->setSearchInstance($searchInstance);
175 // Remember the update in database result
176 $this->getResultInstance()->add2UpdateQueue($updateInstance);
180 * Flushs all pending updates to the database layer
184 public function flushPendingUpdates () {
185 // Do we have data to update?
186 if ($this->getResultInstance()->ifDataNeedsFlush()) {
187 // Get a database wrapper
188 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
190 // Yes, then send the whole result to the database layer
191 $wrapperInstance->doUpdateByResult($this->getResultInstance());