createUniqueID -> generateUniqueId renamed, dataset criteria added, registration...
[shipsimu.git] / inc / classes / main / 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 BaseFrameworkSystem implements ManageableUser, Registerable {
25         /**
26          * Username of current user
27          */
28         private $userName = "";
29
30         /**
31          * Email of current user
32          */
33         private $email = "";
34
35         // Exceptions
36         const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct ($class = "") {
44                 // Is the class name empty? Then this is not a specialized user class
45                 if (empty($class)) $class = __CLASS__;
46
47                 // Call parent constructor
48                 parent::__construct($class);
49
50                 // Set part description
51                 $this->setObjectDescription("Generic user class");
52
53                 // Create unique ID number
54                 $this->generateUniqueId();
55
56                 // Clean up a little
57                 $this->removeNumberFormaters();
58                 $this->removeSystemArray();
59         }
60
61         /**
62          * Creates an instance of this user class by a provided username. This
63          * factory method will check if the username is already taken and if not
64          * so it will throw an exception.
65          *
66          * @param       $userName               Username we need a class instance for
67          * @return      $userInstance   An instance of this user class
68          * @throws      UsernameMissingException        If the username does not exist
69          */
70         public final static function createUserByUsername ($userName) {
71                 // Get a new instance
72                 $userInstance = new User();
73
74                 // Set the username
75                 $userInstance->setUsername($userName);
76
77                 // Check if the username exists
78                 if (!$userInstance->ifUsernameExists()) {
79                         // Throw an exception here
80                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
81                 }
82
83                 // Return the instance
84                 return $userInstance;
85         }
86
87         /**
88          * Creates an instance of this user class by a provided email address. This
89          * factory method will not check if the email address is there.
90          *
91          * @param       $email                  Email address of the user
92          * @return      $userInstance   An instance of this user class
93          */
94         public final static function createUserByEmail ($email) {
95                 // Get a new instance
96                 $userInstance = new User();
97
98                 // Set the username
99                 $userInstance->setEmail($email);
100
101                 // Return the instance
102                 return $userInstance;
103         }
104
105         /**
106          * Setter for username
107          *
108          * @param       $userName       The username to set
109          * @return      void
110          */
111         protected final function setUsername ($userName) {
112                 $this->UserName = $userName;
113         }
114
115         /**
116          * Setter for email
117          *
118          * @param       $email  The email to set
119          * @return      void
120          */
121         protected final function setEmail ($email) {
122                 $this->email = $email;
123         }
124
125         /**
126          * Getter for username
127          *
128          * @return      $userName       The username to get
129          */
130         public final function getUsername () {
131                 return $this->userName;
132         }
133
134         /**
135          * Getter for email
136          *
137          * @return      $email  The email to get
138          */
139         public final function getEmail () {
140                 return $this->email;
141         }
142
143         /**
144          * Determines wether the username exists or not
145          *
146          * @return      $exists         Wether the username exists
147          */
148         public function ifUsernameExists () {
149                 // By default the username does exist
150                 $exists = true;
151
152                 // Get a UserDatabaseWrapper instance
153                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
154
155                 // Create a search criteria
156                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
157
158                 // Add the username as a criteria and set limit to one entry
159                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
160                 $criteriaInstance->setLimit(1);
161
162                 // Get a search result
163                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
164
165                 // Search for it
166                 if (!$result->next()) {
167                         // Entry not found
168                         $exists = false;
169                 } // END - if
170
171                 // Return the status
172                 return $exists;
173         }
174
175         /**
176          * Determines wether the email exists or not
177          *
178          * @return      $exists         Wether the email exists
179          */
180         public function ifEmailAddressExists () {
181                 // By default the username does exist
182                 $exists = true;
183
184                 // Get a UserDatabaseWrapper instance
185                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
186
187                 // Create a search criteria
188                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
189
190                 // Add the username as a criteria and set limit to one entry
191                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
192                 $criteriaInstance->setLimit(1);
193
194                 // Get a search result
195                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
196
197                 // Search for it
198                 if (!$result->next()) {
199                         // Entry not found
200                         $exists = false;
201                 } // END - if
202
203                 // Return the status
204                 return $exists;
205         }
206 }
207
208 // [EOF]
209 ?>