/** * Get revision. * * @param ItemDao $itemdao * @param int $number * @return false|ItemRevisionDao * @throws Zend_Exception */ public function getRevision($itemdao, $number) { if (!$itemdao instanceof ItemDao || !$itemdao->saved) { throw new Zend_Exception('Error in param itemdao when getting Item revision.'); } return $this->initDao('ItemRevision', $this->database->fetchRow($this->database->select()->from('itemrevision')->where('item_id = ?', $itemdao->getItemId())->where('revision = ?', $number)->limit(1)->setIntegrityCheck(false))); }
/** * Delete an itemrevision from an item, will reduce all other * itemrevision revision numbers appropriately. * * @param ItemDao $itemdao * @param ItemRevisionDao $revisiondao * @throws Zend_Exception */ public function removeRevision($itemdao, $revisiondao) { if (!$itemdao instanceof ItemDao) { throw new Zend_Exception('First argument should be an item'); } if (!$revisiondao instanceof ItemRevisionDao) { throw new Zend_Exception('Second argument should be an item revision'); } if ($revisiondao->getItemId() != $itemdao->getItemId()) { throw new Zend_Exception('Revision needs to be associated with Item'); } /** @var ItemRevisionModel $itemRevisionModel */ $itemRevisionModel = MidasLoader::loadModel('ItemRevision'); $lastRevisionDao = $this->getLastRevision($itemdao); $numRevisions = $lastRevisionDao->getRevision(); $deletedRevisionNum = $revisiondao->getRevision(); $itemRevisionModel->delete($revisiondao); // compact the revision numbers if necessary if ($deletedRevisionNum < $numRevisions) { // reach past the deleted revision, reduce each revision number by 1 $revisions = range($deletedRevisionNum + 1, $numRevisions); foreach ($revisions as $revision) { $itemRevisionDao = $this->getRevision($itemdao, $revision); $itemRevisionDao->setRevision($itemRevisionDao->getRevision() - 1); $itemRevisionModel->save($itemRevisionDao); } } // reload the item and last revision now that we have updated revisions $itemId = $itemdao->getItemId(); $itemdao = $this->load($itemId); $lastRevisionDao = $this->getLastRevision($itemdao); // now that we have deleted a revision, recalculate size of item if (empty($lastRevisionDao)) { $itemdao->setSizebytes(0); } else { $itemdao->setSizebytes($itemRevisionModel->getSize($lastRevisionDao)); } $this->save($itemdao, true); }
/** * Return the latest revision of a model. * * @param ItemDao $itemdao * @return false|ItemRevisionDao */ public function getLatestRevision($itemdao) { $row = $this->database->fetchRow($this->database->select()->from($this->_name)->where('item_id=?', $itemdao->getItemId())->order('revision DESC')->limit(1)); return $this->initDao('ItemRevision', $row); }