00001 <?php
00002
00016
00017
00018
00019 import('article.ArticleFile');
00020
00021 define('INLINEABLE_TYPES_FILE', Config::getVar('general', 'registry_dir') . DIRECTORY_SEPARATOR . 'inlineTypes.txt');
00022
00023 class ArticleFileDAO extends DAO {
00027 var $inlineableTypes;
00028
00036 function &getArticleFile($fileId, $revision = null, $articleId = null) {
00037 if ($fileId === null) {
00038 $returner = null;
00039 return $returner;
00040 }
00041 if ($revision == null) {
00042 if ($articleId != null) {
00043 $result = &$this->retrieveLimit(
00044 'SELECT a.* FROM article_files a WHERE file_id = ? AND article_id = ? ORDER BY revision DESC',
00045 array($fileId, $articleId),
00046 1
00047 );
00048 } else {
00049 $result = &$this->retrieveLimit(
00050 'SELECT a.* FROM article_files a WHERE file_id = ? ORDER BY revision DESC',
00051 $fileId,
00052 1
00053 );
00054 }
00055
00056 } else {
00057 if ($articleId != null) {
00058 $result = &$this->retrieve(
00059 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision = ? AND article_id = ?',
00060 array($fileId, $revision, $articleId)
00061 );
00062 } else {
00063 $result = &$this->retrieve(
00064 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision = ?',
00065 array($fileId, $revision)
00066 );
00067 }
00068 }
00069
00070 $returner = null;
00071 if (isset($result) && $result->RecordCount() != 0) {
00072 $returner = &$this->_returnArticleFileFromRow($result->GetRowAssoc(false));
00073 }
00074
00075 $result->Close();
00076 unset($result);
00077
00078 return $returner;
00079 }
00080
00086 function &getArticleFileRevisions($fileId, $round = null) {
00087 if ($fileId === null) {
00088 $returner = null;
00089 return $returner;
00090 }
00091 $articleFiles = array();
00092
00093
00094 if ($round == null) {
00095 $result = &$this->retrieve(
00096 'SELECT a.* FROM article_files a WHERE file_id = ? ORDER BY revision',
00097 $fileId
00098 );
00099 } else {
00100 $result = &$this->retrieve(
00101 'SELECT a.* FROM article_files a WHERE file_id = ? AND round = ? ORDER BY revision',
00102 array($fileId, $round)
00103 );
00104 }
00105
00106 while (!$result->EOF) {
00107 $articleFiles[] = &$this->_returnArticleFileFromRow($result->GetRowAssoc(false));
00108 $result->moveNext();
00109 }
00110
00111 $result->Close();
00112 unset($result);
00113
00114 return $articleFiles;
00115 }
00116
00122 function &getArticleFileRevisionsInRange($fileId, $start = 1, $end = null) {
00123 if ($fileId === null) {
00124 $returner = null;
00125 return $returner;
00126 }
00127 $articleFiles = array();
00128
00129 if ($end == null) {
00130 $result = &$this->retrieve(
00131 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ?',
00132 array($fileId, $start)
00133 );
00134 } else {
00135 $result = &$this->retrieve(
00136 'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ? AND revision <= ?',
00137 array($fileId, $start, $end)
00138 );
00139 }
00140
00141 while (!$result->EOF) {
00142 $articleFiles[] = &$this->_returnArticleFileFromRow($result->GetRowAssoc(false));
00143 $result->moveNext();
00144 }
00145
00146 $result->Close();
00147 unset($result);
00148
00149 return $articleFiles;
00150 }
00151
00157 function &getRevisionNumber($fileId) {
00158 if ($fileId === null) {
00159 $returner = null;
00160 return $returner;
00161 }
00162 $result = &$this->retrieve(
00163 'SELECT MAX(revision) AS max_revision FROM article_files a WHERE file_id = ?',
00164 $fileId
00165 );
00166
00167 if ($result->RecordCount() == 0) {
00168 $returner = null;
00169 } else {
00170 $row = $result->FetchRow();
00171 $returner = $row['max_revision'];
00172 }
00173
00174 $result->Close();
00175 unset($result);
00176
00177 return $returner;
00178 }
00179
00185 function &getArticleFilesByArticle($articleId) {
00186 $articleFiles = array();
00187
00188 $result = &$this->retrieve(
00189 'SELECT * FROM article_files WHERE article_id = ?',
00190 $articleId
00191 );
00192
00193 while (!$result->EOF) {
00194 $articleFiles[] = &$this->_returnArticleFileFromRow($result->GetRowAssoc(false));
00195 $result->moveNext();
00196 }
00197
00198 $result->Close();
00199 unset($result);
00200
00201 return $articleFiles;
00202 }
00203
00210 function &getArticleFilesByAssocId($assocId, $type) {
00211 import('file.ArticleFileManager');
00212 $articleFiles = array();
00213
00214 $result = &$this->retrieve(
00215 'SELECT * FROM article_files WHERE assoc_id = ? AND type = ?',
00216 array($assocId, ArticleFileManager::typeToPath($type))
00217 );
00218
00219 while (!$result->EOF) {
00220 $articleFiles[] = &$this->_returnArticleFileFromRow($result->GetRowAssoc(false));
00221 $result->moveNext();
00222 }
00223
00224 $result->Close();
00225 unset($result);
00226
00227 return $articleFiles;
00228 }
00229
00235 function &_returnArticleFileFromRow(&$row) {
00236 $articleFile = &new ArticleFile();
00237 $articleFile->setFileId($row['file_id']);
00238 $articleFile->setSourceFileId($row['source_file_id']);
00239 $articleFile->setSourceRevision($row['source_revision']);
00240 $articleFile->setRevision($row['revision']);
00241 $articleFile->setArticleId($row['article_id']);
00242 $articleFile->setFileName($row['file_name']);
00243 $articleFile->setFileType($row['file_type']);
00244 $articleFile->setFileSize($row['file_size']);
00245 $articleFile->setOriginalFileName($row['original_file_name']);
00246 $articleFile->setType($row['type']);
00247 $articleFile->setAssocId($row['assoc_id']);
00248 $articleFile->setStatus($row['status']);
00249 $articleFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
00250 $articleFile->setDateModified($this->datetimeFromDB($row['date_modified']));
00251 $articleFile->setRound($row['round']);
00252 $articleFile->setViewable($row['viewable']);
00253 HookRegistry::call('ArticleFileDAO::_returnArticleFileFromRow', array(&$articleFile, &$row));
00254 return $articleFile;
00255 }
00256
00262 function insertArticleFile(&$articleFile) {
00263 $fileId = $articleFile->getFileId();
00264 $params = array(
00265 $articleFile->getRevision() === null ? 1 : $articleFile->getRevision(),
00266 $articleFile->getArticleId(),
00267 $articleFile->getSourceFileId(),
00268 $articleFile->getSourceRevision(),
00269 $articleFile->getFileName(),
00270 $articleFile->getFileType(),
00271 $articleFile->getFileSize(),
00272 $articleFile->getOriginalFileName(),
00273 $articleFile->getType(),
00274 $articleFile->getStatus(),
00275 $articleFile->getRound(),
00276 $articleFile->getViewable(),
00277 $articleFile->getAssocId()
00278 );
00279
00280 if ($fileId) {
00281 array_unshift($params, $fileId);
00282 }
00283
00284 $this->update(
00285 sprintf('INSERT INTO article_files
00286 (' . ($fileId ? 'file_id, ' : '') . 'revision, article_id, source_file_id, source_revision, file_name, file_type, file_size, original_file_name, type, status, date_uploaded, date_modified, round, viewable, assoc_id)
00287 VALUES
00288 (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?)',
00289 $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
00290 $params
00291 );
00292
00293 if (!$fileId) {
00294 $articleFile->setFileId($this->getInsertArticleFileId());
00295 }
00296
00297 return $articleFile->getFileId();
00298 }
00299
00304 function updateArticleFile(&$articleFile) {
00305 $this->update(
00306 sprintf('UPDATE article_files
00307 SET
00308 article_id = ?,
00309 source_file_id = ?,
00310 source_revision = ?,
00311 file_name = ?,
00312 file_type = ?,
00313 file_size = ?,
00314 original_file_name = ?,
00315 type = ?,
00316 status = ?,
00317 date_uploaded = %s,
00318 date_modified = %s,
00319 round = ?,
00320 viewable = ?,
00321 assoc_id = ?
00322 WHERE file_id = ? AND revision = ?',
00323 $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
00324 array(
00325 $articleFile->getArticleId(),
00326 $articleFile->getSourceFileId(),
00327 $articleFile->getSourceRevision(),
00328 $articleFile->getFileName(),
00329 $articleFile->getFileType(),
00330 $articleFile->getFileSize(),
00331 $articleFile->getOriginalFileName(),
00332 $articleFile->getType(),
00333 $articleFile->getStatus(),
00334 $articleFile->getRound(),
00335 $articleFile->getViewable(),
00336 $articleFile->getAssocId(),
00337 $articleFile->getFileId(),
00338 $articleFile->getRevision()
00339 )
00340 );
00341
00342 return $articleFile->getFileId();
00343
00344 }
00345
00350 function deleteArticleFile(&$articleFile) {
00351 return $this->deleteArticleFileById($articleFile->getFileId(), $articleFile->getRevision());
00352 }
00353
00359 function deleteArticleFileById($fileId, $revision = null) {
00360 if ($revision == null) {
00361 return $this->update(
00362 'DELETE FROM article_files WHERE file_id = ?', $fileId
00363 );
00364 } else {
00365 return $this->update(
00366 'DELETE FROM article_files WHERE file_id = ? AND revision = ?', array($fileId, $revision)
00367 );
00368 }
00369 }
00370
00375 function deleteArticleFiles($articleId) {
00376 return $this->update(
00377 'DELETE FROM article_files WHERE article_id = ?', $articleId
00378 );
00379 }
00380
00385 function getInsertArticleFileId() {
00386 return $this->getInsertId('article_files', 'file_id');
00387 }
00388
00394 function isInlineable(&$articleFile) {
00395 if (!isset($this->inlineableTypes)) {
00396 $this->inlineableTypes = array_filter(file(INLINEABLE_TYPES_FILE), create_function('&$a', 'return ($a = trim($a)) && !empty($a) && $a[0] != \'#\';'));
00397 }
00398 return in_array($articleFile->getFileType(), $this->inlineableTypes);
00399 }
00400 }
00401
00402 ?>