From cde604079ffb7db422f295b1a4f06c4ae6e58f03 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sat, 19 May 2012 21:29:46 +0200 Subject: [PATCH] First steps in company category administration. --- apps/companyfront/config/routing.yml | 6 ++ .../modules/category/actions/actions.class.php | 89 ++++++++++++++++++++++ .../modules/category/templates/_form.php | 81 ++++++++++++++++++++ .../modules/category/templates/_list.php | 76 ++++++++++++++++++ .../modules/category/templates/editSuccess.php | 3 + .../modules/category/templates/indexSuccess.php | 25 ++++++ .../modules/category/templates/newSuccess.php | 3 + .../modules/category/templates/showSuccess.php | 46 +++++++++++ apps/companyfront/templates/layout.php | 2 +- lib/model/doctrine/AdDescriptionTable.class.php | 1 - .../CompanyCategoryDescriptionTable.class.php | 18 ----- lib/model/doctrine/CompanyCategoryTable.class.php | 13 +++- lib/model/doctrine/GeneralCategory.class.php | 39 +++++++++- .../companyfront/categoryActionsTest.php | 19 +++++ 14 files changed, 398 insertions(+), 23 deletions(-) create mode 100644 apps/companyfront/modules/category/actions/actions.class.php create mode 100644 apps/companyfront/modules/category/templates/_form.php create mode 100644 apps/companyfront/modules/category/templates/_list.php create mode 100644 apps/companyfront/modules/category/templates/editSuccess.php create mode 100644 apps/companyfront/modules/category/templates/indexSuccess.php create mode 100644 apps/companyfront/modules/category/templates/newSuccess.php create mode 100644 apps/companyfront/modules/category/templates/showSuccess.php create mode 100644 test/functional/companyfront/categoryActionsTest.php diff --git a/apps/companyfront/config/routing.yml b/apps/companyfront/config/routing.yml index a5d1a2c..927ab60 100644 --- a/apps/companyfront/config/routing.yml +++ b/apps/companyfront/config/routing.yml @@ -20,6 +20,12 @@ ads_index: param: { module: ad, action: index } options: { model: Ad, type: object } +companycategories_index: + url: /category/index + class: sfDoctrineRoute + param: { module: category, action: index } + options: { model: CompanyCategory, type: object } + # generic rules # please, remove them by adding more specific rules default_index: diff --git a/apps/companyfront/modules/category/actions/actions.class.php b/apps/companyfront/modules/category/actions/actions.class.php new file mode 100644 index 0000000..e144889 --- /dev/null +++ b/apps/companyfront/modules/category/actions/actions.class.php @@ -0,0 +1,89 @@ +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(); + + //Doctrine Query used to show a pager with the Company Categories. + $query=CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($companyId); + + $this->pager = new sfDoctrinePager('CompanyCategoryDescription', sfConfig::get('app_max_categories_on_pager')); + $this->pager->setQuery($query); + $this->pager->setPage($request->getParameter('page', 1)); + $this->pager->init(); + } + + public function executeShow(sfWebRequest $request) + { + $this->company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))); + $this->forward404Unless($this->company_category); + } + + public function executeNew(sfWebRequest $request) + { + $this->form = new CompanyCategoryForm(); + } + + public function executeCreate(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST)); + + $this->form = new CompanyCategoryForm(); + + $this->processForm($request, $this->form); + + $this->setTemplate('new'); + } + + public function executeEdit(sfWebRequest $request) + { + $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id'))); + $this->form = new CompanyCategoryForm($company_category); + } + + public function executeUpdate(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); + $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id'))); + $this->form = new CompanyCategoryForm($company_category); + + $this->processForm($request, $this->form); + + $this->setTemplate('edit'); + } + + public function executeDelete(sfWebRequest $request) + { + $request->checkCSRFProtection(); + + $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id'))); + $company_category->delete(); + + $this->redirect('category/index'); + } + + protected function processForm(sfWebRequest $request, sfForm $form) + { + $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); + if ($form->isValid()) + { + $company_category = $form->save(); + + $this->redirect('category/edit?id='.$company_category->getId()); + } + } +} diff --git a/apps/companyfront/modules/category/templates/_form.php b/apps/companyfront/modules/category/templates/_form.php new file mode 100644 index 0000000..493967b --- /dev/null +++ b/apps/companyfront/modules/category/templates/_form.php @@ -0,0 +1,81 @@ + + + +
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() ?> + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
renderLabel() ?> + renderError() ?> + +
+
diff --git a/apps/companyfront/modules/category/templates/_list.php b/apps/companyfront/modules/category/templates/_list.php new file mode 100644 index 0000000..8d9ae35 --- /dev/null +++ b/apps/companyfront/modules/category/templates/_list.php @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + getNode(); + if ($node->isValidNode() && $node->hasParent() && ($node->getParent()->getId() != '1')) + { + echo 'class="child-of-node-'.$node->getParent()->getId().'"'; + } + ?>> + + + + + + + + +
NameCurrent General CategoryAssociate/DeleteEditDelete
+ + + + + getGeneralCategory() ?> + + getId() != 1): ?> + getGeneralCategId() != null): ?> + + + + + + + + + + getId() != 1): ?> + + + + + + + getId() != 1): ?> + ', 'categempresa/delete?id='.$categ_empresa->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> + + + +
diff --git a/apps/companyfront/modules/category/templates/editSuccess.php b/apps/companyfront/modules/category/templates/editSuccess.php new file mode 100644 index 0000000..e7dfb14 --- /dev/null +++ b/apps/companyfront/modules/category/templates/editSuccess.php @@ -0,0 +1,3 @@ +

Edit Company category

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

