/** * Move category to another parent node * * @param EM_Blog_Model_Category $category * @param EM_Blog_Model_Category $newParent * @param null|int $afterCategoryId * @return EM_Blog_Model_Resource_Category */ public function changeParent(EM_Blog_Model_Category $category, EM_Blog_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; }
/** * Return parent category of current category with own custom design settings * * @param EM_Blog_Model_Category $category * @return EM_Blog_Model_Category */ public function getParentDesignCategory($category) { $pathIds = array_reverse($category->getPathIds()); $collection = $category->getCollection()->setStore(Mage::app()->getStore())->addAttributeToSelect('custom_design')->addAttributeToSelect('custom_design_from')->addAttributeToSelect('custom_design_to')->addAttributeToSelect('page_layout')->addAttributeToSelect('custom_layout_update')->addAttributeToSelect('custom_apply_to_products')->addFieldToFilter('entity_id', array('in' => $pathIds))->addFieldToFilter('custom_use_parent_settings', 0)->addFieldToFilter('level', array('neq' => 0))->setOrder('level', 'DESC')->load(); return $collection->getFirstItem(); }