Open Journal Systems  2.4.4
 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 article_files a WHERE file_id = ? AND article_id = ? ORDER BY revision DESC',
39  array($fileId, $articleId),
40  1
41  );
42  } else {
43  $result =& $this->retrieveLimit(
44  'SELECT a.* FROM article_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 article_files a WHERE file_id = ? AND revision = ? AND article_id = ?',
54  array($fileId, $revision, $articleId)
55  );
56  } else {
57  $result =& $this->retrieve(
58  'SELECT a.* FROM article_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  unset($result);
71 
72  return $returner;
73  }
74 
80  function &getArticleFileRevisions($fileId, $round = null) {
81  if ($fileId === null) {
82  $returner = null;
83  return $returner;
84  }
85  $articleFiles = array();
86 
87  // FIXME If "round" is review-specific, it shouldn't be in this table
88  if ($round == null) {
89  $result =& $this->retrieve(
90  'SELECT a.* FROM article_files a WHERE file_id = ? ORDER BY revision',
91  $fileId
92  );
93  } else {
94  $result =& $this->retrieve(
95  'SELECT a.* FROM article_files a WHERE file_id = ? AND round = ? ORDER BY revision',
96  array($fileId, $round)
97  );
98  }
99 
100  while (!$result->EOF) {
101  $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
102  $result->moveNext();
103  }
104 
105  $result->Close();
106  unset($result);
107 
108  return $articleFiles;
109  }
110 
116  function &getArticleFileRevisionsInRange($fileId, $start = 1, $end = null) {
117  if ($fileId === null) {
118  $returner = null;
119  return $returner;
120  }
121  $articleFiles = array();
122 
123  if ($end == null) {
124  $result =& $this->retrieve(
125  'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ?',
126  array($fileId, $start)
127  );
128  } else {
129  $result =& $this->retrieve(
130  'SELECT a.* FROM article_files a WHERE file_id = ? AND revision >= ? AND revision <= ?',
131  array($fileId, $start, $end)
132  );
133  }
134 
135  while (!$result->EOF) {
136  $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
137  $result->moveNext();
138  }
139 
140  $result->Close();
141  unset($result);
142 
143  return $articleFiles;
144  }
145 
151  function &getRevisionNumber($fileId) {
152  if ($fileId === null) {
153  $returner = null;
154  return $returner;
155  }
156  $result =& $this->retrieve(
157  'SELECT MAX(revision) AS max_revision FROM article_files a WHERE file_id = ?',
158  $fileId
159  );
160 
161  if ($result->RecordCount() == 0) {
162  $returner = null;
163  } else {
164  $row = $result->FetchRow();
165  $returner = $row['max_revision'];
166  }
167 
168  $result->Close();
169  unset($result);
170 
171  return $returner;
172  }
173 
179  function &getArticleFilesByArticle($articleId) {
180  $articleFiles = array();
181 
182  $result =& $this->retrieve(
183  'SELECT * FROM article_files WHERE article_id = ?',
184  $articleId
185  );
186 
187  while (!$result->EOF) {
188  $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
189  $result->moveNext();
190  }
191 
192  $result->Close();
193  unset($result);
194 
195  return $articleFiles;
196  }
197 
204  function &getArticleFilesByAssocId($assocId, $fileStage) {
205  import('classes.file.ArticleFileManager');
206  $articleFiles = array();
207 
208  $result =& $this->retrieve(
209  'SELECT * FROM article_files WHERE assoc_id = ? AND file_stage = ?',
210  array($assocId, $fileStage)
211  );
212 
213  while (!$result->EOF) {
214  $articleFiles[] =& $this->_returnArticleFileFromRow($result->GetRowAssoc(false));
215  $result->moveNext();
216  }
217 
218  $result->Close();
219  unset($result);
220 
221  return $articleFiles;
222  }
223 
229  function &_returnArticleFileFromRow(&$row) {
230  $articleFile = new ArticleFile();
231  $articleFile->setFileId($row['file_id']);
232  $articleFile->setSourceFileId($row['source_file_id']);
233  $articleFile->setSourceRevision($row['source_revision']);
234  $articleFile->setRevision($row['revision']);
235  $articleFile->setArticleId($row['article_id']);
236  $articleFile->setFileName($row['file_name']);
237  $articleFile->setFileType($row['file_type']);
238  $articleFile->setFileSize($row['file_size']);
239  $articleFile->setOriginalFileName($row['original_file_name']);
240  $articleFile->setFileStage($row['file_stage']);
241  $articleFile->setAssocId($row['assoc_id']);
242  $articleFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
243  $articleFile->setDateModified($this->datetimeFromDB($row['date_modified']));
244  $articleFile->setRound($row['round']);
245  $articleFile->setViewable($row['viewable']);
246  HookRegistry::call('ArticleFileDAO::_returnArticleFileFromRow', array(&$articleFile, &$row));
247  return $articleFile;
248  }
249 
255  function insertArticleFile(&$articleFile) {
256  $fileId = $articleFile->getFileId();
257  $params = array(
258  $articleFile->getRevision() === null ? 1 : $articleFile->getRevision(),
259  (int) $articleFile->getArticleId(),
260  $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
261  $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
262  $articleFile->getFileName(),
263  $articleFile->getFileType(),
264  $articleFile->getFileSize(),
265  $articleFile->getOriginalFileName(),
266  $articleFile->getFileStage(),
267  (int) $articleFile->getRound(),
268  $articleFile->getViewable(),
269  $articleFile->getAssocId()
270  );
271 
272  if ($fileId) {
273  array_unshift($params, $fileId);
274  }
275 
276  $this->update(
277  sprintf('INSERT INTO article_files
278  (' . ($fileId ? 'file_id, ' : '') . 'revision, article_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)
279  VALUES
280  (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?)',
281  $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
282  $params
283  );
284 
285  if (!$fileId) {
286  $articleFile->setFileId($this->getInsertArticleFileId());
287  }
288 
289  return $articleFile->getFileId();
290  }
291 
296  function updateArticleFile(&$articleFile) {
297  $this->update(
298  sprintf('UPDATE article_files
299  SET
300  article_id = ?,
301  source_file_id = ?,
302  source_revision = ?,
303  file_name = ?,
304  file_type = ?,
305  file_size = ?,
306  original_file_name = ?,
307  file_stage = ?,
308  date_uploaded = %s,
309  date_modified = %s,
310  round = ?,
311  viewable = ?,
312  assoc_id = ?
313  WHERE file_id = ? AND revision = ?',
314  $this->datetimeToDB($articleFile->getDateUploaded()), $this->datetimeToDB($articleFile->getDateModified())),
315  array(
316  (int) $articleFile->getArticleId(),
317  $articleFile->getSourceFileId()?$articleFile->getSourceFileId():null,
318  $articleFile->getSourceRevision()?$articleFile->getSourceRevision():null,
319  $articleFile->getFileName(),
320  $articleFile->getFileType(),
321  $articleFile->getFileSize(),
322  $articleFile->getOriginalFileName(),
323  $articleFile->getFileStage(),
324  (int) $articleFile->getRound(),
325  $articleFile->getViewable(),
326  $articleFile->getAssocId(),
327  $articleFile->getFileId(),
328  $articleFile->getRevision()
329  )
330  );
331 
332  return $articleFile->getFileId();
333 
334  }
335 
340  function deleteArticleFile(&$articleFile) {
341  return $this->deleteArticleFileById($articleFile->getFileId(), $articleFile->getRevision());
342  }
343 
349  function deleteArticleFileById($fileId, $revision = null) {
350  if ($revision == null) {
351  return $this->update(
352  'DELETE FROM article_files WHERE file_id = ?', $fileId
353  );
354  } else {
355  return $this->update(
356  'DELETE FROM article_files WHERE file_id = ? AND revision = ?', array($fileId, $revision)
357  );
358  }
359  }
360 
365  function deleteArticleFiles($articleId) {
366  return $this->update(
367  'DELETE FROM article_files WHERE article_id = ?', $articleId
368  );
369  }
370 
376  return $this->getInsertId('article_files', 'file_id');
377  }
378 }
379 
380 ?>
& retrieveLimit($sql, $params=false, $numRows=false, $offset=false, $callHooks=true)
Definition: DAO.inc.php:147
insertArticleFile(&$articleFile)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
& getArticleFile($fileId, $revision=null, $articleId=null)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& getArticleFilesByAssocId($assocId, $fileStage)
& getArticleFileRevisionsInRange($fileId, $start=1, $end=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
Operations for retrieving and modifying ArticleFile objects.
Abstract base class for retrieving and modifying PKPFile objects and their decendents.
call($hookName, $args=null)
updateArticleFile(&$articleFile)
& getArticleFilesByArticle($articleId)
deleteArticleFileById($fileId, $revision=null)
deleteArticleFile(&$articleFile)
& _returnArticleFileFromRow(&$row)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getRevisionNumber($fileId)
deleteArticleFiles($articleId)
Article file class.
& getArticleFileRevisions($fileId, $round=null)