Deprecated method addUpdateData() removed
[shipsimu.git] / application / ship-simu / main / user / class_ShipSimuMember.php
1 <?php
2 /**
3  * A special member class for Ship-Simu
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 ShipSimuMember 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("Special member class for Ship-Simu");
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 ShipSimuMember();
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 ShipSimuMember();
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 ShipSimuMember();
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          * Updates the last activity timestamp and last performed action in the
130          * database result. You should call flushPendingUpdates() to flush these updates
131          * to the database layer.
132          *
133          * @param       $requestInstance        A requestable class instance
134          * @return      void
135          */
136         public function updateLastActivity (Requestable $requestInstance) {
137                 // Set last action
138                 $lastAction = $requestInstance->getRequestElement('action');
139
140                 // If there is no action use the default on
141                 if (is_null($lastAction)) {
142                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
143                 } // END - if
144
145                 // Get a critieria instance
146                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
147
148                 // Add search criteria
149                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
150                 $searchInstance->setLimit(1);
151
152                 // Now get another criteria
153                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
154
155                 // And add our both entries
156                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
157                 $updateInstance->addCriteria('last_action', $lastAction);
158
159                 // Add the search criteria for searching for the right entry
160                 $updateInstance->setSearchInstance($searchInstance);
161
162                 // Remember the update in database result
163                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
164         }
165
166         /**
167          * Flushs all pending updates to the database layer
168          *
169          * @return      void
170          */
171         public function flushPendingUpdates () {
172                 // Do we have data to update?
173                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
174                         // Get a database wrapper
175                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
176
177                         // Yes, then send the whole result to the database layer
178                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
179                 } // END - if
180         }
181 }
182
183 // [EOF]
184 ?>