From: Gustavo Martin Morcuende Date: Tue, 11 Dec 2012 22:42:40 +0000 (+0100) Subject: Improving the OfficeForm's behaviour. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6bb01e4a9ebf5b5fb220d1693622ea9ba5e47448;p=mobi%2F.git Improving the OfficeForm's behaviour. --- diff --git a/apps/companyfront/modules/office/actions/actions.class.php b/apps/companyfront/modules/office/actions/actions.class.php index d505502..babe1ec 100644 --- a/apps/companyfront/modules/office/actions/actions.class.php +++ b/apps/companyfront/modules/office/actions/actions.class.php @@ -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) diff --git a/lib/form/doctrine/CityForm.class.php b/lib/form/doctrine/CityForm.class.php index 978824e..36cefd3 100644 --- a/lib/form/doctrine/CityForm.class.php +++ b/lib/form/doctrine/CityForm.class.php @@ -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()) { diff --git a/lib/form/doctrine/OfficeForm.class.php b/lib/form/doctrine/OfficeForm.class.php index 5e94bce..15f32c6 100644 --- a/lib/form/doctrine/OfficeForm.class.php +++ b/lib/form/doctrine/OfficeForm.class.php @@ -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) diff --git a/lib/model/doctrine/CityTable.class.php b/lib/model/doctrine/CityTable.class.php index 21b8c98..be04be8 100644 --- a/lib/model/doctrine/CityTable.class.php +++ b/lib/model/doctrine/CityTable.class.php @@ -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); diff --git a/lib/model/doctrine/RegionTable.class.php b/lib/model/doctrine/RegionTable.class.php index 8de6aa0..d726f69 100644 --- a/lib/model/doctrine/RegionTable.class.php +++ b/lib/model/doctrine/RegionTable.class.php @@ -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); + } +}