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 // Set result instance
96 $managerInstance->setResultInstance($resultInstance);
98 // Has it been founded?
99 $hasFounded = $resultInstance->valid();
106 * Checks whether the given region name is taken
108 * @param $regionName Name of region
109 * @return $isTaken Whether the given region name is taken
111 public function ifRegionExists ($regionName) {
112 // Now get a search criteria instance
113 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
115 // Search for the region number one which is hard-coded the default
116 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
117 $searchInstance->setLimit(1);
120 $resultInstance = $this->doSelectByCriteria($searchInstance);
123 $isTaken = $resultInstance->next();
124 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
126 // Get manger instance
127 $managerInstance = ManagerFactory::createManagerByType('region');
129 // Set result instance
130 $managerInstance->setResultInstance($resultInstance);
137 * Creates a region by given name
139 * @param $regionName Name of region
142 public function createRegionByName ($regionName) {
144 assert(!$this->ifRegionExists($regionName));
147 $userInstance = Registry::getRegistry()->getInstance('user');
149 // Get a dataset instance
150 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
152 // Set the primary key
153 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
155 // Add region name and assign user id
156 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID , ($this->countTotalRows() + 1));
157 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME , $regionName);
158 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
160 // "Insert" this dataset instance completely into the database
161 $this->queryInsertDataSet($dataSetInstance);
164 assert($this->ifRegionExists($regionName));