Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
IssueDAO.inc.php
1 <?php
2 
17 import ('classes.issue.Issue');
18 
19 class IssueDAO extends DAO {
20  var $caches;
21 
28  function _cacheMiss($cache, $id) {
29  if ($cache->getCacheId() === 'current') {
30  $issue = $this->getCurrent($id, false);
31  } else {
32  $issue = $this->getById($id, null, false);
33  }
34  $cache->setCache($id, $issue);
35  return $issue;
36  }
37 
43  function _getCache($cacheId) {
44  if (!isset($this->caches)) $this->caches = array();
45  if (!isset($this->caches[$cacheId])) {
46  $cacheManager = CacheManager::getManager();
47  $this->caches[$cacheId] = $cacheManager->getObjectCache('issues', $cacheId, array($this, '_cacheMiss'));
48  }
49  return $this->caches[$cacheId];
50  }
51 
59  function getById($issueId, $journalId = null, $useCache = false) {
60  if ($useCache) {
61  $cache = $this->_getCache('issues');
62  $returner = $cache->get($issueId);
63  if ($returner && $journalId != null && $journalId != $returner->getJournalId()) $returner = null;
64  return $returner;
65  }
66 
67  $params = array((int) $issueId);
68  if ($journalId) $params[] = (int) $journalId;
69  $result = $this->retrieve(
70  'SELECT i.* FROM issues i WHERE issue_id = ?'
71  . ($journalId?' AND journal_id = ?':''),
72  $params
73  );
74 
75  $issue = null;
76  if ($result->RecordCount() != 0) {
77  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
78  }
79  $result->Close();
80  return $issue;
81  }
82 
93  function getByPubId($pubIdType, $pubId, $journalId = null, $useCache = false) {
94  if ($useCache && $pubIdType == 'publisher-id') {
95  $cache = $this->_getCache('issues');
96  $returner = $cache->get($pubId);
97  if ($returner && $journalId != null && $journalId != $returner->getJournalId()) $returner = null;
98  return $returner;
99  }
100 
101  $issues = $this->getBySetting('pub-id::'.$pubIdType, $pubId, $journalId);
102  if (empty($issues)) {
103  return null;
104  } else {
105  assert(count($issues) == 1);
106  return $issues[0];
107  }
108  }
109 
117  function getBySetting($settingName, $settingValue, $journalId = null) {
118  $params = array($settingName);
119  $sql = 'SELECT i.*
120  FROM issues i ';
121  if (is_null($settingValue)) {
122  $sql .= 'LEFT JOIN issue_settings ist ON i.issue_id = ist.issue_id AND ist.setting_name = ?
123  WHERE (ist.setting_value IS NULL OR ist.setting_value = "")';
124  } else {
125  $params[] = $settingValue;
126  $sql .= 'INNER JOIN issue_settings ist ON i.issue_id = ist.issue_id
127  WHERE ist.setting_name = ? AND ist.setting_value = ?';
128  }
129  if ($journalId) {
130  $params[] = (int) $journalId;
131  $sql .= ' AND i.journal_id = ?';
132  }
133  $sql .= ' ORDER BY i.issue_id';
134  $result = $this->retrieve($sql, $params);
135 
136  $issues = array();
137  while (!$result->EOF) {
138  $issues[] = $this->_returnIssueFromRow($result->GetRowAssoc(false));
139  $result->MoveNext();
140  }
141  $result->Close();
142  return $issues;
143  }
144 
153  function getPublishedIssuesByNumber($journalId, $volume = null, $number = null, $year = null) {
154  $sql = 'SELECT i.* FROM issues i WHERE i.published = 1 AND i.journal_id = ?';
155  $params = array((int) $journalId);
156 
157  if ($volume !== null) {
158  $sql .= ' AND i.volume = ?';
159  $params[] = $volume;
160  }
161  if ($number !== null) {
162  $sql .= ' AND i.number = ?';
163  $params[] = $number;
164  }
165  if ($year !== null) {
166  $sql .= ' AND i.year = ?';
167  $params[] = $year;
168  }
169 
170  $result = $this->retrieve($sql, $params);
171  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
172  }
173 
182  function getByBestId($issueId, $journalId = null, $useCache = false) {
183  $issue = $this->getByPubId('publisher-id', $issueId, $journalId, $useCache);
184  if (!isset($issue) && ctype_digit("$issueId")) $issue = $this->getById((int) $issueId, $journalId, $useCache);
185  return $issue;
186  }
187 
194  function getCurrent($journalId, $useCache = false) {
195  if ($useCache) {
196  $cache = $this->_getCache('current');
197  return $cache->get($journalId);
198  }
199 
200  $result = $this->retrieve(
201  'SELECT i.* FROM issues i WHERE journal_id = ? AND current = 1', (int) $journalId
202  );
203 
204  $issue = null;
205  if ($result->RecordCount() != 0) {
206  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
207  }
208  $result->Close();
209  return $issue;
210  }
211 
216  function updateCurrent($journalId, $issue = null) {
217  $this->update(
218  'UPDATE issues SET current = 0 WHERE journal_id = ? AND current = 1', (int) $journalId
219  );
220  if ($issue) $this->updateObject($issue);
221 
222  $this->flushCache();
223  }
224 
225 
234  function changePubId($issueId, $pubIdType, $pubId) {
235  $idFields = array(
236  'issue_id', 'locale', 'setting_name'
237  );
238  $updateArray = array(
239  'issue_id' => $issueId,
240  'locale' => '',
241  'setting_name' => 'pub-id::'.$pubIdType,
242  'setting_type' => 'string',
243  'setting_value' => (string)$pubId
244  );
245  $this->replace('issue_settings', $updateArray, $idFields);
246  $this->flushCache();
247  }
248 
253  function newDataObject() {
254  return new Issue();
255  }
256 
262  function _returnIssueFromRow($row) {
263  $issue = $this->newDataObject();
264  $issue->setId($row['issue_id']);
265  $issue->setJournalId($row['journal_id']);
266  $issue->setVolume($row['volume']);
267  $issue->setNumber($row['number']);
268  $issue->setYear($row['year']);
269  $issue->setPublished($row['published']);
270  $issue->setCurrent($row['current']);
271  $issue->setDatePublished($this->datetimeFromDB($row['date_published']));
272  $issue->setDateNotified($this->datetimeFromDB($row['date_notified']));
273  $issue->setLastModified($this->datetimeFromDB($row['last_modified']));
274  $issue->setAccessStatus($row['access_status']);
275  $issue->setOpenAccessDate($this->datetimeFromDB($row['open_access_date']));
276  $issue->setShowVolume($row['show_volume']);
277  $issue->setShowNumber($row['show_number']);
278  $issue->setShowYear($row['show_year']);
279  $issue->setShowTitle($row['show_title']);
280  $issue->setStyleFileName($row['style_file_name']);
281  $issue->setOriginalStyleFileName($row['original_style_file_name']);
282 
283  $this->getDataObjectSettings('issue_settings', 'issue_id', $row['issue_id'], $issue);
284 
285  HookRegistry::call('IssueDAO::_returnIssueFromRow', array(&$issue, &$row));
286 
287  return $issue;
288  }
289 
294  function getLocaleFieldNames() {
295  return array('title', 'coverPageDescription', 'coverPageAltText', 'showCoverPage', 'hideCoverPageArchives', 'hideCoverPageCover', 'originalFileName', 'fileName', 'width', 'height', 'description');
296  }
297 
304  $additionalFields = parent::getAdditionalFieldNames();
305  // FIXME: Move this to a PID plug-in.
306  $additionalFields[] = 'pub-id::publisher-id';
307  return $additionalFields;
308  }
309 
314  function updateLocaleFields(&$issue) {
315  $this->updateDataObjectSettings('issue_settings', $issue, array(
316  'issue_id' => $issue->getId()
317  ));
318  }
319 
325  function insertObject($issue) {
326  $this->update(
327  sprintf('INSERT INTO issues
328  (journal_id, volume, number, year, published, current, date_published, date_notified, last_modified, access_status, open_access_date, show_volume, show_number, show_year, show_title, style_file_name, original_style_file_name)
329  VALUES
330  (?, ?, ?, ?, ?, ?, %s, %s, %s, ?, %s, ?, ?, ?, ?, ?, ?)',
331  $this->datetimeToDB($issue->getDatePublished()), $this->datetimeToDB($issue->getDateNotified()), $this->datetimeToDB($issue->getLastModified()), $this->datetimeToDB($issue->getOpenAccessDate())),
332  array(
333  (int) $issue->getJournalId(),
334  $issue->getVolume(),
335  $issue->getNumber(),
336  $issue->getYear(),
337  (int) $issue->getPublished(),
338  (int) $issue->getCurrent(),
339  (int) $issue->getAccessStatus(),
340  (int) $issue->getShowVolume(),
341  (int) $issue->getShowNumber(),
342  (int) $issue->getShowYear(),
343  (int) $issue->getShowTitle(),
344  $issue->getStyleFileName(),
345  $issue->getOriginalStyleFileName()
346  )
347  );
348 
349  $issue->setId($this->getInsertId());
350 
351  $this->updateLocaleFields($issue);
352 
353  if ($this->customIssueOrderingExists($issue->getJournalId())) {
354  $this->resequenceCustomIssueOrders($issue->getJournalId());
355  }
356 
357  return $issue->getId();
358  }
359 
364  function getInsertId() {
365  return $this->_getInsertId('issues', 'issue_id');
366  }
367 
377  function issueExists($journalId, $volume, $number, $year, $issueId) {
378  $result = $this->retrieve(
379  'SELECT i.* FROM issues i WHERE journal_id = ? AND volume = ? AND number = ? AND year = ? AND issue_id <> ?',
380  array((int) $journalId, $volume, $number, $year, (int) $issueId)
381  );
382  $returner = $result->RecordCount() != 0 ? true : false;
383  $result->Close();
384  return $returner;
385  }
386 
391  function updateObject($issue) {
392  $issue->stampModified();
393  $this->update(
394  sprintf('UPDATE issues
395  SET
396  journal_id = ?,
397  volume = ?,
398  number = ?,
399  year = ?,
400  published = ?,
401  current = ?,
402  date_published = %s,
403  date_notified = %s,
404  last_modified = %s,
405  open_access_date = %s,
406  access_status = ?,
407  show_volume = ?,
408  show_number = ?,
409  show_year = ?,
410  show_title = ?,
411  style_file_name = ?,
412  original_style_file_name = ?
413  WHERE issue_id = ?',
414  $this->datetimeToDB($issue->getDatePublished()), $this->datetimeToDB($issue->getDateNotified()), $this->datetimeToDB($issue->getLastModified()), $this->datetimeToDB($issue->getOpenAccessDate())),
415  array(
416  (int) $issue->getJournalId(),
417  $issue->getVolume(),
418  $issue->getNumber(),
419  $issue->getYear(),
420  (int) $issue->getPublished(),
421  (int) $issue->getCurrent(),
422  (int) $issue->getAccessStatus(),
423  (int) $issue->getShowVolume(),
424  (int) $issue->getShowNumber(),
425  (int) $issue->getShowYear(),
426  (int) $issue->getShowTitle(),
427  $issue->getStyleFileName(),
428  $issue->getOriginalStyleFileName(),
429  (int) $issue->getId()
430  )
431  );
432 
433  $this->updateLocaleFields($issue);
434 
435  if ($this->customIssueOrderingExists($issue->getJournalId())) {
436  $this->resequenceCustomIssueOrders($issue->getJournalId());
437  }
438 
439  $this->flushCache();
440  }
441 
446  function deleteObject($issue) {
447  import('classes.file.PublicFileManager');
448  $publicFileManager = new PublicFileManager();
449 
450  if (is_array($issue->getFileName(null))) foreach ($issue->getFileName(null) as $fileName) {
451  if ($fileName != '') {
452  $publicFileManager->removeJournalFile($issue->getJournalId(), $fileName);
453  }
454  }
455  if (($fileName = $issue->getStyleFileName()) != '') {
456  $publicFileManager->removeJournalFile($issue->getJournalId(), $fileName);
457  }
458 
459  $issueId = $issue->getId();
460 
461  // Delete issue-specific ordering if it exists.
462  $sectionDao = DAORegistry::getDAO('SectionDAO');
463  $sectionDao->deleteCustomSectionOrdering($issueId);
464 
465  // Delete published issue galleys and issue files
466  $issueGalleyDao = DAORegistry::getDAO('IssueGalleyDAO');
467  $issueGalleyDao->deleteByIssueId($issueId);
468 
469  $issueFileDao = DAORegistry::getDAO('IssueFileDAO');
470  $issueFileDao->deleteByIssueId($issueId);
471 
472  import('classes.file.IssueFileManager');
473  $issueFileManager = new IssueFileManager($issueId);
474  $issueFileManager->deleteIssueTree();
475 
476  // Delete published articles
477  $publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
478  $publishedArticleDao->deletePublishedArticlesByIssueId($issueId);
479 
480  // Delete issue settings and issue
481  $this->update('DELETE FROM issue_settings WHERE issue_id = ?', (int) $issueId);
482  $this->update('DELETE FROM issues WHERE issue_id = ?', (int) $issueId);
483  $this->resequenceCustomIssueOrders($issue->getJournalId());
484 
485  $this->flushCache();
486  }
487 
492  function deleteByJournalId($journalId) {
493  $issues = $this->getIssues($journalId);
494  while ($issue = $issues->next()) {
495  $this->deleteObject($issue);
496  }
497  }
498 
505  function issueIdExists($issueId, $journalId) {
506  $result = $this->retrieve(
507  'SELECT COUNT(*) FROM issues WHERE issue_id = ? AND journal_id = ?',
508  array((int) $issueId, (int) $journalId)
509  );
510  return $result->fields[0] ? true : false;
511  }
512 
524  function pubIdExists($pubIdType, $pubId, $issueId, $journalId) {
525  $result = $this->retrieve(
526  'SELECT COUNT(*)
527  FROM issue_settings ist
528  INNER JOIN issues i ON ist.issue_id = i.issue_id
529  WHERE ist.setting_name = ? AND ist.setting_value = ? AND i.issue_id <> ? AND i.journal_id = ?',
530  array(
531  'pub-id::'.$pubIdType,
532  $pubId,
533  (int) $issueId,
534  (int) $journalId
535  )
536  );
537  $returner = $result->fields[0] ? true : false;
538  $result->Close();
539  return $returner;
540  }
541 
548  function getIssueByArticleId($articleId, $journalId = null) {
549  $params = array((int) $articleId);
550  $sql = 'SELECT i.*
551  FROM issues i,
552  published_submissions pa,
553  submissions a
554  WHERE i.issue_id = pa.issue_id AND
555  pa.submission_id = ? AND
556  pa.submission_id = a.submission_id';
557  if ($journalId !== null) {
558  $sql .= ' AND i.journal_id = ? AND a.context_id = i.journal_id';
559  $params[] = (int) $journalId;
560  }
561 
562  $result = $this->retrieve($sql, $params);
563 
564  $issue = null;
565  if ($result->RecordCount() != 0) {
566  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
567  }
568 
569  $result->Close();
570  return $issue;
571  }
572 
579  function getIssues($journalId, $rangeInfo = null) {
580  $result = $this->retrieveRange(
581  'SELECT i.* FROM issues i WHERE journal_id = ? ORDER BY current DESC, date_published DESC',
582  (int) $journalId, $rangeInfo
583  );
584 
585  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
586  }
587 
594  function getPublishedIssues($journalId, $rangeInfo = null) {
595  $result = $this->retrieveRange(
596  'SELECT i.* FROM issues i LEFT JOIN custom_issue_orders o ON (o.issue_id = i.issue_id) WHERE i.journal_id = ? AND i.published = 1 ORDER BY o.seq ASC, i.current DESC, i.date_published DESC',
597  (int) $journalId, $rangeInfo
598  );
599 
600  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
601  }
602 
609  function getUnpublishedIssues($journalId, $rangeInfo = null) {
610  $result = $this->retrieveRange(
611  'SELECT i.* FROM issues i WHERE journal_id = ? AND published = 0 ORDER BY year ASC, volume ASC, number ASC',
612  (int) $journalId, $rangeInfo
613  );
614 
615  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
616  }
617 
623  function getNumArticles($issueId) {
624  $result = $this->retrieve('SELECT COUNT(*) FROM published_submissions WHERE issue_id = ?', (int) $issueId);
625  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
626  $result->Close();
627  return $returner;
628  }
629 
634  function deleteCustomIssueOrdering($journalId) {
635  $this->update(
636  'DELETE FROM custom_issue_orders WHERE journal_id = ?', (int) $journalId
637  );
638  }
639 
644  function resequenceCustomIssueOrders($journalId) {
645  $result = $this->retrieve(
646  'SELECT i.issue_id FROM issues i LEFT JOIN custom_issue_orders o ON (o.issue_id = i.issue_id) WHERE i.journal_id = ? ORDER BY o.seq',
647  (int) $journalId
648  );
649 
650  for ($i=1; !$result->EOF; $i++) {
651  list($issueId) = $result->fields;
652  $resultB = $this->retrieve('SELECT issue_id FROM custom_issue_orders WHERE journal_id=? AND issue_id=?', array($journalId, $issueId));
653  if (!$resultB->EOF) {
654  $this->update(
655  'UPDATE custom_issue_orders SET seq = ? WHERE issue_id = ? AND journal_id = ?',
656  array($i, $issueId, $journalId)
657  );
658  } else {
659  // This entry is missing. Create it.
660  $this->insertCustomIssueOrder($journalId, $issueId, $i);
661  }
662  $resultB->Close();
663  $result->MoveNext();
664  }
665  $result->Close();
666  }
667 
673  function customIssueOrderingExists($journalId) {
674  $result = $this->retrieve(
675  'SELECT COUNT(*) FROM custom_issue_orders WHERE journal_id = ?',
676  (int) $journalId
677  );
678  $returner = isset($result->fields[0]) && $result->fields[0] == 0 ? false : true;
679  $result->Close();
680  return $returner;
681  }
682 
689  function getCustomIssueOrder($journalId, $issueId) {
690  $result = $this->retrieve(
691  'SELECT seq FROM custom_issue_orders WHERE journal_id = ? AND issue_id = ?',
692  array((int) $journalId, (int) $issueId)
693  );
694 
695  $returner = null;
696  if (!$result->EOF) {
697  list($returner) = $result->fields;
698  }
699  $result->Close();
700  return $returner;
701  }
702 
708  function setDefaultCustomIssueOrders($journalId) {
709  $publishedIssues = $this->getPublishedIssues($journalId);
710  for ($i=1; $issue = $publishedIssues->next(); $i++) {
711  $this->insertCustomIssueOrder($journalId, $issue->getId(), $i);
712  }
713  }
714 
721  function insertCustomIssueOrder($journalId, $issueId, $seq) {
722  $this->update(
723  'INSERT INTO custom_issue_orders (issue_id, journal_id, seq) VALUES (?, ?, ?)',
724  array(
725  (int) $issueId,
726  (int) $journalId,
727  $seq
728  )
729  );
730  }
731 
738  function moveCustomIssueOrder($journalId, $issueId, $newPos) {
739  $result = $this->retrieve('SELECT issue_id FROM custom_issue_orders WHERE journal_id=? AND issue_id=?', array((int) $journalId, (int) $issueId));
740  if (!$result->EOF) {
741  $this->update(
742  'UPDATE custom_issue_orders SET seq = ? WHERE journal_id = ? AND issue_id = ?',
743  array($newPos, (int) $journalId, (int) $issueId)
744  );
745  } else {
746  // This entry is missing. Create it.
747  $this->insertCustomIssueOrder($journalId, $issueId, $newPos);
748  }
749  $result->Close();
750  $this->resequenceCustomIssueOrders($journalId);
751  }
752 
760  function deleteAllPubIds($journalId, $pubIdType) {
761  $journalId = (int) $journalId;
762  $settingName = 'pub-id::'.$pubIdType;
763 
764  // issues
765  $issues = $this->getIssues($journalId);
766  while ($issue = $issues->next()) {
767  $this->update(
768  'DELETE FROM issue_settings WHERE setting_name = ? AND issue_id = ?',
769  array(
770  $settingName,
771  (int)$issue->getId()
772  )
773  );
774  }
775  $this->flushCache();
776  }
777 
781  function flushCache() {
782  $this->_getCache('issues')->flush();
783  $this->_getCache('current')->flush();
784  }
785 }
786 
787 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
deleteAllPubIds($journalId, $pubIdType)
Wrapper class for uploading files to a site/journal&#39;s public directory.
updateCurrent($journalId, $issue=null)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
insertCustomIssueOrder($journalId, $issueId, $seq)
getNumArticles($issueId)
getIssues($journalId, $rangeInfo=null)
resequenceCustomIssueOrders($journalId)
getAdditionalFieldNames()
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
Class defining operations for issue file management.
getByBestId($issueId, $journalId=null, $useCache=false)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
getCurrent($journalId, $useCache=false)
getCustomIssueOrder($journalId, $issueId)
getBySetting($settingName, $settingValue, $journalId=null)
insertObject($issue)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getByPubId($pubIdType, $pubId, $journalId=null, $useCache=false)
_getCache($cacheId)
Operations for retrieving and modifying Issue objects.
getIssueByArticleId($articleId, $journalId=null)
deleteCustomIssueOrdering($journalId)
getUnpublishedIssues($journalId, $rangeInfo=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
setDefaultCustomIssueOrders($journalId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
issueIdExists($issueId, $journalId)
Class for Issue.
Definition: Issue.inc.php:25
getPublishedIssuesByNumber($journalId, $volume=null, $number=null, $year=null)
updateObject($issue)
issueExists($journalId, $volume, $number, $year, $issueId)
_returnIssueFromRow($row)
pubIdExists($pubIdType, $pubId, $issueId, $journalId)
updateLocaleFields(&$issue)
customIssueOrderingExists($journalId)
moveCustomIssueOrder($journalId, $issueId, $newPos)
getLocaleFieldNames()
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
deleteByJournalId($journalId)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
deleteObject($issue)
_cacheMiss($cache, $id)
getById($issueId, $journalId=null, $useCache=false)
changePubId($issueId, $pubIdType, $pubId)
getPublishedIssues($journalId, $rangeInfo=null)