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