$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);
$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)
{
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())
{
{
$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));
'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)
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);
{
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);
+ }
+}