User class / resending of confirm link updated:
[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 {
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          * @todo        0% done
140          */
141         public function addUpdateData ($column, $value) {
142                 $this->partialStub("Column={$column}, value={$value}");
143         }
144
145         /**
146          * Updates the last activity timestamp and last performed action in the
147          * database result. You should call flushPendingUpdates() to flush these updates
148          * to the database layer.
149          *
150          * @param       $requestInstance        A requestable class instance
151          * @return      void
152          */
153         public function updateLastActivity (Requestable $requestInstance) {
154                 // Set last action
155                 $lastAction = $requestInstance->getRequestElement('action');
156
157                 // If there is no action use the default on
158                 if (is_null($lastAction)) {
159                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
160                 } // END - if
161
162                 // Get a critieria instance
163                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
164
165                 // Add search criteria
166                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
167                 $searchInstance->setLimit(1);
168
169                 // Now get another criteria
170                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
171
172                 // And add our both entries
173                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
174                 $updateInstance->addCriteria('last_action', $lastAction);
175
176                 // Add the search criteria for searching for the right entry
177                 $updateInstance->setSearchInstance($searchInstance);
178
179                 // Remember the update in database result
180                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
181         }
182
183         /**
184          * Updates a given field with new value
185          *
186          * @param       $fieldName              Field to update
187          * @param       $fieldValue             New value to store
188          * @return      void
189          */
190         public function updateDatabaseField ($fieldName, $fieldValue) {
191                 // Get a critieria instance
192                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
193
194                 // Add search criteria
195                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
196                 $searchInstance->setLimit(1);
197
198                 // Now get another criteria
199                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
200
201                 // And add our both entries
202                 $updateInstance->addCriteria($fieldName, $fieldValue);
203
204                 // Add the search criteria for searching for the right entry
205                 $updateInstance->setSearchInstance($searchInstance);
206
207                 // Remember the update in database result
208                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
209         }
210
211         /**
212          * Flushs all pending updates to the database layer
213          *
214          * @return      void
215          */
216         public function flushPendingUpdates () {
217                 // Do we have data to update?
218                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
219                         // Get a database wrapper
220                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
221
222                         // Yes, then send the whole result to the database layer
223                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
224                 } // END - if
225         }
226 }
227
228 // [EOF]
229 ?>