From aa8adc0c79a05eb8625525692caea42218269dac Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 20 May 2012 21:46:02 +0200 Subject: [PATCH] Company Category edit, new, list, remove Everything about company categories. --- apps/companyfront/config/app.yml | 2 +- apps/companyfront/modules/ad/templates/_list.php | 6 +- .../modules/category/actions/actions.class.php | 45 ++++++- .../modules/category/templates/_form.php | 66 +--------- .../modules/category/templates/_list.php | 39 +++--- .../modules/category/templates/editSuccess.php | 3 +- .../modules/category/templates/newSuccess.php | 2 +- .../CompanyCategoryDescriptionForm.class.php | 11 +- lib/form/doctrine/CompanyCategoryForm.class.php | 136 ++++++++++++++++++++- lib/model/doctrine/CompanyCategory.class.php | 2 +- lib/model/doctrine/CompanyCategoryTable.class.php | 4 +- 11 files changed, 216 insertions(+), 100 deletions(-) diff --git a/apps/companyfront/config/app.yml b/apps/companyfront/config/app.yml index adb5077..40a5a99 100644 --- a/apps/companyfront/config/app.yml +++ b/apps/companyfront/config/app.yml @@ -5,7 +5,7 @@ all: max_offices_on_pager: 3 max_ads_on_pager: 2 - max_categories_on_pager: 2 + max_categories_on_pager: 3 default_language: eng # Everything must exist at least with this language sf_guard_plugin: diff --git a/apps/companyfront/modules/ad/templates/_list.php b/apps/companyfront/modules/ad/templates/_list.php index 5160b2f..72be869 100644 --- a/apps/companyfront/modules/ad/templates/_list.php +++ b/apps/companyfront/modules/ad/templates/_list.php @@ -18,7 +18,11 @@ <?php echo $ad->getAdName() ?> - getAd()->getCompanyCategory() ?> + + getAd()->getCompanyCategId() != null): ?> + getAd()->getCompanyCategory() ?> + + getAdName() ?> ', 'ad/delete?id='.$ad->getAd()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> diff --git a/apps/companyfront/modules/category/actions/actions.class.php b/apps/companyfront/modules/category/actions/actions.class.php index b27e802..57727a5 100644 --- a/apps/companyfront/modules/category/actions/actions.class.php +++ b/apps/companyfront/modules/category/actions/actions.class.php @@ -32,14 +32,25 @@ class categoryActions extends sfActions public function executeNew(sfWebRequest $request) { - $this->form = new CompanyCategoryForm(); + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + $this->form = new CompanyCategoryForm(null, array('company_user_id' => CompanyTable::getInstance()->findOneByUserId($userId)->getId())); } public function executeCreate(sfWebRequest $request) { $this->forward404Unless($request->isMethod(sfRequest::POST)); - $this->form = new CompanyCategoryForm(); + $companyCategoryInit = new CompanyCategory(); + + //Get user Id + $userId = $this->getUser()->getGuardUser()->getId(); + + //Get company owned by that user and insert value in form + $companyCategoryInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); + + $this->form = new CompanyCategoryForm($companyCategoryInit, array('company_user_id' => CompanyTable::getInstance()->findOneByUserId($userId)->getId())); $this->processForm($request, $this->form); @@ -49,13 +60,41 @@ class categoryActions extends sfActions 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); + + //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) + $companyCategoryId = $request->getParameter('id'); + + $companyOfficeId = CompanyCategoryTable::getInstance()->findOneById($companyCategoryId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id'))); + + $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId)); } 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'))); + + //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) + $companyCategoryId = $request->getParameter('id'); + + $companyOfficeId = CompanyCategoryTable::getInstance()->findOneById($companyCategoryId)->getCompanyId(); + + $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id'))); + $this->form = new CompanyCategoryForm($company_category); $this->processForm($request, $this->form); diff --git a/apps/companyfront/modules/category/templates/_form.php b/apps/companyfront/modules/category/templates/_form.php index 493967b..ada3fd8 100644 --- a/apps/companyfront/modules/category/templates/_form.php +++ b/apps/companyfront/modules/category/templates/_form.php @@ -10,72 +10,14 @@ renderHiddenFields(false) ?> -  Back to list - getObject()->isNew()): ?> -  getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> - - +   + - renderGlobalErrors() ?> - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - - - renderLabel() ?> - - renderError() ?> - - - + renderGlobalErrors(false) ?> + diff --git a/apps/companyfront/modules/category/templates/_list.php b/apps/companyfront/modules/category/templates/_list.php index 8d9ae35..8fea7d6 100644 --- a/apps/companyfront/modules/category/templates/_list.php +++ b/apps/companyfront/modules/category/templates/_list.php @@ -18,18 +18,17 @@ jQuery(document).ready(function(){ - - - - - + + + + - - + getNode(); + $node = $company_category->getNode(); if ($node->isValidNode() && $node->hasParent() && ($node->getParent()->getId() != '1')) { echo 'class="child-of-node-'.$node->getParent()->getId().'"'; @@ -37,35 +36,25 @@ jQuery(document).ready(function(){ ?>> -
NameCurrent General CategoryAssociate/DeleteEditDelete
- + - getGeneralCategory() ?> - - getId() != 1): ?> - getGeneralCategId() != null): ?> - - - - - - - + getGeneralCategId() != null): ?> + getGeneralCategory() ?> - getId() != 1): ?> - + getId() != 1): ?> + - getId() != 1): ?> - ', 'categempresa/delete?id='.$categ_empresa->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> + getId() != 1): ?> + ', 'category/delete?id='.$company_category->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 index e7dfb14..bfa90a8 100644 --- a/apps/companyfront/modules/category/templates/editSuccess.php +++ b/apps/companyfront/modules/category/templates/editSuccess.php @@ -1,3 +1,4 @@ -

