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_ID = 'city_id';
30 const DB_COLUMN_CITY_MODE = 'city_mode';
31 const DB_COLUMN_CITY_NAME = 'city_name';
32 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
33 const DB_COLUMN_CITY_REGION_ID = 'city_region_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) {
68 // Now get a search criteria instance
69 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
71 // Search for the city number one which is hard-coded the default
72 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
73 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
74 $searchInstance->setLimit(1);
77 $resultInstance = $this->doSelectByCriteria($searchInstance);
79 // Set result instance in city instance
80 $cityInstance->setResultInstance($resultInstance);
83 $isFound = $resultInstance->next();
90 * 'Registers' a new city id along with data provided in the city instance.
91 * This may sound confusing but avoids double code very nicely...
93 * @param $cityInstance A city instance
94 * @param $requestInstance An instance of a Requestable class
97 public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
98 // Get a dataset instance
99 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
101 // Set the primary key
102 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
104 // Add registration elements to the dataset
105 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
107 // "Insert" this dataset instance completely into the database
108 $this->queryInsertDataSet($dataSetInstance);
112 * Removes non-public data from given array.
114 * @param $data An array with possible non-public data that needs to be removed.
115 * @return $data A cleaned up array with only public data.
117 public function removeNonPublicDataFromArray(array $data) {
118 // Currently call only inner method
119 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
120 $data = parent::removeNonPublicDataFromArray($data);
122 // Return cleaned data
127 * Checks whether the user has already founded a city
129 * @return $hasFounded Whether the user has already founded a city
131 public function ifUserHasFoundedCity () {
133 $userInstance = Registry::getRegistry()->getInstance('user');
135 // Now get a search criteria instance
136 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
138 // Search for user's cities
139 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
142 $resultInstance = $this->doSelectByCriteria($searchInstance);
144 // Get city manager instance
145 $managerInstance = ManagerFactory::createManagerByType('city');
147 // Set result instance
148 $managerInstance->setResultInstance($resultInstance);
150 // Has it been founded?
151 $hasFounded = $resultInstance->next();
158 * Checks whether the given city name is taken
160 * @para $cityName Name of city
161 * @return $isTaken Whether the given city name is taken
163 public function ifCityExists ($cityName) {
164 // Now get a search criteria instance
165 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
167 // Search for the city number one which is hard-coded the default
168 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
169 $searchInstance->setLimit(1);
172 $resultInstance = $this->doSelectByCriteria($searchInstance);
175 $isTaken = $resultInstance->valid();
177 // Get manger instance
178 $managerInstance = ManagerFactory::createManagerByType('city');
180 // Set result instance
181 $managerInstance->setResultInstance($resultInstance);
188 * Creates a city from given request
190 * @para $requestInstance An instance of a Requestable class
193 public function createCityByRequest (Requestable $requestInstance) {
194 // Make sure all required fields are there
195 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
196 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
198 // Get city name (to save some calls)
199 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
201 // Make sure the city name is not taken yet
202 assert(!$this->ifCityExists($cityName));
205 $userInstance = Registry::getRegistry()->getInstance('user');
207 // Get a dataset instance
208 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
210 // Set the primary key
211 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
213 // Add city name and assign user id
214 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1));
215 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName);
216 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId());
217 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
219 // "Insert" this dataset instance completely into the database
220 $this->queryInsertDataSet($dataSetInstance);
223 assert($this->ifCityExists($cityName));
227 * Getter for all city ids as an array
229 * @return $cityIds All city ids as an array
231 public function getAllCityIds () {
232 // Init empty search instance
233 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
235 // And run it on the database
236 $resultInstance = $this->doSelectByCriteria($searchInstance);
242 if ($resultInstance->count() == 0) {
247 // Now get all 'city_id' columns
248 while ($resultInstance->next()) {
250 $current = $resultInstance->current();
252 // 'city_id' should be there
253 assert(isset($current[self::DB_COLUMN_CITY_ID]));
255 // Add it to the array
256 array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);