From 3f3b8d8c51b9acb3101ef19485d23cb8e4bcc79b Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 15 May 2012 19:14:19 +0200 Subject: [PATCH] First steps with Ads. Index web page. Index web page with internationalization. We can change the shown language without JavaScript. --- apps/companyfront/config/app.yml | 1 + apps/companyfront/config/routing.yml | 6 + .../modules/ad/actions/actions.class.php | 139 +++++++++++++++++++++ apps/companyfront/modules/ad/templates/_form.php | 60 +++++++++ apps/companyfront/modules/ad/templates/_list.php | 29 +++++ .../modules/ad/templates/editSuccess.php | 3 + .../modules/ad/templates/indexSuccess.php | 38 ++++++ .../modules/ad/templates/newSuccess.php | 3 + .../modules/ad/templates/showSuccess.php | 34 +++++ lib/model/doctrine/AdDescriptionTable.class.php | 17 ++- test/functional/companyfront/adActionsTest.php | 19 +++ 11 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 apps/companyfront/modules/ad/actions/actions.class.php create mode 100644 apps/companyfront/modules/ad/templates/_form.php create mode 100644 apps/companyfront/modules/ad/templates/_list.php create mode 100644 apps/companyfront/modules/ad/templates/editSuccess.php create mode 100644 apps/companyfront/modules/ad/templates/indexSuccess.php create mode 100644 apps/companyfront/modules/ad/templates/newSuccess.php create mode 100644 apps/companyfront/modules/ad/templates/showSuccess.php create mode 100644 test/functional/companyfront/adActionsTest.php diff --git a/apps/companyfront/config/app.yml b/apps/companyfront/config/app.yml index 1bd9aa3..7cad5cf 100644 --- a/apps/companyfront/config/app.yml +++ b/apps/companyfront/config/app.yml @@ -4,6 +4,7 @@ # default values all: max_offices_on_pager: 3 + max_ads_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 cd57e68..a5d1a2c 100644 --- a/apps/companyfront/config/routing.yml +++ b/apps/companyfront/config/routing.yml @@ -14,6 +14,12 @@ offices_index: param: { module: office, action: index } options: { model: Office, type: object } +ads_index: + url: /ad/index + class: sfDoctrineRoute + param: { module: ad, action: index } + options: { model: Ad, type: object } + # generic rules # please, remove them by adding more specific rules default_index: diff --git a/apps/companyfront/modules/ad/actions/actions.class.php b/apps/companyfront/modules/ad/actions/actions.class.php new file mode 100644 index 0000000..869ea1b --- /dev/null +++ b/apps/companyfront/modules/ad/actions/actions.class.php @@ -0,0 +1,139 @@ +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(); + + + $languageCode = $request->getParameter('language'); + if ($languageCode != null) + { + $language = LanguageTable::getInstance()->findOneByCode($languageCode); + if ($language == null) + { + //By default we use the current user's language. + $language = $this->getUser()->getGuardUser()->getLanguage(); + } + } + else + { + //By default we use the current user's language. + $language = $this->getUser()->getGuardUser()->getLanguage(); + } + + + //Init combobox with the right value. + $this->formLanguage = new LanguageListForm($language); + + + //Doctrine Query used to show a pager with the Ads. + $query=AdDescriptionTable::getInstance()->getAdsByCompanyandLanguageIdQuery($companyId, $language->getId()); + + + $this->pager = new sfDoctrinePager('AdDescription', sfConfig::get('app_max_ads_on_pager')); + $this->pager->setQuery($query); + $this->pager->setPage($request->getParameter('page', 1)); + $this->pager->init(); + } + + public function executeShow(sfWebRequest $request) + { + $this->ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))); + $this->forward404Unless($this->ad); + } + + public function executeNew(sfWebRequest $request) + { + $this->form = new AdForm(); + } + + public function executeCreate(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST)); + + $this->form = new AdForm(); + + $this->processForm($request, $this->form); + + $this->setTemplate('new'); + } + + public function executeEdit(sfWebRequest $request) + { + $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id'))); + $this->form = new AdForm($ad); + } + + public function executeUpdate(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); + $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id'))); + $this->form = new AdForm($ad); + + $this->processForm($request, $this->form); + + $this->setTemplate('edit'); + } + + public function executeDelete(sfWebRequest $request) + { + $request->checkCSRFProtection(); + + $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id'))); + $ad->delete(); + + $this->redirect('ad/index'); + } + + protected function processForm(sfWebRequest $request, sfForm $form) + { + $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); + if ($form->isValid()) + { + $ad = $form->save(); + + $this->redirect('ad/edit?id='.$ad->getId()); + } + } + + public function executeIndexLanguage(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST)); + + $formLanguage = new LanguageListForm(); + + //Retrieve post parameters + $postArray = $request->getPostParameter($formLanguage->getName()); + + //Retrieve Doctrine Record related to the chosen language + $language = LanguageTable::getInstance()->findOneById($postArray['id']); + + $this->forward404Unless($language != null, sprintf('Language does not exist')); + + $formLanguage = new LanguageListForm($language); + + $formLanguage->bind($request->getParameter($formLanguage->getName()), $request->getFiles($formLanguage->getName())); + if ($formLanguage->isValid()) + { + $this->redirect('ad/index?language='.$language->getCode()); + } + + //By default current user's language + $this->redirect('ad/index?language='.$this->getUser()->getGuardUser()->getLanguage()->getCode()); + } +} diff --git a/apps/companyfront/modules/ad/templates/_form.php b/apps/companyfront/modules/ad/templates/_form.php new file mode 100644 index 0000000..055a634 --- /dev/null +++ b/apps/companyfront/modules/ad/templates/_form.php @@ -0,0 +1,60 @@ + + + +
isMultipart() and print 'enctype="multipart/form-data" ' ?>> +getObject()->isNew()): ?> + + + + + + + + + + renderGlobalErrors() ?> + + + + + + + + + + + + + + + + + + + + + +
+ renderHiddenFields(false) ?> +  Back to list + getObject()->isNew()): ?> +  getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> + + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
+
diff --git a/apps/companyfront/modules/ad/templates/_list.php b/apps/companyfront/modules/ad/templates/_list.php new file mode 100644 index 0000000..1ddfa3d --- /dev/null +++ b/apps/companyfront/modules/ad/templates/_list.php @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
getAd()->getAdMobileImageLink() ?>getAd()->getCompanyCategId() ?>getAdName() ?>', 'ad/delete?id='.$ad->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
diff --git a/apps/companyfront/modules/ad/templates/editSuccess.php b/apps/companyfront/modules/ad/templates/editSuccess.php new file mode 100644 index 0000000..b6d77be --- /dev/null +++ b/apps/companyfront/modules/ad/templates/editSuccess.php @@ -0,0 +1,3 @@ +

