/**
  * 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;
 }
 /**
  * Check is category in list of store categories
  *
  * @param EM_Blog_Model_Category $category
  * @return boolean
  */
 public function isInRootCategoryList($category)
 {
     $rootCategoryId = Mage::helper('blog/category')->getRootCategory()->getId();
     return in_array($rootCategoryId, $category->getParentIds());
 }