Edit Company category

+

+ $form)) ?> diff --git a/apps/companyfront/modules/category/templates/newSuccess.php b/apps/companyfront/modules/category/templates/newSuccess.php index 735bafa..bc57398 100644 --- a/apps/companyfront/modules/category/templates/newSuccess.php +++ b/apps/companyfront/modules/category/templates/newSuccess.php @@ -1,3 +1,3 @@ -

New Company category

+

$form)) ?> diff --git a/lib/form/doctrine/CompanyCategoryDescriptionForm.class.php b/lib/form/doctrine/CompanyCategoryDescriptionForm.class.php index 52d2af2..6b0c289 100644 --- a/lib/form/doctrine/CompanyCategoryDescriptionForm.class.php +++ b/lib/form/doctrine/CompanyCategoryDescriptionForm.class.php @@ -5,12 +5,19 @@ * * @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 CompanyCategoryDescriptionForm extends BaseCompanyCategoryDescriptionForm { public function configure() { + unset($this['company_categ_id']); + + if ($this->object->exists()) + { + $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox(); + $this->validatorSchema['delete'] = new sfValidatorPass(); + } } } diff --git a/lib/form/doctrine/CompanyCategoryForm.class.php b/lib/form/doctrine/CompanyCategoryForm.class.php index 52d4c27..2a5ade5 100644 --- a/lib/form/doctrine/CompanyCategoryForm.class.php +++ b/lib/form/doctrine/CompanyCategoryForm.class.php @@ -5,12 +5,144 @@ * * @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 CompanyCategoryForm extends BaseCompanyCategoryForm { public function configure() { + $this->useFields(array('general_categ_id')); + + //Narrow down the valid options for some field validators + $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id')); + + $this->widgetSchema['parent_category'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getModelName(), + 'add_empty' => false, + 'query' => $companyCategs)); + + $this->validatorSchema['parent_category'] = new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), + 'required' => true, + 'query' => $companyCategs)); + + if (!$this->getObject()->isNew()) + { + $this->setDefault('parent_category', $this->getObject()->getNode()->getParent()->getId()); + } + + $this->widgetSchema->setLabels(array('parent_category' => 'Parent Company Category')); + $this->widgetSchema->setLabels(array('general_categ_id' => 'General Category')); + + + + //Company categ creation form + $companyCategoryDescription = new CompanyCategoryDescription(); + $companyCategoryDescription->CompanyCategory = $this->getObject(); + $newCompanyCategDescriptionForm = new CompanyCategoryDescriptionForm($companyCategoryDescription); + + $this->embedForm('new', $newCompanyCategDescriptionForm); + + $this->embedRelation('CompanyCategoryDescription'); + + + //i18n (Internationalization) + $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_categor_form'); + } + + protected function doBind(array $values) + { + if ('' === trim($values['new']['company_categ_description']) && '' === trim($values['new']['company_categ_name'])) + { + unset($values['new'], $this['new']); + } + + if (isset($values['CompanyCategoryDescription'])) + { + foreach ($values['CompanyCategoryDescription'] as $i => $companyCategoryDescriptionValues) + { + if (isset($companyCategoryDescriptionValues['delete']) && $companyCategoryDescriptionValues['id']) + { + $this->scheduledForDeletion[$i] = $companyCategoryDescriptionValues['id']; + } + } + } + + + parent::doBind($values); + } + + + /** + * Updates object with provided values, dealing with evantual relation deletion + * + * @see sfFormDoctrine::doUpdateObject() + */ + protected function doUpdateObject($values) + { + if (count($this->scheduledForDeletion)) + { + foreach ($this->scheduledForDeletion as $index => $id) + { + unset($values['CompanyCategoryDescription'][$index]); + unset($this->object['CompanyCategoryDescription'][$index]); + Doctrine::getTable('CompanyCategoryDescription')->findOneById($id)->delete(); + } + } + + $this->getObject()->fromArray($values); + } + + /** + * Saves embedded form objects. + * + * @param mixed $con An optional connection object + * @param array $forms An array of forms + */ + public function saveEmbeddedForms($con = null, $forms = null) + { + if (null === $con) + { + $con = $this->getConnection(); + } + + if (null === $forms) + { + $forms = $this->embeddedForms; + } + + foreach ($forms as $form) + { + if ($form instanceof sfFormObject) + { + if (!in_array($form->getObject()->getId(), $this->scheduledForDeletion)) + { + $form->saveEmbeddedForms($con); + $form->getObject()->save($con); + } + } + else + { + $this->saveEmbeddedForms($con, $form->getEmbeddedForms()); + } + } + } + + + /** + * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php + * + * We need to save new objects in a Nested Tree where the object must have always a parent node + * We retrieve the parent node id and insert the new object as a child of that node. + * + * @param mixed $con An optional connection object + */ + protected function doSave($con = null) + { + //In this way we are writing on the database twice. Right now and the second one inserting the node as a child of its parent. + parent::doSave($con); + + $companyCateg = CompanyCategoryTable::getInstance()->findOneById($this->values['parent_category']); + //Second one, right here + $this->getObject()->getNode()->insertAsFirstChildOf($companyCateg); } } diff --git a/lib/model/doctrine/CompanyCategory.class.php b/lib/model/doctrine/CompanyCategory.class.php index e64ac0d..aacdf06 100644 --- a/lib/model/doctrine/CompanyCategory.class.php +++ b/lib/model/doctrine/CompanyCategory.class.php @@ -34,7 +34,7 @@ class CompanyCategory extends BaseCompanyCategory //Otherwise return with the default language $languageCode = sfConfig::get('app_default_language'); - $languageId = LanguageTable::getInstance()->findByCode($languageCode); + $languageId = LanguageTable::getInstance()->findOneByCode($languageCode)->getId(); foreach ($categoryDescriptions as $categoryDescription) { if ($categoryDescription->getLanguageId() == $languageId) diff --git a/lib/model/doctrine/CompanyCategoryTable.class.php b/lib/model/doctrine/CompanyCategoryTable.class.php index 1947289..3a2a9a4 100644 --- a/lib/model/doctrine/CompanyCategoryTable.class.php +++ b/lib/model/doctrine/CompanyCategoryTable.class.php @@ -25,6 +25,8 @@ class CompanyCategoryTable extends Doctrine_Table */ public function getCompanyCategoriesByCompanyIdQuery($companyId) { - return $this->createQuery('cg')->where('cg.company_id = ?', $companyId); + return $this->createQuery('cg')->where('cg.company_id = ?', $companyId) + ->orWhere('cg.id = ?', '1') + ->orderBy('cg.id'); } } -- 2.1.4