3 * A database wrapper for city 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 CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
25 // Constants for database table names
26 const DB_TABLE_CITY_INFORMATION = 'city_data';
28 // Constants for database column names
29 const DB_COLUMN_CITY_NR = 'city_nr';
30 const DB_COLUMN_CITY_ID = 'city_id';
31 const DB_COLUMN_CITY_MODE = 'city_mode';
32 const DB_COLUMN_CITY_NAME = 'city_name';
33 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this database wrapper by a provided user class
48 * @return $wrapperInstance An instance of the created wrapper class
50 public static final function createCityInformationDatabaseWrapper () {
52 $wrapperInstance = new CityInformationDatabaseWrapper();
54 // Set (primary!) table name
55 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
57 // Return the instance
58 return $wrapperInstance;
62 * Checks whether there is an entry for given city instance
64 * @param $cityInstance An instance of a CityHelper class
65 * @return $isFound Whether a city id has been found for this city
67 public function ifCityDataIsFound (CityHelper $cityInstance) {
69 if (!isset($GLOBALS[__METHOD__])) {
70 // Now get a search criteria instance
71 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
73 // Search for the city number one which is hard-coded the default
74 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NR , 1);
75 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
76 $searchInstance->setLimit(1);
79 $resultInstance = $this->doSelectByCriteria($searchInstance);
81 // Set result instance in city instance
82 $cityInstance->setResultInstance($resultInstance);
85 $GLOBALS[__METHOD__] = $resultInstance->next();
89 return $GLOBALS[__METHOD__];
93 * 'Registers' a new city id along with data provided in the city instance.
94 * This may sound confusing but avoids double code very nicely...
96 * @param $cityInstance A city instance
97 * @param $requestInstance An instance of a Requestable class
100 public function registerCityId (BaseCity $cityInstance, Requestable $requestInstance) {
101 // Get a dataset instance
102 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
104 // Set the primary key
105 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
107 // Add registration elements to the dataset
108 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
110 // "Insert" this dataset instance completely into the database
111 $this->queryInsertDataSet($dataSetInstance);
115 * Removes non-public data from given array.
117 * @param $data An array with possible non-public data that needs to be removed.
118 * @return $data A cleaned up array with only public data.
120 public function removeNonPublicDataFromArray(array $data) {
121 // Currently call only inner method
122 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
123 $data = parent::removeNonPublicDataFromArray($data);
125 // Return cleaned data
130 * Checks whether the user has already founded a city
132 * @return $hasFounded Whether the user has already founded a city
134 public function ifUserHasFoundedCity () {
136 $userInstance = Registry::getRegistry()->getInstance('user');
138 // Now get a search criteria instance
139 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
141 // Search for user's cities
142 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
145 $resultInstance = $this->doSelectByCriteria($searchInstance);
147 // Get city manager instance
148 $managerInstance = ManagerFactory::createManagerByType('city');
150 // Set result instance
151 $managerInstance->setResultInstance($resultInstance);
153 // Has it been founded?
154 $hasFounded = $resultInstance->valid();
161 * Checks whether the given city name is taken
163 * @return $isTaken Whether the given city name is taken
165 public function ifCityExists ($cityName) {
166 // Now get a search criteria instance
167 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
169 // Search for the city number one which is hard-coded the default
170 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
171 $searchInstance->setLimit(1);
174 $resultInstance = $this->doSelectByCriteria($searchInstance);
177 $isTaken = $resultInstance->valid();