]> git.mxchange.org Git - city.git/blob - application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php
6624b56100f39db126518f70953e9099e8bd38ad
[city.git] / application / city / main / database / frontend / city / class_CityInformationDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for city informations
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2015 City Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable {
25         // Constants for database table names
26         const DB_TABLE_CITY_INFORMATION = 'city_data';
27
28         // Constants for database column names
29         const DB_COLUMN_CITY_ID          = 'city_id';
30         const DB_COLUMN_CITY_MODE        = 'city_mode';
31         const DB_COLUMN_CITY_NAME        = 'city_name';
32         const DB_COLUMN_CITY_USER_ID     = 'city_user_id';
33         const DB_COLUMN_CITY_REGION_ID   = 'city_region_id';
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43         }
44
45         /**
46          * Creates an instance of this database wrapper by a provided user class
47          *
48          * @return      $wrapperInstance        An instance of the created wrapper class
49          */
50         public static final function createCityInformationDatabaseWrapper () {
51                 // Get a new instance
52                 $wrapperInstance = new CityInformationDatabaseWrapper();
53
54                 // Set (primary!) table name
55                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
56
57                 // Return the instance
58                 return $wrapperInstance;
59         }
60
61         /**
62          * Checks whether there is an entry for given city instance
63          *
64          * @param       $cityInstance   An instance of a CityHelper class
65          * @return      $isFound                Whether a city id has been found for this city
66          */
67         public function ifCityDataIsFound (CityHelper $cityInstance) {
68                 // Now get a search criteria instance
69                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
70
71                 // Search for the city number one which is hard-coded the default
72                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID  , 1);
73                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
74                 $searchInstance->setLimit(1);
75
76                 // Get a result back
77                 $resultInstance = $this->doSelectByCriteria($searchInstance);
78
79                 // Set result instance in city instance
80                 $cityInstance->setResultInstance($resultInstance);
81
82                 // Is it valid?
83                 $isFound = $resultInstance->next();
84
85                 // Return it
86                 return $isFound;
87         }
88
89         /**
90          * 'Registers' a new city id along with data provided in the city instance.
91          * This may sound confusing but avoids double code very nicely...
92          *
93          * @param       $cityInstance           A city instance
94          * @param       $requestInstance        An instance of a Requestable class
95          * @return      void
96          */
97         public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) {
98                 // Get a dataset instance
99                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
100
101                 // Set the primary key
102                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
103
104                 // Add registration elements to the dataset
105                 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
106
107                 // "Insert" this dataset instance completely into the database
108                 $this->queryInsertDataSet($dataSetInstance);
109         }
110
111         /**
112          * Removes non-public data from given array.
113          *
114          * @param       $data   An array with possible non-public data that needs to be removed.
115          * @return      $data   A cleaned up array with only public data.
116          */
117         public function removeNonPublicDataFromArray(array $data) {
118                 // Currently call only inner method
119                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
120                 $data = parent::removeNonPublicDataFromArray($data);
121
122                 // Return cleaned data
123                 return $data;
124         }
125
126         /**
127          * Checks whether the user has already founded a city
128          *
129          * @return      $hasFounded             Whether the user has already founded a city
130          */
131         public function ifUserHasFoundedCity () {
132                 // Get user instance
133                 $userInstance = Registry::getRegistry()->getInstance('user');
134
135                 // Now get a search criteria instance
136                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
137
138                 // Search for user's cities
139                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
140
141                 // Get a result back
142                 $resultInstance = $this->doSelectByCriteria($searchInstance);
143
144                 // Get city manager instance
145                 $managerInstance = ManagerFactory::createManagerByType('city');
146
147                 // Set result instance
148                 $managerInstance->setResultInstance($resultInstance);
149
150                 // Has it been founded?
151                 $hasFounded = $resultInstance->next();
152
153                 // Return result
154                 return $hasFounded;
155         }
156
157         /**
158          * Checks whether the given city name is taken
159          *
160          * @para        $cityName       Name of city
161          * @return      $isTaken        Whether the given city name is taken
162          */
163         public function ifCityExists ($cityName) {
164                 // Now get a search criteria instance
165                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
166
167                 // Search for the city number one which is hard-coded the default
168                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
169                 $searchInstance->setLimit(1);
170
171                 // Get a result back
172                 $resultInstance = $this->doSelectByCriteria($searchInstance);
173
174                 // Check it
175                 $isTaken = $resultInstance->valid();
176
177                 // Get manger instance
178                 $managerInstance = ManagerFactory::createManagerByType('city');
179
180                 // Set result instance
181                 $managerInstance->setResultInstance($resultInstance);
182
183                 // Return result
184                 return $isTaken;
185         }
186
187         /**
188          * Creates a city from given request
189          *
190          * @para        $requestInstance        An instance of a Requestable class
191          * @return      void
192          */
193         public function createCityByRequest (Requestable $requestInstance) {
194                 // Make sure all required fields are there
195                 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
196                 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
197
198                 // Get city name (to save some calls)
199                 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
200
201                 // Make sure the city name is not taken yet
202                 assert(!$this->ifCityExists($cityName));
203
204                 // Get user instance
205                 $userInstance = Registry::getRegistry()->getInstance('user');
206
207                 // Get a dataset instance
208                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
209
210                 // Set the primary key
211                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
212
213                 // Add city name and assign user id
214                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID       , ($this->countTotalRows() + 1));
215                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME     , $cityName);
216                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID  , $userInstance->getUserId());
217                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
218
219                 // "Insert" this dataset instance completely into the database
220                 $this->queryInsertDataSet($dataSetInstance);
221
222                 // Post-check name
223                 assert($this->ifCityExists($cityName));
224         }
225
226         /**
227          * Getter for all city ids as an array
228          *
229          * @return      $cityIds        All city ids as an array
230          */
231         public function getAllCityIds () {
232                 // Init empty search instance
233                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
234
235                 // And run it on the database
236                 $resultInstance = $this->doSelectByCriteria($searchInstance);
237
238                 // Init array
239                 $cityIds = array();
240
241                 // Anything found?
242                 if ($resultInstance->count() == 0) {
243                         // Nothing found
244                         return $cityIds;
245                 } // END - if
246
247                 // Now get all 'city_id' columns
248                 while ($resultInstance->next()) {
249                         // Get current entry
250                         $current = $resultInstance->current();
251
252                         // 'city_id' should be there
253                         assert(isset($current[self::DB_COLUMN_CITY_ID]));
254
255                         // Add it to the array
256                         array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]);
257                 } // END - while
258
259                 // Return result
260                 return $cityIds;
261         }
262 }
263
264 // [EOF]
265 ?>