/** * 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; }