+ + $pager->getResults())) ?> + +haveToPaginate()): ?> + + + +Create new Category diff --git a/apps/companyfront/modules/category/templates/newSuccess.php b/apps/companyfront/modules/category/templates/newSuccess.php new file mode 100644 index 0000000..735bafa --- /dev/null +++ b/apps/companyfront/modules/category/templates/newSuccess.php @@ -0,0 +1,3 @@ +

New Company category

+ + $form)) ?> diff --git a/apps/companyfront/modules/category/templates/showSuccess.php b/apps/companyfront/modules/category/templates/showSuccess.php new file mode 100644 index 0000000..5cee794 --- /dev/null +++ b/apps/companyfront/modules/category/templates/showSuccess.php @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id:getId() ?>
Company:getCompanyId() ?>
General categ:getGeneralCategId() ?>
Created at:getCreatedAt() ?>
Updated at:getUpdatedAt() ?>
Root:getRootId() ?>
Lft:getLft() ?>
Rgt:getRgt() ?>
Level:getLevel() ?>
+ +
+ +Edit +  +List diff --git a/apps/companyfront/templates/layout.php b/apps/companyfront/templates/layout.php index 65de699..f7f0af6 100644 --- a/apps/companyfront/templates/layout.php +++ b/apps/companyfront/templates/layout.php @@ -94,7 +94,7 @@ Company Categories User Reference diff --git a/lib/model/doctrine/AdDescriptionTable.class.php b/lib/model/doctrine/AdDescriptionTable.class.php index 4a0355a..ba48ecc 100644 --- a/lib/model/doctrine/AdDescriptionTable.class.php +++ b/lib/model/doctrine/AdDescriptionTable.class.php @@ -30,5 +30,4 @@ class AdDescriptionTable extends Doctrine_Table ->innerjoin('addescription.Ad ad') ->orderBy('ad.id'); } - } diff --git a/lib/model/doctrine/CompanyCategoryDescriptionTable.class.php b/lib/model/doctrine/CompanyCategoryDescriptionTable.class.php index c3b0ece..b7e5096 100644 --- a/lib/model/doctrine/CompanyCategoryDescriptionTable.class.php +++ b/lib/model/doctrine/CompanyCategoryDescriptionTable.class.php @@ -16,22 +16,4 @@ class CompanyCategoryDescriptionTable extends Doctrine_Table { return Doctrine_Core::getTable('CompanyCategoryDescription'); } - - - /** - * Return - * - * @return Doctrine Collection - */ - public function getCategGeneralFromIdDAOImpl($languageId, $companyCategId) - { - $q = Doctrine_Query::create() - ->from('CompanyCategoryDescription cgd') - ->where('cgd.company_categ_id = ?', $companyCategId) - ->andWhere('cgd.language_id = ?', $languageId); - - $doccollect=$q->execute(); - - return $doccollect; - } } diff --git a/lib/model/doctrine/CompanyCategoryTable.class.php b/lib/model/doctrine/CompanyCategoryTable.class.php index ad9400a..1947289 100644 --- a/lib/model/doctrine/CompanyCategoryTable.class.php +++ b/lib/model/doctrine/CompanyCategoryTable.class.php @@ -16,4 +16,15 @@ class CompanyCategoryTable extends Doctrine_Table { return Doctrine_Core::getTable('CompanyCategory'); } -} \ No newline at end of file + + + /** + * Returns company categories by company id. + * + * @return related company categories to a company as Doctrine Query + */ + public function getCompanyCategoriesByCompanyIdQuery($companyId) + { + return $this->createQuery('cg')->where('cg.company_id = ?', $companyId); + } +} diff --git a/lib/model/doctrine/GeneralCategory.class.php b/lib/model/doctrine/GeneralCategory.class.php index 9c145eb..a96f051 100644 --- a/lib/model/doctrine/GeneralCategory.class.php +++ b/lib/model/doctrine/GeneralCategory.class.php @@ -7,9 +7,44 @@ * * @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 GeneralCategory extends BaseGeneralCategory { + /** + * Returns the string representation of this object. + * + * @return string + */ + public function __toString() + { + $languageId = sfContext::getInstance()->getUser()->getGuardUser()->getLanguage()->getId(); + + //Check if there is description with the user's language + $generalCategoryDescriptions = GeneralCategoryDescriptionTable::getInstance()->findByGeneralCategId($this->getId()); + foreach ($generalCategoryDescriptions as $generalCategoryDescription) + { + if ($generalCategoryDescription->getLanguageId() == $languageId) + { + //We found it!!! + return (string) $generalCategoryDescription->getGeneralCategName(); + } + } + + //Otherwise return with the default language + $languageCode = sfConfig::get('app_default_language'); + $languageId = LanguageTable::getInstance()->findByCode($languageCode); + foreach ($generalCategoryDescriptions as $generalCategoryDescription) + { + if ($generalCategoryDescription->getLanguageId() == $languageId) + { + //We found the default name description!!! + return (string) $generalCategoryDescription->getGeneralCategName(); + } + } + + //Finally, if nothing was found, return nice error message. + return (string) "General category without default language"; + } } diff --git a/test/functional/companyfront/categoryActionsTest.php b/test/functional/companyfront/categoryActionsTest.php new file mode 100644 index 0000000..3372571 --- /dev/null +++ b/test/functional/companyfront/categoryActionsTest.php @@ -0,0 +1,19 @@ + + get('/category/index')-> + + with('request')->begin()-> + isParameter('module', 'category')-> + isParameter('action', 'index')-> + end()-> + + with('response')->begin()-> + isStatusCode(200)-> + checkElement('body', '!/This is a temporary page/')-> + end() +; -- 2.1.4