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';
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of this database wrapper by a provided user class
47 * @return $wrapperInstance An instance of the created wrapper class
49 public static final function createCityInformationDatabaseWrapper () {
51 $wrapperInstance = new CityInformationDatabaseWrapper();
53 // Set (primary!) table name
54 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
56 // Return the instance
57 return $wrapperInstance;
61 * Checks whether there is an entry for given city instance
63 * @param $cityInstance An instance of a CityHelper class
64 * @return $isFound Whether a city id has been found for this city
66 public function ifCityDataIsFound (CityHelper $cityInstance) {
68 if (!isset($GLOBALS[__METHOD__])) {
69 // Now get a search criteria instance
70 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
72 // Search for the city number one which is hard-coded the default
73 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NR , 1);
74 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
75 $searchInstance->setLimit(1);
78 $resultInstance = $this->doSelectByCriteria($searchInstance);
80 // Set result instance in city instance
81 $cityInstance->setResultInstance($resultInstance);
84 $GLOBALS[__METHOD__] = $resultInstance->next();
88 return $GLOBALS[__METHOD__];
92 * 'Registers' a new city id along with data provided in the city instance.
93 * This may sound confusing but avoids double code very nicely...
95 * @param $cityInstance A city instance
96 * @param $requestInstance An instance of a Requestable class
99 public function registerCityId (BaseCity $cityInstance, Requestable $requestInstance) {
100 // Get a dataset instance
101 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
103 // Set the primary key
104 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
106 // Add registration elements to the dataset
107 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
109 // "Insert" this dataset instance completely into the database
110 $this->queryInsertDataSet($dataSetInstance);
114 * Removes non-public data from given array.
116 * @param $data An array with possible non-public data that needs to be removed.
117 * @return $data A cleaned up array with only public data.
119 public function removeNonPublicDataFromArray(array $data) {
120 // Currently call only inner method
121 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
122 $data = parent::removeNonPublicDataFromArray($data);
124 // Return cleaned data