3 * A database wrapper for region informations
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2015 City Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
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.
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.
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/>.
24 class RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
25 // Constants for database table names
26 const DB_TABLE_REGION_INFORMATION = 'region_data';
28 // Constants for database column names
29 const DB_COLUMN_REGION_ID = 'region_id';
30 const DB_COLUMN_REGION_NAME = 'region_name';
31 const DB_COLUMN_REGION_USER_ID = 'region_user_id';
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this database wrapper by a provided user class
46 * @return $wrapperInstance An instance of the created wrapper class
48 public static final function createRegionInformationDatabaseWrapper () {
50 $wrapperInstance = new RegionInformationDatabaseWrapper();
52 // Set (primary!) table name
53 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
55 // Return the instance
56 return $wrapperInstance;
60 * Removes non-public data from given array.
62 * @param $data An array with possible non-public data that needs to be removed.
63 * @return $data A cleaned up array with only public data.
65 public function removeNonPublicDataFromArray(array $data) {
66 // Currently call only inner method
67 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
68 $data = parent::removeNonPublicDataFromArray($data);
70 // Return cleaned data
75 * Checks whether the user has already founded a region
77 * @return $hasFounded Whether the user has already founded a region
79 public function ifUserHasCreatedRegion () {
81 $userInstance = Registry::getRegistry()->getInstance('user');
83 // Now get a search criteria instance
84 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
86 // Search for user's cities
87 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
90 $resultInstance = $this->doSelectByCriteria($searchInstance);
92 // Get region manager instance
93 $managerInstance = ManagerFactory::createManagerByType('region');
95 // Make sure the instance is valid
96 assert($managerInstance instanceof ManageableRegion);
98 // Set result instance
99 $managerInstance->setResultInstance($resultInstance);
101 // Has it been founded?
102 $hasFounded = $resultInstance->valid();
109 * Checks whether the given region name is taken
111 * @param $regionName Name of region
112 * @return $isTaken Whether the given region name is taken
114 public function ifRegionExists ($regionName) {
115 // Now get a search criteria instance
116 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
118 // Search for the region number one which is hard-coded the default
119 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
120 $searchInstance->setLimit(1);
123 $resultInstance = $this->doSelectByCriteria($searchInstance);
126 $isTaken = $resultInstance->next();
127 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
129 // Get manger instance
130 $managerInstance = ManagerFactory::createManagerByType('region');
132 // Make sure the instance is valid
133 assert($managerInstance instanceof ManageableRegion);
135 // Set result instance
136 $managerInstance->setResultInstance($resultInstance);
143 * Creates a region by given name
145 * @param $regionName Name of region
148 public function createRegionByName ($regionName) {
150 assert(!$this->ifRegionExists($regionName));
153 $userInstance = Registry::getRegistry()->getInstance('user');
155 // Get a dataset instance
156 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
158 // Set the primary key
159 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
161 // Add region name and assign user id
162 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID , ($this->countTotalRows() + 1));
163 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME , $regionName);
164 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
166 // "Insert" this dataset instance completely into the database
167 $this->queryInsertDataSet($dataSetInstance);
170 assert($this->ifRegionExists($regionName));