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