3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
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 {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this user class by a provided username. This
37 * factory method will check if username is already taken and if not so it
38 * will throw an exception.
40 * @param $userName Username we need a class instance for
41 * @return $userInstance An instance of this user class
42 * @throws UsernameMissingException If the username does not exist
44 public final static function createMemberByUsername ($userName) {
46 $userInstance = new Member();
49 $userInstance->setUserName($userName);
51 // Check if username exists
52 if ($userInstance->ifUsernameExists() === false) {
53 // Throw an exception here
54 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
57 // Return the instance
62 * Creates an instance of this user class by a provided email address. This
63 * factory method will not check if email address is there.
65 * @param $email Email address of the user
66 * @return $userInstance An instance of this user class
68 public final static function createMemberByEmail ($email) {
70 $userInstance = new Member();
73 $userInstance->setEmail($email);
75 // Return the instance
80 * Creates a user by a given request instance
82 * @param $requestInstance An instance of a Requestable class
83 * @return $userInstance An instance of this user class
84 * @todo Add more ways over creating user classes
86 public final static function createMemberByRequest (Requestable $requestInstance) {
87 // Determine if by email or username
88 if (!is_null($requestInstance->getRequestElement('username'))) {
90 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
91 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
93 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
96 $userInstance = new Member();
97 $userInstance->partialStub("We need to add more ways of creating user classes here.");
98 $userInstance->debugBackTrace();
102 // Return the prepared instance
103 return $userInstance;
107 * Updates the last activity timestamp and last performed action in the
108 * database result. You should call flushPendingUpdates() to flush these updates
109 * to the database layer.
111 * @param $requestInstance A requestable class instance
114 public function updateLastActivity (Requestable $requestInstance) {
116 $lastAction = $requestInstance->getRequestElement('action');
118 // If there is no action use the default on
119 if (is_null($lastAction)) {
120 $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
123 // Get a critieria instance
124 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
126 // Add search criteria
127 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
128 $searchInstance->setLimit(1);
130 // Now get another criteria
131 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
133 // And add our both entries
134 $updateInstance->addCriteria("last_activity", date("Y-m-d H:i:s", time()));
135 $updateInstance->addCriteria("last_action", $lastAction);
137 // Add the search criteria for searching for the right entry
138 $updateInstance->setSearchInstance($searchInstance);
140 // Set wrapper class name
141 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
143 // Remember the update in database result
144 $this->getResultInstance()->add2UpdateQueue($updateInstance);