Now we really have ship-simu specific user/member classes
[shipsimu.git] / inc / classes / main / user / member / class_Member.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 Member extends BaseUser implements ManageableMember, Registerable, Updateable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33
34                 // Set part description
35                 $this->setObjectDescription("Generic user class");
36
37                 // Create unique ID number
38                 $this->generateUniqueId();
39         }
40
41         /**
42          * Destructor to always flush updates
43          *
44          * @return      void
45          */
46         public function __destruct () {
47                 // Flush any updated entries to the database
48                 $this->flushPendingUpdates();
49
50                 // Call parent destructor
51                 parent::__destruct();
52         }
53
54         /**
55          * Creates an instance of this user class by a provided username. This
56          * factory method will check if the username is already taken and if not
57          * so it will throw an exception.
58          *
59          * @param       $userName               Username we need a class instance for
60          * @return      $userInstance   An instance of this user class
61          * @throws      UsernameMissingException        If the username does not exist
62          */
63         public final static function createMemberByUsername ($userName) {
64                 // Get a new instance
65                 $userInstance = new Member();
66
67                 // Set the username
68                 $userInstance->setUserName($userName);
69
70                 // Check if the username exists
71                 if (!$userInstance->ifUsernameExists()) {
72                         // Throw an exception here
73                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
74                 }
75
76                 // Return the instance
77                 return $userInstance;
78         }
79
80         /**
81          * Creates an instance of this user class by a provided email address. This
82          * factory method will not check if the email address is there.
83          *
84          * @param       $email                  Email address of the user
85          * @return      $userInstance   An instance of this user class
86          */
87         public final static function createMemberByEmail ($email) {
88                 // Get a new instance
89                 $userInstance = new Member();
90
91                 // Set the username
92                 $userInstance->setEmail($email);
93
94                 // Return the instance
95                 return $userInstance;
96         }
97
98         /**
99          * Creates a user by a given request instance
100          *
101          * @param       $requestInstance        An instance of a Requestable class
102          * @return      $userInstance           An instance of this user class
103          */
104         public final static function createMemberByRequest (Requestable $requestInstance) {
105                 // Determine if by email or username
106                 if (!is_null($requestInstance->getRequestElement('username'))) {
107                         // Username supplied
108                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
109                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
110                         // Email supplied
111                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
112                 } else {
113                         // Unsupported mode
114                         $userInstance = new Member();
115                         $userInstance->partialStub("We need to add more ways of creating user classes here.");
116                         $userInstance->debugBackTrace();
117                         exit();
118                 }
119
120                 // Return the prepared instance
121                 return $userInstance;
122         }
123
124         /**
125          * Adds data for later complete update
126          *
127          * @param       $column         Column we want to update
128          * @param       $value          New value to store in database
129          * @return      void
130          * @deprecated
131          */
132         public function addUpdateData ($column, $value) {
133                 $this->deprecatedMethod("Please use updateDatabaseField() instead!");
134                 $this->updateDatabaseField($column, $value);
135         }
136
137         /**
138          * Updates the last activity timestamp and last performed action in the
139          * database result. You should call flushPendingUpdates() to flush these updates
140          * to the database layer.
141          *
142          * @param       $requestInstance        A requestable class instance
143          * @return      void
144          */
145         public function updateLastActivity (Requestable $requestInstance) {
146                 // Set last action
147                 $lastAction = $requestInstance->getRequestElement('action');
148
149                 // If there is no action use the default on
150                 if (is_null($lastAction)) {
151                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
152                 } // END - if
153
154                 // Get a critieria instance
155                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
156
157                 // Add search criteria
158                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
159                 $searchInstance->setLimit(1);
160
161                 // Now get another criteria
162                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
163
164                 // And add our both entries
165                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
166                 $updateInstance->addCriteria('last_action', $lastAction);
167
168                 // Add the search criteria for searching for the right entry
169                 $updateInstance->setSearchInstance($searchInstance);
170
171                 // Remember the update in database result
172                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
173         }
174
175         /**
176          * Flushs all pending updates to the database layer
177          *
178          * @return      void
179          */
180         public function flushPendingUpdates () {
181                 // Do we have data to update?
182                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
183                         // Get a database wrapper
184                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
185
186                         // Yes, then send the whole result to the database layer
187                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
188                 } // END - if
189         }
190 }
191
192 // [EOF]
193 ?>