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