Edit Ad

+ + $form)) ?> diff --git a/apps/companyfront/modules/ad/templates/indexSuccess.php b/apps/companyfront/modules/ad/templates/indexSuccess.php new file mode 100644 index 0000000..04d0d0c --- /dev/null +++ b/apps/companyfront/modules/ad/templates/indexSuccess.php @@ -0,0 +1,38 @@ +

+ +
isMultipart() and print 'enctype="multipart/form-data" ' ?>> + + + + + + + + +
renderHiddenFields(false) ?> />
+
+ + $pager->getResults())) ?> + +haveToPaginate()): ?> + + + + + diff --git a/apps/companyfront/modules/ad/templates/newSuccess.php b/apps/companyfront/modules/ad/templates/newSuccess.php new file mode 100644 index 0000000..3d12b7f --- /dev/null +++ b/apps/companyfront/modules/ad/templates/newSuccess.php @@ -0,0 +1,3 @@ +

New Ad

+ + $form)) ?> diff --git a/apps/companyfront/modules/ad/templates/showSuccess.php b/apps/companyfront/modules/ad/templates/showSuccess.php new file mode 100644 index 0000000..e7007e9 --- /dev/null +++ b/apps/companyfront/modules/ad/templates/showSuccess.php @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id:getId() ?>
Company:getCompanyId() ?>
Company categ:getCompanyCategId() ?>
Ad mobile image link:getAdMobileImageLink() ?>
Created at:getCreatedAt() ?>
Updated at:getUpdatedAt() ?>
+ +
+ +Edit +  +List diff --git a/lib/model/doctrine/AdDescriptionTable.class.php b/lib/model/doctrine/AdDescriptionTable.class.php index fe6b171..4a0355a 100644 --- a/lib/model/doctrine/AdDescriptionTable.class.php +++ b/lib/model/doctrine/AdDescriptionTable.class.php @@ -16,4 +16,19 @@ class AdDescriptionTable extends Doctrine_Table { return Doctrine_Core::getTable('AdDescription'); } -} \ No newline at end of file + + + /** + * Returns ads with ad descriptions by company and language id. + * + * @return related ad to a company and language id as Doctrine Query + */ + public function getAdsByCompanyandLanguageIdQuery($companyId, $languageId) + { + return $this->createQuery('addescription')->where('ad.company_id = ?', $companyId) + ->andWhere('addescription.language_id = ?', $languageId) + ->innerjoin('addescription.Ad ad') + ->orderBy('ad.id'); + } + +} diff --git a/test/functional/companyfront/adActionsTest.php b/test/functional/companyfront/adActionsTest.php new file mode 100644 index 0000000..a908bd7 --- /dev/null +++ b/test/functional/companyfront/adActionsTest.php @@ -0,0 +1,19 @@ + + get('/ad/index')-> + + with('request')->begin()-> + isParameter('module', 'ad')-> + isParameter('action', 'index')-> + end()-> + + with('response')->begin()-> + isStatusCode(200)-> + checkElement('body', '!/This is a temporary page/')-> + end() +; -- 2.1.4