Generic methods created from non-generic implementation:
[shipsimu.git] / inc / classes / main / user / user / class_User.php
1 <?php
2 /**
3  * A generic class for handling users
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class User extends BaseUser implements ManageableUser, Registerable, Updateable {
25         // Exceptions
26         const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
27         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
28         const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the class
34          * @return      void
35          */
36         protected function __construct ($className = "") {
37                 // Is the class name empty? Then this is not a specialized user class
38                 if (empty($className)) $className = __CLASS__;
39
40                 // Call parent constructor
41                 parent::__construct($className);
42
43                 // Set part description
44                 $this->setObjectDescription("Generic user class");
45
46                 // Create unique ID number
47                 $this->generateUniqueId();
48         }
49
50         /**
51          * Destructor to always flush updates
52          *
53          * @return      void
54          */
55         public function __destruct () {
56                 // Flush any updated entries to the database
57                 $this->flushPendingUpdates();
58
59                 // Call parent destructor
60                 parent::__destruct();
61         }
62
63         /**
64          * Creates an instance of this user class by a provided username. This
65          * factory method will check if the username is already taken and if not
66          * so it will throw an exception.
67          *
68          * @param       $userName               Username we need a class instance for
69          * @return      $userInstance   An instance of this user class
70          * @throws      UsernameMissingException        If the username does not exist
71          */
72         public final static function createUserByUsername ($userName) {
73                 // Get a new instance
74                 $userInstance = new User();
75
76                 // Set the username
77                 $userInstance->setUserName($userName);
78
79                 // Check if the username exists
80                 if (!$userInstance->ifUsernameExists()) {
81                         // Throw an exception here
82                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
83                 }
84
85                 // Return the instance
86                 return $userInstance;
87         }
88
89         /**
90          * Creates an instance of this user class by a provided email address. This
91          * factory method will not check if the email address is there.
92          *
93          * @param       $email                  Email address of the user
94          * @return      $userInstance   An instance of this user class
95          */
96         public final static function createUserByEmail ($email) {
97                 // Get a new instance
98                 $userInstance = new User();
99
100                 // Set the username
101                 $userInstance->setEmail($email);
102
103                 // Return the instance
104                 return $userInstance;
105         }
106
107         /**
108          * Creates a user by a given request instance
109          *
110          * @param       $requestInstance        An instance of a Requestable class
111          * @return      $userInstance           An instance of this user class
112          */
113         public final static function createUserByRequest (Requestable $requestInstance) {
114                 // Determine if by email or username
115                 if (!is_null($requestInstance->getRequestElement('username'))) {
116                         // Username supplied
117                         $userInstance = self::createUserByUserName($requestInstance->getRequestElement('username'));
118                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
119                         // Email supplied
120                         $userInstance = self::createUserByEmail($requestInstance->getRequestElement('email'));
121                 } else {
122                         // Unsupported mode
123                         $userInstance = new User();
124                         $userInstance->partialStub("We need to add more ways of creating user classes here.");
125                         $userInstance->debugBackTrace();
126                         exit();
127                 }
128
129                 // Return the prepared instance
130                 return $userInstance;
131         }
132
133         /**
134          * Adds data for later complete update
135          *
136          * @param       $column         Column we want to update
137          * @param       $value          New value to store in database
138          * @return      void
139          * @deprecated
140          */
141         public function addUpdateData ($column, $value) {
142                 $this->deprecatedMethod("Please use updateDatabaseField() instead!");
143                 $this->updateDatabaseField($column, $value);
144         }
145
146         /**
147          * Updates the last activity timestamp and last performed action in the
148          * database result. You should call flushPendingUpdates() to flush these updates
149          * to the database layer.
150          *
151          * @param       $requestInstance        A requestable class instance
152          * @return      void
153          */
154         public function updateLastActivity (Requestable $requestInstance) {
155                 // Set last action
156                 $lastAction = $requestInstance->getRequestElement('action');
157
158                 // If there is no action use the default on
159                 if (is_null($lastAction)) {
160                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
161                 } // END - if
162
163                 // Get a critieria instance
164                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
165
166                 // Add search criteria
167                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
168                 $searchInstance->setLimit(1);
169
170                 // Now get another criteria
171                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
172
173                 // And add our both entries
174                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
175                 $updateInstance->addCriteria('last_action', $lastAction);
176
177                 // Add the search criteria for searching for the right entry
178                 $updateInstance->setSearchInstance($searchInstance);
179
180                 // Remember the update in database result
181                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
182         }
183
184         /**
185          * Flushs all pending updates to the database layer
186          *
187          * @return      void
188          */
189         public function flushPendingUpdates () {
190                 // Do we have data to update?
191                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
192                         // Get a database wrapper
193                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
194
195                         // Yes, then send the whole result to the database layer
196                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
197                 } // END - if
198         }
199 }
200
201 // [EOF]
202 ?>