Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
ArticleFileDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.file.PKPFileDAO');
18 import('classes.article.ArticleFile');
19 
20 
21 class ArticleFileDAO extends PKPFileDAO {
22 
30  function &getArticleFile($fileId, $revision = null, $articleId = null) {
31  if ($fileId === null) {
32  $returner = null;
33  return $returner;
34  }
35  if ($revision == null) {
36  if ($articleId != null) {
37  $result = $this->retrieveLimit(
38  'SELECT a.* FROM submission_files a WHERE file_id = ? AND submission_id = ? ORDER BY revision DESC',
39  array($fileId, $articleId),
40  1
41  );
42  } else {
43  $result = $this->retrieveLimit(
44  'SELECT a.* FROM submission_files a WHERE file_id = ? ORDER BY revision DESC',
45  $fileId,
46  1
47  );
48  }
49 
50  } else {
51  if ($articleId != null) {
52  $result = $this->retrieve(
53  'SELECT a.* FROM submission_files a WHERE file_id = ? AND revision = ? AND submission_id = ?',
54  array($fileId, $revision, $articleId)
55  );
56  } else {
57  $result = $this->retrieve(
58  'SELECT a.* FROM submission_files a WHERE file_id = ? AND revision = ?',
59  array($fileId, $revision)
60  );
61  }
62  }
63 
64  $returner = null;
65  if (isset($result) && $result->RecordCount() != 0) {
66  $returner = $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
67  }
68 
69  $result->Close();
70  return $returner;
71  }
72 
78  function &getArticleFileRevisions($fileId, $round = null) {
79  if ($fileId === null) {
80  $returner = null;
81  return $returner;
82  }
83  $articleFiles = array();
84 
85  // FIXME If "round" is review-specific, it shouldn't be in this table
86  if ($round == null) {
87  $result = $this->retrieve(
88  'SELECT a.* FROM submission_files a WHERE file_id = ? ORDER BY revision',
89  $fileId
90  );
91  } else {
92  $result = $this->retrieve(
93  'SELECT a.* FROM submission_files a WHERE file_id = ? AND round = ? ORDER BY revision',
94  array($fileId, $round)
95  );
96  }
97 
98  while (!$result->EOF) {
99  $articleFiles[] = $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
100  $result->MoveNext();
101  }
102 
103  $result->Close();
104  return $articleFiles;
105  }
106 
112  function &getArticleFileRevisionsInRange($fileId, $start = 1, $end = null) {
113  if ($fileId === null) {
114  $returner = null;
115  return $returner;
116  }
117  $articleFiles = array();
118 
119  if ($end == null) {
120  $result = $this->retrieve(
121  'SELECT a.* FROM submission_files a WHERE file_id = ? AND revision >= ?',
122  array($fileId, $start)
123  );
124  } else {
125  $result = $this->retrieve(
126  'SELECT a.* FROM submission_files a WHERE file_id = ? AND revision >= ? AND revision <= ?',
127  array($fileId, $start, $end)
128  );
129  }
130 
131  while (!$result->EOF) {
132  $articleFiles[] = $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
133  $result->MoveNext();
134  }
135 
136  $result->Close();
137  return $articleFiles;
138  }
139 
145  function &getRevisionNumber($fileId) {
146  if ($fileId === null) {
147  $returner = null;
148  return $returner;
149  }
150  $result = $this->retrieve(
151  'SELECT MAX(revision) AS max_revision FROM submission_files a WHERE file_id = ?',
152  $fileId
153  );
154 
155  if ($result->RecordCount() == 0) {
156  $returner = null;
157  } else {
158  $row = $result->FetchRow();
159  $returner = $row['max_revision'];
160  }
161 
162  $result->Close();
163  return $returner;
164  }
165 
171  function &getArticleFilesByArticle($articleId) {
172  $articleFiles = array();
173 
174  $result = $this->retrieve(
175  'SELECT * FROM submission_files WHERE submission_id = ?',
176  $articleId
177  );
178 
179  while (!$result->EOF) {
180  $articleFiles[] = $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
181  $result->MoveNext();
182  }
183 
184  $result->Close();
185  return $articleFiles;
186  }
187 
194  function &getArticleFilesByAssocId($assocId, $fileStage) {
195  import('classes.file.ArticleFileManager');
196  $articleFiles = array();
197 
198  $result = $this->retrieve(
199  'SELECT * FROM submission_files WHERE assoc_id = ? AND file_stage = ?',
200  array($assocId, $fileStage)
201  );
202 
203  while (!$result->EOF) {
204  $articleFiles[] = $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
205  $result->MoveNext();
206  }
207 
208  $result->Close();
209  return $articleFiles;
210  }
211 
217  function &_returnArticleFileFromRow($row) {
218  $articleFile = new ArticleFile();
219  $articleFile->setFileId($row['file_id']);
220  $articleFile->setSourceFileId($row['source_file_id']);
221  $articleFile->setSourceRevision($row['source_revision']);
222  $articleFile->setRevision($row['revision']);
223  $articleFile->setArticleId($row['submission_id']);
224  $articleFile->setFileName($row['file_name']);
225  $articleFile->setFileType($row['file_type']);
226  $articleFile->setFileSize($row['file_size']);
227  $articleFile->setOriginalFileName($row['original_file_name']);
228  $articleFile->setFileStage($row['file_stage']);
229  $articleFile->setAssocId($row['assoc_id']);
230  $articleFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
231  $articleFile->setDateModified($this->datetimeFromDB($row['date_modified']));
232  $articleFile->setRound($row['round']);
233  $articleFile->setViewable($row['viewable']);
234  HookRegistry::call('ArticleFileDAO::_returnArticleFileFromRow', array(&$articleFile, &$row));
235  return $articleFile;
236  }
237 
243  function insertArticleFile(&$articleFile) {
244  $fileId = $articleFile->getFileId();
245  $params = array(
246  $articleFile->getRevision() === null ? 1 : $articleFile->getRevision(),
247  (int) $articleFile->getArticleId(),
248  $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
249  $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
250  $articleFile->getFileName(),
251  $articleFile->getFileType(),
252  $articleFile->getFileSize(),
253  $articleFile->getOriginalFileName(),
254  $articleFile->getFileStage(),
255  (int) $articleFile->getRound(),
256  $articleFile->getViewable(),
257  $articleFile->getAssocId()
258  );
259 
260  if ($fileId) {
261  array_unshift($params, $fileId);
262  }
263 
264  $this->update(
265  sprintf('INSERT INTO submission_files
266  (' . ($fileId ? 'file_id, ' : '') . 'revision, submission_id, source_file_id, source_revision, file_name, file_type, file_size, original_file_name, file_stage, date_uploaded, date_modified, round, viewable, assoc_id)
267  VALUES
268  (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?)',
269  $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
270  $params
271  );
272 
273  if (!$fileId) {
274  $articleFile->setFileId($this->getInsertId());
275  }
276 
277  return $articleFile->getFileId();
278  }
279 
284  function updateArticleFile(&$articleFile) {
285  $this->update(
286  sprintf('UPDATE submission_files
287  SET
288  submission_id = ?,
289  source_file_id = ?,
290  source_revision = ?,
291  file_name = ?,
292  file_type = ?,
293  file_size = ?,
294  original_file_name = ?,
295  file_stage = ?,
296  date_uploaded = %s,
297  date_modified = %s,
298  round = ?,
299  viewable = ?,
300  assoc_id = ?
301  WHERE file_id = ? AND revision = ?',
302  $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
303  array(
304  (int) $articleFile->getArticleId(),
305  $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
306  $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
307  $articleFile->getFileName(),
308  $articleFile->getFileType(),
309  $articleFile->getFileSize(),
310  $articleFile->getOriginalFileName(),
311  $articleFile->getFileStage(),
312  (int) $articleFile->getRound(),
313  $articleFile->getViewable(),
314  $articleFile->getAssocId(),
315  $articleFile->getFileId(),
316  $articleFile->getRevision()
317  )
318  );
319 
320  return $articleFile->getFileId();
321 
322  }
323 
328  function deleteArticleFile(&$articleFile) {
329  return $this->deleteArticleFileById($articleFile->getFileId(), $articleFile->getRevision());
330  }
331 
337  function deleteArticleFileById($fileId, $revision = null) {
338  if ($revision == null) {
339  return $this->update(
340  'DELETE FROM submission_files WHERE file_id = ?', $fileId
341  );
342  } else {
343  return $this->update(
344  'DELETE FROM submission_files WHERE file_id = ? AND revision = ?', array($fileId, $revision)
345  );
346  }
347  }
348 
353  function deleteArticleFiles($articleId) {
354  return $this->update(
355  'DELETE FROM submission_files WHERE submission_id = ?', $articleId
356  );
357  }
358 
363  function getInsertId() {
364  return $this->_getInsertId('submission_files', 'file_id');
365  }
366 }
367 
368 ?>
& retrieveLimit($sql, $params=false, $numRows=false, $offset=false, $callHooks=true)
Definition: DAO.inc.php:146
insertArticleFile(&$articleFile)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
& getArticleFile($fileId, $revision=null, $articleId=null)
& getArticleFilesByAssocId($assocId, $fileStage)
& getArticleFileRevisionsInRange($fileId, $start=1, $end=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
Operations for retrieving and modifying ArticleFile objects.
& _returnArticleFileFromRow($row)
Abstract base class for retrieving and modifying PKPFile objects and their decendents.
updateArticleFile(&$articleFile)
& getArticleFilesByArticle($articleId)
deleteArticleFileById($fileId, $revision=null)
deleteArticleFile(&$articleFile)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
& getRevisionNumber($fileId)
deleteArticleFiles($articleId)
Article file class.
& getArticleFileRevisions($fileId, $round=null)