Improving the OfficeForm's behaviour.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 11 Dec 2012 22:42:40 +0000 (23:42 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 11 Dec 2012 22:42:40 +0000 (23:42 +0100)
apps/companyfront/modules/office/actions/actions.class.php
lib/form/doctrine/CityForm.class.php
lib/form/doctrine/OfficeForm.class.php
lib/model/doctrine/CityTable.class.php
lib/model/doctrine/RegionTable.class.php

index d505502..babe1ec 100644 (file)
@@ -57,7 +57,18 @@ class officeActions extends sfActions
     $officeInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
     $officeInit->city_id = null;
 
-    $this->form = new OfficeForm($officeInit);
+    $officeParameters = $request->getParameter('office');
+    //Never trust data coming from users.
+    $cityId = 1;
+    if ($officeParameters['city_id'])
+    {
+        $cityId = $officeParameters['city_id'];
+    }
+    $city = CityTable::getInstance()->findOneById($cityId);
+    $regionId = $city->getRegion()->getId();
+    $countryId = $city->getRegion()->getCountry()->getId();
+
+    $this->form = new OfficeForm($officeInit, array('region_id' => $regionId, 'country_id' => $countryId));
 
     $this->sort = $request->getParameter('sort', 'id');
     $this->page = $request->getParameter('page', 1);
@@ -87,7 +98,10 @@ class officeActions extends sfActions
     $this->sort = $request->getParameter('sort', 'id');
     $this->page = $request->getParameter('page', 1);
 
-    $this->form = new OfficeForm($office);
+    $regionId = $office->getCity()->getRegion()->getId();
+    $countryId = $office->getCity()->getRegion()->getCountry()->getId();
+
+    $this->form = new OfficeForm($office, array('region_id' => $regionId, 'country_id' => $countryId));
   }
 
   public function executeUpdate(sfWebRequest $request)
index 978824e..36cefd3 100644 (file)
@@ -14,8 +14,16 @@ class CityForm extends BaseCityForm
   {
     unset($this['city_name']);
 
-    $this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 
-                                                                            'add_empty' => true));
+    //Narrow down the valid options for some field validators
+    $regionsQuery = null;
+    if ($this->getOption('country_id'))
+    {
+        $regionsQuery = RegionTable::getInstance()->getRegionsByCountryIdQuery($this->getOption('country_id'));
+    }
+
+    $this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getRelatedModelName('Region'),
+                                                                            'add_empty' => true,
+                                                                            'query'     => $regionsQuery));
 
     if($this->isNew())
     {
index 5e94bce..15f32c6 100644 (file)
@@ -14,13 +14,21 @@ class OfficeForm extends BaseOfficeForm
   {
     $this->useFields(array('city_id', 'office_street_address', 'office_zip'));
 
+    //Narrow down the valid options for some field validators
+    $citiesQuery = null;
+    if ($this->getOption('region_id'))
+    {
+        $citiesQuery = CityTable::getInstance()->getCitiesByRegionIdQuery($this->getOption('region_id'));
+    }
+
     $this->widgetSchema['longitude'] = new sfWidgetFormInputFloat();
     $this->widgetSchema['latitude'] = new sfWidgetFormInputFloat();
 
-    $this->widgetSchema['city_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('City'), 
-                                                                          'add_empty' => true));
+    $this->widgetSchema['city_id'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getRelatedModelName('City'),
+                                                                          'add_empty' => true,
+                                                                          'query'     => $citiesQuery));
 
-    $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model'   => $this->getRelatedModelName('City'),
+    $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model'    => $this->getRelatedModelName('City'),
                                                                             'required' => true));
 
 
@@ -58,7 +66,7 @@ class OfficeForm extends BaseOfficeForm
                                          'office_street_address' => 'Address: ',
                                          'office_zip'            => 'ZIP:',));
 
-    $this->embedRelation('City');
+    $this->embedRelation('City', null, array('country_id' => $this->getOption('country_id')));
 
 
     //In the future the companies could pay for improvements in their accounts (premium accounts)
index 21b8c98..be04be8 100644 (file)
@@ -31,6 +31,11 @@ class CityTable extends Doctrine_Table
         return $query;
     }
 
+   /**
+    * Returns a query which is able to retrieve cities related to a specified region.
+    *
+    * @return object Doctrine_Query
+    */
     public function getCitiesByRegionIdQuery($regionId)
     {
         return $this->createQuery('city')->where('city.region_id = ?', $regionId);
index 8de6aa0..d726f69 100644 (file)
@@ -16,4 +16,14 @@ class RegionTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('Region');
     }
-}
\ No newline at end of file
+
+   /**
+    * Returns a query which is able to retrieve regions related to a specified country.
+    *
+    * @return object Doctrine_Query
+    */
+    public function getRegionsByCountryIdQuery($countryId)
+    {
+        return $this->createQuery('region')->where('region.country_id = ?', $countryId);
+    }
+}