3 namespace CoreFramework\Database\Wrapper\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Registerable;
10 * A database wrapper for the User class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
32 // Constants for exceptions
33 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
35 // Constants for database columns
36 const DB_COLUMN_USERID = 'userid';
37 const DB_COLUMN_USERNAME = 'username';
38 const DB_COLUMN_EMAIL = 'email';
39 const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
40 const DB_COLUMN_USER_STATUS = 'user_status';
42 // Constants for database table names
43 const DB_TABLE_USER = 'user';
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this database wrapper by a provided user class
58 * @return $wrapperInstance An instance of the created wrapper class
60 public static final function createUserDatabaseWrapper () {
62 $wrapperInstance = new UserDatabaseWrapper();
64 // Set (primary!) table name
65 $wrapperInstance->setTableName(self::DB_TABLE_USER);
67 // Return the instance
68 return $wrapperInstance;
72 * Handles inserting the registration data from a registration instance into the database
74 * @param $registrationInstance An instance of a registration class
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));
81 // Set the primary key
82 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
84 // Add registration elements to the dataset
85 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
87 // "Insert" this request instance completely into the database
88 $this->queryInsertDataSet($dataSetInstance);
92 * Updates an user database entry with given result
94 * @param $resultInstance An instance of a Updateable database result
96 * @throws NullPointerException If $updateInstance or $searchInstance is null
98 public function doUpdateByResult (UpdateableResult $resultInstance) {
99 // Get the search instance from result
100 $searchInstance = $resultInstance->getSearchInstance();
103 if (is_null($searchInstance)) {
104 // Get the update instance
105 $updateInstance = $resultInstance->getUpdateInstance();
108 if (is_null($updateInstance)) {
109 // Throw an exception here
110 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
113 // Get search instance from update instance
114 $searchInstance = $updateInstance->getSearchInstance();
117 if (is_null($searchInstance)) {
118 // Throw an exception here
119 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
123 // Generate a data set object
124 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
126 // Add seach criteria
127 $dataSetInstance->setSearchInstance($searchInstance);
129 // Set the primary key
130 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
132 // Add all update criteria to the database set
133 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
135 // "Update" this request with the database
136 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);