]> git.mxchange.org Git - city.git/blob - application/city/classes/database/frontend/city/class_CityInformationDatabaseWrapper.php
Next wave:
[city.git] / application / city / classes / database / frontend / city / class_CityInformationDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Database\Frontend\Information;
4
5 // Import own stuff
6 use Org\Mxchange\City\Daemon\BaseCityDaemon;
7 use Org\Mxchange\City\Helper\CityHelper;
8
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;
13
14 /**
15  * A database wrapper for city informations
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
37         // Constants for database table names
38         const DB_TABLE_CITY_INFORMATION = 'city_data';
39
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';
46
47         /**
48          * Protected constructor
49          *
50          * @return      void
51          */
52         protected function __construct () {
53                 // Call parent constructor
54                 parent::__construct(__CLASS__);
55         }
56
57         /**
58          * Creates an instance of this database wrapper by a provided user class
59          *
60          * @return      $wrapperInstance        An instance of the created wrapper class
61          */
62         public static final function createCityInformationDatabaseWrapper () {
63                 // Get a new instance
64                 $wrapperInstance = new CityInformationDatabaseWrapper();
65
66                 // Set (primary!) table name
67                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
68
69                 // Return the instance
70                 return $wrapperInstance;
71         }
72
73         /**
74          * Checks whether there is an entry for given city instance
75          *
76          * @param       $cityInstance   An instance of a CityHelper class
77          * @return      $isFound                Whether a city id has been found for this city
78          */
79         public function ifCityDataIsFound (CityHelper $cityInstance) {
80                 // Now get a search criteria instance
81                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
82
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);
87
88                 // Get a result back
89                 $resultInstance = $this->doSelectByCriteria($searchInstance);
90
91                 // Set result instance in city instance
92                 $cityInstance->setResultInstance($resultInstance);
93
94                 // Is it valid?
95                 $isFound = $resultInstance->next();
96
97                 // Return it
98                 return $isFound;
99         }
100
101         /**
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...
104          *
105          * @param       $cityInstance           A city instance
106          * @param       $requestInstance        An instance of a Requestable class
107          * @return      void
108          */
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));
112
113                 // Set the primary key
114                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
115
116                 // Add registration elements to the dataset
117                 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
118
119                 // "Insert" this dataset instance completely into the database
120                 $this->queryInsertDataSet($dataSetInstance);
121         }
122
123         /**
124          * Removes non-public data from given array.
125          *
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.
128          */
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);
133
134                 // Return cleaned data
135                 return $data;
136         }
137
138         /**
139          * Checks whether the user has already founded a city
140          *
141          * @return      $hasFounded             Whether the user has already founded a city
142          */
143         public function ifUserHasFoundedCity () {
144                 // Get user instance
145                 $userInstance = Registry::getRegistry()->getInstance('user');
146
147                 // Now get a search criteria instance
148                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
149
150                 // Search for user's cities
151                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
152
153                 // Get a result back
154                 $resultInstance = $this->doSelectByCriteria($searchInstance);
155
156                 // Get city manager instance
157                 $managerInstance = ManagerFactory::createManagerByType('city');
158
159                 // Make sure the manager instance is valid
160                 assert($managerInstance instanceof ManageableCity);
161
162                 // Set result instance
163                 $managerInstance->setResultInstance($resultInstance);
164
165                 // Has it been founded?
166                 $hasFounded = $resultInstance->next();
167
168                 // Return result
169                 return $hasFounded;
170         }
171
172         /**
173          * Checks whether the given city name is taken
174          *
175          * @para        $cityName       Name of city
176          * @return      $isTaken        Whether the given city name is taken
177          */
178         public function ifCityExists ($cityName) {
179                 // Now get a search criteria instance
180                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
181
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);
185
186                 // Get a result back
187                 $resultInstance = $this->doSelectByCriteria($searchInstance);
188
189                 // Check it
190                 $isTaken = $resultInstance->valid();
191
192                 // Get manger instance
193                 $managerInstance = ManagerFactory::createManagerByType('city');
194
195                 // Make sure the instance is valid
196                 assert($managerInstance instanceof ManageableCity);
197
198                 // Set result instance
199                 $managerInstance->setResultInstance($resultInstance);
200
201                 // Return result
202                 return $isTaken;
203         }
204
205         /**
206          * Creates a city from given request
207          *
208          * @para        $requestInstance        An instance of a Requestable class
209          * @return      void
210          */
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));
215
216                 // Get city name (to save some calls)
217                 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
218
219                 // Make sure the city name is not taken yet
220                 assert(!$this->ifCityExists($cityName));
221
222                 // Get user instance
223                 $userInstance = Registry::getRegistry()->getInstance('user');
224
225                 // Get a dataset instance
226                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
227
228                 // Set the primary key
229                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
230
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));
236
237                 // "Insert" this dataset instance completely into the database
238                 $this->queryInsertDataSet($dataSetInstance);
239
240                 // Post-check name
241                 assert($this->ifCityExists($cityName));
242         }
243
244         /**
245          * Getter for all city ids as an array
246          *
247          * @return      $cityIds        All city ids as an array
248          */
249         public function getAllCityIds () {
250                 // Init empty search instance
251                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
252
253                 // And run it on the database
254                 $resultInstance = $this->doSelectByCriteria($searchInstance);
255
256                 // Init array
257                 $cityIds = array();
258
259                 // Anything found?
260                 if ($resultInstance->count() == 0) {
261                         // Nothing found
262                         return $cityIds;
263                 } // END - if
264
265                 // Now get all 'city_id' columns
266                 while ($resultInstance->next()) {
267                         // Get current entry
268                         $current = $resultInstance->current();
269
270                         // 'city_id' should be there
271                         assert(isset($current[self::DB_COLUMN_CITY_ID]));
272
273                         // Add it to the array
274                         array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);
275                 } // END - while
276
277                 // Return result
278                 return $cityIds;
279         }
280 }