From: Gustavo Martin Morcuende Date: Mon, 14 May 2012 00:04:11 +0000 (+0200) Subject: Create, edit, remove office. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=0849cf6f7d07eebfebd114563733f8de700f77ad;p=mobi%2F.git Create, edit, remove office. Right now very simple, wihtout using javascript. In the future many improvements if I use javascript. --- diff --git a/apps/companyfront/config/app.yml b/apps/companyfront/config/app.yml index 4e4dcc4..1bd9aa3 100644 --- a/apps/companyfront/config/app.yml +++ b/apps/companyfront/config/app.yml @@ -3,7 +3,7 @@ # default values all: - max_jobs_on_category: 3 + max_offices_on_pager: 3 sf_guard_plugin: remember_key_expiration_age: 2592000 # 30 days in seconds diff --git a/apps/companyfront/config/routing.yml b/apps/companyfront/config/routing.yml index b27bbb3..cd57e68 100644 --- a/apps/companyfront/config/routing.yml +++ b/apps/companyfront/config/routing.yml @@ -8,7 +8,7 @@ homepage: param: { module: office, action: index } options: { model: Office, type: object } -office_index: +offices_index: url: /office/index class: sfDoctrineRoute param: { module: office, action: index } diff --git a/apps/companyfront/modules/office/actions/actions.class.php b/apps/companyfront/modules/office/actions/actions.class.php index ce035a7..b4e3d73 100644 --- a/apps/companyfront/modules/office/actions/actions.class.php +++ b/apps/companyfront/modules/office/actions/actions.class.php @@ -12,9 +12,22 @@ class officeActions extends sfActions { public function executeIndex(sfWebRequest $request) { - $this->offices = Doctrine_Core::getTable('Office') - ->createQuery('a') - ->execute(); + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user + //Just 1 user owns a company. Should this be improved? + $companyId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + $this->offices = Doctrine_Core::getTable('Office')->findByCompanyId($companyId); + + $query=OfficeTable::getInstance()->getOfficesByCompanyIdQuery($companyId); + + $this->pager = new sfDoctrinePager('Office', sfConfig::get('app_max_offices_on_pager')); + $this->pager->setQuery($query); + $this->pager->setPage($request->getParameter('page', 1)); + $this->pager->init(); + } public function executeShow(sfWebRequest $request) @@ -32,7 +45,15 @@ class officeActions extends sfActions { $this->forward404Unless($request->isMethod(sfRequest::POST)); - $this->form = new OfficeForm(); + $officeInit = new Office(); + + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user and insert value in form + $officeInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + $this->form = new OfficeForm($officeInit); $this->processForm($request, $this->form); @@ -42,6 +63,20 @@ class officeActions extends sfActions public function executeEdit(sfWebRequest $request) { $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office does not exist (%s).', $request->getParameter('id'))); + + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user and insert value in form + $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + //Get id number sent by the user (never trust the users) + $officeId = $request->getParameter('id'); + + $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); + $this->form = new OfficeForm($office); } @@ -49,6 +84,20 @@ class officeActions extends sfActions { $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office does not exist (%s).', $request->getParameter('id'))); + + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user and insert value in form + $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + //Get id number sent by the user (never trust the users) + $officeId = $request->getParameter('id'); + + $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); + $this->form = new OfficeForm($office); $this->processForm($request, $this->form); @@ -61,6 +110,20 @@ class officeActions extends sfActions $request->checkCSRFProtection(); $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office does not exist (%s).', $request->getParameter('id'))); + + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user and insert value in form + $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + //Get id number sent by the user (never trust the users) + $officeId = $request->getParameter('id'); + + $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); + $office->delete(); $this->redirect('office/index'); diff --git a/apps/companyfront/modules/office/templates/_formCustomOffice.php b/apps/companyfront/modules/office/templates/_formCustomOffice.php new file mode 100644 index 0000000..8d1b4b6 --- /dev/null +++ b/apps/companyfront/modules/office/templates/_formCustomOffice.php @@ -0,0 +1,25 @@ + + + +
isMultipart() and print 'enctype="multipart/form-data" ' ?>> +getObject()->isNew()): ?> + + + + + + + + + + + +
+ renderHiddenFields(false) ?> +   + getObject()->isNew()): ?> +  getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> + + +
+
diff --git a/apps/companyfront/modules/office/templates/_list.php b/apps/companyfront/modules/office/templates/_list.php new file mode 100644 index 0000000..67c935d --- /dev/null +++ b/apps/companyfront/modules/office/templates/_list.php @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
getOfficeStreetAddress() ?>getLongitude() ?>getLatitude() ?>', 'office/delete?id='.$office->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
diff --git a/apps/companyfront/modules/office/templates/editSuccess.php b/apps/companyfront/modules/office/templates/editSuccess.php index 8e1c570..3ebc182 100644 --- a/apps/companyfront/modules/office/templates/editSuccess.php +++ b/apps/companyfront/modules/office/templates/editSuccess.php @@ -1,3 +1,3 @@ -

