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