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\Helper\CityHelper;
9 // Import framework stuff
10 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper;
11 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Registry\Registry;
14 use Org\Mxchange\CoreFramework\Request\Requestable;
17 * A database wrapper for city informations
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2015, 2016 City Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
39 // Constants for database table names
40 const DB_TABLE_CITY_INFORMATION = 'city_data';
42 // Constants for database column names
43 const DB_COLUMN_CITY_ID = 'city_id';
44 const DB_COLUMN_CITY_MODE = 'city_mode';
45 const DB_COLUMN_CITY_NAME = 'city_name';
46 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
47 const DB_COLUMN_CITY_REGION_ID = 'city_region_id';
50 * Protected constructor
54 protected function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Creates an instance of this database wrapper by a provided user class
62 * @return $wrapperInstance An instance of the created wrapper class
64 public static final function createCityInformationDatabaseWrapper () {
66 $wrapperInstance = new CityInformationDatabaseWrapper();
68 // Set (primary!) table name
69 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
71 // Return the instance
72 return $wrapperInstance;
76 * Checks whether there is an entry for given city instance
78 * @param $cityInstance An instance of a CityHelper class
79 * @return $isFound Whether a city id has been found for this city
81 public function ifCityDataIsFound (CityHelper $cityInstance) {
82 // Now get a search criteria instance
83 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
85 // Search for the city number one which is hard-coded the default
86 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
87 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
88 $searchInstance->setLimit(1);
91 $resultInstance = $this->doSelectByCriteria($searchInstance);
93 // Set result instance in city instance
94 $cityInstance->setResultInstance($resultInstance);
97 $isFound = $resultInstance->next();
104 * 'Registers' a new city id along with data provided in the city instance.
105 * This may sound confusing but avoids double code very nicely...
107 * @param $cityInstance A city instance
108 * @param $requestInstance An instance of a Requestable class
111 public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
112 // Get a dataset instance
113 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
115 // Set the primary key
116 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
118 // Add registration elements to the dataset
119 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
121 // "Insert" this dataset instance completely into the database
122 $this->queryInsertDataSet($dataSetInstance);
126 * Removes non-public data from given array.
128 * @param $data An array with possible non-public data that needs to be removed.
129 * @return $data A cleaned up array with only public data.
131 public function removeNonPublicDataFromArray(array $data) {
132 // Currently call only inner method
133 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
134 $data = parent::removeNonPublicDataFromArray($data);
136 // Return cleaned data
141 * Checks whether the user has already founded a city
143 * @return $hasFounded Whether the user has already founded a city
145 public function ifUserHasFoundedCity () {
147 $userInstance = Registry::getRegistry()->getInstance('user');
149 // Now get a search criteria instance
150 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
152 // Search for user's cities
153 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
156 $resultInstance = $this->doSelectByCriteria($searchInstance);
158 // Get city manager instance
159 $managerInstance = ManagerFactory::createManagerByType('city');
161 // Make sure the manager instance is valid
162 assert($managerInstance instanceof ManageableCity);
164 // Set result instance
165 $managerInstance->setResultInstance($resultInstance);
167 // Has it been founded?
168 $hasFounded = $resultInstance->next();
175 * Checks whether the given city name is taken
177 * @para $cityName Name of city
178 * @return $isTaken Whether the given city name is taken
180 public function ifCityExists ($cityName) {
181 // Now get a search criteria instance
182 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
184 // Search for the city number one which is hard-coded the default
185 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
186 $searchInstance->setLimit(1);
189 $resultInstance = $this->doSelectByCriteria($searchInstance);
192 $isTaken = $resultInstance->valid();
194 // Get manger instance
195 $managerInstance = ManagerFactory::createManagerByType('city');
197 // Make sure the instance is valid
198 assert($managerInstance instanceof ManageableCity);
200 // Set result instance
201 $managerInstance->setResultInstance($resultInstance);
208 * Creates a city from given request
210 * @para $requestInstance An instance of a Requestable class
213 public function createCityByRequest (Requestable $requestInstance) {
214 // Make sure all required fields are there
215 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
216 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
218 // Get city name (to save some calls)
219 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
221 // Make sure the city name is not taken yet
222 assert(!$this->ifCityExists($cityName));
225 $userInstance = Registry::getRegistry()->getInstance('user');
227 // Get a dataset instance
228 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
230 // Set the primary key
231 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
233 // Add city name and assign user id
234 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1));
235 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName);
236 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId());
237 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
239 // "Insert" this dataset instance completely into the database
240 $this->queryInsertDataSet($dataSetInstance);
243 assert($this->ifCityExists($cityName));
247 * Getter for all city ids as an array
249 * @return $cityIds All city ids as an array
251 public function getAllCityIds () {
252 // Init empty search instance
253 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
255 // And run it on the database
256 $resultInstance = $this->doSelectByCriteria($searchInstance);
262 if ($resultInstance->count() == 0) {
267 // Now get all 'city_id' columns
268 while ($resultInstance->next()) {
270 $current = $resultInstance->current();
272 // 'city_id' should be there
273 assert(isset($current[self::DB_COLUMN_CITY_ID]));
275 // Add it to the array
276 array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);