d68f2883cff5a6204404d3e6416ae68cdd6c0af1
[core.git] / inc / classes / main / database / wrapper / class_UserDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for the User class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 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 UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper {
25         // Constants for exceptions
26         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
27
28         // Constants for database columns
29         const DB_COLUMN_USERID       = 'userid';
30         const DB_COLUMN_USERNAME     = 'username';
31         const DB_COLUMN_EMAIL        = 'email';
32         const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
33         const DB_COLUMN_USER_STATUS  = 'user_status';
34
35         // Constants for database table names
36         const DB_TABLE_USER = 'user';
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this database wrapper by a provided user class
50          *
51          * @return      $wrapperInstance        An instance of the created wrapper class
52          */
53         public static final function createUserDatabaseWrapper () {
54                 // Get a new instance
55                 $wrapperInstance = new UserDatabaseWrapper();
56
57                 // Set (primary!) table name
58                 $wrapperInstance->setTableName(self::DB_TABLE_USER);
59
60                 // Return the instance
61                 return $wrapperInstance;
62         }
63
64         /**
65          * Handles inserting the registration data from a registration instance into the database
66          *
67          * @param       $registrationInstance   An instance of a registration class
68          * @return      void
69          */
70         public function insertRegistrationObject (UserRegister $registrationInstance) {
71                 // Generate a data set for the request
72                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
73
74                 // Set the primary key
75                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
76
77                 // Add registration elements to the dataset
78                 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
79
80                 // "Insert" this request instance completely into the database
81                 $this->queryInsertDataSet($dataSetInstance);
82         }
83
84         /**
85          * Updates an user database entry with given result
86          *
87          * @param       $resultInstance         An instance of a Updateable database result
88          * @return      void
89          * @throws      NullPointerException    If $updateInstance or $searchInstance is null
90          */
91         public function doUpdateByResult (UpdateableResult $resultInstance) {
92                 // Get the search instance from result
93                 $searchInstance = $resultInstance->getSearchInstance();
94
95                 // Is this null?
96                 if (is_null($searchInstance)) {
97                         // Get the update instance
98                         $updateInstance = $resultInstance->getUpdateInstance();
99
100                         // Is this null?
101                         if (is_null($updateInstance)) {
102                                 // Throw an exception here
103                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
104                         } // END - if
105
106                         // Get search instance from update instance
107                         $searchInstance = $updateInstance->getSearchInstance();
108
109                         // Is it still null?
110                         if (is_null($searchInstance)) {
111                                 // Throw an exception here
112                                 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
113                         } // END - if
114                 } // END - if
115
116                 // Generate a data set object
117                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
118
119                 // Add seach criteria
120                 $dataSetInstance->setSearchInstance($searchInstance);
121
122                 // Set the primary key
123                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
124
125                 // Add all update criteria to the database set
126                 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
127
128                 // "Update" this request with the database
129                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
130         }
131 }
132
133 // [EOF]
134 ?>