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