readConfig() is not naming convention, renamed to getConfigEntry()
[shipsimu.git] / application / ship-simu / main / user / extended / 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, 2009 Ship-Simu Developer Team
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 ShipSimuBaseUser implements ManageableMember, BookableAccount {
25         /**
26          * Protected constructor
27          *
28          * @param       $className      Name of the class
29          * @return      void
30          */
31         protected function __construct ($className = __CLASS__) {
32                 // Call parent constructor
33                 parent::__construct($className);
34         }
35
36         /**
37          * Destructor for e.g. flushing pending updates to the database
38          *
39          * @return      void
40          */
41         public function __destruct () {
42                 // Flush any updated entries to the database
43                 $this->flushPendingUpdates();
44
45                 // Call parent destructor
46                 parent::__destruct();
47         }
48
49         /**
50          * Creates an instance of this user class by a provided username. This
51          * factory method will check if username is already taken and if not
52          * so it will throw an exception.
53          *
54          * @param       $userName               Username we need a class instance for
55          * @return      $userInstance   An instance of this user class
56          * @throws      UsernameMissingException        If the username does not exist
57          */
58         public final static function createMemberByUsername ($userName) {
59                 // Get a new instance
60                 $userInstance = new ShipSimuMember();
61
62                 // Set the username
63                 $userInstance->setUserName($userName);
64
65                 // Check if username exists
66                 if ($userInstance->ifUsernameExists() === false) {
67                         // Throw an exception here
68                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
69                 } // END - if
70
71                 // Return the instance
72                 return $userInstance;
73         }
74
75         /**
76          * Creates an instance of this user class by a provided email address. This
77          * factory method will not check if email address is there.
78          *
79          * @param       $email                  Email address of the user
80          * @return      $userInstance   An instance of this user class
81          */
82         public final static function createMemberByEmail ($email) {
83                 // Get a new instance
84                 $userInstance = new ShipSimuMember();
85
86                 // Set the username
87                 $userInstance->setEmail($email);
88
89                 // Return the instance
90                 return $userInstance;
91         }
92
93         /**
94          * Creates a user by a given request instance
95          *
96          * @param       $requestInstance        An instance of a Requestable class
97          * @return      $userInstance           An instance of this user class
98          * @todo        Add more ways of creating user instances
99          */
100         public final static function createMemberByRequest (Requestable $requestInstance) {
101                 // Determine if by email or username
102                 if (!is_null($requestInstance->getRequestElement('username'))) {
103                         // Username supplied
104                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
105                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
106                         // Email supplied
107                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
108                 } else {
109                         // Unsupported mode
110                         $userInstance = new ShipSimuMember();
111                         $userInstance->partialStub("We need to add more ways of creating user classes here.");
112                         $userInstance->debugBackTrace();
113                         exit();
114                 }
115
116                 // Return the prepared instance
117                 return $userInstance;
118         }
119
120         /**
121          * Updates the last activity timestamp and last performed action in the
122          * database result. You should call flushPendingUpdates() to flush these updates
123          * to the database layer.
124          *
125          * @param       $requestInstance        A requestable class instance
126          * @return      void
127          */
128         public function updateLastActivity (Requestable $requestInstance) {
129                 // Set last action
130                 $lastAction = $requestInstance->getRequestElement('action');
131
132                 // If there is no action use the default on
133                 if (is_null($lastAction)) {
134                         $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
135                 } // END - if
136
137                 // Get a critieria instance
138                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139
140                 // Add search criteria
141                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
142                 $searchInstance->setLimit(1);
143
144                 // Now get another criteria
145                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
146
147                 // And add our both entries
148                 $updateInstance->addCriteria("last_activity", date("Y-m-d H:i:s", time()));
149                 $updateInstance->addCriteria("last_action", $lastAction);
150
151                 // Add the search criteria for searching for the right entry
152                 $updateInstance->setSearchInstance($searchInstance);
153
154                 // Set wrapper class name
155                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
156
157                 // Remember the update in database result
158                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
159         }
160
161         /**
162          * Books the given 'amount' in the request instance on the users "points
163          * account"
164          *
165          * @param       $requestInstance        An instance of a Requestable class
166          * @return      void
167          */
168         public function bookAmountDirectly (Requestable $requestInstance) {
169                 // Get the points class from registry
170                 $pointsInstance = Registry::getRegistry()->getInstance('points');
171
172                 // Is the points instance null?
173                 if (is_null($pointsInstance)) {
174                         // Then get a new one
175                         $pointsInstance = ObjectFactory::createObjectByConfiguredName('user_points_class', array($this));
176
177                         // And store it in registry
178                         Registry::getRegistry()->addInstance('points', $pointsInstance);
179                 } // END - if
180
181                 // Get the amount
182                 $amount = $requestInstance->getRequestElement('amount');
183
184                 // Call the method for booking points
185                 $pointsInstance->bookPointsDirectly($amount);
186         }
187
188         /**
189          * Flushs all pending updates to the database layer
190          *
191          * @return      void
192          */
193         public function flushPendingUpdates () {
194                 // Is the object valid?
195                 if (!$this->getResultInstance() instanceof SearchableResult) {
196                         // Abort here
197                         return;
198                 } // END - if
199
200                 // Do we have data to update?
201                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
202                         // Get a database wrapper
203                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
204
205                         // Yes, then send the whole result to the database layer
206                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
207                 } // END - if
208         }
209 }
210
211 // [EOF]
212 ?>