]> git.mxchange.org Git - city.git/blob - application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php
Added region database handling and creation (partly finished).
[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                 // Is there cache?
69                 if (!isset($GLOBALS[__METHOD__])) {
70                         // Now get a search criteria instance
71                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
72
73                         // Search for the city number one which is hard-coded the default
74                         $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID  , 1);
75                         $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
76                         $searchInstance->setLimit(1);
77
78                         // Get a result back
79                         $resultInstance = $this->doSelectByCriteria($searchInstance);
80
81                         // Set result instance in city instance
82                         $cityInstance->setResultInstance($resultInstance);
83
84                         // Is it valid?
85                         $GLOBALS[__METHOD__] = $resultInstance->next();
86                 } // END - if
87
88                 // Return it
89                 return $GLOBALS[__METHOD__];
90         }
91
92         /**
93          * 'Registers' a new city id along with data provided in the city instance.
94          * This may sound confusing but avoids double code very nicely...
95          *
96          * @param       $cityInstance           A city instance
97          * @param       $requestInstance        An instance of a Requestable class
98          * @return      void
99          */
100         public function registerCityId (BaseCity $cityInstance, Requestable $requestInstance) {
101                 // Get a dataset instance
102                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
103
104                 // Set the primary key
105                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
106
107                 // Add registration elements to the dataset
108                 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
109
110                 // "Insert" this dataset instance completely into the database
111                 $this->queryInsertDataSet($dataSetInstance);
112         }
113
114         /**
115          * Removes non-public data from given array.
116          *
117          * @param       $data   An array with possible non-public data that needs to be removed.
118          * @return      $data   A cleaned up array with only public data.
119          */
120         public function removeNonPublicDataFromArray(array $data) {
121                 // Currently call only inner method
122                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
123                 $data = parent::removeNonPublicDataFromArray($data);
124
125                 // Return cleaned data
126                 return $data;
127         }
128
129         /**
130          * Checks whether the user has already founded a city
131          *
132          * @return      $hasFounded             Whether the user has already founded a city
133          */
134         public function ifUserHasFoundedCity () {
135                 // Get user instance
136                 $userInstance = Registry::getRegistry()->getInstance('user');
137
138                 // Now get a search criteria instance
139                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
140
141                 // Search for user's cities
142                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId());
143
144                 // Get a result back
145                 $resultInstance = $this->doSelectByCriteria($searchInstance);
146
147                 // Get city manager instance
148                 $managerInstance = ManagerFactory::createManagerByType('city');
149
150                 // Set result instance
151                 $managerInstance->setResultInstance($resultInstance);
152
153                 // Has it been founded?
154                 $hasFounded = $resultInstance->valid();
155
156                 // Return result
157                 return $hasFounded;
158         }
159
160         /**
161          * Checks whether the given city name is taken
162          *
163          * @para        $cityName       Name of city
164          * @return      $isTaken        Whether the given city name is taken
165          */
166         public function ifCityExists ($cityName) {
167                 // Now get a search criteria instance
168                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
169
170                 // Search for the city number one which is hard-coded the default
171                 $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName);
172                 $searchInstance->setLimit(1);
173
174                 // Get a result back
175                 $resultInstance = $this->doSelectByCriteria($searchInstance);
176
177                 // Check it
178                 $isTaken = $resultInstance->valid();
179
180                 // Get manger instance
181                 $managerInstance = ManagerFactory::createManagerByType('city');
182
183                 // Set result instance
184                 $managerInstance->setResultInstance($resultInstance);
185
186                 // Return result
187                 return $isTaken;
188
189         /**
190          * Creates a city from given request
191          *
192          * @para        $requestInstance        An instance of a Requestable class
193          * @return      void
194          */
195         public function createCityByRequest (Requestable $requestInstance) {
196                 // Make sure all required fields are there
197                 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME));
198                 assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID));
199
200                 // Get city name (to save some calls)
201                 $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME);
202
203                 // Make sure the city name is not taken yet
204                 assert(!$this->ifCityExists($cityName));
205
206                 // Get user instance
207                 $userInstance = Registry::getRegistry()->getInstance('user');
208
209                 // Get a dataset instance
210                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
211
212                 // Set the primary key
213                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
214
215                 // Add city name and assign user id
216                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID       , ($this->countTotalRows() + 1));
217                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME     , $cityName);
218                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID  , $userInstance->getUserId());
219                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID));
220
221                 // "Insert" this dataset instance completely into the database
222                 $this->queryInsertDataSet($dataSetInstance);
223
224                 // Post-check name
225                 assert($this->ifCityExists($cityName));
226         }
227 }
228
229 // [EOF]
230 ?>