PKPSubmissionFileDAO::updateObject PHP Method

updateObject() public method

NB: We implement a delete + insert strategy to deal with various casting problems (e.g. file implementation/genre may change, file path may change, etc.).
public updateObject ( $updatedFile, $previousFileId = null, $previousRevision = null ) : SubmissionFile
$updatedFile SubmissionFile
$previousFileId integer The file id before the file was changed. Must only be given if the file id changed so that the previous file can be identified.
$previousRevision integer The revision before the file was changed. Must only be given if the revision changed so that the previous file can be identified.
return SubmissionFile The updated file. This file may be of a different file implementation than the file passed into the method if the genre of the file didn't fit its implementation.
    function updateObject($updatedFile, $previousFileId = null, $previousRevision = null)
    {
        // Make sure that the implementation of the updated file
        // is compatible with its genre.
        $updatedFile = $this->_castToGenre($updatedFile);
        // Complete the identifying data of the previous file if not given.
        $previousFileId = (int) ($previousFileId ? $previousFileId : $updatedFile->getFileId());
        $previousRevision = (int) ($previousRevision ? $previousRevision : $updatedFile->getRevision());
        // Retrieve the previous file.
        $previousFile = $this->getRevision($previousFileId, $previousRevision);
        assert(is_a($previousFile, 'SubmissionFile'));
        // Canonicalized the implementation of the previous file.
        $previousImplementation = strtolower_codesafe(get_class($previousFile));
        // Find the required target implementation and delegate.
        $targetImplementation = strtolower_codesafe($this->_getFileImplementationForGenreId($updatedFile->getGenreId()));
        $targetDaoDelegate = $this->_getDaoDelegate($targetImplementation);
        // If the implementation in the database differs from the target
        // implementation then we'll have to delete + insert the object
        // to make sure that the database contains consistent data.
        if ($previousImplementation != $targetImplementation) {
            // We'll have to copy the previous file to its target
            // destination so that it is not lost when we delete the
            // previous file.
            // When the implementation (i.e. genre) changes then the
            // file locations will also change so we should not get
            // a file name clash.
            $previousFilePath = $previousFile->getFilePath();
            $targetFilePath = $updatedFile->getFilePath();
            assert($previousFilePath != $targetFilePath && !file_exists($targetFilePath));
            import('lib.pkp.classes.file.FileManager');
            $fileManager = new FileManager();
            $fileManager->copyFile($previousFilePath, $targetFilePath);
            // We use the delegates directly to make sure
            // that we address the right implementation in the database
            // on delete and insert.
            $sourceDaoDelegate = $this->_getDaoDelegate($previousImplementation);
            $sourceDaoDelegate->deleteObject($previousFile);
            $targetDaoDelegate->insertObject($updatedFile, $targetFilePath);
        } else {
            // If the implementation in the database does not change then we
            // can do an efficient update.
            if (!$targetDaoDelegate->updateObject($updatedFile, $previousFile)) {
                return null;
            }
        }
        // If the updated file does not have the correct target type then we'll have
        // to retrieve it again from the database to cast it to the right type.
        if (strtolower_codesafe(get_class($updatedFile)) != $targetImplementation) {
            $updatedFile = $this->_castToDatabase($updatedFile);
        }
        return $updatedFile;
    }