/**
  * Save data for page-product relation
  * @access public
  * @param  Ibrams_CmsExtended_Model_Page $page
  * @return Ibrams_CmsExtended_Model_Page_Product
  * @author Ultimate Module Creator
  */
 public function savePageRelation($page)
 {
     $data = $page->getProductsData();
     if (!is_null($data)) {
         $this->_getResource()->savePageRelation($page, $data);
     }
     return $this;
 }
 /**
  * Save page - product relations
  *
  * @access public
  * @param Ibrams_CmsExtended_Model_Page $page
  * @param array $data
  * @return Ibrams_CmsExtended_Model_Resource_Page_Product
  * @author Ultimate Module Creator
  */
 public function savePageRelation($page, $data)
 {
     if (!is_array($data)) {
         $data = array();
     }
     $deleteCondition = $this->_getWriteAdapter()->quoteInto('page_id=?', $page->getId());
     $this->_getWriteAdapter()->delete($this->getMainTable(), $deleteCondition);
     foreach ($data as $productId => $info) {
         $this->_getWriteAdapter()->insert($this->getMainTable(), array('page_id' => $page->getId(), 'product_id' => $productId, 'position' => @$info['position']));
     }
     return $this;
 }
 /**
  * Save page - category relations
  *
  * @access public
  * @param Ibrams_CmsExtended_Model_Page $page
  * @param array $data
  * @return Ibrams_CmsExtended_Model_Resource_Page_Category
  * @author Ultimate Module Creator
  */
 public function savePageRelation($page, $data)
 {
     if (!is_array($data)) {
         $data = array();
     }
     $deleteCondition = $this->_getWriteAdapter()->quoteInto('page_id=?', $page->getId());
     $this->_getWriteAdapter()->delete($this->getMainTable(), $deleteCondition);
     foreach ($data as $categoryId) {
         if (!empty($categoryId)) {
             $insert = array('page_id' => $page->getId(), 'category_id' => $categoryId, 'position' => 1);
             $this->_getWriteAdapter()->insertOnDuplicate($this->getMainTable(), $insert, array_keys($insert));
         }
     }
     return $this;
 }
 /**
  * Process positions of old parent page children and new parent page children.
  * Get position for moved page
  *
  * @access protected
  * @param Ibrams_CmsExtended_Model_Page $page
  * @param Ibrams_CmsExtended_Model_Page $newParent
  * @param null|int $afterPageId
  * @return int
  * @author Ultimate Module Creator
  */
 protected function _processPositions($page, $newParent, $afterPageId)
 {
     $table = $this->getMainTable();
     $adapter = $this->_getWriteAdapter();
     $positionField = $adapter->quoteIdentifier('position');
     $bind = array('position' => new Zend_Db_Expr($positionField . ' - 1'));
     $where = array('parent_id = ?' => $page->getParentId(), $positionField . ' > ?' => $page->getPosition());
     $adapter->update($table, $bind, $where);
     /**
      * Prepare position value
      */
     if ($afterPageId) {
         $select = $adapter->select()->from($table, 'position')->where('entity_id = :entity_id');
         $position = $adapter->fetchOne($select, array('entity_id' => $afterPageId));
         $bind = array('position' => new Zend_Db_Expr($positionField . ' + 1'));
         $where = array('parent_id = ?' => $newParent->getId(), $positionField . ' > ?' => $position);
         $adapter->update($table, $bind, $where);
     } elseif ($afterPageId !== null) {
         $position = 0;
         $bind = array('position' => new Zend_Db_Expr($positionField . ' + 1'));
         $where = array('parent_id = ?' => $newParent->getId(), $positionField . ' > ?' => $position);
         $adapter->update($table, $bind, $where);
     } else {
         $select = $adapter->select()->from($table, array('position' => new Zend_Db_Expr('MIN(' . $positionField . ')')))->where('parent_id = :parent_id');
         $position = $adapter->fetchOne($select, array('parent_id' => $newParent->getId()));
     }
     $position += 1;
     return $position;
 }