Edit Office

+

- $form)) ?> + $form)) ?> diff --git a/apps/companyfront/modules/office/templates/indexSuccess.php b/apps/companyfront/modules/office/templates/indexSuccess.php index a2b7a2d..2908f40 100644 --- a/apps/companyfront/modules/office/templates/indexSuccess.php +++ b/apps/companyfront/modules/office/templates/indexSuccess.php @@ -1,32 +1,28 @@ -

Offices List

- - - - - - - - - - - - - - - - - - - - - - - - - - +

+ + $pager->getResults())) ?> + +haveToPaginate()): ?> + -
IdCompanyCityOffice gpsOffice street addressOffice zipCreated atUpdated at
getId() ?>getCompanyId() ?>getCityId() ?>getOfficeGps() ?>getOfficeStreetAddress() ?>getOfficeZip() ?>getCreatedAt() ?>getUpdatedAt() ?>
- New + >') ?> + + + + + + + + + diff --git a/apps/companyfront/modules/office/templates/newSuccess.php b/apps/companyfront/modules/office/templates/newSuccess.php index 9113985..ceb3620 100644 --- a/apps/companyfront/modules/office/templates/newSuccess.php +++ b/apps/companyfront/modules/office/templates/newSuccess.php @@ -1,3 +1,3 @@

New Office

