/**
  * Specify category filter for post collection
  *
  * @param EM_Blog_Model_Category $category
  * @return EM_Blog_Model_Resource_Post_Collection
  */
 public function addCategoryFilter(EM_Blog_Model_Category $category)
 {
     $this->_productLimitationFilters['category_id'] = $category->getId();
     if ($category->getIsAnchor()) {
         unset($this->_productLimitationFilters['category_is_anchor']);
     } else {
         $this->_productLimitationFilters['category_is_anchor'] = 1;
     }
     if ($this->getStoreId() == EM_Blog_Model_Abstract::DEFAULT_STORE_ID) {
         $this->_applyZeroStoreProductLimitations();
     } else {
         $this->_applyProductLimitations();
     }
     return $this;
 }
 /**
  * Save category products relation
  *
  * @param EM_Blog_Model_Category $category
  * @return EM_Blog_Model_Resource_Category
  */
 protected function _saveCategoryPosts($category)
 {
     $category->setIsChangedProductList(false);
     $id = $category->getId();
     /**
      * new category-product relationships
      */
     $posts = $category->getPostedPosts();
     /**
      * Example re-save category
      */
     if ($posts === null) {
         return $this;
     }
     /**
      * old category-product relationships
      */
     $oldPosts = $category->getPostsPosition();
     $insert = array_diff_key($posts, $oldPosts);
     $delete = array_diff_key($oldPosts, $posts);
     /**
      * Find product ids which are presented in both arrays
      * and saved before (check $oldPosts array)
      */
     $update = array_intersect_key($posts, $oldPosts);
     $update = array_diff_assoc($update, $oldPosts);
     $adapter = $this->_getWriteAdapter();
     /**
      * Delete posts from category
      */
     if (!empty($delete)) {
         $cond = array('post_id IN(?)' => array_keys($delete), 'category_id=?' => $id);
         $adapter->delete($this->_categoryPostTable, $cond);
     }
     /**
      * Add products to category
      */
     if (!empty($insert)) {
         $data = array();
         foreach ($insert as $postId => $position) {
             $data[] = array('category_id' => (int) $id, 'post_id' => (int) $postId, 'position' => (int) $position);
         }
         $adapter->insertMultiple($this->_categoryPostTable, $data);
         //echo 'save category aaaaaaaaa';
     }
     /**
      * Update product positions in category
      */
     if (!empty($update)) {
         foreach ($update as $productId => $position) {
             $where = array('category_id = ?' => (int) $id, 'post_id = ?' => (int) $productId);
             $bind = array('position' => (int) $position);
             $adapter->update($this->_categoryPostTable, $bind, $where);
         }
     }
     if (!empty($insert) || !empty($delete)) {
         $postIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
         /*Mage::dispatchEvent('catalog_category_change_products', array(
               'category'      => $category,
               'product_ids'   => $productIds
           ));*/
     }
     if (!empty($insert) || !empty($update) || !empty($delete) || $category->getIsAnchor()) {
         $category->setIsChangedProductList(true);
         /**
          * Setting affected products to category for third party engine index refresh
          */
         $postIds = array_keys($insert + $delete + $update);
         $category->setAffectedProductIds($postIds);
     }
     /* Remove is_parent = 0 if is_anchor = 0 */
     if (!$category->getIsAnchor()) {
         $this->removeNonAnchor($category);
     }
     return $this;
 }