3 namespace CoreFramework\Wrapper\Database\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\NullPointerException;
8 use CoreFramework\Registration\User\UserRegister;
9 use CoreFramework\Registry\Registerable;
10 use CoreFramework\Result\Update\UpdateableResult;
11 use CoreFramework\Wrapper\Account\ManageableAccountWrapper;
12 use CoreFramework\Wrapper\Database\BaseDatabaseWrapper;
15 * A database wrapper for the User class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
37 // Constants for exceptions
38 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
40 // Constants for database columns
41 const DB_COLUMN_USERID = 'userid';
42 const DB_COLUMN_USERNAME = 'username';
43 const DB_COLUMN_EMAIL = 'email';
44 const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
45 const DB_COLUMN_USER_STATUS = 'user_status';
47 // Constants for database table names
48 const DB_TABLE_USER = 'user';
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Creates an instance of this database wrapper by a provided user class
63 * @return $wrapperInstance An instance of the created wrapper class
65 public static final function createUserDatabaseWrapper () {
67 $wrapperInstance = new UserDatabaseWrapper();
69 // Set (primary!) table name
70 $wrapperInstance->setTableName(self::DB_TABLE_USER);
72 // Return the instance
73 return $wrapperInstance;
77 * Handles inserting the registration data from a registration instance into the database
79 * @param $registrationInstance An instance of a registration class
82 public function insertRegistrationObject (UserRegister $registrationInstance) {
83 // Generate a data set for the request
84 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
86 // Set the primary key
87 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
89 // Add registration elements to the dataset
90 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
92 // "Insert" this request instance completely into the database
93 $this->queryInsertDataSet($dataSetInstance);
97 * Updates an user database entry with given result
99 * @param $resultInstance An instance of a Updateable database result
101 * @throws NullPointerException If $updateInstance or $searchInstance is null
103 public function doUpdateByResult (UpdateableResult $resultInstance) {
104 // Get the search instance from result
105 $searchInstance = $resultInstance->getSearchInstance();
108 if (is_null($searchInstance)) {
109 // Get the update instance
110 $updateInstance = $resultInstance->getUpdateInstance();
113 if (is_null($updateInstance)) {
114 // Throw an exception here
115 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
118 // Get search instance from update instance
119 $searchInstance = $updateInstance->getSearchInstance();
122 if (is_null($searchInstance)) {
123 // Throw an exception here
124 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
128 // Generate a data set object
129 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
131 // Add seach criteria
132 $dataSetInstance->setSearchInstance($searchInstance);
134 // Set the primary key
135 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
137 // Add all update criteria to the database set
138 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
140 // "Update" this request with the database
141 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);