const DB_TABLE_CITY_INFORMATION = 'city_data';
// Constants for database column names
- const DB_COLUMN_CITY_NR = 'city_nr';
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
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
// Search for the city number one which is hard-coded the default
- $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NR , 1);
+ $searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
$searchInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $cityInstance->getRequestInstance()->getRequestElement('mode'));
$searchInstance->setLimit(1);
/**
* 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) {
// Check it
$isTaken = $resultInstance->valid();
+ // Get manger instance
+ $managerInstance = ManagerFactory::createManagerByType('city');
+
+ // 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));
}
}