]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/user/user/class_User.php
User/guest classes now have base class
[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          * Creates an instance of this user class by a provided username. This
52          * factory method will check if the username is already taken and if not
53          * so it will throw an exception.
54          *
55          * @param       $userName               Username we need a class instance for
56          * @return      $userInstance   An instance of this user class
57          * @throws      UsernameMissingException        If the username does not exist
58          */
59         public final static function createUserByUsername ($userName) {
60                 // Get a new instance
61                 $userInstance = new User();
62
63                 // Set the username
64                 $userInstance->setUserName($userName);
65
66                 // Check if the username exists
67                 if (!$userInstance->ifUsernameExists()) {
68                         // Throw an exception here
69                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
70                 }
71
72                 // Return the instance
73                 return $userInstance;
74         }
75
76         /**
77          * Creates an instance of this user class by a provided email address. This
78          * factory method will not check if the email address is there.
79          *
80          * @param       $email                  Email address of the user
81          * @return      $userInstance   An instance of this user class
82          */
83         public final static function createUserByEmail ($email) {
84                 // Get a new instance
85                 $userInstance = new User();
86
87                 // Set the username
88                 $userInstance->setEmail($email);
89
90                 // Return the instance
91                 return $userInstance;
92         }
93
94         /**
95          * Creates a user by a given request instance
96          *
97          * @param       $requestInstance        An instance of a Requestable class
98          * @return      $userInstance           An instance of this user class
99          */
100         public final static function createUserByRequest (Requestable $requestInstance) {
101                 // Determine if by email or username
102                 if (!is_null($requestInstance->getRequestElement('username'))) {
103                         // Username supplied
104                         $userInstance = self::createUserByUserName($requestInstance->getRequestElement('username'));
105                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
106                         // Email supplied
107                         $userInstance = self::createUserByEmail($requestInstance->getRequestElement('email'));
108                 } else {
109                         // Unsupported mode
110                         $userInstance = new User();
111                         $userInstance->partialStub("We need to add more ways of creating user classes here.");
112                         $userInstance->debugBackTrace();
113                         exit();
114                 }
115
116                 // Return the prepared instance
117                 return $userInstance;
118         }
119
120         /**
121          * Adds data for later complete update
122          *
123          * @param       $column         Column we want to update
124          * @param       $value          New value to store in database
125          * @return      void
126          * @todo        0% done
127          */
128         public function addUpdateData ($column, $value) {
129                 $this->partialStub("Column={$column}, value={$value}");
130         }
131
132         /**
133          * Updates the last activity timestamp and last performed action in the
134          * database result. You should call flushUpdates() to flush these updates
135          * to the database layer.
136          *
137          * @param       $requestInstance        A requestable class instance
138          * @return      void
139          */
140         public function updateLastActivity (Requestable $requestInstance) {
141                 // Set last action
142                 $lastAction = $requestInstance->getRequestElement('action');
143
144                 // If there is no action use the default on
145                 if (is_null($lastAction)) {
146                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
147                 } // END - if
148
149                 // Get a critieria instance
150                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151
152                 // Add search criteria
153                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
154                 $searchInstance->setLimit(1);
155
156                 // Now get another criteria
157                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
158
159                 // And add our both entries
160                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
161                 $updateInstance->addCriteria('last_action', $lastAction);
162
163                 // Add the search criteria for searching for the right entry
164                 $updateInstance->setSearchInstance($searchInstance);
165
166                 // Remember the update in database result
167                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
168         }
169
170         /**
171          * Flushs all updated entries to the database layer
172          *
173          * @return      void
174          */
175         public function flushUpdates () {
176                 // Get a database wrapper
177                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
178
179                 // Do we have data to update?
180                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
181                         // Yes, then send the whole result to the database layer
182                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
183                 } // END - if
184         }
185 }
186
187 // [EOF]
188 ?>