More game classes added:
[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, 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 ShipSimuBaseUser implements ManageableMember {
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          * Creates an instance of this user class by a provided username. This
47          * factory method will check if the username is already taken and if not
48          * so it will throw an exception.
49          *
50          * @param       $userName               Username we need a class instance for
51          * @return      $userInstance   An instance of this user class
52          * @throws      UsernameMissingException        If the username does not exist
53          */
54         public final static function createMemberByUsername ($userName) {
55                 // Get a new instance
56                 $userInstance = new ShipSimuMember();
57
58                 // Set the username
59                 $userInstance->setUserName($userName);
60
61                 // Check if the username exists
62                 if (!$userInstance->ifUsernameExists()) {
63                         // Throw an exception here
64                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
65                 }
66
67                 // Return the instance
68                 return $userInstance;
69         }
70
71         /**
72          * Creates an instance of this user class by a provided email address. This
73          * factory method will not check if the email address is there.
74          *
75          * @param       $email                  Email address of the user
76          * @return      $userInstance   An instance of this user class
77          */
78         public final static function createMemberByEmail ($email) {
79                 // Get a new instance
80                 $userInstance = new ShipSimuMember();
81
82                 // Set the username
83                 $userInstance->setEmail($email);
84
85                 // Return the instance
86                 return $userInstance;
87         }
88
89         /**
90          * Creates a user by a given request instance
91          *
92          * @param       $requestInstance        An instance of a Requestable class
93          * @return      $userInstance           An instance of this user class
94          */
95         public final static function createMemberByRequest (Requestable $requestInstance) {
96                 // Determine if by email or username
97                 if (!is_null($requestInstance->getRequestElement('username'))) {
98                         // Username supplied
99                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
100                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
101                         // Email supplied
102                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
103                 } else {
104                         // Unsupported mode
105                         $userInstance = new ShipSimuMember();
106                         $userInstance->partialStub("We need to add more ways of creating user classes here.");
107                         $userInstance->debugBackTrace();
108                         exit();
109                 }
110
111                 // Return the prepared instance
112                 return $userInstance;
113         }
114
115         /**
116          * Updates the last activity timestamp and last performed action in the
117          * database result. You should call flushPendingUpdates() to flush these updates
118          * to the database layer.
119          *
120          * @param       $requestInstance        A requestable class instance
121          * @return      void
122          */
123         public function updateLastActivity (Requestable $requestInstance) {
124                 // Set last action
125                 $lastAction = $requestInstance->getRequestElement('action');
126
127                 // If there is no action use the default on
128                 if (is_null($lastAction)) {
129                         $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
130                 } // END - if
131
132                 // Get a critieria instance
133                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
134
135                 // Add search criteria
136                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137                 $searchInstance->setLimit(1);
138
139                 // Now get another criteria
140                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
141
142                 // And add our both entries
143                 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
144                 $updateInstance->addCriteria('last_action', $lastAction);
145
146                 // Add the search criteria for searching for the right entry
147                 $updateInstance->setSearchInstance($searchInstance);
148
149                 // Remember the update in database result
150                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
151         }
152
153         /**
154          * Flushs all pending updates to the database layer
155          *
156          * @return      void
157          */
158         public function flushPendingUpdates () {
159                 // Do we have data to update?
160                 if ($this->getResultInstance()->ifDataNeedsFlush()) {
161                         // Get a database wrapper
162                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
163
164                         // Yes, then send the whole result to the database layer
165                         $wrapperInstance->doUpdateByResult($this->getResultInstance());
166                 } // END - if
167         }
168 }
169
170 // [EOF]
171 ?>