5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2015 City Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
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.
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.
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/>.
24 class BaseCity extends BaseCitySystem implements Updateable, AddableCriteria {
28 const CITY_TYPE_TESTING = 'testing';
31 * Whether this City is active (default: FALSE)
33 private $isActive = FALSE;
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
45 // Get a wrapper instance
46 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
49 $this->setWrapperInstance($wrapperInstance);
51 // Get a crypto instance
52 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
55 $this->setCryptoInstance($cryptoInstance);
57 // Init state which sets the state to 'init'
62 * Initializes the City's state which sets it to 'init'
66 private function initState() {
67 // Get the state factory and create the initial state.
68 CityStateFactory::createCityStateInstanceByName('init', $this);
72 * Outputs the console teaser. This should only be executed on startup or
73 * full restarts. This method generates some space around the teaser.
77 public function outputConsoleTeaser () {
78 // Get the app instance (for shortening our code)
79 $app = $this->getApplicationInstance();
82 self::createDebugInstance(__CLASS__)->debugOutput(' ');
83 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
84 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 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(' ');
93 * Adds City data elements to a given dataset instance
95 * @param $criteriaInstance An instance of a storeable criteria
96 * @param $requestInstance An instance of a Requestable class
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);
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'));
108 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
110 // Add the session id if acquired
111 if ($this->getSessionId() != '') {
112 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
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());
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());
127 * Updates a given field with new value
129 * @param $fieldName Field to update
130 * @param $fieldValue New value to store
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
135 public function updateDatabaseField ($fieldName, $fieldValue) {
137 $this->partialStub('Unfinished!');
140 // Get a critieria instance
141 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143 // Add search criteria
144 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
145 $searchInstance->setLimit(1);
147 // Now get another criteria
148 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
150 // Add criteria entry which we shall update
151 $updateInstance->addCriteria($fieldName, $fieldValue);
153 // Add the search criteria for searching for the right entry
154 $updateInstance->setSearchInstance($searchInstance);
156 // Set wrapper class name
157 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
159 // Remember the update in database result
160 $this->getResultInstance()->add2UpdateQueue($updateInstance);
164 * Activates the City by doing some final preparation and setting
165 * $CityIsActive to TRUE.
167 * @param $requestInstance A Requestable class
168 * @param $responseInstance A Responseable class
171 public function activateCity (Requestable $requestInstance, Responseable $responseInstance) {
172 // Get the controller here
173 $controllerInstance = Registry::getRegistry()->getInstance('controller');
175 // Run all filters for the City activation
176 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
178 // ----------------------- Last step from here ------------------------
179 // Activate the City. This is ALWAYS the last step in this method
180 $this->getStateInstance()->citySimulationIsActivated();
181 // ---------------------- Last step until here ------------------------
185 * Getter for isActive attribute
187 * @return $isActive Whether the City is active
189 public final function isCityActive () {
190 return $this->isActive;
194 * Enables (default) or disables isActive flag
196 * @param $isActive Whether the City is active
199 public final function enableIsActive ($isActive = TRUE) {
200 $this->isActive = (bool) $isActive;
204 * Updates/refreshes City data (e.g. status).
207 * @todo Find more to do here
209 public function updateCityData () {
210 // Set some dummy configuration entries, e.g. city_status
211 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
215 * Adds all required elements from given array into data set instance
217 * @param $dataSetInstance An instance of a StoreableCriteria class
218 * @param $CityData An array with valid City data
221 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
222 // Add all data the array provides
223 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
224 // Is the element there?
225 if (isset($cityData[$element])) {
227 $dataSetInstance->addCriteria($element, $cityData[$element]);
229 // Output warning message
230 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('City[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');