9b480db18b4de7e456f70f253cbe2fed5827d0f0
[shipsimu.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@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 UserDatabaseWrapper extends BaseDatabaseWrapper {
25         // Constants for exceptions
26         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
27
28         // Constants for database columns
29         const DB_COLUMN_USERNAME = "username";
30         const DB_COLUMN_EMAIL    = "email";
31
32         // Constants for database table names
33         const DB_TABLE_USER = "user";
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct() {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Set part description
45                 $this->setObjectDescription("Database wrapper for user objects");
46
47                 // Create unique ID number
48                 $this->generateUniqueId();
49         }
50
51         /**
52          * Creates an instance of this database wrapper by a provided user class
53          *
54          * @return      $wrapperInstance        An instance of the created wrapper class
55          * @throws      WrapperUserNameNotFoundException        If the supplied username
56          *                                                                                              does not exist
57          */
58         public final static function createUserDatabaseWrapper () {
59                 // Get a new instance
60                 $wrapperInstance = new UserDatabaseWrapper();
61
62                 // Set (primary!) table name
63                 $wrapperInstance->setTableName(self::DB_TABLE_USER);
64
65                 // Return the instance
66                 return $wrapperInstance;
67         }
68
69         /**
70          * Handles inserting the registration data from a registration instance into the database
71          *
72          * @param       $registrationInstance   An instance of a registration class
73          * @return      void
74          */
75         public function insertRegistrationObject (UserRegister $registrationInstance) {
76                 // Generate a data set for the request
77                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
78
79                 // Set the primary key
80                 $dataSetInstance->setUniqueKey('username');
81
82                 // Add registration elements to the dataset
83                 $registrationInstance->addElementsToDataSet($dataSetInstance);
84
85                 // "Insert" this request instance completely into the database
86                 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
87         }
88
89         /**
90          * Updates an user database entry with given result
91          *
92          * @param       $resultInstance         An instance of a Updateable database result
93          * @return      void
94          */
95         public function doUpdateByResult (UpdateableResult $resultInstance) {
96                 // Generate a data set object
97                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
98
99                 // Add all update criteria to the database set
100                 $resultInstance->addElementsToDataSet($dataSetInstance);
101
102                 // Add seach criteria
103                 $dataSetInstance->setSearchInstance($resultInstance->getSearchInstance());
104
105                 // Set the primary key
106                 $dataSetInstance->setUniqueKey('username');
107
108                 // "Update" this request with the database
109                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
110         }
111 }
112
113 // [EOF]
114 ?>