Open Journal Systems  2.4.8
 All Classes Namespaces Functions Variables Groups Pages
SuppFileDAO.inc.php
1 <?php
2 
17 import('classes.article.SuppFile');
18 
19 class SuppFileDAO extends DAO {
20 
25  function &_getSuppFileCache() {
26  if (!isset($this->suppFileCache)) {
27  $cacheManager =& CacheManager::getManager();
28  $this->suppFileCache =& $cacheManager->getObjectCache('suppfile', 0, array(&$this, '_suppFileCacheMiss'));
29  }
30  return $this->suppFileCache;
31  }
32 
39  function &_suppFileCacheMiss(&$cache, $id) {
40  $suppFile =& $this->getSuppFile($id, null, false);
41  $cache->setCache($id, $suppFile);
42  return $suppFile;
43  }
44 
48  function flushCache() {
49  $cache =& $this->_getSuppFileCache();
50  $cache->flush();
51  unset($cache);
52  }
53 
61  function &getSuppFile($suppFileId, $articleId = null, $useCache = false) {
62  if ($useCache) {
63  $cache =& $this->_getSuppFileCache();
64  $returner = $cache->get($suppFileId);
65  if ($returner && $articleId != null && $articleId != $returner->getArticleId()) $returner = null;
66  return $returner;
67  }
68 
69  $params = array($suppFileId);
70  if ($articleId) $params[] = $articleId;
71 
72  $result =& $this->retrieve(
73  '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 = ?':''),
74  $params
75  );
76 
77  $returner = null;
78  if ($result->RecordCount() != 0) {
79  $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
80  }
81 
82  $result->Close();
83  unset($result);
84 
85  return $returner;
86  }
87 
97  function &getSuppFileByPubId($pubIdType, $pubId, $articleId = null) {
98  $suppFiles =& $this->getSuppFilesBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
99  if (empty($suppFiles)) {
100  $suppFile = null;
101  } else {
102  assert(count($suppFiles) == 1);
103  $suppFile =& $suppFiles[0];
104  }
105 
106  return $suppFile;
107  }
108 
117  function &getSuppFilesBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
118  $params = array($settingName);
119 
120  $sql = 'SELECT s.*, af.file_name, af.original_file_name, af.file_type, af.file_size, af.date_uploaded, af.date_modified
121  FROM article_supplementary_files s
122  LEFT JOIN article_files af ON s.file_id = af.file_id
123  INNER JOIN articles a ON a.article_id = s.article_id
124  LEFT JOIN published_articles pa ON s.article_id = pa.article_id ';
125  if (is_null($settingValue)) {
126  $sql .= 'LEFT JOIN article_supp_file_settings sfs ON s.supp_id = sfs.supp_id AND sfs.setting_name = ?
127  WHERE (sfs.setting_value IS NULL OR sfs.setting_value = \'\')';
128  } else {
129  $params[] = $settingValue;
130  $sql .= 'INNER JOIN article_supp_file_settings sfs ON s.supp_id = sfs.supp_id
131  WHERE sfs.setting_name = ? AND sfs.setting_value = ?';
132  }
133  if ($articleId) {
134  $params[] = (int) $articleId;
135  $sql .= ' AND s.article_id = ?';
136  }
137  if ($journalId) {
138  $params[] = (int) $journalId;
139  $sql .= ' AND a.journal_id = ?';
140  }
141  $sql .= ' ORDER BY a.journal_id, pa.issue_id, s.supp_id';
142  $result =& $this->retrieve($sql, $params);
143 
144  $suppFiles = array();
145  while (!$result->EOF) {
146  $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
147  $result->moveNext();
148  }
149  $result->Close();
150 
151  return $suppFiles;
152  }
153 
159  function &getSuppFilesByArticle($articleId) {
160  $suppFiles = array();
161 
162  $result =& $this->retrieve(
163  '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',
164  (int) $articleId
165  );
166 
167  while (!$result->EOF) {
168  $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
169  $result->moveNext();
170  }
171 
172  $result->Close();
173  unset($result);
174 
175  return $suppFiles;
176  }
177 
183  function &getSuppFilesByJournalId($journalId) {
184  $result =& $this->retrieve(
185  'SELECT
186  s.*,
187  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
188  FROM article_supplementary_files s
189  LEFT JOIN article_files af ON (s.file_id = af.file_id)
190  INNER JOIN articles a ON (s.article_id = a.article_id)
191  WHERE a.journal_id = ?',
192  (int) $journalId
193  );
194 
195  $returner = new DAOResultFactory($result, $this, '_returnSuppFileFromRow');
196  return $returner;
197  }
198 
203  function getLocaleFieldNames() {
204  return array('title', 'creator', 'subject', 'typeOther', 'description', 'publisher', 'sponsor', 'source');
205  }
206 
213  $additionalFields = parent::getAdditionalFieldNames();
214  // FIXME: Move this to a PID plug-in.
215  $additionalFields[] = 'pub-id::publisher-id';
216  return $additionalFields;
217  }
218 
223  function updateLocaleFields(&$suppFile) {
224  $this->updateDataObjectSettings('article_supp_file_settings', $suppFile, array(
225  'supp_id' => $suppFile->getId()
226  ));
227  }
228 
234  function &_returnSuppFileFromRow(&$row) {
235  $suppFile = new SuppFile();
236  $suppFile->setId($row['supp_id']);
237  $suppFile->setRemoteURL($row['remote_url']);
238  $suppFile->setFileId($row['file_id']);
239  $suppFile->setArticleId($row['article_id']);
240  $suppFile->setType($row['type']);
241  $suppFile->setDateCreated($this->dateFromDB($row['date_created']));
242  $suppFile->setLanguage($row['language']);
243  $suppFile->setShowReviewers($row['show_reviewers']);
244  $suppFile->setDateSubmitted($this->datetimeFromDB($row['date_submitted']));
245  $suppFile->setSequence($row['seq']);
246 
247  //ArticleFile set methods
248  $suppFile->setFileName($row['file_name']);
249  $suppFile->setOriginalFileName($row['original_file_name']);
250  $suppFile->setFileType($row['file_type']);
251  $suppFile->setFileSize($row['file_size']);
252  $suppFile->setDateModified($this->datetimeFromDB($row['date_modified']));
253  $suppFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
254 
255  $this->getDataObjectSettings('article_supp_file_settings', 'supp_id', $row['supp_id'], $suppFile);
256 
257  HookRegistry::call('SuppFileDAO::_returnSuppFileFromRow', array(&$suppFile, &$row));
258 
259  return $suppFile;
260  }
261 
266  function insertSuppFile(&$suppFile) {
267  if ($suppFile->getDateSubmitted() == null) {
268  $suppFile->setDateSubmitted(Core::getCurrentDate());
269  }
270  if ($suppFile->getSequence() == null) {
271  $suppFile->setSequence($this->getNextSuppFileSequence($suppFile->getArticleId()));
272  }
273  $this->update(
274  sprintf('INSERT INTO article_supplementary_files
275  (remote_url, file_id, article_id, type, date_created, language, show_reviewers, date_submitted, seq)
276  VALUES
277  (?, ?, ?, ?, %s, ?, ?, %s, ?)',
278  $this->dateToDB($suppFile->getDateCreated()), $this->datetimeToDB($suppFile->getDateSubmitted())),
279  array(
280  $suppFile->getRemoteURL(),
281  $suppFile->getFileId(),
282  $suppFile->getArticleId(),
283  $suppFile->getType(),
284  $suppFile->getLanguage(),
285  $suppFile->getShowReviewers(),
286  $suppFile->getSequence()
287  )
288  );
289  $suppFile->setId($this->getInsertSuppFileId());
290  $this->updateLocaleFields($suppFile);
291  return $suppFile->getId();
292  }
293 
298  function updateSuppFile(&$suppFile) {
299  $returner = $this->update(
300  sprintf('UPDATE article_supplementary_files
301  SET
302  remote_url = ?,
303  file_id = ?,
304  type = ?,
305  date_created = %s,
306  language = ?,
307  show_reviewers = ?,
308  seq = ?
309  WHERE supp_id = ?',
310  $this->dateToDB($suppFile->getDateCreated())),
311  array(
312  $suppFile->getRemoteURL(),
313  $suppFile->getFileId(),
314  $suppFile->getType(),
315  $suppFile->getLanguage(),
316  $suppFile->getShowReviewers(),
317  $suppFile->getSequence(),
318  $suppFile->getId()
319  )
320  );
321  $this->updateLocaleFields($suppFile);
322  return $returner;
323  }
324 
329  function deleteSuppFile(&$suppFile) {
330  return $this->deleteSuppFileById($suppFile->getId());
331  }
332 
338  function deleteSuppFileById($suppFileId, $articleId = null) {
339  if (isset($articleId)) {
340  $returner = $this->update('DELETE FROM article_supplementary_files WHERE supp_id = ? AND article_id = ?', array($suppFileId, $articleId));
341  if ($returner) $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
342  return $returner;
343 
344  } else {
345  $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
346  return $this->update(
347  'DELETE FROM article_supplementary_files WHERE supp_id = ?', $suppFileId
348  );
349  }
350  }
351 
356  function deleteSuppFilesByArticle($articleId) {
357  $suppFiles =& $this->getSuppFilesByArticle($articleId);
358  foreach ($suppFiles as $suppFile) {
359  $this->deleteSuppFile($suppFile);
360  }
361  }
362 
369  function suppFileExistsByFileId($articleId, $fileId) {
370  $result =& $this->retrieve(
371  'SELECT COUNT(*) FROM article_supplementary_files
372  WHERE article_id = ? AND file_id = ?',
373  array($articleId, $fileId)
374  );
375 
376  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
377 
378  $result->Close();
379  unset($result);
380 
381  return $returner;
382  }
383 
388  function resequenceSuppFiles($articleId) {
389  $result =& $this->retrieve(
390  'SELECT supp_id FROM article_supplementary_files WHERE article_id = ? ORDER BY seq',
391  $articleId
392  );
393 
394  for ($i=1; !$result->EOF; $i++) {
395  list($suppId) = $result->fields;
396  $this->update(
397  'UPDATE article_supplementary_files SET seq = ? WHERE supp_id = ?',
398  array($i, $suppId)
399  );
400  $result->moveNext();
401  }
402 
403  $result->close();
404  unset($result);
405  }
406 
412  function getNextSuppFileSequence($articleId) {
413  $result =& $this->retrieve(
414  'SELECT MAX(seq) + 1 FROM article_supplementary_files WHERE article_id = ?',
415  $articleId
416  );
417  $returner = floor($result->fields[0]);
418 
419  $result->Close();
420  unset($result);
421 
422  return $returner;
423  }
424 
429  function getInsertSuppFileId() {
430  return $this->getInsertId('article_supplementary_files', 'supp_id');
431  }
432 
440  function &getSuppFileByBestSuppFileId($suppId, $articleId) {
441  $suppFile =& $this->getSuppFileByPubId('publisher-id', $suppId, $articleId);
442  if (!isset($suppFile) && ctype_digit("$suppId")) $suppFile =& $this->getSuppFile((int) $suppId, $articleId);
443  return $suppFile;
444  }
445 
457  function pubIdExists($pubIdType, $pubId, $suppId, $journalId) {
458  $result =& $this->retrieve(
459  'SELECT COUNT(*)
460  FROM article_supp_file_settings sfs
461  INNER JOIN article_supplementary_files f ON sfs.supp_id = f.supp_id
462  INNER JOIN articles a ON f.article_id = a.article_id
463  WHERE sfs.setting_name = ? AND sfs.setting_value = ? AND f.supp_id <> ? AND a.journal_id = ?',
464  array(
465  'pub-id::'.$pubIdType,
466  $pubId,
467  (int) $suppId,
468  (int) $journalId
469  )
470  );
471  $returner = $result->fields[0] ? true : false;
472  $result->Close();
473  return $returner;
474  }
475 
484  function changePubId($suppFileId, $pubIdType, $pubId) {
485  $idFields = array(
486  'supp_id', 'locale', 'setting_name'
487  );
488  $updateArray = array(
489  'supp_id' => $suppFileId,
490  'locale' => '',
491  'setting_name' => 'pub-id::'.$pubIdType,
492  'setting_type' => 'string',
493  'setting_value' => (string)$pubId
494  );
495  $this->replace('article_supp_file_settings', $updateArray, $idFields);
496  }
497 
498 
506  function deleteAllPubIds($journalId, $pubIdType) {
507  $journalId = (int) $journalId;
508  $settingName = 'pub-id::'.$pubIdType;
509 
510  $suppFiles =& $this->getSuppFilesByJournalId($journalId);
511  while ($suppFile =& $suppFiles->next()) {
512  $this->update(
513  'DELETE FROM article_supp_file_settings WHERE setting_name = ? AND supp_id = ?',
514  array(
515  $settingName,
516  (int)$suppFile->getId()
517  )
518  );
519  unset($suppFile);
520  }
521  $this->flushCache();
522  }
523 
531  function deletePubId($suppFileId, $pubIdType) {
532  $settingName = 'pub-id::'.$pubIdType;
533  $this->update(
534  'DELETE FROM article_supp_file_settings WHERE setting_name = ? AND supp_id = ?',
535  array(
536  $settingName,
537  (int)$suppFileId
538  )
539  );
540  $this->flushCache();
541  }
542 }
543 
544 ?>
updateSuppFile(&$suppFile)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
deletePubId($suppFileId, $pubIdType)
& 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)
& getSuppFile($suppFileId, $articleId=null, $useCache=false)
& _suppFileCacheMiss(&$cache, $id)
deleteSuppFilesByArticle($articleId)
call($hookName, $args=null)
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)
dateFromDB($d)
Definition: DAO.inc.php:326
getNextSuppFileSequence($articleId)
& getSuppFilesBySetting($settingName, $settingValue, $articleId=null, $journalId=null)