3 namespace Org\Mxchange\City\Database\Frontend\Information;
6 use Org\Mxchange\City\Daemon\BaseCityDaemon;
8 // Import framework stuff
9 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
13 * A database wrapper for city informations
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2015, 2016 City Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
35 // Constants for database table names
36 const DB_TABLE_CITY_INFORMATION = 'city_data';
38 // Constants for database column names
39 const DB_COLUMN_CITY_ID = 'city_id';
40 const DB_COLUMN_CITY_MODE = 'city_mode';
41 const DB_COLUMN_CITY_NAME = 'city_name';
42 const DB_COLUMN_CITY_USER_ID = 'city_user_id';
43 const DB_COLUMN_CITY_REGION_ID = 'city_region_id';
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this database wrapper by a provided user class
58 * @return $wrapperInstance An instance of the created wrapper class
60 public static final function createCityInformationDatabaseWrapper () {
62 $wrapperInstance = new CityInformationDatabaseWrapper();
64 // Set (primary!) table name
65 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
67 // Return the instance
68 return $wrapperInstance;
72 * Checks whether there is an entry for given city instance
74 * @param $cityInstance An instance of a CityHelper class
75 * @return $isFound Whether a city id has been found for this city
77 public function ifCityDataIsFound (CityHelper $cityInstance) {
78 // Now get a search criteria instance
79 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
81 // Search for the city number one which is hard-coded the default
82 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
83 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
84 $searchInstance->setLimit(1);
87 $resultInstance = $this->doSelectByCriteria($searchInstance);
89 // Set result instance in city instance
90 $cityInstance->setResultInstance($resultInstance);
93 $isFound = $resultInstance->next();
100 * 'Registers' a new city id along with data provided in the city instance.
101 * This may sound confusing but avoids double code very nicely...
103 * @param $cityInstance A city instance
104 * @param $requestInstance An instance of a Requestable class
107 public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
108 // Get a dataset instance
109 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
111 // Set the primary key
112 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
114 // Add registration elements to the dataset
115 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
117 // "Insert" this dataset instance completely into the database
118 $this->queryInsertDataSet($dataSetInstance);
122 * Removes non-public data from given array.
124 * @param $data An array with possible non-public data that needs to be removed.
125 * @return $data A cleaned up array with only public data.
127 public function removeNonPublicDataFromArray(array $data) {
128 // Currently call only inner method
129 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
130 $data = parent::removeNonPublicDataFromArray($data);
132 // Return cleaned data
137 * Checks whether the user has already founded a city
139 * @return $hasFounded Whether the user has already founded a city
141 public function ifUserHasFoundedCity () {
143 $userInstance = Registry::getRegistry()->getInstance('user');
145 // Now get a search criteria instance
146 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
148 // Search for user's cities
149 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
152 $resultInstance = $this->doSelectByCriteria($searchInstance);
154 // Get city manager instance
155 $managerInstance = ManagerFactory::createManagerByType('city');
157 // Make sure the manager instance is valid
158 assert($managerInstance instanceof ManageableCity);
160 // Set result instance
161 $managerInstance->setResultInstance($resultInstance);
163 // Has it been founded?
164 $hasFounded = $resultInstance->next();
171 * Checks whether the given city name is taken
173 * @para $cityName Name of city
174 * @return $isTaken Whether the given city name is taken
176 public function ifCityExists ($cityName) {
177 // Now get a search criteria instance
178 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
180 // Search for the city number one which is hard-coded the default
181 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
182 $searchInstance->setLimit(1);
185 $resultInstance = $this->doSelectByCriteria($searchInstance);
188 $isTaken = $resultInstance->valid();
190 // Get manger instance
191 $managerInstance = ManagerFactory::createManagerByType('city');
193 // Make sure the instance is valid
194 assert($managerInstance instanceof ManageableCity);
196 // Set result instance
197 $managerInstance->setResultInstance($resultInstance);
204 * Creates a city from given request
206 * @para $requestInstance An instance of a Requestable class
209 public function createCityByRequest (Requestable $requestInstance) {
210 // Make sure all required fields are there
211 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
212 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
214 // Get city name (to save some calls)
215 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
217 // Make sure the city name is not taken yet
218 assert(!$this->ifCityExists($cityName));
221 $userInstance = Registry::getRegistry()->getInstance('user');
223 // Get a dataset instance
224 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
226 // Set the primary key
227 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
229 // Add city name and assign user id
230 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1));
231 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName);
232 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId());
233 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
235 // "Insert" this dataset instance completely into the database
236 $this->queryInsertDataSet($dataSetInstance);
239 assert($this->ifCityExists($cityName));
243 * Getter for all city ids as an array
245 * @return $cityIds All city ids as an array
247 public function getAllCityIds () {
248 // Init empty search instance
249 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
251 // And run it on the database
252 $resultInstance = $this->doSelectByCriteria($searchInstance);
258 if ($resultInstance->count() == 0) {
263 // Now get all 'city_id' columns
264 while ($resultInstance->next()) {
266 $current = $resultInstance->current();
268 // 'city_id' should be there
269 assert(isset($current[self::DB_COLUMN_CITY_ID]));
271 // Add it to the array
272 array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);