From 1aa6e39410113837a2577686f3f93cbf0e87e3d9 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 4 Jun 2012 07:04:52 +0200 Subject: [PATCH] Create and edit office with JQuery. Country, Region and City are chosen using JQuery. --- .../modules/office/actions/actions.class.php | 49 +++++++++++++++------- .../modules/office/templates/_formCustomOffice.php | 18 ++++++++ lib/form/doctrine/CityForm.class.php | 7 +++- lib/form/doctrine/CountryForm.class.php | 4 +- lib/form/doctrine/OfficeForm.class.php | 32 ++++++++++++-- lib/form/doctrine/RegionForm.class.php | 7 +++- 6 files changed, 92 insertions(+), 25 deletions(-) diff --git a/apps/companyfront/modules/office/actions/actions.class.php b/apps/companyfront/modules/office/actions/actions.class.php index 70d8527..d505502 100644 --- a/apps/companyfront/modules/office/actions/actions.class.php +++ b/apps/companyfront/modules/office/actions/actions.class.php @@ -55,6 +55,7 @@ class officeActions extends sfActions //Get company owned by that user and insert value in form $officeInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + $officeInit->city_id = null; $this->form = new OfficeForm($officeInit); @@ -285,7 +286,13 @@ class officeActions extends sfActions return $orderBy; } - public function executeChosencountry($request) + /** + * Run action from JQuery POST while chosing the country in the select HTML field + * for offices creation and edition. + * + * @param sfWebRequest with the chosen country + */ + public function executeChosencountry(sfWebRequest $request) { $countryId = $request->getParameter('countryId'); @@ -325,42 +332,52 @@ class officeActions extends sfActions return $this->renderText(json_encode($regionsJSON)); } - public function executeChosencountry($request) + /** + * Run action from JQuery POST while chosing the region in the select HTML field + * for offices creation and edition. + * + * @param sfWebRequest with the chosen region + */ + public function executeChosenregion(sfWebRequest $request) { $regionId = $request->getParameter('regionId'); + //set content type HTTP field with the right value (we are going to use a JSON response) + $this->getResponse()->setContentType('application/json'); + + //Never trust data coming from user if (!isset($regionId)) { - //Incorrect data from user. Using default value. - $country = RegionTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country')); + //Incorrect data from user. + //TODO: JSON error + return $this->renderText(json_encode("")); } else { - $country = CountryTable::getInstance()->findOneById($countryId); - if (!isset($country)) + $region = RegionTable::getInstance()->findOneById($regionId); + if (!isset($region)) { - //Incorrect data from user. Using default value. - $country = CountryTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country')); + //Incorrect data from user. + //TODO: JSON error + return $this->renderText(json_encode("")); } } - $regionsJSON = array(); + $citiesJSON = array(); //Retrieve Doctrine_Collection - $regions = RegionTable::getInstance()->findByCountryId($country->getId()); + $cities = CityTable::getInstance()->findByRegionId($region->getId()); //Using Doctrine_Collection_Iterator - $iterator = $regions->getIterator(); - while ($region = $iterator->current()) + $iterator = $cities->getIterator(); + while ($city = $iterator->current()) { - $regionsJSON[$region->getId()] = $region->getRegionName(); + $citiesJSON[$city->getId()] = $city->getCityName(); $iterator->next(); } - //set content type HTTP field with the right value (we are going to use a JSON response) - $this->getResponse()->setContentType('application/json'); //Bypass completely the view layer and set the response code directly from this action. //In this way the user may know if the data were updated - return $this->renderText(json_encode($regionsJSON)); + return $this->renderText(json_encode($citiesJSON)); } } diff --git a/apps/companyfront/modules/office/templates/_formCustomOffice.php b/apps/companyfront/modules/office/templates/_formCustomOffice.php index 717f8dd..8963f67 100644 --- a/apps/companyfront/modules/office/templates/_formCustomOffice.php +++ b/apps/companyfront/modules/office/templates/_formCustomOffice.php @@ -8,6 +8,7 @@ function(data){ $('#office_City_region_id').empty(); $('#office_City_region_id').removeAttr('disabled'); + $('#office_City_region_id').append($("").attr("value", "").text("")); $.each(data, function(value, key) { $('#office_City_region_id').append($("").attr("value", value).text(key)); }); @@ -16,6 +17,23 @@ }); + + +
isMultipart() and print 'enctype="multipart/form-data" ' ?>> getObject()->isNew()): ?> diff --git a/lib/form/doctrine/CityForm.class.php b/lib/form/doctrine/CityForm.class.php index 5bd697c..4bb45a2 100644 --- a/lib/form/doctrine/CityForm.class.php +++ b/lib/form/doctrine/CityForm.class.php @@ -5,8 +5,8 @@ * * @package mobiads * @subpackage form - * @author Your name here - * @version SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ + * @author Gustavo Martin Morcuende + * @version */ class CityForm extends BaseCityForm { @@ -14,6 +14,9 @@ class CityForm extends BaseCityForm { unset($this['city_name']); + $this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), + 'add_empty' => true)); + $this->widgetSchema['region_id']->setAttribute('disabled', 'disabled'); $this->embedRelation('Region'); diff --git a/lib/form/doctrine/CountryForm.class.php b/lib/form/doctrine/CountryForm.class.php index 04f4d49..77df0bc 100644 --- a/lib/form/doctrine/CountryForm.class.php +++ b/lib/form/doctrine/CountryForm.class.php @@ -5,8 +5,8 @@ * * @package mobiads * @subpackage form - * @author Your name here - * @version SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ + * @author Gustavo Martin Morcuende + * @version */ class CountryForm extends BaseCountryForm { diff --git a/lib/form/doctrine/OfficeForm.class.php b/lib/form/doctrine/OfficeForm.class.php index cb5417e..b6f4b5d 100644 --- a/lib/form/doctrine/OfficeForm.class.php +++ b/lib/form/doctrine/OfficeForm.class.php @@ -28,6 +28,10 @@ class OfficeForm extends BaseOfficeForm 'add_empty' => true, 'query' => $query)); + $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('City'), + 'required' => true)); + + $this->widgetSchema['city_id']->setAttribute('disabled', 'disabled'); @@ -91,7 +95,29 @@ class OfficeForm extends BaseOfficeForm */ protected function doSave($con = null) { - parent::doSave($con); + if (null === $con) + { + $con = $this->getConnection(); + } + + + if (!$this->getObject()->isNew()) + { + $office = OfficeTable::getInstance()->findOneById($this->getObject()->getId()); + } + else + { + $office = new Office(); + $office->company_id = $this->getObject()->getCompanyId(); + } + $office->city_id = $this->values['city_id']; + $office->office_street_address = $this->values['office_street_address']; + $office->office_zip = $this->values['office_zip']; + + //TODO: Symfony sucks + $this->object = $office; + + $office->save(); //Get latitude and longitude values. They will be translated to GEOGRAPHIC data. foreach ($this->values as $field => $value) @@ -102,10 +128,10 @@ class OfficeForm extends BaseOfficeForm $latitude = $value; } //Catch id element. We will use this id to insert the PostGIS value in the right row. - $arrowId = $this->getObject()->getId(); + $rowId = $office->getId(); //Update PostGIS //This connection will throw exception in case of error. - Doctrine_Manager::connection()->execute("UPDATE office SET office_gps=ST_GeographyFromText('SRID=4326;POINT($longitude $latitude)') WHERE id=$arrowId"); + Doctrine_Manager::connection()->execute("UPDATE office SET office_gps=ST_GeographyFromText('SRID=4326;POINT($longitude $latitude)') WHERE id=$rowId"); } diff --git a/lib/form/doctrine/RegionForm.class.php b/lib/form/doctrine/RegionForm.class.php index 5f48455..b10071e 100644 --- a/lib/form/doctrine/RegionForm.class.php +++ b/lib/form/doctrine/RegionForm.class.php @@ -5,8 +5,8 @@ * * @package mobiads * @subpackage form - * @author Your name here - * @version SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ + * @author Gustavo Martin Morcuende + * @version */ class RegionForm extends BaseRegionForm { @@ -14,6 +14,9 @@ class RegionForm extends BaseRegionForm { unset($this['region_name']); + $this->widgetSchema['country_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), + 'add_empty' => true)); + $this->embedRelation('Country'); } } -- 2.1.4