From 72a1a3dd44a6a8a794f24782348903b9d12c0a68 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 10 Jun 2012 23:23:22 +0200 Subject: [PATCH] Company category may not choose itself as parent. --- .../modules/category/actions/actions.class.php | 2 +- lib/form/doctrine/CompanyCategoryForm.class.php | 3 ++- lib/model/doctrine/CompanyCategoryTable.class.php | 18 ++++++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/companyfront/modules/category/actions/actions.class.php b/apps/companyfront/modules/category/actions/actions.class.php index 153e4db..fca69a5 100644 --- a/apps/companyfront/modules/category/actions/actions.class.php +++ b/apps/companyfront/modules/category/actions/actions.class.php @@ -75,7 +75,7 @@ class categoryActions extends sfActions $this->forward404Unless($companyId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id'))); - $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId)); + $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId, 'current_category' => $companyCategoryId)); } public function executeUpdate(sfWebRequest $request) diff --git a/lib/form/doctrine/CompanyCategoryForm.class.php b/lib/form/doctrine/CompanyCategoryForm.class.php index f8ce6db..2067c28 100644 --- a/lib/form/doctrine/CompanyCategoryForm.class.php +++ b/lib/form/doctrine/CompanyCategoryForm.class.php @@ -19,7 +19,8 @@ class CompanyCategoryForm extends BaseCompanyCategoryForm $this->useFields(array('general_categ_id')); //Narrow down the valid options for some field validators - $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id')); + $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id'), + $this->getOption('current_category')); $this->widgetSchema['parent_category'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getModelName(), 'add_empty' => false, diff --git a/lib/model/doctrine/CompanyCategoryTable.class.php b/lib/model/doctrine/CompanyCategoryTable.class.php index 874b7c4..ca668d9 100644 --- a/lib/model/doctrine/CompanyCategoryTable.class.php +++ b/lib/model/doctrine/CompanyCategoryTable.class.php @@ -23,10 +23,20 @@ class CompanyCategoryTable extends Doctrine_Table * * @return related company categories to a company as Doctrine Query */ - public function getCompanyCategoriesByCompanyIdQuery($companyId) + public function getCompanyCategoriesByCompanyIdQuery($companyId, $currentCategory = null) { - return $this->createQuery('cc')->where('cc.company_id = ?', $companyId) - ->orWhere('cc.id = ?', '1') - ->orderBy('cc.lft'); + if ($currentCategory != null) + { + return $this->createQuery('cc')->where('cc.company_id = ?', $companyId) + ->andWhere('cc.id != ?', $currentCategory) + ->orWhere('cc.id = ?', '1') + ->orderBy('cc.lft'); + } + else + { + return $this->createQuery('cc')->where('cc.company_id = ?', $companyId) + ->orWhere('cc.id = ?', '1') + ->orderBy('cc.lft'); + } } } -- 2.1.4