Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
SuppFileDAO.inc.php
1 <?php
2 
17 import('classes.article.SuppFile');
18 
19 class SuppFileDAO extends DAO {
26  function &getSuppFile($suppFileId, $articleId = null) {
27  $params = array($suppFileId);
28  if ($articleId) $params[] = $articleId;
29 
30  $result =& $this->retrieve(
31  'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM article_supplementary_files s LEFT JOIN article_files a ON (s.file_id = a.file_id) WHERE s.supp_id = ?' . ($articleId?' AND s.article_id = ?':''),
32  $params
33  );
34 
35  $returner = null;
36  if ($result->RecordCount() != 0) {
37  $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
38  }
39 
40  $result->Close();
41  unset($result);
42 
43  return $returner;
44  }
45 
55  function &getSuppFileByPubId($pubIdType, $pubId, $articleId = null) {
56  $suppFiles =& $this->getSuppFilesBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
57  if (empty($suppFiles)) {
58  $suppFile = null;
59  } else {
60  assert(count($suppFiles) == 1);
61  $suppFile =& $suppFiles[0];
62  }
63 
64  return $suppFile;
65  }
66 
75  function &getSuppFilesBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
76  $params = array($settingName);
77 
78  $sql = 'SELECT s.*, af.file_name, af.original_file_name, af.file_type, af.file_size, af.date_uploaded, af.date_modified
79  FROM article_supplementary_files s
80  LEFT JOIN article_files af ON s.file_id = af.file_id
81  INNER JOIN articles a ON a.article_id = s.article_id
82  LEFT JOIN published_articles pa ON s.article_id = pa.article_id ';
83  if (is_null($settingValue)) {
84  $sql .= 'LEFT JOIN article_supp_file_settings sfs ON s.supp_id = sfs.supp_id AND sfs.setting_name = ?
85  WHERE (sfs.setting_value IS NULL OR sfs.setting_value = "")';
86  } else {
87  $params[] = $settingValue;
88  $sql .= 'INNER JOIN article_supp_file_settings sfs ON s.supp_id = sfs.supp_id
89  WHERE sfs.setting_name = ? AND sfs.setting_value = ?';
90  }
91  if ($articleId) {
92  $params[] = (int) $articleId;
93  $sql .= ' AND s.article_id = ?';
94  }
95  if ($journalId) {
96  $params[] = (int) $journalId;
97  $sql .= ' AND a.journal_id = ?';
98  }
99  $sql .= ' ORDER BY a.journal_id, pa.issue_id, s.supp_id';
100  $result =& $this->retrieve($sql, $params);
101 
102  $suppFiles = array();
103  while (!$result->EOF) {
104  $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
105  $result->moveNext();
106  }
107  $result->Close();
108 
109  return $suppFiles;
110  }
111 
117  function &getSuppFilesByArticle($articleId) {
118  $suppFiles = array();
119 
120  $result =& $this->retrieve(
121  'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM article_supplementary_files s LEFT JOIN article_files a ON (s.file_id = a.file_id) WHERE s.article_id = ? ORDER BY s.seq',
122  (int) $articleId
123  );
124 
125  while (!$result->EOF) {
126  $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
127  $result->moveNext();
128  }
129 
130  $result->Close();
131  unset($result);
132 
133  return $suppFiles;
134  }
135 
141  function &getSuppFilesByJournalId($journalId) {
142  $result =& $this->retrieve(
143  'SELECT
144  s.*,
145  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
146  FROM article_supplementary_files s
147  LEFT JOIN article_files af ON (s.file_id = af.file_id)
148  INNER JOIN articles a ON (s.article_id = a.article_id)
149  WHERE a.journal_id = ?',
150  (int) $journalId
151  );
152 
153  $returner = new DAOResultFactory($result, $this, '_returnSuppFileFromRow');
154  return $returner;
155  }
156 
161  function getLocaleFieldNames() {
162  return array('title', 'creator', 'subject', 'typeOther', 'description', 'publisher', 'sponsor', 'source');
163  }
164 
171  $additionalFields = parent::getAdditionalFieldNames();
172  // FIXME: Move this to a PID plug-in.
173  $additionalFields[] = 'pub-id::publisher-id';
174  return $additionalFields;
175  }
176 
181  function updateLocaleFields(&$suppFile) {
182  $this->updateDataObjectSettings('article_supp_file_settings', $suppFile, array(
183  'supp_id' => $suppFile->getId()
184  ));
185  }
186 
192  function &_returnSuppFileFromRow(&$row) {
193  $suppFile = new SuppFile();
194  $suppFile->setId($row['supp_id']);
195  $suppFile->setRemoteURL($row['remote_url']);
196  $suppFile->setFileId($row['file_id']);
197  $suppFile->setArticleId($row['article_id']);
198  $suppFile->setType($row['type']);
199  $suppFile->setDateCreated($this->dateFromDB($row['date_created']));
200  $suppFile->setLanguage($row['language']);
201  $suppFile->setShowReviewers($row['show_reviewers']);
202  $suppFile->setDateSubmitted($this->datetimeFromDB($row['date_submitted']));
203  $suppFile->setSequence($row['seq']);
204 
205  //ArticleFile set methods
206  $suppFile->setFileName($row['file_name']);
207  $suppFile->setOriginalFileName($row['original_file_name']);
208  $suppFile->setFileType($row['file_type']);
209  $suppFile->setFileSize($row['file_size']);
210  $suppFile->setDateModified($this->datetimeFromDB($row['date_modified']));
211  $suppFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
212 
213  $this->getDataObjectSettings('article_supp_file_settings', 'supp_id', $row['supp_id'], $suppFile);
214 
215  HookRegistry::call('SuppFileDAO::_returnSuppFileFromRow', array(&$suppFile, &$row));
216 
217  return $suppFile;
218  }
219 
224  function insertSuppFile(&$suppFile) {
225  if ($suppFile->getDateSubmitted() == null) {
226  $suppFile->setDateSubmitted(Core::getCurrentDate());
227  }
228  if ($suppFile->getSequence() == null) {
229  $suppFile->setSequence($this->getNextSuppFileSequence($suppFile->getArticleId()));
230  }
231  $this->update(
232  sprintf('INSERT INTO article_supplementary_files
233  (remote_url, file_id, article_id, type, date_created, language, show_reviewers, date_submitted, seq)
234  VALUES
235  (?, ?, ?, ?, %s, ?, ?, %s, ?)',
236  $this->dateToDB($suppFile->getDateCreated()), $this->datetimeToDB($suppFile->getDateSubmitted())),
237  array(
238  $suppFile->getRemoteURL(),
239  $suppFile->getFileId(),
240  $suppFile->getArticleId(),
241  $suppFile->getType(),
242  $suppFile->getLanguage(),
243  $suppFile->getShowReviewers(),
244  $suppFile->getSequence()
245  )
246  );
247  $suppFile->setId($this->getInsertSuppFileId());
248  $this->updateLocaleFields($suppFile);
249  return $suppFile->getId();
250  }
251 
256  function updateSuppFile(&$suppFile) {
257  $returner = $this->update(
258  sprintf('UPDATE article_supplementary_files
259  SET
260  remote_url = ?,
261  file_id = ?,
262  type = ?,
263  date_created = %s,
264  language = ?,
265  show_reviewers = ?,
266  seq = ?
267  WHERE supp_id = ?',
268  $this->dateToDB($suppFile->getDateCreated())),
269  array(
270  $suppFile->getRemoteURL(),
271  $suppFile->getFileId(),
272  $suppFile->getType(),
273  $suppFile->getLanguage(),
274  $suppFile->getShowReviewers(),
275  $suppFile->getSequence(),
276  $suppFile->getId()
277  )
278  );
279  $this->updateLocaleFields($suppFile);
280  return $returner;
281  }
282 
287  function deleteSuppFile(&$suppFile) {
288  return $this->deleteSuppFileById($suppFile->getId());
289  }
290 
296  function deleteSuppFileById($suppFileId, $articleId = null) {
297  if (isset($articleId)) {
298  $returner = $this->update('DELETE FROM article_supplementary_files WHERE supp_id = ? AND article_id = ?', array($suppFileId, $articleId));
299  if ($returner) $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
300  return $returner;
301 
302  } else {
303  $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
304  return $this->update(
305  'DELETE FROM article_supplementary_files WHERE supp_id = ?', $suppFileId
306  );
307  }
308  }
309 
314  function deleteSuppFilesByArticle($articleId) {
315  $suppFiles =& $this->getSuppFilesByArticle($articleId);
316  foreach ($suppFiles as $suppFile) {
317  $this->deleteSuppFile($suppFile);
318  }
319  }
320 
327  function suppFileExistsByFileId($articleId, $fileId) {
328  $result =& $this->retrieve(
329  'SELECT COUNT(*) FROM article_supplementary_files
330  WHERE article_id = ? AND file_id = ?',
331  array($articleId, $fileId)
332  );
333 
334  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
335 
336  $result->Close();
337  unset($result);
338 
339  return $returner;
340  }
341 
346  function resequenceSuppFiles($articleId) {
347  $result =& $this->retrieve(
348  'SELECT supp_id FROM article_supplementary_files WHERE article_id = ? ORDER BY seq',
349  $articleId
350  );
351 
352  for ($i=1; !$result->EOF; $i++) {
353  list($suppId) = $result->fields;
354  $this->update(
355  'UPDATE article_supplementary_files SET seq = ? WHERE supp_id = ?',
356  array($i, $suppId)
357  );
358  $result->moveNext();
359  }
360 
361  $result->close();
362  unset($result);
363  }
364 
370  function getNextSuppFileSequence($articleId) {
371  $result =& $this->retrieve(
372  'SELECT MAX(seq) + 1 FROM article_supplementary_files WHERE article_id = ?',
373  $articleId
374  );
375  $returner = floor($result->fields[0]);
376 
377  $result->Close();
378  unset($result);
379 
380  return $returner;
381  }
382 
387  function getInsertSuppFileId() {
388  return $this->getInsertId('article_supplementary_files', 'supp_id');
389  }
390 
398  function &getSuppFileByBestSuppFileId($suppId, $articleId) {
399  $suppFile =& $this->getSuppFileByPubId('publisher-id', $suppId, $articleId);
400  if (!isset($suppFile) && ctype_digit("$suppId")) $suppFile =& $this->getSuppFile((int) $suppId, $articleId);
401  return $suppFile;
402  }
403 
415  function pubIdExists($pubIdType, $pubId, $suppId, $journalId) {
416  $result =& $this->retrieve(
417  'SELECT COUNT(*)
418  FROM article_supp_file_settings sfs
419  INNER JOIN article_supplementary_files f ON sfs.supp_id = f.supp_id
420  INNER JOIN articles a ON f.article_id = a.article_id
421  WHERE sfs.setting_name = ? AND sfs.setting_value = ? AND f.supp_id <> ? AND a.journal_id = ?',
422  array(
423  'pub-id::'.$pubIdType,
424  $pubId,
425  (int) $suppId,
426  (int) $journalId
427  )
428  );
429  $returner = $result->fields[0] ? true : false;
430  $result->Close();
431  return $returner;
432  }
433 
442  function changePubId($suppFileId, $pubIdType, $pubId) {
443  $idFields = array(
444  'supp_id', 'locale', 'setting_name'
445  );
446  $updateArray = array(
447  'supp_id' => $suppFileId,
448  'locale' => '',
449  'setting_name' => 'pub-id::'.$pubIdType,
450  'setting_type' => 'string',
451  'setting_value' => (string)$pubId
452  );
453  $this->replace('article_supp_file_settings', $updateArray, $idFields);
454  }
455 
456 
464  function deleteAllPubIds($journalId, $pubIdType) {
465  $journalId = (int) $journalId;
466  $settingName = 'pub-id::'.$pubIdType;
467 
468  $suppFiles =& $this->getSuppFilesByJournalId($journalId);
469  while ($suppFile =& $suppFiles->next()) {
470  $this->update(
471  'DELETE FROM article_supp_file_settings WHERE setting_name = ? AND supp_id = ?',
472  array(
473  $settingName,
474  (int)$suppFile->getId()
475  )
476  );
477  unset($suppFile);
478  }
479  $this->flushCache();
480  }
481 }
482 
483 ?>
updateSuppFile(&$suppFile)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
deleteAllPubIds($journalId, $pubIdType)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:460
getCurrentDate($ts=null)
Definition: Core.inc.php:94
suppFileExistsByFileId($articleId, $fileId)
pubIdExists($pubIdType, $pubId, $suppId, $journalId)
dateToDB($d)
Definition: DAO.inc.php:306
& getSuppFileByBestSuppFileId($suppId, $articleId)
& getSuppFilesByArticle($articleId)
Supplementary file class.
datetimeFromDB($dt)
Definition: DAO.inc.php:316
& getSuppFilesByJournalId($journalId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
Operations for retrieving and modifying SuppFile objects.
& getSuppFileByPubId($pubIdType, $pubId, $articleId=null)
& _returnSuppFileFromRow(&$row)
deleteSuppFilesByArticle($articleId)
call($hookName, $args=null)
flushCache()
Definition: DAO.inc.php:285
insertSuppFile(&$suppFile)
resequenceSuppFiles($articleId)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
deleteSuppFile(&$suppFile)
datetimeToDB($dt)
Definition: DAO.inc.php:296
changePubId($suppFileId, $pubIdType, $pubId)
updateLocaleFields(&$suppFile)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
deleteSuppFileById($suppFileId, $articleId=null)
& getSuppFile($suppFileId, $articleId=null)
dateFromDB($d)
Definition: DAO.inc.php:326
getNextSuppFileSequence($articleId)
& getSuppFilesBySetting($settingName, $settingValue, $articleId=null, $journalId=null)