// 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');
* @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]
*/
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();
// 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'));
}
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
// 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]
--- /dev/null
+Deny from all
--- /dev/null
+<?php
+/**
+ * A factory class for managers
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
--- /dev/null
+Deny from all
--- /dev/null
+<?php
+/**
+ * A concrete filter for verfying the city name. This filter may intercept the
+ * filter chain if no city name is given or if supplied city name has an invalid
+ * form. It could also intercept our filter chain if city name was not found.
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class CityManager extends BaseFrameworkSystem implements ManageableCity {
+class CityManager extends BaseManager implements ManageableCity {
/**
* Protected constructor
*
* @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]
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class BaseManager extends BaseCitySystem {
+class BaseManager extends BaseCitySystem implements Registerable {
/**
* Protected constructor
*
$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');
// 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');