]> git.mxchange.org Git - city.git/blob - application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php
Added new stuff for founding first city (unfinished).
[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_NR          = 'city_nr';
30         const DB_COLUMN_CITY_ID          = 'city_id';
31         const DB_COLUMN_CITY_MODE        = 'city_mode';
32         const DB_COLUMN_CITY_NAME        = 'city_name';
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Creates an instance of this database wrapper by a provided user class
46          *
47          * @return      $wrapperInstance        An instance of the created wrapper class
48          */
49         public static final function createCityInformationDatabaseWrapper () {
50                 // Get a new instance
51                 $wrapperInstance = new CityInformationDatabaseWrapper();
52
53                 // Set (primary!) table name
54                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION);
55
56                 // Return the instance
57                 return $wrapperInstance;
58         }
59
60         /**
61          * Checks whether there is an entry for given city instance
62          *
63          * @param       $cityInstance   An instance of a CityHelper class
64          * @return      $isFound                Whether a city id has been found for this city
65          */
66         public function ifCityDataIsFound (CityHelper $cityInstance) {
67                 // Is there cache?
68                 if (!isset($GLOBALS[__METHOD__])) {
69                         // Now get a search criteria instance
70                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
71
72                         // Search for the city number one which is hard-coded the default
73                         $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NR  , 1);
74                         $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
75                         $searchInstance->setLimit(1);
76
77                         // Get a result back
78                         $resultInstance = $this->doSelectByCriteria($searchInstance);
79
80                         // Set result instance in city instance
81                         $cityInstance->setResultInstance($resultInstance);
82
83                         // Is it valid?
84                         $GLOBALS[__METHOD__] = $resultInstance->next();
85                 } // END - if
86
87                 // Return it
88                 return $GLOBALS[__METHOD__];
89         }
90
91         /**
92          * 'Registers' a new city id along with data provided in the city instance.
93          * This may sound confusing but avoids double code very nicely...
94          *
95          * @param       $cityInstance           A city instance
96          * @param       $requestInstance        An instance of a Requestable class
97          * @return      void
98          */
99         public function registerCityId (BaseCity $cityInstance, Requestable $requestInstance) {
100                 // Get a dataset instance
101                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION));
102
103                 // Set the primary key
104                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID);
105
106                 // Add registration elements to the dataset
107                 $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
108
109                 // "Insert" this dataset instance completely into the database
110                 $this->queryInsertDataSet($dataSetInstance);
111         }
112
113         /**
114          * Removes non-public data from given array.
115          *
116          * @param       $data   An array with possible non-public data that needs to be removed.
117          * @return      $data   A cleaned up array with only public data.
118          */
119         public function removeNonPublicDataFromArray(array $data) {
120                 // Currently call only inner method
121                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
122                 $data = parent::removeNonPublicDataFromArray($data);
123
124                 // Return cleaned data
125                 return $data;
126         }
127 }
128
129 // [EOF]
130 ?>