Fixed error. While editing company categories.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 11 Dec 2012 04:17:47 +0000 (05:17 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 11 Dec 2012 04:17:47 +0000 (05:17 +0100)
Parent category may not become a child category of its own child category or categories.

apps/companyfront/modules/category/actions/actions.class.php
lib/form/doctrine/CompanyCategoryForm.class.php
lib/model/doctrine/CompanyCategoryTable.class.php

index fca69a5..b902fcc 100644 (file)
@@ -75,7 +75,9 @@ 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, 'current_category' => $companyCategoryId));
+    $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId, 'current_category' => $companyCategoryId,
+                                                                   'current_category_lft' => $company_category->getLft(),
+                                                                   'current_category_rgt' => $company_category->getRgt()));
   }
 
   public function executeUpdate(sfWebRequest $request)
index d894346..2fee475 100644 (file)
@@ -20,7 +20,9 @@ class CompanyCategoryForm extends BaseCompanyCategoryForm
 
     //Narrow down the valid options for some field validators
     $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id'),
-                                                                                               $this->getOption('current_category'));
+                                                                                               $this->getOption('current_category'),
+                                                                                               $this->getOption('current_category_lft'),
+                                                                                               $this->getOption('current_category_rgt'));
 
     $this->widgetSchema['parent_category'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getModelName(),
                                                                                   'add_empty' => false,
index 69c4b1d..0d4bafc 100644 (file)
@@ -23,13 +23,13 @@ class CompanyCategoryTable extends Doctrine_Table
     *
     * @return related company categories to a company as Doctrine Query
     */
-    public function getCompanyCategoriesByCompanyIdQuery($companyId, $currentCategory = null)
+    public function getCompanyCategoriesByCompanyIdQuery($companyId, $currentCategory = null, $currentLft = null, $currentRgt = null)
     {
         if ($currentCategory != null)
         {
-            return $this->createQuery('cc')->where('cc.company_id = ?', $companyId)
-                                           ->andWhere('cc.id != ?', $currentCategory)
-                                           ->orWhere('cc.level = ?', '0')
+            return $this->createQuery('cc')->where('cc.level = ?', '0')
+                                           ->orWhere('cc.company_id = ?', $companyId)
+                                           ->andWhere('cc.lft > ? or cc.lft < ?', array($currentRgt, $currentLft))
                                            ->orderBy('cc.lft');
         }
         else