3 namespace Org\Mxchange\City\Database\Frontend\Information;
6 use Org\Mxchange\City\Daemon\BaseCityDaemon;
7 use Org\Mxchange\City\Helper\CityHelper;
9 // Import framework stuff
10 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Registry\Registry;
12 use Org\Mxchange\CoreFramework\Request\Requestable;
15 * A database wrapper for city informations
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2015, 2016 City Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
37 // Constants for database table names
38 const DB_TABLE_CITY_INFORMATION = 'city_data';
40 // Constants for database column names
41 const DB_COLUMN_CITY_ID = 'city_id';
42 const DB_COLUMN_CITY_MODE = 'city_mode';
43 const DB_COLUMN_CITY_NAME = 'city_name';
44 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
45 const DB_COLUMN_CITY_REGION_ID = 'city_region_id';
48 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Creates an instance of this database wrapper by a provided user class
60 * @return $wrapperInstance An instance of the created wrapper class
62 public static final function createCityInformationDatabaseWrapper () {
64 $wrapperInstance = new CityInformationDatabaseWrapper();
66 // Set (primary!) table name
67 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
69 // Return the instance
70 return $wrapperInstance;
74 * Checks whether there is an entry for given city instance
76 * @param $cityInstance An instance of a CityHelper class
77 * @return $isFound Whether a city id has been found for this city
79 public function ifCityDataIsFound (CityHelper $cityInstance) {
80 // Now get a search criteria instance
81 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
83 // Search for the city number one which is hard-coded the default
84 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
85 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
86 $searchInstance->setLimit(1);
89 $resultInstance = $this->doSelectByCriteria($searchInstance);
91 // Set result instance in city instance
92 $cityInstance->setResultInstance($resultInstance);
95 $isFound = $resultInstance->next();
102 * 'Registers' a new city id along with data provided in the city instance.
103 * This may sound confusing but avoids double code very nicely...
105 * @param $cityInstance A city instance
106 * @param $requestInstance An instance of a Requestable class
109 public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
110 // Get a dataset instance
111 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
113 // Set the primary key
114 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
116 // Add registration elements to the dataset
117 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
119 // "Insert" this dataset instance completely into the database
120 $this->queryInsertDataSet($dataSetInstance);
124 * Removes non-public data from given array.
126 * @param $data An array with possible non-public data that needs to be removed.
127 * @return $data A cleaned up array with only public data.
129 public function removeNonPublicDataFromArray(array $data) {
130 // Currently call only inner method
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
132 $data = parent::removeNonPublicDataFromArray($data);
134 // Return cleaned data
139 * Checks whether the user has already founded a city
141 * @return $hasFounded Whether the user has already founded a city
143 public function ifUserHasFoundedCity () {
145 $userInstance = Registry::getRegistry()->getInstance('user');
147 // Now get a search criteria instance
148 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
150 // Search for user's cities
151 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
154 $resultInstance = $this->doSelectByCriteria($searchInstance);
156 // Get city manager instance
157 $managerInstance = ManagerFactory::createManagerByType('city');
159 // Make sure the manager instance is valid
160 assert($managerInstance instanceof ManageableCity);
162 // Set result instance
163 $managerInstance->setResultInstance($resultInstance);
165 // Has it been founded?
166 $hasFounded = $resultInstance->next();
173 * Checks whether the given city name is taken
175 * @para $cityName Name of city
176 * @return $isTaken Whether the given city name is taken
178 public function ifCityExists ($cityName) {
179 // Now get a search criteria instance
180 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
182 // Search for the city number one which is hard-coded the default
183 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
184 $searchInstance->setLimit(1);
187 $resultInstance = $this->doSelectByCriteria($searchInstance);
190 $isTaken = $resultInstance->valid();
192 // Get manger instance
193 $managerInstance = ManagerFactory::createManagerByType('city');
195 // Make sure the instance is valid
196 assert($managerInstance instanceof ManageableCity);
198 // Set result instance
199 $managerInstance->setResultInstance($resultInstance);
206 * Creates a city from given request
208 * @para $requestInstance An instance of a Requestable class
211 public function createCityByRequest (Requestable $requestInstance) {
212 // Make sure all required fields are there
213 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
214 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
216 // Get city name (to save some calls)
217 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
219 // Make sure the city name is not taken yet
220 assert(!$this->ifCityExists($cityName));
223 $userInstance = Registry::getRegistry()->getInstance('user');
225 // Get a dataset instance
226 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
228 // Set the primary key
229 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
231 // Add city name and assign user id
232 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1));
233 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName);
234 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId());
235 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
237 // "Insert" this dataset instance completely into the database
238 $this->queryInsertDataSet($dataSetInstance);
241 assert($this->ifCityExists($cityName));
245 * Getter for all city ids as an array
247 * @return $cityIds All city ids as an array
249 public function getAllCityIds () {
250 // Init empty search instance
251 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
253 // And run it on the database
254 $resultInstance = $this->doSelectByCriteria($searchInstance);
260 if ($resultInstance->count() == 0) {
265 // Now get all 'city_id' columns
266 while ($resultInstance->next()) {
268 $current = $resultInstance->current();
270 // 'city_id' should be there
271 assert(isset($current[self::DB_COLUMN_CITY_ID]));
273 // Add it to the array
274 array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);