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