3 namespace Org\Mxchange\City\Database\Frontend\Information;
5 // Import application-specific stuff
6 use Org\Mxchange\City\Daemon\BaseCityDaemon;
7 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
8 use Org\Mxchange\City\Helper\CityHelper;
9 use Org\Mxchange\City\Manager\City\ManageableCity;
11 // Import framework stuff
12 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
13 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
14 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
15 use Org\Mxchange\CoreFramework\Registry\Registerable;
16 use Org\Mxchange\CoreFramework\Request\Requestable;
19 * A database frontend for city informations
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2015 - 2023 City Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class CityInformationDatabaseFrontend extends BaseDatabaseFrontend implements CityInformationFrontend, Registerable {
41 // Constants for database table names
42 const DB_TABLE_CITY_INFORMATION = 'city_data';
44 // Constants for database column names
45 const DB_COLUMN_CITY_ID = 'city_id';
46 const DB_COLUMN_CITY_MODE = 'city_mode';
47 const DB_COLUMN_CITY_NAME = 'city_name';
48 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
49 const DB_COLUMN_CITY_REGION_ID = 'city_region_id';
52 * Protected constructor
56 protected function __construct () {
57 // Call parent constructor
58 parent::__construct(__CLASS__);
62 * Creates an instance of this database frontend by a provided user class
64 * @return $frontendInstance An instance of the created frontend class
66 public static final function createCityInformationDatabaseFrontend () {
68 $frontendInstance = new CityInformationDatabaseFrontend();
70 // Set (primary!) table name
71 $frontendInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
73 // Return the instance
74 return $frontendInstance;
78 * Checks whether there is an entry for given city instance
80 * @param $cityInstance An instance of a CityHelper class
81 * @return $isFound Whether a city id has been found for this city
83 public function ifCityDataIsFound (CityHelper $cityInstance) {
84 // Now get a search criteria instance
85 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
87 // Search for the city number one which is hard-coded the default
88 $searchInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_ID , 1);
89 $searchInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
90 $searchInstance->setLimit(1);
93 $resultInstance = $this->doSelectByCriteria($searchInstance);
95 // Set result instance in city instance
96 $cityInstance->setResultInstance($resultInstance);
99 $isFound = $resultInstance->next();
106 * 'Registers' a new city id along with data provided in the city instance.
107 * This may sound confusing but avoids double code very nicely...
109 * @param $cityInstance A city instance
110 * @param $requestInstance An instance of a Requestable class
113 public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
114 // Get a dataset instance
115 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
117 // Set the primary key
118 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
120 // Add registration elements to the dataset
121 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
123 // "Insert" this dataset instance completely into the database
124 $this->queryInsertDataSet($dataSetInstance);
128 * Removes non-public data from given array.
130 * @param $data An array with possible non-public data that needs to be removed.
131 * @return $data A cleaned up array with only public data.
133 public function removeNonPublicDataFromArray(array $data) {
134 // Currently call only inner method
135 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
136 $data = parent::removeNonPublicDataFromArray($data);
138 // Return cleaned data
143 * Checks whether the user has already founded a city
145 * @return $hasFounded Whether the user has already founded a city
147 public function ifUserHasFoundedCity () {
149 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
151 // Now get a search criteria instance
152 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
154 // Search for user's cities
155 $searchInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
158 $resultInstance = $this->doSelectByCriteria($searchInstance);
160 // Get city manager instance
161 $managerInstance = ManagerFactory::createManagerByType('city');
163 // Make sure the manager instance is valid
164 assert($managerInstance instanceof ManageableCity);
166 // Set result instance
167 $managerInstance->setResultInstance($resultInstance);
169 // Has it been founded?
170 $hasFounded = $resultInstance->next();
177 * Checks whether the given city name is taken
179 * @para $cityName Name of city
180 * @return $isTaken Whether the given city name is taken
182 public function ifCityExists (string $cityName) {
183 // Now get a search criteria instance
184 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
186 // Search for the city number one which is hard-coded the default
187 $searchInstance->addCriteria(CityInformationDatabaseFrontend::DB_COLUMN_CITY_NAME, $cityName);
188 $searchInstance->setLimit(1);
191 $resultInstance = $this->doSelectByCriteria($searchInstance);
194 $isTaken = $resultInstance->valid();
196 // Get manger instance
197 $managerInstance = ManagerFactory::createManagerByType('city');
199 // Make sure the instance is valid
200 assert($managerInstance instanceof ManageableCity);
202 // Set result instance
203 $managerInstance->setResultInstance($resultInstance);
210 * Creates a city from given request
212 * @para $requestInstance An instance of a Requestable class
215 public function createCityByRequest (Requestable $requestInstance) {
216 // Make sure all required fields are there
217 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
218 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
220 // Get city name (to save some calls)
221 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
223 // Make sure the city name is not taken yet
224 assert(!$this->ifCityExists($cityName));
227 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
229 // Get a dataset instance
230 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
232 // Set the primary key
233 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
235 // Add city name and assign user id
236 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1));
237 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName);
238 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId());
239 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
241 // "Insert" this dataset instance completely into the database
242 $this->queryInsertDataSet($dataSetInstance);
245 assert($this->ifCityExists($cityName));
249 * Getter for all city ids as an array
251 * @return $cityIds All city ids as an array
253 public function getAllCityIds () {
254 // Init empty search instance
255 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
257 // And run it on the database
258 $resultInstance = $this->doSelectByCriteria($searchInstance);
264 if ($resultInstance->count() == 0) {
269 // Now get all 'city_id' columns
270 while ($resultInstance->next()) {
272 $current = $resultInstance->current();
274 // 'city_id' should be there
275 assert(isset($current[self::DB_COLUMN_CITY_ID]));
277 // Add it to the array
278 array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);