classes/file/PaperFileManager.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00025 //$Id$
00026 
00027 import('file.FileManager');
00028 
00029 /* File type suffixes */
00030 define('PAPER_FILE_SUBMISSION',  'SM');
00031 define('PAPER_FILE_REVIEW',      'RV');
00032 define('PAPER_FILE_DIRECTOR',    'DR');
00033 define('PAPER_FILE_LAYOUT',      'LE');
00034 define('PAPER_FILE_PUBLIC',      'PB');
00035 define('PAPER_FILE_SUPP',     'SP');
00036 define('PAPER_FILE_NOTE',     'NT');
00037 
00038 class PaperFileManager extends FileManager {
00039 
00041    var $filesDir;
00042 
00044    var $paperId;
00045 
00047    var $paper;
00048 
00054    function PaperFileManager($paperId) {
00055       $this->paperId = $paperId;
00056       $paperDao =& DAORegistry::getDAO('PaperDAO');
00057       $this->paper =& $paperDao->getPaper($paperId);
00058       $schedConfId = $this->paper->getSchedConfId();
00059       $schedConfDao =& DAORegistry::getDAO('SchedConfDAO');
00060       $schedConf =& $schedConfDao->getSchedConf($schedConfId);
00061       $this->filesDir = Config::getVar('files', 'files_dir') . '/conferences/' . $schedConf->getConferenceId() . '/schedConfs/' . $schedConfId .
00062       '/papers/' . $paperId . '/';
00063    }
00064 
00071    function uploadSubmissionFile($fileName, $fileId = null, $overwrite = false) {
00072       return $this->handleUpload($fileName, PAPER_FILE_SUBMISSION, $fileId, $overwrite);
00073    }
00074 
00083    function writeSubmissionFile($fileName, &$contents, $mimeType, $fileId = null, $overwrite = true) {
00084       return $this->handleWrite($fileName, $contents, $mimeType, PAPER_FILE_SUBMISSION, $fileId, $overwrite);
00085    }
00086 
00094    function copySubmissionFile($url, $mimeType, $fileId = null, $overwrite = true) {
00095       return $this->handleCopy($url, $mimeType, PAPER_FILE_SUBMISSION, $fileId, $overwrite);
00096    }
00097 
00104    function uploadReviewFile($fileName, $fileId = null) {
00105       return $this->handleUpload($fileName, PAPER_FILE_REVIEW, $fileId);
00106    }
00107 
00114    function uploadDirectorDecisionFile($fileName, $fileId = null) {
00115       return $this->handleUpload($fileName, PAPER_FILE_DIRECTOR, $fileId);
00116    }
00117 
00125    function uploadLayoutFile($fileName, $fileId = null, $overwrite = true) {
00126       return $this->handleUpload($fileName, PAPER_FILE_LAYOUT, $fileId, $overwrite);
00127    }  
00128 
00136    function uploadSuppFile($fileName, $fileId = null, $overwrite = true) {
00137       return $this->handleUpload($fileName, PAPER_FILE_SUPP, $fileId, $overwrite);
00138    }  
00139 
00147    function uploadPublicFile($fileName, $fileId = null, $overwrite = true) {
00148       return $this->handleUpload($fileName, PAPER_FILE_PUBLIC, $fileId, $overwrite);
00149    }  
00150 
00158    function uploadSubmissionNoteFile($fileName, $fileId = null, $overwrite = true) {
00159       return $this->handleUpload($fileName, PAPER_FILE_NOTE, $fileId, $overwrite);
00160    }
00161 
00170    function writePublicFile($fileName, &$contents, $mimeType, $fileId = null, $overwrite = true) {
00171       return $this->handleWrite($fileName, $contents, $mimeType, PAPER_FILE_PUBLIC, $fileId, $overwrite);
00172    }
00173 
00181    function copyPublicFile($url, $mimeType, $fileId = null, $overwrite = true) {
00182       return $this->handleCopy($url, $mimeType, PAPER_FILE_PUBLIC, $fileId, $overwrite);
00183    }
00184 
00193    function writeSuppFile($fileName, &$contents, $mimeType, $fileId = null, $overwrite = true) {
00194       return $this->handleWrite($fileName, $contents, $mimeType, PAPER_FILE_SUPP, $fileId, $overwrite);
00195    }
00196 
00204    function copySuppFile($url, $mimeType, $fileId = null, $overwrite = true) {
00205       return $this->handleCopy($url, $mimeType, PAPER_FILE_SUPP, $fileId, $overwrite);
00206    }
00207 
00212    function &getFile($fileId, $revision = null) {
00213       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00214       $paperFile =& $paperFileDao->getPaperFile($fileId, $revision, $this->paperId);
00215       return $paperFile;
00216    }
00217 
00223    function readFile($fileId, $revision = null, $output = false) {
00224       $paperFile =& $this->getFile($fileId, $revision);
00225 
00226       if (isset($paperFile)) {
00227          $fileType = $paperFile->getFileType();
00228          $filePath = $this->filesDir . $paperFile->getType() . '/' . $paperFile->getFileName();
00229 
00230          return parent::readFile($filePath, $output);
00231 
00232       } else {
00233          return false;
00234       }
00235    }
00236 
00244    function deleteFile($fileId, $revision = null) {
00245       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00246 
00247       $files = array();
00248       if (isset($revision)) {
00249          $file =& $paperFileDao->getPaperFile($fileId, $revision);
00250          if (isset($file)) {
00251             $files[] = $file;
00252          }
00253 
00254       } else {
00255          $files =  &$paperFileDao->getPaperFileRevisions($fileId);
00256       }
00257 
00258       foreach ($files as $f) {
00259          parent::deleteFile($this->filesDir . $f->getType() . '/' . $f->getFileName());
00260       }
00261 
00262       $paperFileDao->deletePaperFileById($fileId, $revision);
00263 
00264       return count($files);
00265    }
00266 
00270    function deletePaperTree() {
00271       parent::rmtree($this->filesDir);
00272    }
00273 
00281    function downloadFile($fileId, $revision = null, $inline = false) {
00282       $paperFile =& $this->getFile($fileId, $revision);
00283       if (isset($paperFile)) {
00284          $fileType = $paperFile->getFileType();
00285          $filePath = $this->filesDir . $paperFile->getType() . '/' . $paperFile->getFileName();
00286 
00287          return parent::downloadFile($filePath, $fileType, $inline);
00288 
00289       } else {
00290          return false;
00291       }
00292    }
00293 
00298    function viewFile($fileId, $revision = null) {
00299       $this->downloadFile($fileId, $revision, true);
00300    }
00301 
00309    function copyToReviewFile($fileId, $revision = null, $destFileId = null) {
00310       return $this->copyAndRenameFile($fileId, $revision, PAPER_FILE_REVIEW, $destFileId);
00311    }
00312 
00320    function copyToDirectorFile($fileId, $revision = null, $destFileId = null) {
00321       return $this->copyAndRenameFile($fileId, $revision, PAPER_FILE_DIRECTOR, $destFileId);
00322    }
00323 
00330    function copyToLayoutFile($fileId, $revision = null) {
00331       return $this->copyAndRenameFile($fileId, $revision, PAPER_FILE_LAYOUT);
00332    }
00333 
00339    function typeToPath($type) {
00340       switch ($type) {
00341          case PAPER_FILE_PUBLIC: return 'public';
00342          case PAPER_FILE_SUPP: return 'supp';
00343          case PAPER_FILE_NOTE: return 'note';
00344          case PAPER_FILE_REVIEW: return 'submission/review';
00345          case PAPER_FILE_DIRECTOR: return 'submission/director';
00346          case PAPER_FILE_LAYOUT: return 'submission/layout';
00347          case PAPER_FILE_SUBMISSION: default: return 'submission/original';
00348       }
00349    }
00350 
00358    function copyAndRenameFile($sourceFileId, $sourceRevision, $destType, $destFileId = null) {
00359       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00360       $paperFile = new PaperFile();
00361 
00362       $destTypePath = $this->typeToPath($destType);
00363       $destDir = $this->filesDir . $destTypePath . '/';
00364 
00365       if ($destFileId != null) {
00366          $currentRevision = $paperFileDao->getRevisionNumber($destFileId);
00367          $revision = $currentRevision + 1;
00368       } else {
00369          $revision = 1;
00370       }  
00371 
00372       $sourcePaperFile = $paperFileDao->getPaperFile($sourceFileId, $sourceRevision, $this->paperId);
00373 
00374       if (!isset($sourcePaperFile)) {
00375          return false;
00376       }
00377 
00378       $sourceDir = $this->filesDir . $sourcePaperFile->getType() . '/';
00379 
00380       if ($destFileId != null) {
00381          $paperFile->setFileId($destFileId);
00382       }
00383       $paperFile->setPaperId($this->paperId);
00384       $paperFile->setFileName($sourcePaperFile->getFileName());
00385       $paperFile->setFileType($sourcePaperFile->getFileType());
00386       $paperFile->setFileSize($sourcePaperFile->getFileSize());
00387       $paperFile->setOriginalFileName(PaperFileManager::truncateFileName($sourcePaperFile->getFileName(), 127));
00388       $paperFile->setType($destTypePath);
00389       $paperFile->setDateUploaded(Core::getCurrentDate());
00390       $paperFile->setDateModified(Core::getCurrentDate());
00391       $paperFile->setStage($this->paper->getCurrentStage());
00392       $paperFile->setRevision($revision);
00393 
00394       $fileId = $paperFileDao->insertPaperFile($paperFile);
00395 
00396       // Rename the file.
00397       $fileExtension = $this->parseFileExtension($sourcePaperFile->getFileName());
00398       $newFileName = $this->paperId.'-'.$fileId.'-'.$revision.'-'.$destType.'.'.$fileExtension;
00399 
00400       if (!$this->fileExists($destDir, 'dir')) {
00401          // Try to create destination directory
00402          $this->mkdirtree($destDir);
00403       }
00404 
00405       copy($sourceDir.$sourcePaperFile->getFileName(), $destDir.$newFileName);
00406 
00407       $paperFile->setFileName($newFileName);
00408       $paperFileDao->updatePaperFile($paperFile);
00409 
00410       return $fileId;
00411    }
00412 
00418    function &generateDummyFile(&$paper) {
00419       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00420       $paperFile = new PaperFile();
00421       $paperFile->setPaperId($paper->getId());
00422       $paperFile->setFileName('temp');
00423       $paperFile->setOriginalFileName('temp');
00424       $paperFile->setFileType('temp');
00425       $paperFile->setFileSize(0);
00426       $paperFile->setType('temp');
00427       $paperFile->setDateUploaded(Core::getCurrentDate());
00428       $paperFile->setDateModified(Core::getCurrentDate());
00429       $paperFile->setStage(0);
00430       $paperFile->setRevision(1);
00431 
00432       $paperFile->setFileId($paperFileDao->insertPaperFile($paperFile));
00433 
00434       return $paperFile;
00435    }
00436 
00440    function removePriorRevisions($fileId, $revision) {
00441       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00442       $revisions = $paperFileDao->getPaperFileRevisions($fileId);
00443       foreach ($revisions as $revisionFile) {
00444          if ($revisionFile->getRevision() != $revision) {
00445             $this->deleteFile($fileId, $revisionFile->getRevision());
00446          }
00447       }
00448    }
00449 
00457    function generateFilename(&$paperFile, $type, $originalName) {
00458       $extension = $this->parseFileExtension($originalName);         
00459       $newFileName = $paperFile->getPaperId().'-'.$paperFile->getFileId().'-'.$paperFile->getRevision().'-'.$type.'.'.$extension;
00460       $paperFile->setFileName($newFileName);
00461       return $newFileName;
00462    }
00463 
00472    function handleUpload($fileName, $type, $fileId = null, $overwrite = false) {
00473       if ($this->uploadError($fileName)) return false;
00474 
00475       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00476 
00477       $typePath = $this->typeToPath($type);
00478       $dir = $this->filesDir . $typePath . '/';
00479 
00480       if (!$fileId) {
00481          // Insert dummy file to generate file id FIXME?
00482          $dummyFile = true;
00483          $paperFile =& $this->generateDummyFile($this->paper);
00484       } else {
00485          $dummyFile = false;
00486          $paperFile = new PaperFile();
00487          $paperFile->setRevision($paperFileDao->getRevisionNumber($fileId)+1);
00488          $paperFile->setPaperId($this->paperId);
00489          $paperFile->setFileId($fileId);
00490          $paperFile->setDateUploaded(Core::getCurrentDate());
00491          $paperFile->setDateModified(Core::getCurrentDate());
00492       }
00493 
00494       $paperFile->setFileType($this->getUploadedFileType($fileName));
00495       $paperFile->setFileSize($_FILES[$fileName]['size']);
00496       $paperFile->setOriginalFileName(PaperFileManager::truncateFileName($_FILES[$fileName]['name'], 127));
00497       $paperFile->setType($typePath);
00498       $paperFile->setStage($this->paper->getCurrentStage());
00499 
00500       $newFileName = $this->generateFilename($paperFile, $type, $this->getUploadedFileName($fileName));
00501 
00502       if (!$this->uploadFile($fileName, $dir.$newFileName)) {
00503          // Delete the dummy file we inserted
00504          $paperFileDao->deletePaperFileById($paperFile->getFileId());
00505 
00506          return false;
00507       }
00508 
00509       if ($dummyFile) $paperFileDao->updatePaperFile($paperFile);
00510       else $paperFileDao->insertPaperFile($paperFile);
00511 
00512       if ($overwrite) $this->removePriorRevisions($paperFile->getFileId(), $paperFile->getRevision());
00513 
00514       return $paperFile->getFileId();
00515    }
00516 
00527    function handleWrite($fileName, &$contents, $mimeType, $type, $fileId = null, $overwrite = false) {
00528       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00529 
00530       $typePath = $this->typeToPath($type);
00531       $dir = $this->filesDir . $typePath . '/';
00532 
00533       if (!$fileId) {
00534          // Insert dummy file to generate file id FIXME?
00535          $dummyFile = true;
00536          $paperFile =& $this->generateDummyFile($this->paper);
00537       } else {
00538          $dummyFile = false;
00539          $paperFile = new PaperFile();
00540          $paperFile->setRevision($paperFileDao->getRevisionNumber($fileId)+1);
00541          $paperFile->setPaperId($this->paperId);
00542          $paperFile->setFileId($fileId);
00543          $paperFile->setDateUploaded(Core::getCurrentDate());
00544          $paperFile->setDateModified(Core::getCurrentDate());
00545       }
00546 
00547       $paperFile->setFileType($mimeType);
00548       $paperFile->setFileSize(strlen($contents));
00549       $paperFile->setOriginalFileName(PaperFileManager::truncateFileName($fileName, 127));
00550       $paperFile->setType($typePath);
00551       $paperFile->setStage($this->paper->getCurrentStage());
00552 
00553       $newFileName = $this->generateFilename($paperFile, $type, $fileName);
00554 
00555       if (!$this->writeFile($dir.$newFileName, $contents)) {
00556          // Delete the dummy file we inserted
00557          $paperFileDao->deletePaperFileById($paperFile->getFileId());
00558 
00559          return false;
00560       }
00561 
00562       if ($dummyFile) $paperFileDao->updatePaperFile($paperFile);
00563       else $paperFileDao->insertPaperFile($paperFile);
00564 
00565       if ($overwrite) $this->removePriorRevisions($paperFile->getFileId(), $paperFile->getRevision());
00566 
00567       return $paperFile->getFileId();
00568    }
00569 
00579    function handleCopy($url, $mimeType, $type, $fileId = null, $overwrite = false) {
00580       $paperFileDao =& DAORegistry::getDAO('PaperFileDAO');
00581 
00582       $typePath = $this->typeToPath($type);
00583       $dir = $this->filesDir . $typePath . '/';
00584 
00585       if (!$fileId) {
00586          // Insert dummy file to generate file id FIXME?
00587          $dummyFile = true;
00588          $paperFile =& $this->generateDummyFile($this->paper);
00589       } else {
00590          $dummyFile = false;
00591          $paperFile = new PaperFile();
00592          $paperFile->setRevision($paperFileDao->getRevisionNumber($fileId)+1);
00593          $paperFile->setPaperId($this->paperId);
00594          $paperFile->setFileId($fileId);
00595          $paperFile->setDateUploaded(Core::getCurrentDate());
00596          $paperFile->setDateModified(Core::getCurrentDate());
00597       }
00598 
00599       $paperFile->setFileType($mimeType);
00600       $paperFile->setOriginalFileName(PaperFileManager::truncateFileName(basename($url), 127));
00601       $paperFile->setType($typePath);
00602       $paperFile->setStage($this->paper->getCurrentStage());
00603 
00604       $newFileName = $this->generateFilename($paperFile, $type, $paperFile->getOriginalFileName());
00605 
00606       if (!$this->copyFile($url, $dir.$newFileName)) {
00607          // Delete the dummy file we inserted
00608          $paperFileDao->deletePaperFileById($paperFile->getFileId());
00609 
00610          return false;
00611       }
00612 
00613       $paperFile->setFileSize(filesize($dir.$newFileName));
00614 
00615       if ($dummyFile) $paperFileDao->updatePaperFile($paperFile);
00616       else $paperFileDao->insertPaperFile($paperFile);
00617 
00618       if ($overwrite) $this->removePriorRevisions($paperFile->getFileId(), $paperFile->getRevision());
00619 
00620       return $paperFile->getFileId();
00621    }
00622 }
00623 
00624 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7