/**
  * @param bool $deleteSource
  *
  * @return bool
  */
 public function mergeCategories($deleteSource = false)
 {
     $this->_deleteSource = $deleteSource;
     try {
         // Check if both categories exist
         if (!$this->_source->getId() || !$this->_target->getId()) {
             return false;
         }
         $this->_moveProducts();
         $this->_moveChildrenCategories();
         // Just delete a category which is not parent of the target
         if ($this->_deleteSource && !in_array($this->_source->getId(), $this->_target->getParentIds())) {
             $this->_source->delete();
         }
         /** @var $index Mage_Index_Model_Process */
         $index = Mage::getModel('index/process')->load('catalog_category_product', 'indexer_code');
         if ($index->getId() && $index->getMode() == Mage_Index_Model_Process::MODE_REAL_TIME) {
             $index->reindexEverything();
         }
         Mage::dispatchEvent('mkleine_category_merge_finished', array('source_category' => $this->_source, 'target_category' => $this->_target));
         return true;
     } catch (Exception $e) {
         Mage::logException($e);
         return false;
     }
 }
 public function testGetParentIds()
 {
     $this->assertEquals(array(), $this->_model->getParentIds());
     $this->_model->unsetData();
     $this->_model->load(4);
     $this->assertContains(3, $this->_model->getParentIds());
     $this->assertNotContains(4, $this->_model->getParentIds());
 }
Exemple #3
0
 /**
  * Should return list of tags to clean
  *
  * @param Mage_Catalog_Model_Category $object
  * @return string[]|string
  */
 protected function _collectTags($object)
 {
     $tags = array(self::TAG_PREFIX . $object->getId());
     if ($this->_isForUpdate) {
         foreach ($object->getParentIds() as $categoryId) {
             $tags[] = self::TAG_PREFIX . $categoryId;
         }
     }
     return $tags;
 }
