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