Company category may not choose itself as parent.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 10 Jun 2012 21:23:22 +0000 (23:23 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 10 Jun 2012 21:23:22 +0000 (23:23 +0200)
apps/companyfront/modules/category/actions/actions.class.php
lib/form/doctrine/CompanyCategoryForm.class.php
lib/model/doctrine/CompanyCategoryTable.class.php

index 153e4db..fca69a5 100644 (file)
@@ -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)
index f8ce6db..2067c28 100644 (file)
@@ -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,
index 874b7c4..ca668d9 100644 (file)
@@ -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');
+        }
     }
 }