]> git.mxchange.org Git - city.git/commitdiff
Added verifications, if the user has already founded a city and if the city name
authorRoland Haeder <roland@mxchange.org>
Fri, 17 Apr 2015 20:46:21 +0000 (22:46 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 17 Apr 2015 20:46:21 +0000 (22:46 +0200)
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 <roland@mxchange.org>
application/city/config.php
application/city/interfaces/manager/city/class_ManageableCity.php
application/city/main/commands/html/class_HtmlCityMapCommand.php
application/city/main/database/frontend/city/class_CityInformationDatabaseWrapper.php
application/city/main/factories/manager/.htaccess [new file with mode: 0644]
application/city/main/factories/manager/class_ManagerFactory.php [new file with mode: 0644]
application/city/main/filter/verifier/.htaccess [new file with mode: 0644]
application/city/main/filter/verifier/class_CityNameVerifierFilter.php [new file with mode: 0644]
application/city/main/manager/city/class_CityManager.php
application/city/main/manager/class_BaseManager.php
application/city/templates/de/code/action_city_login_city_map.ctp

index 29ed1ad6f57a96a7fb616e556d87cc1e2c1daf24..46d08155809d6df498631a85f39f112490e907d8 100644 (file)
@@ -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');
 
index 18d5f1904ede348e934b7ed7a820bf9fdd439641..454b248ce2372583cebe82b1136592284fe24caa 100644 (file)
@@ -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]
index 48a048e62c28f070342090655e4af965e2435850..965accd216322b3a476e6945f1fd1ad7e5f5ee8d 100644 (file)
@@ -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'));
        }
index 0fea306d70038218a75b01222e92ee88bbf0d671..0e786839a20d7b911a268d404cd9cd3f13c1bce4 100644 (file)
@@ -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 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..c0a93bd
--- /dev/null
@@ -0,0 +1,66 @@
+<?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]
+?>
diff --git a/application/city/main/filter/verifier/.htaccess b/application/city/main/filter/verifier/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -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 (file)
index 0000000..e7ca6d8
--- /dev/null
@@ -0,0 +1,112 @@
+<?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]
+?>
index cc6924ca432de0e1c4a4192402a1fd0a1642f5ed..97abac722ca4f8cd90a31f11ac2dda7290daada3 100644 (file)
@@ -21,7 +21,7 @@
  * 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
         *
@@ -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]
index 408edef70a06144146e307798cc8354357136fdb..69de06b703d1abdb43cc464a17899873d8c6596c 100644 (file)
@@ -21,7 +21,7 @@
  * 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
         *
index 03db31c9e4d741cc579dd0ce1f8352acd5bd73a1..09a2faef5a584964ac8b833cdfc005c0a0d0e314 100644 (file)
@@ -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&uuml;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');