* @version 0.0.0 * @copyright Copyright (c) 2015, 2016 City Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements CityInformationWrapper, Registerable { // Constants for database table names const DB_TABLE_CITY_INFORMATION = 'city_data'; // Constants for database column names const DB_COLUMN_CITY_ID = 'city_id'; const DB_COLUMN_CITY_MODE = 'city_mode'; const DB_COLUMN_CITY_NAME = 'city_name'; const DB_COLUMN_CITY_USER_ID = 'city_user_id'; const DB_COLUMN_CITY_REGION_ID = 'city_region_id'; /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this database wrapper by a provided user class * * @return $wrapperInstance An instance of the created wrapper class */ public static final function createCityInformationDatabaseWrapper () { // Get a new instance $wrapperInstance = new CityInformationDatabaseWrapper(); // Set (primary!) table name $wrapperInstance->setTableName(self::DB_TABLE_CITY_INFORMATION); // Return the instance return $wrapperInstance; } /** * Checks whether there is an entry for given city instance * * @param $cityInstance An instance of a CityHelper class * @return $isFound Whether a city id has been found for this city */ public function ifCityDataIsFound (CityHelper $cityInstance) { // Now get a search criteria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Search for the city number one which is hard-coded the default $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1); $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode')); $searchInstance->setLimit(1); // Get a result back $resultInstance = $this->doSelectByCriteria($searchInstance); // Set result instance in city instance $cityInstance->setResultInstance($resultInstance); // Is it valid? $isFound = $resultInstance->next(); // Return it return $isFound; } /** * 'Registers' a new city id along with data provided in the city instance. * This may sound confusing but avoids double code very nicely... * * @param $cityInstance A city instance * @param $requestInstance An instance of a Requestable class * @return void */ public function registerCityId (BaseCityDaemon $cityInstance, Requestable $requestInstance) { // Get a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION)); // Set the primary key $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID); // Add registration elements to the dataset $cityInstance->addElementsToDataSet($dataSetInstance, $requestInstance); // "Insert" this dataset instance completely into the database $this->queryInsertDataSet($dataSetInstance); } /** * Removes non-public data from given array. * * @param $data An array with possible non-public data that needs to be removed. * @return $data A cleaned up array with only public data. */ public function removeNonPublicDataFromArray(array $data) { // Currently call only inner method /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...'); $data = parent::removeNonPublicDataFromArray($data); // Return cleaned data return $data; } /** * Checks whether the user has already founded a city * * @return $hasFounded Whether the user has already founded a city */ public function ifUserHasFoundedCity () { // Get user instance $userInstance = Registry::getRegistry()->getInstance('user'); // Now get a search criteria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Search for user's cities $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_USER_ID, $userInstance->getUserId()); // Get a result back $resultInstance = $this->doSelectByCriteria($searchInstance); // Get city manager instance $managerInstance = ManagerFactory::createManagerByType('city'); // Make sure the manager instance is valid assert($managerInstance instanceof ManageableCity); // Set result instance $managerInstance->setResultInstance($resultInstance); // Has it been founded? $hasFounded = $resultInstance->next(); // Return result return $hasFounded; } /** * Checks whether the given city name is taken * * @para $cityName Name of city * @return $isTaken Whether the given city name is taken */ public function ifCityExists ($cityName) { // Now get a search criteria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Search for the city number one which is hard-coded the default $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME, $cityName); $searchInstance->setLimit(1); // Get a result back $resultInstance = $this->doSelectByCriteria($searchInstance); // Check it $isTaken = $resultInstance->valid(); // Get manger instance $managerInstance = ManagerFactory::createManagerByType('city'); // Make sure the instance is valid assert($managerInstance instanceof ManageableCity); // Set result instance $managerInstance->setResultInstance($resultInstance); // Return result return $isTaken; } /** * Creates a city from given request * * @para $requestInstance An instance of a Requestable class * @return void */ public function createCityByRequest (Requestable $requestInstance) { // Make sure all required fields are there assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_NAME)); assert($requestInstance->isRequestElementSet(self::DB_COLUMN_CITY_REGION_ID)); // Get city name (to save some calls) $cityName = $requestInstance->getRequestElement(self::DB_COLUMN_CITY_NAME); // Make sure the city name is not taken yet assert(!$this->ifCityExists($cityName)); // Get user instance $userInstance = Registry::getRegistry()->getInstance('user'); // Get a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_INFORMATION)); // Set the primary key $dataSetInstance->setUniqueKey(self::DB_COLUMN_CITY_ID); // Add city name and assign user id $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , ($this->countTotalRows() + 1)); $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_NAME , $cityName); $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_USER_ID , $userInstance->getUserId()); $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_REGION_ID, $requestInstance->getRequestElement(self::DB_COLUMN_CITY_REGION_ID)); // "Insert" this dataset instance completely into the database $this->queryInsertDataSet($dataSetInstance); // Post-check name assert($this->ifCityExists($cityName)); } /** * Getter for all city ids as an array * * @return $cityIds All city ids as an array */ public function getAllCityIds () { // Init empty search instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // And run it on the database $resultInstance = $this->doSelectByCriteria($searchInstance); // Init array $cityIds = array(); // Anything found? if ($resultInstance->count() == 0) { // Nothing found return $cityIds; } // END - if // Now get all 'city_id' columns while ($resultInstance->next()) { // Get current entry $current = $resultInstance->current(); // 'city_id' should be there assert(isset($current[self::DB_COLUMN_CITY_ID])); // Add it to the array array_push($cityIds, $current[self::DB_COLUMN_CITY_ID]); } // END - while // Return result return $cityIds; } }