• Main Page
  • Modules
  • Classes
  • Files
  • File List

omp.JipGdx/omp-1.0b/classes/monograph/MonographFileDAODelegate.inc.php

00001 <?php
00002 
00021 import('classes.monograph.MonographFile');
00022 import('lib.pkp.classes.submission.SubmissionFileDAODelegate');
00023 
00024 class MonographFileDAODelegate extends SubmissionFileDAODelegate {
00028    function MonographFileDAODelegate() {
00029       parent::SubmissionFileDAODelegate();
00030    }
00031 
00032 
00033    //
00034    // Public methods
00035    //
00039    function getSubmissionEntityName() {
00040       return 'monograph';
00041    }
00042 
00048    function &insertObject(&$monographFile, $sourceFile, $isUpload = false) {
00049       $fileId = $monographFile->getFileId();
00050 
00051       if (!is_numeric($monographFile->getRevision())) {
00052          // Set the initial revision.
00053          $monographFile->setRevision(1);
00054       }
00055 
00056       if (!is_bool($monographFile->getViewable())) {
00057          // Set the viewable default.
00058          $monographFile->setViewable(false);
00059       }
00060 
00061       $params = array(
00062          (int)$monographFile->getRevision(),
00063          (int)$monographFile->getMonographId(),
00064          is_null($monographFile->getSourceFileId()) ? null : (int)$monographFile->getSourceFileId(),
00065          is_null($monographFile->getSourceRevision()) ? null : (int)$monographFile->getSourceRevision(),
00066          $monographFile->getFileType(),
00067          (int)$monographFile->getFileSize(),
00068          $monographFile->getOriginalFileName(),
00069          (int)$monographFile->getFileStage(),
00070          (boolean)$monographFile->getViewable() ? 1 : 0,
00071          is_null($monographFile->getUploaderUserId()) ? null : (int)$monographFile->getUploaderUserId(),
00072          is_null($monographFile->getUserGroupId()) ? null : (int)$monographFile->getUserGroupId(),
00073          is_null($monographFile->getAssocType()) ? null : (int)$monographFile->getAssocType(),
00074          is_null($monographFile->getAssocId()) ? null : (int)$monographFile->getAssocId(),
00075          is_null($monographFile->getGenreId()) ? null : (int)$monographFile->getGenreId(),
00076          $monographFile->getDirectSalesPrice(),
00077       );
00078 
00079       if ($fileId) {
00080          array_unshift($params, $fileId);
00081       }
00082 
00083       $this->update(
00084          sprintf('INSERT INTO monograph_files
00085             (' . ($fileId ? 'file_id, ' : '') . 'revision, monograph_id, source_file_id, source_revision, file_type, file_size, original_file_name, file_stage, date_uploaded, date_modified, viewable, uploader_user_id, user_group_id, assoc_type, assoc_id, genre_id, direct_sales_price)
00086             VALUES
00087             (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?, ?, ?, ?)',
00088             $this->datetimeToDB($monographFile->getDateUploaded()), $this->datetimeToDB($monographFile->getDateModified())),
00089          $params
00090       );
00091 
00092       if (!$fileId) {
00093          $monographFile->setFileId($this->getInsertId('monograph_files', 'file_id'));
00094       }
00095 
00096       $this->updateLocaleFields($monographFile);
00097 
00098       // Determine the final destination of the file (requires
00099       // the file id we just generated).
00100       $targetFilePath = $monographFile->getFilePath();
00101 
00102       // Only copy the file if it is not yet in the target position.
00103       if ($isUpload || $sourceFile != $targetFilePath) {
00104          // Copy the file from its current location to the target destination.
00105          import('lib.pkp.classes.file.FileManager');
00106          $fileManager = new FileManager();
00107          if ($isUpload) {
00108             $success = $fileManager->uploadFile($sourceFile, $targetFilePath);
00109          } else {
00110             assert(is_readable($sourceFile));
00111             $success = $fileManager->copyFile($sourceFile, $targetFilePath);
00112          }
00113          if (!$success) {
00114             // If the copy/upload operation fails then remove
00115             // the already inserted meta-data.
00116             $this->deleteObject($monographFile);
00117             $nullVar = null;
00118             return $nullVar;
00119          }
00120       }
00121       assert(is_readable($targetFilePath));
00122 
00123       return $monographFile;
00124    }
00125 
00131    function updateObject(&$monographFile, &$previousFile) {
00132       // Update the file in the database.
00133       $this->update(
00134          sprintf('UPDATE monograph_files
00135             SET
00136                file_id = ?,
00137                revision = ?,
00138                monograph_id = ?,
00139                source_file_id = ?,
00140                source_revision = ?,
00141                file_type = ?,
00142                file_size = ?,
00143                original_file_name = ?,
00144                file_stage = ?,
00145                date_uploaded = %s,
00146                date_modified = %s,
00147                viewable = ?,
00148                uploader_user_id = ?,
00149                user_group_id = ?,
00150                assoc_type = ?,
00151                assoc_id = ?,
00152                genre_id = ?,
00153                direct_sales_price = ?
00154             WHERE file_id = ? AND revision = ?',
00155             $this->datetimeToDB($monographFile->getDateUploaded()), $this->datetimeToDB($monographFile->getDateModified())),
00156          array(
00157             (int)$monographFile->getFileId(),
00158             (int)$monographFile->getRevision(),
00159             (int)$monographFile->getMonographId(),
00160             is_null($monographFile->getSourceFileId()) ? null : (int)$monographFile->getSourceFileId(),
00161             is_null($monographFile->getSourceRevision()) ? null : (int)$monographFile->getSourceRevision(),
00162             $monographFile->getFileType(),
00163             $monographFile->getFileSize(),
00164             $monographFile->getOriginalFileName(),
00165             $monographFile->getFileStage(),
00166             (boolean)$monographFile->getViewable() ? 1 : 0,
00167             is_null($monographFile->getUploaderUserId()) ? null : (int)$monographFile->getUploaderUserId(),
00168             is_null($monographFile->getUserGroupId()) ? null : (int)$monographFile->getUserGroupId(),
00169             is_null($monographFile->getAssocType()) ? null : (int)$monographFile->getAssocType(),
00170             is_null($monographFile->getAssocId()) ? null : (int)$monographFile->getAssocId(),
00171             is_null($monographFile->getGenreId()) ? null : (int)$monographFile->getGenreId(),
00172             $monographFile->getDirectSalesPrice(),
00173             (int)$previousFile->getFileId(),
00174             (int)$previousFile->getRevision(),
00175          )
00176       );
00177 
00178       $this->updateLocaleFields($monographFile);
00179 
00180       // Update all dependent objects.
00181       $this->_updateDependentObjects($monographFile, $previousFile);
00182 
00183       // Copy the file from its current location to the target destination
00184       // if necessary.
00185       $previousFilePath = $previousFile->getFilePath();
00186       $targetFilePath = $monographFile->getFilePath();
00187       if ($previousFilePath != $targetFilePath && is_file($previousFilePath)) {
00188          // The file location changed so let's move the file on
00189          // the file system, too.
00190          assert(is_readable($previousFilePath));
00191          import('lib.pkp.classes.file.FileManager');
00192          $fileManager = new FileManager();
00193          if (!$fileManager->copyFile($previousFilePath, $targetFilePath)) return false;
00194          if (!$fileManager->deleteFile($previousFilePath)) return false;
00195       }
00196 
00197       return file_exists($targetFilePath);
00198    }
00199 
00203    function deleteObject(&$submissionFile) {
00204       if (!$this->update(
00205          'DELETE FROM monograph_files
00206           WHERE file_id = ? AND revision = ?',
00207          array(
00208             (int)$submissionFile->getFileId(),
00209             (int)$submissionFile->getRevision()
00210          ))) return false;
00211 
00212       // if we've removed the last revision of this file, clean up
00213       // the settings for this file as well.
00214       $result =& $this->retrieve(
00215          'SELECT * FROM monograph_files WHERE file_id = ?',
00216          array((int)$submissionFile->getFileId())
00217       );
00218 
00219       if ($result->RecordCount() == 0) {
00220          $this->update('DELETE FROM monograph_file_settings WHERE file_id = ?',
00221          array((int) $submissionFile->getFileId()));
00222       }
00223 
00224       // Delete all dependent objects.
00225       $this->_deleteDependentObjects($submissionFile);
00226 
00227       // Delete the file on the file system, too.
00228       $filePath = $submissionFile->getFilePath();
00229       if(!(is_file($filePath) && is_readable($filePath))) return false;
00230       assert(is_writable(dirname($filePath)));
00231 
00232       import('lib.pkp.classes.file.FileManager');
00233       $fileManager = new FileManager();
00234       $fileManager->deleteFile($filePath);
00235 
00236       return !file_exists($filePath);
00237    }
00238 
00243    function &fromRow(&$row) {
00244       $monographFile = $this->newDataObject();
00245       $monographFile->setFileId((int)$row['monograph_file_id']);
00246       $monographFile->setRevision((int)$row['monograph_revision']);
00247       $monographFile->setAssocType(is_null($row['assoc_type']) ? null : (int)$row['assoc_type']);
00248       $monographFile->setAssocId(is_null($row['assoc_id']) ? null : (int)$row['assoc_id']);
00249       $monographFile->setSourceFileId(is_null($row['source_file_id']) ? null : (int)$row['source_file_id']);
00250       $monographFile->setSourceRevision(is_null($row['source_revision']) ? null : (int)$row['source_revision']);
00251       $monographFile->setMonographId((int)$row['monograph_id']);
00252       $monographFile->setFileStage((int)$row['file_stage']);
00253       $monographFile->setOriginalFileName($row['original_file_name']);
00254       $monographFile->setFileType($row['file_type']);
00255       $monographFile->setGenreId(is_null($row['genre_id']) ? null : (int)$row['genre_id']);
00256       $monographFile->setFileSize((int)$row['file_size']);
00257       $monographFile->setUploaderUserId(is_null($row['uploader_user_id']) ? null : (int)$row['uploader_user_id']);
00258       $monographFile->setUserGroupId(is_null($row['user_group_id']) ? null : (int)$row['user_group_id']);
00259       $monographFile->setViewable((boolean)$row['viewable']);
00260       $monographFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
00261       $monographFile->setDateModified($this->datetimeFromDB($row['date_modified']));
00262       $monographFile->setDirectSalesPrice($row['direct_sales_price']);
00263 
00264       $this->getDataObjectSettings('monograph_file_settings', 'file_id', $row['monograph_file_id'], $monographFile);
00265 
00266       return $monographFile;
00267    }
00268 
00273    function newDataObject() {
00274       return new MonographFile();
00275    }
00276 
00277 
00278    //
00279    // Protected helper methods
00280    //
00284    function getLocaleFieldNames() {
00285       $localeFieldNames = parent::getLocaleFieldNames();
00286       $localeFieldNames[] = 'name';
00287       return $localeFieldNames;
00288    }
00289 
00290 
00291    //
00292    // Private helper methods
00293    //
00299    function _updateDependentObjects(&$monographFile, &$previousFile) {
00300       // If the file ids didn't change then we do not have to
00301       // do anything.
00302       if (
00303          $previousFile->getFileId() == $monographFile->getFileId() ||
00304          $previousFile->getRevision() == $monographFile->getRevision()
00305       ) return;
00306 
00307       // Update signoffs that refer to this file.
00308       $signoffDao =& DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
00309       $signoffFactory =& $signoffDao->getByFileRevision(
00310          $previousFile->getFileId(), $previousFile->getRevision()
00311       );
00312       while ($signoff =& $signoffFactory->next()) { /* @var $signoff Signoff */
00313          $signoff->setFileId($monographFile->getFileId());
00314          $signoff->setFileRevision($monographFile->getRevision());
00315          $signoffDao->updateObject($signoff);
00316          unset($signoff);
00317       }
00318 
00319       // Update file views that refer to this file.
00320       $viewsDao =& DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
00321       $viewsDao->moveViews(
00322          ASSOC_TYPE_MONOGRAPH_FILE,
00323          $previousFile->getFileIdAndRevision(), $monographFile->getFileIdAndRevision()
00324       );
00325    }
00326 
00331    function _deleteDependentObjects(&$monographFile) {
00332       // Delete signoffs that refer to this file.
00333       $signoffDao =& DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
00334       $signoffFactory =& $signoffDao->getByFileRevision(
00335          $monographFile->getFileId(), $monographFile->getRevision()
00336       );
00337       while ($signoff =& $signoffFactory->next()) { /* @var $signoff Signoff */
00338          $signoffDao->deleteObject($signoff);
00339          unset($signoff);
00340       }
00341 
00342       // Delete file views that refer to this file.
00343       $viewsDao =& DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
00344       $viewsDao->deleteViews(
00345          ASSOC_TYPE_MONOGRAPH_FILE, $monographFile->getFileIdAndRevision()
00346       );
00347    }
00348 }
00349 
00350 ?>

Generated on Mon Sep 17 2012 13:00:00 for Open Monograph Press by  doxygen 1.7.1