From 983ccf5c5a6ebf66345b9833e70b5dd55034123e Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sat, 19 May 2012 21:03:21 +0200 Subject: [PATCH] Link Office to Ads. I hate Symfony and PHP. --- apps/companyfront/config/app.yml | 1 + .../modules/office/actions/actions.class.php | 138 ++++++++++++++++++++- .../modules/office/templates/_formCustomOffice.php | 4 +- .../office/templates/_formCustomOfficeAds.php | 4 +- .../modules/office/templates/_list.php | 20 ++- .../modules/office/templates/editSuccess.php | 2 +- .../modules/office/templates/indexSuccess.php | 21 ++-- .../modules/office/templates/linkSuccess.php | 2 +- .../modules/office/templates/newSuccess.php | 2 +- lib/form/doctrine/MaxItemsPerPage.class.php | 42 +++++++ lib/form/doctrine/OfficeAdsForm.class.php | 74 +++++++++++ lib/form/doctrine/OfficeForm.class.php | 11 ++ lib/model/doctrine/City.class.php | 8 +- lib/model/doctrine/Country.class.php | 9 ++ lib/model/doctrine/Office.class.php | 4 +- lib/model/doctrine/OfficeTable.class.php | 10 +- lib/model/doctrine/Region.class.php | 13 +- web/sfFormExtraPlugin | 1 + 18 files changed, 328 insertions(+), 38 deletions(-) create mode 100644 lib/form/doctrine/MaxItemsPerPage.class.php create mode 120000 web/sfFormExtraPlugin diff --git a/apps/companyfront/config/app.yml b/apps/companyfront/config/app.yml index 3c2b78e..adb5077 100644 --- a/apps/companyfront/config/app.yml +++ b/apps/companyfront/config/app.yml @@ -5,6 +5,7 @@ all: max_offices_on_pager: 3 max_ads_on_pager: 2 + max_categories_on_pager: 2 default_language: eng # Everything must exist at least with this language sf_guard_plugin: diff --git a/apps/companyfront/modules/office/actions/actions.class.php b/apps/companyfront/modules/office/actions/actions.class.php index 14c3305..71fe3c0 100644 --- a/apps/companyfront/modules/office/actions/actions.class.php +++ b/apps/companyfront/modules/office/actions/actions.class.php @@ -19,7 +19,11 @@ class officeActions extends sfActions //Just 1 user owns a company. Should this be improved? $companyId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); - $query=OfficeTable::getInstance()->getOfficesByCompanyIdQuery($companyId); + //Sort list + $this->sort = $request->getParameter('sort', 'id'); + $orderBy = $this->validateSort($this->sort); + + $query=OfficeTable::getInstance()->getOfficesByCompanyIdWithSortQuery($companyId, $orderBy); $this->pager = new sfDoctrinePager('Office', sfConfig::get('app_max_offices_on_pager')); $this->pager->setQuery($query); @@ -36,6 +40,8 @@ class officeActions extends sfActions public function executeNew(sfWebRequest $request) { $this->form = new OfficeForm(); + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); } public function executeCreate(sfWebRequest $request) @@ -52,7 +58,10 @@ class officeActions extends sfActions $this->form = new OfficeForm($officeInit); - $this->processForm($request, $this->form); + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + + $this->processForm($request, $this->form, $this->sort, $this->page); $this->setTemplate('new'); } @@ -74,6 +83,9 @@ class officeActions extends sfActions $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + $this->form = new OfficeForm($office); } @@ -97,7 +109,10 @@ class officeActions extends sfActions $this->form = new OfficeForm($office); - $this->processForm($request, $this->form); + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + + $this->processForm($request, $this->form, $this->sort, $this->page); $this->setTemplate('edit'); } @@ -123,17 +138,20 @@ class officeActions extends sfActions $office->delete(); - $this->redirect('office/index'); + $sort = $request->getParameter('sort', 'id'); + $page = $request->getParameter('page', 1); + + $this->redirect('office/index?page='.$page.'&sort='.$sort); } - protected function processForm(sfWebRequest $request, sfForm $form) + protected function processForm(sfWebRequest $request, sfForm $form, $sort, $page) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $office = $form->save(); - $this->redirect('office/edit?id='.$office->getId()); + $this->redirect('office/edit?id='.$office->getId().'&page='.$page.'&sort='.$sort); } } @@ -157,5 +175,113 @@ class officeActions extends sfActions $officeAds = OfficeAdsTable::getInstance()->findOneByOfficeId($officeId); $this->form = new OfficeAdsForm($officeAds, array('companyId' => $companyOfficeId)); + + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + $this->officeId = $officeId; + } + + public function executeCreateLink(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST)); + + $officeId = $request->getParameter('officeId'); + + //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 company owned by that user and insert value in form + $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('officeId'))); + + $officeAdsInit = new OfficeAds(); + $officeAdsInit->office_id = $officeId; + + $this->form = new OfficeAdsForm($officeAdsInit, array('companyId' => $companyOfficeId)); + + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + $this->officeId = $officeId; + + $this->processAdsForm($request, $this->form, $this->sort, $this->page); + + $this->setTemplate('link'); + } + + public function executeUpdateLink(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); + $this->forward404Unless($officeAds = Doctrine_Core::getTable('OfficeAds')->find(array($request->getParameter('id'))), sprintf('Object office ads 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) + $officeAdsId = $request->getParameter('id'); + $companyOfficeId = OfficeAdsTable::getInstance()->findOneById($officeAdsId)->getOffice()->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); + + + $this->form = new OfficeAdsForm($officeAds, array('companyId' => $companyOfficeId)); + + $this->sort = $request->getParameter('sort', 'id'); + $this->page = $request->getParameter('page', 1); + $this->officeId = $officeAds->getOfficeId(); + + $this->processAdsForm($request, $this->form, $this->sort, $this->page); + + $this->setTemplate('link'); + } + + + protected function processAdsForm(sfWebRequest $request, sfForm $form, $sort, $page) + { + $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); + if ($form->isValid()) + { + $officeAds = $form->save(); + + $this->redirect('office/link?id='.$officeAds->getOfficeId().'&page='.$page.'&sort='.$sort); + } + } + + + /** + * We must validate the data coming from the user. + * + * @param sort value as string + * @return custom sort value, the office id by default + */ + private function validateSort($sort) + { + switch ($sort) { + case "country": + $orderBy = "country.id"; + break; + case "region": + $orderBy = "region.id"; + break; + case "city": + $orderBy = "city.id"; + break; + case "address": + $orderBy = "office_street_address"; + break; + case "zip": + $orderBy = "office_zip"; + break; + default: + $orderBy = "office.id"; + } + + return $orderBy; } } diff --git a/apps/companyfront/modules/office/templates/_formCustomOffice.php b/apps/companyfront/modules/office/templates/_formCustomOffice.php index 989caba..9dfda12 100644 --- a/apps/companyfront/modules/office/templates/_formCustomOffice.php +++ b/apps/companyfront/modules/office/templates/_formCustomOffice.php @@ -10,9 +10,9 @@ renderHiddenFields(false) ?> -   +  Back to list getObject()->isNew()): ?> -  getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> +  getObject()->getId().'&page='.$page.'&sort='.$sort, array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> /> diff --git a/apps/companyfront/modules/office/templates/_formCustomOfficeAds.php b/apps/companyfront/modules/office/templates/_formCustomOfficeAds.php index 74bb006..5b4d515 100644 --- a/apps/companyfront/modules/office/templates/_formCustomOfficeAds.php +++ b/apps/companyfront/modules/office/templates/_formCustomOfficeAds.php @@ -2,7 +2,7 @@ -
isMultipart() and print 'enctype="multipart/form-data" ' ?>> +isMultipart() and print 'enctype="multipart/form-data" ' ?>> getObject()->isNew()): ?> @@ -11,7 +11,7 @@ renderHiddenFields(false) ?> -   +   /> diff --git a/apps/companyfront/modules/office/templates/_list.php b/apps/companyfront/modules/office/templates/_list.php index 12cfef5..2e51a4f 100644 --- a/apps/companyfront/modules/office/templates/_list.php +++ b/apps/companyfront/modules/office/templates/_list.php @@ -1,29 +1,37 @@ - + + + + + - + - + + + + + - - - + + + diff --git a/apps/companyfront/modules/office/templates/editSuccess.php b/apps/companyfront/modules/office/templates/editSuccess.php index 5a0da86..14a7687 100644 --- a/apps/companyfront/modules/office/templates/editSuccess.php +++ b/apps/companyfront/modules/office/templates/editSuccess.php @@ -1,3 +1,3 @@

