From 41dd7b2bcb65c88911235dd34276994556a75f00 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 17 Apr 2015 22:46:21 +0200 Subject: [PATCH] Added verifications, if the user has already founded a city and if the city name is already taken. If the first city shall be founded, a dummy region must be created, too. Then the city needs to be assigned with it. The region is more an organizational way of handling more than one city, for example the user then can apply policies to an entire region and not just a city or a district. Signed-off-by: Roland Haeder --- application/city/config.php | 3 + .../manager/city/class_ManageableCity.php | 8 ++ .../html/class_HtmlCityMapCommand.php | 18 ++- .../class_CityInformationDatabaseWrapper.php | 55 +++++++++ .../city/main/factories/manager/.htaccess | 1 + .../manager/class_ManagerFactory.php | 66 +++++++++++ .../city/main/filter/verifier/.htaccess | 1 + .../verifier/class_CityNameVerifierFilter.php | 112 ++++++++++++++++++ .../main/manager/city/class_CityManager.php | 20 +++- .../city/main/manager/class_BaseManager.php | 2 +- .../de/code/action_city_login_city_map.ctp | 4 +- 11 files changed, 278 insertions(+), 12 deletions(-) create mode 100644 application/city/main/factories/manager/.htaccess create mode 100644 application/city/main/factories/manager/class_ManagerFactory.php create mode 100644 application/city/main/filter/verifier/.htaccess create mode 100644 application/city/main/filter/verifier/class_CityNameVerifierFilter.php diff --git a/application/city/config.php b/application/city/config.php index 29ed1ad..46d0815 100644 --- a/application/city/config.php +++ b/application/city/config.php @@ -284,6 +284,9 @@ $cfg->setConfigEntry('birthday_register_verifier_filter', 'BirthdayVerifierFilte // CFG: BIRTHDAY-PROFILE-VERIFIER-FILTER $cfg->setConfigEntry('birthday_profile_verifier_filter', 'BirthdayVerifierFilter'); +// CFG: CITY-NAME-VERIFIER-FILTER +$cfg->setConfigEntry('city_name_verifier_filter', 'CityNameVerifierFilter'); + // CFG: USER-STATUS-FILTER $cfg->setConfigEntry('user_status_filter', 'UserStatusVerifierFilter'); diff --git a/application/city/interfaces/manager/city/class_ManageableCity.php b/application/city/interfaces/manager/city/class_ManageableCity.php index 18d5f19..454b248 100644 --- a/application/city/interfaces/manager/city/class_ManageableCity.php +++ b/application/city/interfaces/manager/city/class_ManageableCity.php @@ -28,6 +28,14 @@ interface ManageableCity extends FrameworkInterface { * @return $isFounded Whether the current user has already founded a city */ function isCityAlreadyFounded (); + + /** + * Checks whether the given city name is already taken + * + * @para $cityName Name of city + * @return $isTaken Whether the given city name is already taken + */ + function ifCityNameExists ($cityName); } // [EOF] diff --git a/application/city/main/commands/html/class_HtmlCityMapCommand.php b/application/city/main/commands/html/class_HtmlCityMapCommand.php index 48a048e..965accd 100644 --- a/application/city/main/commands/html/class_HtmlCityMapCommand.php +++ b/application/city/main/commands/html/class_HtmlCityMapCommand.php @@ -58,17 +58,20 @@ class HtmlCityMapCommand extends BaseCommand implements Commandable { */ public function execute (Requestable $requestInstance, Responseable $responseInstance) { // First get a UserRegistration instance - $managerInstance = ObjectFactory::createObjectByConfiguredName('city_manager_class'); + $managerInstance = ManagerFactory::createManagerByType('city'); // First set request and response instance $managerInstance->setRequestInstance($requestInstance); $managerInstance->setResponseInstance($responseInstance); - // Do things before adding city - $managerInstance->doPreAddCity(); - - // Add new city - $managerInstance->addNewCity(); + // Is there already a city the user has founded? + if ($managerInstance->isCityAlreadyFounded()) { + // Found 2nd,3rd,... city + $managerInstance->foundNewCity(); + } else { + // Found first city + $managerInstance->foundFirstCity(); + } // Redirect or login after registration $managerInstance->doPostAction(); @@ -89,6 +92,9 @@ class HtmlCityMapCommand extends BaseCommand implements Commandable { // Validate user status ('confirmed' no guest) $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('user_status_filter')); + // Check if city name is already used + $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_name_verifier_filter')); + // Validate ... //$controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('email_validator_filter')); } diff --git a/application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php b/application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php index 0fea306..0e78683 100644 --- a/application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php +++ b/application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php @@ -30,6 +30,7 @@ class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements City 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'; /** * Protected constructor @@ -124,6 +125,60 @@ class CityInformationDatabaseWrapper extends BaseDatabaseWrapper implements City // 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'); + + // Set result instance + $managerInstance->setResultInstance($resultInstance); + + // Has it been founded? + $hasFounded = $resultInstance->valid(); + + // Return result + return $hasFounded; + } + + /** + * Checks whether the given city name is taken + * + * @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(); + + // Return result + return $isTaken; + } } // [EOF] diff --git a/application/city/main/factories/manager/.htaccess b/application/city/main/factories/manager/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/application/city/main/factories/manager/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/city/main/factories/manager/class_ManagerFactory.php b/application/city/main/factories/manager/class_ManagerFactory.php new file mode 100644 index 0000000..c0a93bd --- /dev/null +++ b/application/city/main/factories/manager/class_ManagerFactory.php @@ -0,0 +1,66 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2015 City Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.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 ManagerFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton (registry-based) ManageableFoo instance + * + * @param $type Type of manager to return + * @return $managerInstance An instance of a ManageableFoo class + */ + public static final function createManagerByType ($type) { + // Get new factory instance + $factoryInstance = new ManagerFactory(); + + // Generate key + $key = $type . '_manager'; + + // If there is no handler? + if (Registry::getRegistry()->instanceExists($key)) { + // Get handler from registry + $managerInstance = Registry::getRegistry()->getInstance($key); + } else { + // Get the proper manager instance + $managerInstance = ObjectFactory::createObjectByConfiguredName($key . '_class'); + + // Add it to the registry + Registry::getRegistry()->addInstance($key, $managerInstance); + } + + // Return the instance + return $managerInstance; + } +} + +// [EOF] +?> diff --git a/application/city/main/filter/verifier/.htaccess b/application/city/main/filter/verifier/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/application/city/main/filter/verifier/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/city/main/filter/verifier/class_CityNameVerifierFilter.php b/application/city/main/filter/verifier/class_CityNameVerifierFilter.php new file mode 100644 index 0000000..e7ca6d8 --- /dev/null +++ b/application/city/main/filter/verifier/class_CityNameVerifierFilter.php @@ -0,0 +1,112 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core 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 CityNameVerifierFilter extends BaseFilter implements Filterable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this filter class + * + * @return $filterInstance An instance of this filter class + */ + public static final function createCityNameVerifierFilter () { + // Get a new instance + $filterInstance = new CityNameVerifierFilter(); + + // Return the instance + return $filterInstance; + } + + /** + * Executes the filter with given request and response objects + * + * @param $requestInstance An instance of a class with an Requestable interface + * @param $responseInstance An instance of a class with an Responseable interface + * @return void + * @throws FilterChainException If this filter fails to operate + */ + public function execute (Requestable $requestInstance, Responseable $responseInstance) { + // Get city name from request + $cityName = $requestInstance->getRequestElement('city_name'); + + // Is the city name set? + if (is_null($cityName)) { + // Not found in form so stop the filtering process + $requestInstance->requestIsValid(FALSE); + + // Add a message to the response + $responseInstance->addFatalMessage('city_name_unset'); + + // Abort here + throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED); + } elseif (empty($cityName)) { + // Empty field! + $requestInstance->requestIsValid(FALSE); + + // Add a message to the response + $responseInstance->addFatalMessage('city_name_empty'); + + // Abort here + throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED); + } elseif ($this->ifCityNameIsTaken($cityName) === TRUE) { + // City name is already taken + $requestInstance->requestIsValid(FALSE); + + // Add a message to the response + $responseInstance->addFatalMessage('city_name_taken'); + + // Abort here + throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED); + } + } + + /** + * Check whether the city name as already been taken + * + * @param $cityName Cityname to check for existence + * @return $alreadyTaken Whether the city name has been taken + */ + private function ifCityNameIsTaken ($cityName) { + // Get a new instance + $managerInstance = ManagerFactory::createManagerByType('city'); + + // Does the city name exist? + $alreadyTaken = ($managerInstance->ifCityNameExists($cityName)); + + // Return the result + return $alreadyTaken; + } +} + +// [EOF] +?> diff --git a/application/city/main/manager/city/class_CityManager.php b/application/city/main/manager/city/class_CityManager.php index cc6924c..97abac7 100644 --- a/application/city/main/manager/city/class_CityManager.php +++ b/application/city/main/manager/city/class_CityManager.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class CityManager extends BaseFrameworkSystem implements ManageableCity { +class CityManager extends BaseManager implements ManageableCity { /** * Protected constructor * @@ -57,12 +57,26 @@ class CityManager extends BaseFrameworkSystem implements ManageableCity { * @return $isFounded Whether the current user has already founded a city */ public function isCityAlreadyFounded () { - // Default is not found - $isFounded = FALSE; + // Check if the currently set user has already founded a city + $isFounded = $this->getWrapperInstance()->ifUserHasFoundedCity(); // Return result return $isFounded; } + + /** + * Checks whether the given city name is already taken + * + * @para $cityName Name of city + * @return $isTaken Whether the given city name is already taken + */ + public function ifCityNameExists ($cityName) { + // Check if the given city name is taken + $isTaken = $this->getWrapperInstance()->ifCityExists($cityName); + + // Return result + return $isTaken; + } } // [EOF] diff --git a/application/city/main/manager/class_BaseManager.php b/application/city/main/manager/class_BaseManager.php index 408edef..69de06b 100644 --- a/application/city/main/manager/class_BaseManager.php +++ b/application/city/main/manager/class_BaseManager.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseManager extends BaseCitySystem { +class BaseManager extends BaseCitySystem implements Registerable { /** * Protected constructor * diff --git a/application/city/templates/de/code/action_city_login_city_map.ctp b/application/city/templates/de/code/action_city_login_city_map.ctp index 03db31c..09a2fae 100644 --- a/application/city/templates/de/code/action_city_login_city_map.ctp +++ b/application/city/templates/de/code/action_city_login_city_map.ctp @@ -6,7 +6,7 @@ $helperInstance = ObjectFactory::createObjectByConfiguredName('html_form_helper_ $helperInstance->prefetchValueInstance('user', 'city_manager'); // Get manager instance -$managerInstance = ObjectFactory::createObjectByConfiguredName('city_manager_class'); +$managerInstance = ManagerFactory::createManagerByType('city'); // Get user instance $userInstance = Registry::getRegistry()->getInstance('user'); @@ -20,7 +20,7 @@ if ($userInstance->isGuest()) { // Add group for personal data $helperInstance->addFormGroup('city_create', "Gründe deine erste Stadt."); - // Login with user name only + // City name input field $helperInstance->addFormSubGroup('city_name', "Bitte gebe den Namen der Stadt ein."); $helperInstance->addFieldText('city_name', "Dein Stadtname:"); $helperInstance->addInputTextField('city_name'); -- 2.39.2