Exemple #4
0
 /**
  * Move category to another parent node
  *
  * @param Mage_Catalog_Model_Category $category
  * @param Mage_Catalog_Model_Category $newParent
  * @param null|int $afterCategoryId
  * @return Mage_Catalog_Model_Resource_Category
  */
 public function changeParent(Mage_Catalog_Model_Category $category, Mage_Catalog_Model_Category $newParent, $afterCategoryId = null)
 {
     $childrenCount = $this->getChildrenCount($category->getId()) + 1;
     $table = $this->getEntityTable();
     $adapter = $this->_getWriteAdapter();
     $levelFiled = $adapter->quoteIdentifier('level');
     $pathField = $adapter->quoteIdentifier('path');
     /**
      * Decrease children count for all old category parent categories
      */
     $adapter->update($table, array('children_count' => new Zend_Db_Expr('children_count - ' . $childrenCount)), array('entity_id IN(?)' => $category->getParentIds()));
     /**
      * Increase children count for new category parents
      */
     $adapter->update($table, array('children_count' => new Zend_Db_Expr('children_count + ' . $childrenCount)), array('entity_id IN(?)' => $newParent->getPathIds()));
     $position = $this->_processPositions($category, $newParent, $afterCategoryId);
     $newPath = sprintf('%s/%s', $newParent->getPath(), $category->getId());
     $newLevel = $newParent->getLevel() + 1;
     $levelDisposition = $newLevel - $category->getLevel();
     /**
      * Update children nodes path
      */
     $adapter->update($table, array('path' => new Zend_Db_Expr('REPLACE(' . $pathField . ',' . $adapter->quote($category->getPath() . '/') . ', ' . $adapter->quote($newPath . '/') . ')'), 'level' => new Zend_Db_Expr($levelFiled . ' + ' . $levelDisposition)), array($pathField . ' LIKE ?' => $category->getPath() . '/%'));
     /**
      * Update moved category data
      */
     $data = array('path' => $newPath, 'level' => $newLevel, 'position' => $position, 'parent_id' => $newParent->getId());
     $adapter->update($table, $data, array('entity_id = ?' => $category->getId()));
     // Update category object to new data
     $category->addData($data);
     return $this;
 }
 /**
  * Get design update data of parent categories
  *
  * @param Mage_Catalog_Model_Category $category
  * @return array
  */
 public function getDesignUpdateData($category)
 {
     $categories = array();
     $pathIds = array();
     foreach (array_reverse($category->getParentIds()) as $pathId) {
         if ($pathId == AO::app()->getStore()->getRootCategoryId()) {
             $pathIds[] = $pathId;
             break;
         }
         $pathIds[] = $pathId;
     }
     $select = $this->_getReadAdapter()->select()->from(array('main_table' => $this->getMainStoreTable($category->getStoreId())), array('main_table.entity_id', 'main_table.custom_design', 'main_table.custom_design_apply', 'main_table.custom_design_from', 'main_table.custom_design_to'))->where('main_table.entity_id IN (?)', $pathIds)->where('main_table.is_active = ?', '1')->order('main_table.path DESC');
     $result = $this->_getReadAdapter()->fetchAll($select);
     foreach ($result as $row) {
         $row['id'] = $row['entity_id'];
         $categories[$row['entity_id']] = AO::getModel('catalog/category')->setData($row);
     }
     return $categories;
 }
 /**
  * Move category to another parent node
  *
  * @param   Mage_Catalog_Model_Category $category
  * @param   Mage_Catalog_Model_Category $newParent
  * @param   null|int $afterCategoryId
  * @return  Mage_Catalog_Model_Resource_Eav_Mysql4_Category
  */
 public function changeParent(Mage_Catalog_Model_Category $category, Mage_Catalog_Model_Category $newParent, $afterCategoryId = null)
 {
     $childrenCount = $this->getChildrenCount($category->getId()) + 1;
     $table = $this->getEntityTable();
     $adapter = $this->_getWriteAdapter();
     $categoryId = $category->getId();
     /**
      * Decrease children count for all old category parent categories
      */
     $sql = "UPDATE {$table} SET children_count=children_count-{$childrenCount} WHERE entity_id IN(?)";
     $adapter->query($adapter->quoteInto($sql, $category->getParentIds()));
     /**
      * Increase children count for new category parents
      */
     $sql = "UPDATE {$table} SET children_count=children_count+{$childrenCount} WHERE entity_id IN(?)";
     $adapter->query($adapter->quoteInto($sql, $newParent->getPathIds()));
     $position = $this->_processPositions($category, $newParent, $afterCategoryId);
     $newPath = $newParent->getPath() . '/' . $category->getId();
     $newLevel = $newParent->getLevel() + 1;
     $levelDisposition = $newLevel - $category->getLevel();
     /**
      * Update children nodes path
      */
     $sql = "UPDATE {$table} SET\n            `path`  = REPLACE(`path`, '{$category->getPath()}/', '{$newPath}/'),\n            `level` = `level` + {$levelDisposition}\n            WHERE " . $adapter->quoteInto('path LIKE ?', $category->getPath() . '/%');
     $adapter->query($sql);
     /**
      * Update moved category data
      */
     $data = array('path' => $newPath, 'level' => $newLevel, 'position' => $position, 'parent_id' => $newParent->getId());
     $adapter->update($table, $data, $adapter->quoteInto('entity_id=?', $category->getId()));
     return $this;
 }
Exemple #7
0
 /**
  * Fill the Field is_active in the given array
  * param $isRoot = false not needed here
  *
  * @param Mage_Catalog_Model_Category $category
  */
 protected function _setIsActive($category)
 {
     $catIds = Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_EXPORT_HIDDEN_CATEGORIES);
     $catIdsArray = array();
     if (!empty($cat_ids)) {
         $catIdsArray = explode(",", $catIds);
         foreach ($catIdsArray as &$catId) {
             $catId = trim($catId);
         }
     }
     $isActive = $category->getIsActive();
     if (in_array($category->getId(), $catIdsArray) || array_intersect($catIdsArray, $category->getParentIds())) {
         $isActive = 1;
     }
     if (Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_EXPORT_NAVIGATION_CATEGORIES_ONLY) && !$category->getIncludeInMenu()) {
         $isActive = 0;
     }
     $this->_defaultRow["is_active"] = $isActive;
 }