- $form)) ?> + $form, 'page' => $page, 'sort' => $sort)) ?> diff --git a/apps/companyfront/modules/office/templates/indexSuccess.php b/apps/companyfront/modules/office/templates/indexSuccess.php index 2908f40..bcd27ae 100644 --- a/apps/companyfront/modules/office/templates/indexSuccess.php +++ b/apps/companyfront/modules/office/templates/indexSuccess.php @@ -1,28 +1,33 @@ -

+

+ +haveToPaginate()): ?> + $pager->getResults(), 'page' => $pager->getPage(), 'sort' => $sort)) ?> + + $pager->getResults(), 'page' => '1', 'sort' => $sort)) ?> + - $pager->getResults())) ?> haveToPaginate()): ?> - + diff --git a/apps/companyfront/modules/office/templates/linkSuccess.php b/apps/companyfront/modules/office/templates/linkSuccess.php index f4174d5..4597260 100644 --- a/apps/companyfront/modules/office/templates/linkSuccess.php +++ b/apps/companyfront/modules/office/templates/linkSuccess.php @@ -1,3 +1,3 @@

- $form)) ?> + $form, 'page' => $page, 'sort' => $sort, 'officeId' => $officeId)) ?> diff --git a/apps/companyfront/modules/office/templates/newSuccess.php b/apps/companyfront/modules/office/templates/newSuccess.php index 5b24bdf..ce51efe 100644 --- a/apps/companyfront/modules/office/templates/newSuccess.php +++ b/apps/companyfront/modules/office/templates/newSuccess.php @@ -1,3 +1,3 @@

