Registry introduces, comments fixed ;)
[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 {
25         /**
26          * Username
27          */
28         private $username = "";
29
30         // Exceptions
31         const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
32
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct ($class = "") {
39                 // Is the class name empty? Then this is not a specialized user class
40                 if (empty($class)) $class = __CLASS__;
41
42                 // Call parent constructor
43                 parent::__construct($class);
44
45                 // Set part description
46                 $this->setObjectDescription("Generic user class");
47
48                 // Create unique ID number
49                 $this->createUniqueID();
50
51                 // Clean up a little
52                 $this->removeNumberFormaters();
53                 $this->removeSystemArray();
54         }
55
56         /**
57          * Creates an instance of this user class by a provided username. This
58          * factory method will check if the username is already taken and if not
59          * so it will throw an exception.
60          *
61          * @param       $username               Username we need a class instance for
62          * @return      $userInstance   An instance of this user class
63          * @throws      UsernameMissingException        If the username does not exist
64          */
65         public final static function createUserByUsername ($userName) {
66                 // Get a new instance
67                 $userInstance = new User();
68
69                 // Set the username
70                 $userInstance->setUsername($userName);
71
72                 // Check if the username exists
73                 if (!$userInstance->ifUsernameExists()) {
74                         // Throw an exception here
75                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
76                 }
77
78                 // Return the instance
79                 return $userInstance;
80         }
81
82         /**
83          * Setter for username
84          *
85          * @param       $userName       The username to set
86          * @return      void
87          */
88         protected final function setUsername ($userName) {
89                 $this->userNane = $userName;
90         }
91
92         /**
93          * Getter for username
94          *
95          * @return      $userName       The username to get
96          */
97         public final function getUsername () {
98                 return $this->userNane;
99         }
100
101         /**
102          * Determines wether the username exists or not
103          *
104          * @return      $exists         Wether the username exists
105          */
106         protected function ifUsernameExists () {
107                 // By default the username does exist
108                 $exists = true;
109
110                 // Get a UserDatabaseWrapper instance
111                 $wrapperInstance = UserDatabaseWrapper::createUserDatabaseWrapper();
112
113                 // Create a search criteria
114                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
115
116                 // Add the username as a criteria and set limit to one entry
117                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
118                 $criteriaInstance->setLimit(1);
119
120                 // Get a search result
121                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
122
123                 // Search for it
124                 if (!$result->next()) {
125                         // Entry not found
126                         $exists = false;
127                 } // END - if
128
129                 // Return the status
130                 return $exists;
131         }
132 }
133
134 // [EOF]
135 ?>