- $form)) ?> + $form)) ?> diff --git a/lib/form/doctrine/OfficeForm.class.php b/lib/form/doctrine/OfficeForm.class.php index ba3f4f9..2e79330 100644 --- a/lib/form/doctrine/OfficeForm.class.php +++ b/lib/form/doctrine/OfficeForm.class.php @@ -1,16 +1,103 @@ useFields(array('city_id', 'office_street_address', 'office_zip')); + + + + $this->widgetSchema['longitude'] = new sfWidgetFormInputFloat(); + $this->widgetSchema['latitude'] = new sfWidgetFormInputFloat(); + + + $this->validatorSchema['longitude'] = new sfValidatorNumber(array('max' => 180, + 'min' => -180, + 'required' => true, + 'trim' => true), + array('invalid' => 'Wrong Longitude', + 'required' => 'The longitude field is required', + 'max' => 'Longitude "%value%" must not exceed the %max% value', + 'min' => 'Longitude "%value%" must be equal or higher than %min%')); + + + + $this->validatorSchema['latitude'] = new sfValidatorNumber(array('max' => 90, + 'min' => -90, + 'required' => true, + 'trim' => true), + array('invalid' => 'Wrong Latitude', + 'required' => 'The latitude field is required', + 'max' => 'Latitude "%value%" must not exceed the %max% value', + 'min' => 'Latitude "%value%" must be equal or higher than %min%')); + + $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('City'), + 'required' => false, + 'query' => $cityNamesQuery)); + + + + $this->widgetSchema->setLabels(array('city_id' => 'City: ', + 'longitude' => 'Longitude (180 to -180): ', + 'latitude' => 'Latitude (90 to -90): ', + 'office_street_address' => 'Address: ', + 'office_zip' => 'ZIP:',)); + + $this->validatorSchema->setOption('allow_extra_fields', false); + $this->validatorSchema->setOption('filter_extra_fields', true); + + //i18n (Internationalization) + //See apps/companyfront/modules/office/i18n/office_form.es.xml file + $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('office_form'); + } + + + /** + * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php + * + * We are updating the data base in just 1 transaction + * In case of unsetting longitude or latitude fields you will have to override this method. + * TODO: create a Doctrine_Record for PostGIS + */ + protected function doSave($con = null) + { + parent::doSave($con); + + //Get latitude and longitude values. They will be translated to GEOGRAPHIC data. + foreach ($this->values as $field => $value) + { + if ($field == 'longitude') + $longitude = $value; + if ($field == 'latitude') + $latitude = $value; + } + //Catch id element. We will use this id to insert the PostGIS value in the right row. + $arrowId = $this->getObject()->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"); + } + + + /** + * Overriding updateDefaultsFromObject method from lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php + * + * TODO: create a Doctrine_Record for PostGIS + */ + protected function updateDefaultsFromObject() + { + parent::updateDefaultsFromObject(); + + $this->setDefault('longitude', $this->getObject()->getLongitude()); + $this->setDefault('latitude', $this->getObject()->getLatitude()); } } diff --git a/lib/model/doctrine/City.class.php b/lib/model/doctrine/City.class.php index a23b985..ce0018a 100644 --- a/lib/model/doctrine/City.class.php +++ b/lib/model/doctrine/City.class.php @@ -7,9 +7,18 @@ * * @package mobiads * @subpackage model - * @author Your name here - * @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $ + * @author Gustavo Martin Morcuende + * @version */ class City extends BaseCity { + /** + * Returns the string representation of this object. + * + * @return string + */ + public function __toString() + { + return (string) $this->getCityName(); + } } diff --git a/lib/model/doctrine/CityTable.class.php b/lib/model/doctrine/CityTable.class.php index 7cc5f01..d985b2d 100644 --- a/lib/model/doctrine/CityTable.class.php +++ b/lib/model/doctrine/CityTable.class.php @@ -16,4 +16,18 @@ class CityTable extends Doctrine_Table { return Doctrine_Core::getTable('City'); } -} \ No newline at end of file + + + /** + * Returns a query which is able to recover the city_name field of a City Table + * + * @return object Doctrine_Query + */ + public function getCityNameQuery() + { + $query = $this->createQuery('city') + ->select('city.city_name'); + + return $query; + } +} diff --git a/lib/model/doctrine/Office.class.php b/lib/model/doctrine/Office.class.php index 9992e32..0784d22 100644 --- a/lib/model/doctrine/Office.class.php +++ b/lib/model/doctrine/Office.class.php @@ -12,4 +12,35 @@ */ class Office extends BaseOffice { + public function getGpsST_AsText() + { + $aux=$this->getOfficeGps(); + if ($aux) + { + //Using a PostGIS query to convert a GIS value? This is a bit strange + $results=Doctrine_Manager::getInstance()->getCurrentConnection()->fetchColumn("SELECT ST_AsText('$aux')"); + return trim($results['0'], "POINT()"); + } + else + return 0; + } + + public function getLongitude() + { + $gpsST_AsText=$this->getGpsST_AsText(); + + $longitude = strstr($gpsST_AsText, ' ', true); + + return $longitude; + } + + public function getLatitude() + { + $gpsST_AsText=$this->getGpsST_AsText(); + + $latitude = strstr($gpsST_AsText, ' '); + + return $latitude; + } + } diff --git a/lib/model/doctrine/OfficeTable.class.php b/lib/model/doctrine/OfficeTable.class.php index e58f275..2d0bb0f 100644 --- a/lib/model/doctrine/OfficeTable.class.php +++ b/lib/model/doctrine/OfficeTable.class.php @@ -16,4 +16,17 @@ class OfficeTable extends Doctrine_Table { return Doctrine_Core::getTable('Office'); } -} \ No newline at end of file + + + /** + * Returns offices by company id. + * + * @return related offices to a company id. + */ + public function getOfficesByCompanyIdQuery($companyId) + { + return $this->createQuery('office')->where('office.company_id = ?', $companyId) + ->orderBy('office.id'); + } +} + diff --git a/lib/widget/sfWidgetFormInputFloat.class.php b/lib/widget/sfWidgetFormInputFloat.class.php new file mode 100644 index 0000000..fc3e0a4 --- /dev/null +++ b/lib/widget/sfWidgetFormInputFloat.class.php @@ -0,0 +1,29 @@ +setOption('type', 'float'); + } + +}