]> git.mxchange.org Git - city.git/blob - application/city/main/city/class_BaseCity.php
Moved more methods to 'core' as they are now more generalized + fixed config entries...
[city.git] / application / city / main / city / class_BaseCity.php
1 <?php
2 /**
3  * A general City class
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 BaseCity extends BaseCitySystem implements Updateable, AddableCriteria {
25         /**
26          * City types
27          */
28         const CITY_TYPE_TESTING = 'testing';
29
30         /**
31          * Whether this City is active (default: FALSE)
32          */
33         private $isActive = FALSE;
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Get a wrapper instance
46                 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
47
48                 // Set it here
49                 $this->setWrapperInstance($wrapperInstance);
50
51                 // Get a crypto instance
52                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
53
54                 // Set it here
55                 $this->setCryptoInstance($cryptoInstance);
56
57                 // Init state which sets the state to 'init'
58                 $this->initState();
59         }
60
61         /**
62          * Initializes the City's state which sets it to 'init'
63          *
64          * @return      void
65          */
66         private function initState() {
67                 // Get the state factory and create the initial state.
68                 CityStateFactory::createCityStateInstanceByName('init', $this);
69         }
70
71         /**
72          * Outputs the console teaser. This should only be executed on startup or
73          * full restarts. This method generates some space around the teaser.
74          *
75          * @return      void
76          */
77         public function outputConsoleTeaser () {
78                 // Get the app instance (for shortening our code)
79                 $app = $this->getApplicationInstance();
80
81                 // Output all lines
82                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
83                 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
84                 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2015 City Developer Team');
85                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
86                 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
87                 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
88                 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
89                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
90         }
91
92         /**
93          * Adds City data elements to a given dataset instance
94          *
95          * @param       $criteriaInstance       An instance of a storeable criteria
96          * @param       $requestInstance        An instance of a Requestable class
97          * @return      void
98          */
99         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
100                 // Make sure the request instance is set as it is not optional.
101                 assert($requestInstance instanceof Requestable);
102
103                 // Add City number and type
104                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NR  , 1);
105                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
106
107                 // Add the City id
108                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
109
110                 // Add the session id if acquired
111                 if ($this->getSessionId() != '') {
112                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
113                 } // END - if
114
115                 // Add the private key if acquired
116                 if ($this->getPrivateKey() != '') {
117                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY, base64_encode($this->getPrivateKey()));
118                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $this->getPrivateKeyHash());
119                 } // END - if
120
121                 // Add own external and internal addresses as UNLs
122                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, CityTools::determineOwnInternalAddress());
123                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL, CityTools::determineOwnExternalAddress());
124         }
125
126         /**
127          * Updates a given field with new value
128          *
129          * @param       $fieldName              Field to update
130          * @param       $fieldValue             New value to store
131          * @return      void
132          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
133          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
134          */
135         public function updateDatabaseField ($fieldName, $fieldValue) {
136                 // Unfinished
137                 $this->partialStub('Unfinished!');
138                 return;
139
140                 // Get a critieria instance
141                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
142
143                 // Add search criteria
144                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
145                 $searchInstance->setLimit(1);
146
147                 // Now get another criteria
148                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
149
150                 // Add criteria entry which we shall update
151                 $updateInstance->addCriteria($fieldName, $fieldValue);
152
153                 // Add the search criteria for searching for the right entry
154                 $updateInstance->setSearchInstance($searchInstance);
155
156                 // Set wrapper class name
157                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
158
159                 // Remember the update in database result
160                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
161         }
162
163         /**
164          * Activates the City by doing some final preparation and setting
165          * $CityIsActive to TRUE.
166          *
167          * @param       $requestInstance        A Requestable class
168          * @param       $responseInstance       A Responseable class
169          * @return      void
170          */
171         public function activateCity (Requestable $requestInstance, Responseable $responseInstance) {
172                 // Get the controller here
173                 $controllerInstance = Registry::getRegistry()->getInstance('controller');
174
175                 // Run all filters for the City activation
176                 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
177
178                 // ----------------------- Last step from here ------------------------
179                 // Activate the City. This is ALWAYS the last step in this method
180                 $this->getStateInstance()->activateCitySimulation();
181                 // ---------------------- Last step until here ------------------------
182         }
183
184         /**
185          * Getter for isActive attribute
186          *
187          * @return      $isActive       Whether the City is active
188          */
189         public final function isCityActive () {
190                 return $this->isActive;
191         }
192
193         /**
194          * Enables (default) or disables isActive flag
195          *
196          * @param       $isActive       Whether the City is active
197          * @return      void
198          */
199         public final function enableIsActive ($isActive = TRUE) {
200                 $this->isActive = (bool) $isActive;
201         }
202
203         /**
204          * Updates/refreshes City data (e.g. status).
205          *
206          * @return      void
207          * @todo        Find more to do here
208          */
209         public function updateCityData () {
210                 // Set some dummy configuration entries, e.g. city_status
211                 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
212         }
213
214         /**
215          * Adds all required elements from given array into data set instance
216          *
217          * @param       $dataSetInstance        An instance of a StoreableCriteria class
218          * @param       $CityData                       An array with valid City data
219          * @return      void
220          */
221         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $CityData) {
222                 // Add all data the array provides
223                 foreach (CityDistributedHashTableDatabaseWrapper::getAllElements() as $element) {
224                         // Is the element there?
225                         if (isset($CityData[$element])) {
226                                 // Add it
227                                 $dataSetInstance->addCriteria($element, $CityData[$element]);
228                         } else {
229                                 // Output warning message
230                                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('City[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
231                         }
232                 } // END - foreac
233         }
234 }
235
236 // [EOF]
237 ?>