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