- $form)) ?> + $form, 'page' => $page, 'sort' => $sort)) ?> diff --git a/lib/form/doctrine/MaxItemsPerPage.class.php b/lib/form/doctrine/MaxItemsPerPage.class.php new file mode 100644 index 0000000..fae9546 --- /dev/null +++ b/lib/form/doctrine/MaxItemsPerPage.class.php @@ -0,0 +1,42 @@ +setWidgets(array('max_items' => new sfWidgetFormInputText(),)); + + $this->setValidators(array('max_items' => new sfValidatorNumber(array('required' => true, + 'trim' => true), + array('required' => 'The latitude field is required')),)); + + + $this->widgetSchema->setNameFormat('max_items_page[%s]'); + + + $this->widgetSchema->setLabels(array('max_items' => 'Max items per page: ',)); + + + $this->validatorSchema->setOption('allow_extra_fields', false); + $this->validatorSchema->setOption('filter_extra_fields', true); + + $this->widgetSchema->setFormFormatterName('table'); + + $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); + + //i18n (Internationalization) + $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('max_items_form'); + + parent::setup(); + } +} diff --git a/lib/form/doctrine/OfficeAdsForm.class.php b/lib/form/doctrine/OfficeAdsForm.class.php index f1689bd..dedc2d7 100644 --- a/lib/form/doctrine/OfficeAdsForm.class.php +++ b/lib/form/doctrine/OfficeAdsForm.class.php @@ -10,6 +10,9 @@ */ class OfficeAdsForm extends BaseOfficeAdsForm { + protected $scheduledForSave = array(); + + public function configure() { //Narrow down options. @@ -25,9 +28,18 @@ class OfficeAdsForm extends BaseOfficeAdsForm 'renderer_class' => 'sfWidgetFormSelectDoubleList', 'query' => $query)); + $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['ad_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Ad'), 'multiple' => true, 'query' => $query)); + + $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('office_ads_form'); } /** @@ -51,4 +63,66 @@ class OfficeAdsForm extends BaseOfficeAdsForm $this->setDefault('ad_id', $already_chosen); } } + + + /** + * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php + * + * We are updating the data base in just 1 transaction + */ + protected function doSave($con = null) + { + + if (null === $con) + { + $con = $this->getConnection(); + } + + + foreach ($this->scheduledForSave as $index => $value) + { + $value->save($con); + } + } + + + /** + * Overriding doBind method + * + * TODO: I am breaking the validations. How could I do this in a right way? + */ + protected function doBind(array $values) + { + if (!isset($values['ad_id'])) + { + if (!$this->getObject()->isNew()) + { + $officeAds = OfficeAdsTable::getInstance()->findByOfficeId($this->getObject()->getOfficeId()); + + foreach ($officeAds as $officeAd) + { + $officeAd->delete(); + } + } + return; + } + + $officeAds = OfficeAdsTable::getInstance()->findByOfficeId($this->getObject()->getOfficeId()); + + foreach ($values['ad_id'] as $index => $value) + { + if (!$this->getObject()->isNew()) + { + foreach ($officeAds as $officeAd) + { + if ($officeAd->getAdId() == $value) + continue 2; + } + } + $officeAds = new OfficeAds(); + $officeAds->office_id = $this->getObject()->getOfficeId(); + $officeAds->ad_id = $value; + $this->scheduledForSave[$index] = $officeAds; + } + } } diff --git a/lib/form/doctrine/OfficeForm.class.php b/lib/form/doctrine/OfficeForm.class.php index bab320c..4a0b661 100644 --- a/lib/form/doctrine/OfficeForm.class.php +++ b/lib/form/doctrine/OfficeForm.class.php @@ -47,6 +47,17 @@ class OfficeForm extends BaseOfficeForm 'office_street_address' => 'Address: ', 'office_zip' => 'ZIP:',)); + + //In the future the companies could pay for improvements in their accounts (premium accounts) + //1 premium company could avoid to other companies on the same GPS point. This validator should check if that GPS point is being used by + //another company and if that company is premium or not. + $this->validatorSchema->setPostValidator( + new sfValidatorAnd(array( + new sfValidatorDoctrineUnique(array('model' => $this->getModelName(), 'column' => array('office_gps'))), + )) + ); + + $this->validatorSchema->setOption('allow_extra_fields', false); $this->validatorSchema->setOption('filter_extra_fields', true); diff --git a/lib/model/doctrine/City.class.php b/lib/model/doctrine/City.class.php index ce0018a..64302d5 100644 --- a/lib/model/doctrine/City.class.php +++ b/lib/model/doctrine/City.class.php @@ -17,8 +17,8 @@ class City extends BaseCity * * @return string */ - public function __toString() - { - return (string) $this->getCityName(); - } + public function __toString() + { + return (string) $this->getCityName(); + } } diff --git a/lib/model/doctrine/Country.class.php b/lib/model/doctrine/Country.class.php index 2efc45a..e1b7a68 100644 --- a/lib/model/doctrine/Country.class.php +++ b/lib/model/doctrine/Country.class.php @@ -12,4 +12,13 @@ */ class Country extends BaseCountry { + /** + * Returns the string representation of this object. + * + * @return string + */ + public function __toString() + { + return (string) $this->getCountryName(); + } } diff --git a/lib/model/doctrine/Office.class.php b/lib/model/doctrine/Office.class.php index 0784d22..0789d37 100644 --- a/lib/model/doctrine/Office.class.php +++ b/lib/model/doctrine/Office.class.php @@ -7,8 +7,8 @@ * * @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 Office extends BaseOffice { diff --git a/lib/model/doctrine/OfficeTable.class.php b/lib/model/doctrine/OfficeTable.class.php index 2d0bb0f..92bb1b5 100644 --- a/lib/model/doctrine/OfficeTable.class.php +++ b/lib/model/doctrine/OfficeTable.class.php @@ -19,14 +19,18 @@ class OfficeTable extends Doctrine_Table /** - * Returns offices by company id. + * Returns offices by company id with sort * * @return related offices to a company id. + * TODO: order by longitude and latitude */ - public function getOfficesByCompanyIdQuery($companyId) + public function getOfficesByCompanyIdWithSortQuery($companyId, $sort) { return $this->createQuery('office')->where('office.company_id = ?', $companyId) - ->orderBy('office.id'); + ->innerjoin('office.City city') + ->innerjoin('city.Region region') + ->innerjoin('region.Country country') + ->orderBy($sort); } } diff --git a/lib/model/doctrine/Region.class.php b/lib/model/doctrine/Region.class.php index bcd1c67..8d05853 100644 --- a/lib/model/doctrine/Region.class.php +++ b/lib/model/doctrine/Region.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 Region extends BaseRegion { + /** + * Returns the string representation of this object. + * + * @return string + */ + public function __toString() + { + return (string) $this->getRegionName(); + } } diff --git a/web/sfFormExtraPlugin b/web/sfFormExtraPlugin new file mode 120000 index 0000000..d198159 --- /dev/null +++ b/web/sfFormExtraPlugin @@ -0,0 +1 @@ +../plugins/sfFormExtraPlugin/web \ No newline at end of file -- 2.1.4
 
getCity()->getRegion()->getCountry() ?>getCity()->getRegion() ?>getCity() ?> getOfficeStreetAddress() ?>getOfficeZip() ?> getLongitude() ?> getLatitude() ?>', 'office/delete?id='.$office->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>', 'office/delete?id='.$office->getId().'&page='.$page.'&sort='.$sort, array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>