00001 <?php
00002
00016
00017
00018 import('paper.SuppFile');
00019
00020 class SuppFileDAO extends DAO {
00027 function &getSuppFile($suppFileId, $paperId = null) {
00028 $params = array($suppFileId);
00029 if ($paperId) $params[] = $paperId;
00030
00031 $result =& $this->retrieve(
00032 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM paper_supplementary_files s LEFT JOIN paper_files a ON (s.file_id = a.file_id) WHERE s.supp_id = ?' . ($paperId?' AND s.paper_id = ?':''),
00033 $params
00034 );
00035
00036 $returner = null;
00037 if ($result->RecordCount() != 0) {
00038 $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
00039 }
00040
00041 $result->Close();
00042 unset($result);
00043
00044 return $returner;
00045 }
00046
00053 function &getSuppFileByPublicSuppFileId($publicSuppId, $paperId) {
00054 $result =& $this->retrieve(
00055 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM paper_supplementary_files s LEFT JOIN paper_files a ON (s.file_id = a.file_id) WHERE s.public_supp_file_id = ? AND s.paper_id = ?',
00056 array($publicSuppId, $paperId)
00057 );
00058
00059 $returner = null;
00060 if ($result->RecordCount() != 0) {
00061 $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
00062 }
00063
00064 $result->Close();
00065 unset($result);
00066
00067 return $returner;
00068 }
00069
00075 function &getSuppFilesByPaper($paperId) {
00076 $suppFiles = array();
00077
00078 $result =& $this->retrieve(
00079 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM paper_supplementary_files s LEFT JOIN paper_files a ON (s.file_id = a.file_id) WHERE s.paper_id = ? ORDER BY s.seq',
00080 $paperId
00081 );
00082
00083 while (!$result->EOF) {
00084 $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
00085 $result->moveNext();
00086 }
00087
00088 $result->Close();
00089 unset($result);
00090
00091 return $suppFiles;
00092 }
00093
00098 function getLocaleFieldNames() {
00099 return array('title', 'creator', 'subject', 'typeOther', 'description', 'publisher', 'sponsor', 'source');
00100 }
00101
00106 function updateLocaleFields(&$suppFile) {
00107 $this->updateDataObjectSettings('paper_supp_file_settings', $suppFile, array(
00108 'supp_id' => $suppFile->getId()
00109 ));
00110 }
00111
00117 function &_returnSuppFileFromRow(&$row) {
00118 $suppFile = new SuppFile();
00119 $suppFile->setSuppFileID($row['supp_id']);
00120 $suppFile->setPublicSuppFileID($row['public_supp_file_id']);
00121 $suppFile->setFileId($row['file_id']);
00122 $suppFile->setPaperId($row['paper_id']);
00123 $suppFile->setType($row['type']);
00124 $suppFile->setDateCreated($this->dateFromDB($row['date_created']));
00125 $suppFile->setLanguage($row['language']);
00126 $suppFile->setShowReviewers($row['show_reviewers']);
00127 $suppFile->setDateSubmitted($this->datetimeFromDB($row['date_submitted']));
00128 $suppFile->setSequence($row['seq']);
00129
00130
00131 $suppFile->setFileName($row['file_name']);
00132 $suppFile->setOriginalFileName($row['original_file_name']);
00133 $suppFile->setFileType($row['file_type']);
00134 $suppFile->setFileSize($row['file_size']);
00135 $suppFile->setDateModified($this->datetimeFromDB($row['date_modified']));
00136 $suppFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
00137
00138 $this->getDataObjectSettings('paper_supp_file_settings', 'supp_id', $row['supp_id'], $suppFile);
00139
00140 HookRegistry::call('SuppFileDAO::_returnSuppFileFromRow', array(&$suppFile, &$row));
00141
00142 return $suppFile;
00143 }
00144
00149 function insertSuppFile(&$suppFile) {
00150 if ($suppFile->getDateSubmitted() == null) {
00151 $suppFile->setDateSubmitted(Core::getCurrentDate());
00152 }
00153 if ($suppFile->getSequence() == null) {
00154 $suppFile->setSequence($this->getNextSuppFileSequence($suppFile->getPaperId()));
00155 }
00156 $this->update(
00157 sprintf('INSERT INTO paper_supplementary_files
00158 (public_supp_file_id, file_id, paper_id, type, date_created, language, show_reviewers, date_submitted, seq)
00159 VALUES
00160 (?, ?, ?, ?, %s, ?, ?, %s, ?)',
00161 $this->dateToDB($suppFile->getDateCreated()), $this->datetimeToDB($suppFile->getDateSubmitted())),
00162 array(
00163 $suppFile->getPublicSuppFileId(),
00164 $suppFile->getFileId(),
00165 $suppFile->getPaperId(),
00166 $suppFile->getType(),
00167 $suppFile->getLanguage(),
00168 $suppFile->getShowReviewers(),
00169 $suppFile->getSequence()
00170 )
00171 );
00172 $suppFile->setId($this->getInsertSuppFileId());
00173 $this->updateLocaleFields($suppFile);
00174 return $suppFile->getId();
00175 }
00176
00181 function updateSuppFile(&$suppFile) {
00182 $returner = $this->update(
00183 sprintf('UPDATE paper_supplementary_files
00184 SET
00185 public_supp_file_id = ?,
00186 file_id = ?,
00187 type = ?,
00188 date_created = %s,
00189 language = ?,
00190 show_reviewers = ?,
00191 seq = ?
00192 WHERE supp_id = ?',
00193 $this->dateToDB($suppFile->getDateCreated())),
00194 array(
00195 $suppFile->getPublicSuppFileId(),
00196 $suppFile->getFileId(),
00197 $suppFile->getType(),
00198 $suppFile->getLanguage(),
00199 $suppFile->getShowReviewers(),
00200 $suppFile->getSequence(),
00201 $suppFile->getId()
00202 )
00203 );
00204 $this->updateLocaleFields($suppFile);
00205 return $returner;
00206 }
00207
00212 function deleteSuppFile(&$suppFile) {
00213 return $this->deleteSuppFileById($suppFile->getId());
00214 }
00215
00221 function deleteSuppFileById($suppFileId, $paperId = null) {
00222 if (isset($paperId)) {
00223 $returner = $this->update('DELETE FROM paper_supplementary_files WHERE supp_id = ? AND paper_id = ?', array($suppFileId, $paperId));
00224 if ($returner) $this->update('DELETE FROM paper_supp_file_settings WHERE supp_id = ?', $suppFileId);
00225 return $returner;
00226
00227 } else {
00228 $this->update('DELETE FROM paper_supp_file_settings WHERE supp_id = ?', $suppFileId);
00229 return $this->update(
00230 'DELETE FROM paper_supplementary_files WHERE supp_id = ?', $suppFileId
00231 );
00232 }
00233 }
00234
00239 function deleteSuppFilesByPaper($paperId) {
00240 $suppFiles =& $this->getSuppFilesByPaper($paperId);
00241 foreach ($suppFiles as $suppFile) {
00242 $this->deleteSuppFile($suppFile);
00243 }
00244 }
00245
00252 function suppFileExistsByFileId($paperId, $fileId) {
00253 $result =& $this->retrieve(
00254 'SELECT COUNT(*) FROM paper_supplementary_files
00255 WHERE paper_id = ? AND file_id = ?',
00256 array($paperId, $fileId)
00257 );
00258
00259 $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
00260
00261 $result->Close();
00262 unset($result);
00263
00264 return $returner;
00265 }
00266
00271 function resequenceSuppFiles($paperId) {
00272 $result =& $this->retrieve(
00273 'SELECT supp_id FROM paper_supplementary_files WHERE paper_id = ? ORDER BY seq',
00274 $paperId
00275 );
00276
00277 for ($i=1; !$result->EOF; $i++) {
00278 list($suppId) = $result->fields;
00279 $this->update(
00280 'UPDATE paper_supplementary_files SET seq = ? WHERE supp_id = ?',
00281 array($i, $suppId)
00282 );
00283 $result->moveNext();
00284 }
00285
00286 $result->close();
00287 unset($result);
00288 }
00289
00295 function getNextSuppFileSequence($paperId) {
00296 $result =& $this->retrieve(
00297 'SELECT MAX(seq) + 1 FROM paper_supplementary_files WHERE paper_id = ?',
00298 $paperId
00299 );
00300 $returner = floor($result->fields[0]);
00301
00302 $result->Close();
00303 unset($result);
00304
00305 return $returner;
00306 }
00307
00312 function getInsertSuppFileId() {
00313 return $this->getInsertId('paper_supplementary_files', 'supp_id');
00314 }
00315
00323 function &getSuppFileByBestSuppFileId($paperId, $suppId) {
00324 $suppFile =& $this->getSuppFileByPublicSuppFileId($suppId, $paperId);
00325 if (!isset($suppFile)) $suppFile =& $this->getSuppFile((int) $suppId, $paperId);
00326 return $suppFile;
00327 }
00328
00336 function suppFileExistsByPublicId($publicSuppFileId, $suppId, $schedConfId) {
00337 $result =& $this->retrieve(
00338 'SELECT COUNT(*) FROM paper_supplementary_files f, papers a WHERE f.paper_id = a.paper_id AND f.public_supp_file_id = ? AND f.supp_id <> ? AND a.sched_conf_id = ?', array($publicSuppFileId, $suppId, $schedConfId)
00339 );
00340 $returner = $result->fields[0] ? true : false;
00341
00342 $result->Close();
00343 unset($result);
00344
00345 return $returner;
00346 }
00347 }
00348
00349 ?>