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