333ec1a7c051336d9719f4fe0749bcf2c0868d67
[core.git] / inc / main / classes / database / frontend / class_UserDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Database\Wrapper\User;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7
8 /**
9  * A database wrapper for the User class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
31         // Constants for exceptions
32         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
33
34         // Constants for database columns
35         const DB_COLUMN_USERID       = 'userid';
36         const DB_COLUMN_USERNAME     = 'username';
37         const DB_COLUMN_EMAIL        = 'email';
38         const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
39         const DB_COLUMN_USER_STATUS  = 'user_status';
40
41         // Constants for database table names
42         const DB_TABLE_USER = 'user';
43
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         protected function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Creates an instance of this database wrapper by a provided user class
56          *
57          * @return      $wrapperInstance        An instance of the created wrapper class
58          */
59         public static final function createUserDatabaseWrapper () {
60                 // Get a new instance
61                 $wrapperInstance = new UserDatabaseWrapper();
62
63                 // Set (primary!) table name
64                 $wrapperInstance->setTableName(self::DB_TABLE_USER);
65
66                 // Return the instance
67                 return $wrapperInstance;
68         }
69
70         /**
71          * Handles inserting the registration data from a registration instance into the database
72          *
73          * @param       $registrationInstance   An instance of a registration class
74          * @return      void
75          */
76         public function insertRegistrationObject (UserRegister $registrationInstance) {
77                 // Generate a data set for the request
78                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
79
80                 // Set the primary key
81                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
82
83                 // Add registration elements to the dataset
84                 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
85
86                 // "Insert" this request instance completely into the database
87                 $this->queryInsertDataSet($dataSetInstance);
88         }
89
90         /**
91          * Updates an user database entry with given result
92          *
93          * @param       $resultInstance         An instance of a Updateable database result
94          * @return      void
95          * @throws      NullPointerException    If $updateInstance or $searchInstance is null
96          */
97         public function doUpdateByResult (UpdateableResult $resultInstance) {
98                 // Get the search instance from result
99                 $searchInstance = $resultInstance->getSearchInstance();
100
101                 // Is this null?
102                 if (is_null($searchInstance)) {
103                         // Get the update instance
104                         $updateInstance = $resultInstance->getUpdateInstance();
105
106                         // Is this null?
107                         if (is_null($updateInstance)) {
108                                 // Throw an exception here
109                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
110                         } // END - if
111
112                         // Get search instance from update instance
113                         $searchInstance = $updateInstance->getSearchInstance();
114
115                         // Is it still null?
116                         if (is_null($searchInstance)) {
117                                 // Throw an exception here
118                                 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
119                         } // END - if
120                 } // END - if
121
122                 // Generate a data set object
123                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
124
125                 // Add seach criteria
126                 $dataSetInstance->setSearchInstance($searchInstance);
127
128                 // Set the primary key
129                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
130
131                 // Add all update criteria to the database set
132                 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
133
134                 // "Update" this request with the database
135                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
136         }
137 }
138
139 // [EOF]
140 ?>