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