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 
22  function _cacheMiss($cache, $id) {
23  if ($cache->getCacheId() === 'current') {
24  $issue = $this->getCurrent($id, false);
25  } else {
26  $issue = $this->getById($id, null, false);
27  }
28  $cache->setCache($id, $issue);
29  return $issue;
30  }
31 
32  function _getCache($cacheId) {
33  if (!isset($this->caches)) $this->caches = array();
34  if (!isset($this->caches[$cacheId])) {
35  $cacheManager = CacheManager::getManager();
36  $this->caches[$cacheId] = $cacheManager->getObjectCache('issues', $cacheId, array($this, '_cacheMiss'));
37  }
38  return $this->caches[$cacheId];
39  }
40 
48  function getById($issueId, $journalId = null, $useCache = false) {
49  if ($useCache) {
50  $cache = $this->_getCache('issues');
51  $returner = $cache->get($issueId);
52  if ($returner && $journalId != null && $journalId != $returner->getJournalId()) $returner = null;
53  return $returner;
54  }
55 
56  $params = array((int) $issueId);
57  if ($journalId) $params[] = (int) $journalId;
58  $result = $this->retrieve(
59  'SELECT i.* FROM issues i WHERE issue_id = ?'
60  . ($journalId?' AND journal_id = ?':''),
61  $params
62  );
63 
64  $issue = null;
65  if ($result->RecordCount() != 0) {
66  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
67  }
68  $result->Close();
69  return $issue;
70  }
71 
82  function getByPubId($pubIdType, $pubId, $journalId = null, $useCache = false) {
83  if ($useCache && $pubIdType == 'publisher-id') {
84  $cache = $this->_getCache('issues');
85  $returner = $cache->get($pubId);
86  if ($returner && $journalId != null && $journalId != $returner->getJournalId()) $returner = null;
87  return $returner;
88  }
89 
90  $issues = $this->getBySetting('pub-id::'.$pubIdType, $pubId, $journalId);
91  if (empty($issues)) {
92  return null;
93  } else {
94  assert(count($issues) == 1);
95  return $issues[0];
96  }
97  }
98 
106  function getBySetting($settingName, $settingValue, $journalId = null) {
107  $params = array($settingName);
108  $sql = 'SELECT i.*
109  FROM issues i ';
110  if (is_null($settingValue)) {
111  $sql .= 'LEFT JOIN issue_settings ist ON i.issue_id = ist.issue_id AND ist.setting_name = ?
112  WHERE (ist.setting_value IS NULL OR ist.setting_value = "")';
113  } else {
114  $params[] = $settingValue;
115  $sql .= 'INNER JOIN issue_settings ist ON i.issue_id = ist.issue_id
116  WHERE ist.setting_name = ? AND ist.setting_value = ?';
117  }
118  if ($journalId) {
119  $params[] = (int) $journalId;
120  $sql .= ' AND i.journal_id = ?';
121  }
122  $sql .= ' ORDER BY i.issue_id';
123  $result = $this->retrieve($sql, $params);
124 
125  $issues = array();
126  while (!$result->EOF) {
127  $issues[] = $this->_returnIssueFromRow($result->GetRowAssoc(false));
128  $result->MoveNext();
129  }
130  $result->Close();
131  return $issues;
132  }
133 
142  function getPublishedIssuesByNumber($journalId, $volume = null, $number = null, $year = null) {
143  $sql = 'SELECT i.* FROM issues i WHERE i.published = 1 AND i.journal_id = ?';
144  $params = array((int) $journalId);
145 
146  if ($volume !== null) {
147  $sql .= ' AND i.volume = ?';
148  $params[] = $volume;
149  }
150  if ($number !== null) {
151  $sql .= ' AND i.number = ?';
152  $params[] = $number;
153  }
154  if ($year !== null) {
155  $sql .= ' AND i.year = ?';
156  $params[] = $year;
157  }
158 
159  $result = $this->retrieve($sql, $params);
160  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
161  }
162 
171  function getByBestId($issueId, $journalId = null, $useCache = false) {
172  $issue = $this->getByPubId('publisher-id', $issueId, $journalId, $useCache);
173  if (!isset($issue) && ctype_digit("$issueId")) $issue = $this->getById((int) $issueId, $journalId, $useCache);
174  return $issue;
175  }
176 
183  function getCurrent($journalId, $useCache = false) {
184  if ($useCache) {
185  $cache = $this->_getCache('current');
186  return $cache->get($journalId);
187  }
188 
189  $result = $this->retrieve(
190  'SELECT i.* FROM issues i WHERE journal_id = ? AND current = 1', (int) $journalId
191  );
192 
193  $issue = null;
194  if ($result->RecordCount() != 0) {
195  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
196  }
197  $result->Close();
198  return $issue;
199  }
200 
205  function updateCurrent($journalId, $issue = null) {
206  $this->update(
207  'UPDATE issues SET current = 0 WHERE journal_id = ? AND current = 1', (int) $journalId
208  );
209  if ($issue) $this->updateObject($issue);
210 
211  $this->flushCache();
212  }
213 
214 
223  function changePubId($issueId, $pubIdType, $pubId) {
224  $idFields = array(
225  'issue_id', 'locale', 'setting_name'
226  );
227  $updateArray = array(
228  'issue_id' => $issueId,
229  'locale' => '',
230  'setting_name' => 'pub-id::'.$pubIdType,
231  'setting_type' => 'string',
232  'setting_value' => (string)$pubId
233  );
234  $this->replace('issue_settings', $updateArray, $idFields);
235  $this->flushCache();
236  }
237 
242  function newDataObject() {
243  return new Issue();
244  }
245 
251  function _returnIssueFromRow($row) {
252  $issue = $this->newDataObject();
253  $issue->setId($row['issue_id']);
254  $issue->setJournalId($row['journal_id']);
255  $issue->setVolume($row['volume']);
256  $issue->setNumber($row['number']);
257  $issue->setYear($row['year']);
258  $issue->setPublished($row['published']);
259  $issue->setCurrent($row['current']);
260  $issue->setDatePublished($this->datetimeFromDB($row['date_published']));
261  $issue->setDateNotified($this->datetimeFromDB($row['date_notified']));
262  $issue->setLastModified($this->datetimeFromDB($row['last_modified']));
263  $issue->setAccessStatus($row['access_status']);
264  $issue->setOpenAccessDate($this->datetimeFromDB($row['open_access_date']));
265  $issue->setShowVolume($row['show_volume']);
266  $issue->setShowNumber($row['show_number']);
267  $issue->setShowYear($row['show_year']);
268  $issue->setShowTitle($row['show_title']);
269  $issue->setStyleFileName($row['style_file_name']);
270  $issue->setOriginalStyleFileName($row['original_style_file_name']);
271 
272  $this->getDataObjectSettings('issue_settings', 'issue_id', $row['issue_id'], $issue);
273 
274  HookRegistry::call('IssueDAO::_returnIssueFromRow', array(&$issue, &$row));
275 
276  return $issue;
277  }
278 
283  function getLocaleFieldNames() {
284  return array('title', 'coverPageDescription', 'coverPageAltText', 'showCoverPage', 'hideCoverPageArchives', 'hideCoverPageCover', 'originalFileName', 'fileName', 'width', 'height', 'description');
285  }
286 
293  $additionalFields = parent::getAdditionalFieldNames();
294  // FIXME: Move this to a PID plug-in.
295  $additionalFields[] = 'pub-id::publisher-id';
296  return $additionalFields;
297  }
298 
303  function updateLocaleFields(&$issue) {
304  $this->updateDataObjectSettings('issue_settings', $issue, array(
305  'issue_id' => $issue->getId()
306  ));
307  }
308 
314  function insertObject($issue) {
315  $this->update(
316  sprintf('INSERT INTO issues
317  (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)
318  VALUES
319  (?, ?, ?, ?, ?, ?, %s, %s, %s, ?, %s, ?, ?, ?, ?, ?, ?)',
320  $this->datetimeToDB($issue->getDatePublished()), $this->datetimeToDB($issue->getDateNotified()), $this->datetimeToDB($issue->getLastModified()), $this->datetimeToDB($issue->getOpenAccessDate())),
321  array(
322  (int) $issue->getJournalId(),
323  $issue->getVolume(),
324  $issue->getNumber(),
325  $issue->getYear(),
326  (int) $issue->getPublished(),
327  (int) $issue->getCurrent(),
328  (int) $issue->getAccessStatus(),
329  (int) $issue->getShowVolume(),
330  (int) $issue->getShowNumber(),
331  (int) $issue->getShowYear(),
332  (int) $issue->getShowTitle(),
333  $issue->getStyleFileName(),
334  $issue->getOriginalStyleFileName()
335  )
336  );
337 
338  $issue->setId($this->getInsertId());
339 
340  $this->updateLocaleFields($issue);
341 
342  if ($this->customIssueOrderingExists($issue->getJournalId())) {
343  $this->resequenceCustomIssueOrders($issue->getJournalId());
344  }
345 
346  return $issue->getId();
347  }
348 
353  function getInsertId() {
354  return $this->_getInsertId('issues', 'issue_id');
355  }
356 
366  function issueExists($journalId, $volume, $number, $year, $issueId) {
367  $result = $this->retrieve(
368  'SELECT i.* FROM issues i WHERE journal_id = ? AND volume = ? AND number = ? AND year = ? AND issue_id <> ?',
369  array((int) $journalId, $volume, $number, $year, (int) $issueId)
370  );
371  $returner = $result->RecordCount() != 0 ? true : false;
372  $result->Close();
373  return $returner;
374  }
375 
380  function updateObject($issue) {
381  $issue->stampModified();
382  $this->update(
383  sprintf('UPDATE issues
384  SET
385  journal_id = ?,
386  volume = ?,
387  number = ?,
388  year = ?,
389  published = ?,
390  current = ?,
391  date_published = %s,
392  date_notified = %s,
393  last_modified = %s,
394  open_access_date = %s,
395  access_status = ?,
396  show_volume = ?,
397  show_number = ?,
398  show_year = ?,
399  show_title = ?,
400  style_file_name = ?,
401  original_style_file_name = ?
402  WHERE issue_id = ?',
403  $this->datetimeToDB($issue->getDatePublished()), $this->datetimeToDB($issue->getDateNotified()), $this->datetimeToDB($issue->getLastModified()), $this->datetimeToDB($issue->getOpenAccessDate())),
404  array(
405  (int) $issue->getJournalId(),
406  $issue->getVolume(),
407  $issue->getNumber(),
408  $issue->getYear(),
409  (int) $issue->getPublished(),
410  (int) $issue->getCurrent(),
411  (int) $issue->getAccessStatus(),
412  (int) $issue->getShowVolume(),
413  (int) $issue->getShowNumber(),
414  (int) $issue->getShowYear(),
415  (int) $issue->getShowTitle(),
416  $issue->getStyleFileName(),
417  $issue->getOriginalStyleFileName(),
418  (int) $issue->getId()
419  )
420  );
421 
422  $this->updateLocaleFields($issue);
423 
424  if ($this->customIssueOrderingExists($issue->getJournalId())) {
425  $this->resequenceCustomIssueOrders($issue->getJournalId());
426  }
427 
428  $this->flushCache();
429  }
430 
435  function deleteObject($issue) {
436  import('classes.file.PublicFileManager');
437  $publicFileManager = new PublicFileManager();
438 
439  if (is_array($issue->getFileName(null))) foreach ($issue->getFileName(null) as $fileName) {
440  if ($fileName != '') {
441  $publicFileManager->removeJournalFile($issue->getJournalId(), $fileName);
442  }
443  }
444  if (($fileName = $issue->getStyleFileName()) != '') {
445  $publicFileManager->removeJournalFile($issue->getJournalId(), $fileName);
446  }
447 
448  $issueId = $issue->getId();
449 
450  // Delete issue-specific ordering if it exists.
451  $sectionDao = DAORegistry::getDAO('SectionDAO');
452  $sectionDao->deleteCustomSectionOrdering($issueId);
453 
454  // Delete published issue galleys and issue files
455  $issueGalleyDao = DAORegistry::getDAO('IssueGalleyDAO');
456  $issueGalleyDao->deleteByIssueId($issueId);
457 
458  $issueFileDao = DAORegistry::getDAO('IssueFileDAO');
459  $issueFileDao->deleteByIssueId($issueId);
460 
461  import('classes.file.IssueFileManager');
462  $issueFileManager = new IssueFileManager($issueId);
463  $issueFileManager->deleteIssueTree();
464 
465  // Delete published articles
466  $publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
467  $publishedArticleDao->deletePublishedArticlesByIssueId($issueId);
468 
469  // Delete issue settings and issue
470  $this->update('DELETE FROM issue_settings WHERE issue_id = ?', (int) $issueId);
471  $this->update('DELETE FROM issues WHERE issue_id = ?', (int) $issueId);
472  $this->resequenceCustomIssueOrders($issue->getJournalId());
473 
474  $this->flushCache();
475  }
476 
481  function deleteByJournalId($journalId) {
482  $issues = $this->getIssues($journalId);
483  while ($issue = $issues->next()) {
484  $this->deleteObject($issue);
485  }
486  }
487 
494  function issueIdExists($issueId, $journalId) {
495  $result = $this->retrieve(
496  'SELECT COUNT(*) FROM issues WHERE issue_id = ? AND journal_id = ?',
497  array((int) $issueId, (int) $journalId)
498  );
499  return $result->fields[0] ? true : false;
500  }
501 
513  function pubIdExists($pubIdType, $pubId, $issueId, $journalId) {
514  $result = $this->retrieve(
515  'SELECT COUNT(*)
516  FROM issue_settings ist
517  INNER JOIN issues i ON ist.issue_id = i.issue_id
518  WHERE ist.setting_name = ? AND ist.setting_value = ? AND i.issue_id <> ? AND i.journal_id = ?',
519  array(
520  'pub-id::'.$pubIdType,
521  $pubId,
522  (int) $issueId,
523  (int) $journalId
524  )
525  );
526  $returner = $result->fields[0] ? true : false;
527  $result->Close();
528  return $returner;
529  }
530 
537  function getIssueByArticleId($articleId, $journalId = null) {
538  $params = array((int) $articleId);
539  $sql = 'SELECT i.*
540  FROM issues i,
541  published_submissions pa,
542  submissions a
543  WHERE i.issue_id = pa.issue_id AND
544  pa.submission_id = ? AND
545  pa.submission_id = a.submission_id';
546  if ($journalId !== null) {
547  $sql .= ' AND i.journal_id = ? AND a.context_id = i.journal_id';
548  $params[] = (int) $journalId;
549  }
550 
551  $result = $this->retrieve($sql, $params);
552 
553  $issue = null;
554  if ($result->RecordCount() != 0) {
555  $issue = $this->_returnIssueFromRow($result->GetRowAssoc(false));
556  }
557 
558  $result->Close();
559  return $issue;
560  }
561 
568  function getIssues($journalId, $rangeInfo = null) {
569  $result = $this->retrieveRange(
570  'SELECT i.* FROM issues i WHERE journal_id = ? ORDER BY current DESC, date_published DESC',
571  (int) $journalId, $rangeInfo
572  );
573 
574  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
575  }
576 
583  function getPublishedIssues($journalId, $rangeInfo = null) {
584  $result = $this->retrieveRange(
585  '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',
586  (int) $journalId, $rangeInfo
587  );
588 
589  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
590  }
591 
598  function getUnpublishedIssues($journalId, $rangeInfo = null) {
599  $result = $this->retrieveRange(
600  'SELECT i.* FROM issues i WHERE journal_id = ? AND published = 0 ORDER BY year ASC, volume ASC, number ASC',
601  (int) $journalId, $rangeInfo
602  );
603 
604  return new DAOResultFactory($result, $this, '_returnIssueFromRow');
605  }
606 
612  function getNumArticles($issueId) {
613  $result = $this->retrieve('SELECT COUNT(*) FROM published_submissions WHERE issue_id = ?', (int) $issueId);
614  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
615  $result->Close();
616  return $returner;
617  }
618 
623  function deleteCustomIssueOrdering($journalId) {
624  $this->update(
625  'DELETE FROM custom_issue_orders WHERE journal_id = ?', (int) $journalId
626  );
627  }
628 
633  function resequenceCustomIssueOrders($journalId) {
634  $result = $this->retrieve(
635  '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',
636  (int) $journalId
637  );
638 
639  for ($i=1; !$result->EOF; $i++) {
640  list($issueId) = $result->fields;
641  $resultB = $this->retrieve('SELECT issue_id FROM custom_issue_orders WHERE journal_id=? AND issue_id=?', array($journalId, $issueId));
642  if (!$resultB->EOF) {
643  $this->update(
644  'UPDATE custom_issue_orders SET seq = ? WHERE issue_id = ? AND journal_id = ?',
645  array($i, $issueId, $journalId)
646  );
647  } else {
648  // This entry is missing. Create it.
649  $this->insertCustomIssueOrder($journalId, $issueId, $i);
650  }
651  $resultB->Close();
652  $result->MoveNext();
653  }
654  $result->Close();
655  }
656 
662  function customIssueOrderingExists($journalId) {
663  $result = $this->retrieve(
664  'SELECT COUNT(*) FROM custom_issue_orders WHERE journal_id = ?',
665  (int) $journalId
666  );
667  $returner = isset($result->fields[0]) && $result->fields[0] == 0 ? false : true;
668  $result->Close();
669  return $returner;
670  }
671 
678  function getCustomIssueOrder($journalId, $issueId) {
679  $result = $this->retrieve(
680  'SELECT seq FROM custom_issue_orders WHERE journal_id = ? AND issue_id = ?',
681  array((int) $journalId, (int) $issueId)
682  );
683 
684  $returner = null;
685  if (!$result->EOF) {
686  list($returner) = $result->fields;
687  }
688  $result->Close();
689  return $returner;
690  }
691 
697  function setDefaultCustomIssueOrders($journalId) {
698  $publishedIssues = $this->getPublishedIssues($journalId);
699  for ($i=1; $issue = $publishedIssues->next(); $i++) {
700  $this->insertCustomIssueOrder($journalId, $issue->getId(), $i);
701  }
702  }
703 
710  function insertCustomIssueOrder($journalId, $issueId, $seq) {
711  $this->update(
712  'INSERT INTO custom_issue_orders (issue_id, journal_id, seq) VALUES (?, ?, ?)',
713  array(
714  (int) $issueId,
715  (int) $journalId,
716  $seq
717  )
718  );
719  }
720 
727  function moveCustomIssueOrder($journalId, $issueId, $newPos) {
728  $result = $this->retrieve('SELECT issue_id FROM custom_issue_orders WHERE journal_id=? AND issue_id=?', array((int) $journalId, (int) $issueId));
729  if (!$result->EOF) {
730  $this->update(
731  'UPDATE custom_issue_orders SET seq = ? WHERE journal_id = ? AND issue_id = ?',
732  array($newPos, (int) $journalId, (int) $issueId)
733  );
734  } else {
735  // This entry is missing. Create it.
736  $this->insertCustomIssueOrder($journalId, $issueId, $newPos);
737  }
738  $result->Close();
739  $this->resequenceCustomIssueOrders($journalId);
740  }
741 
749  function deleteAllPubIds($journalId, $pubIdType) {
750  $journalId = (int) $journalId;
751  $settingName = 'pub-id::'.$pubIdType;
752 
753  // issues
754  $issues = $this->getIssues($journalId);
755  while ($issue = $issues->next()) {
756  $this->update(
757  'DELETE FROM issue_settings WHERE setting_name = ? AND issue_id = ?',
758  array(
759  $settingName,
760  (int)$issue->getId()
761  )
762  );
763  }
764  $this->flushCache();
765  }
766 
770  function flushCache() {
771  $cache = $this->_getCache('issues')->flush();
772  $cache = $this->_getCache('current')->flush();
773  }
774 }
775 
776 ?>
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)
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)
getById($issueId, $journalId=null, $useCache=false)
changePubId($issueId, $pubIdType, $pubId)
getPublishedIssues($journalId, $rangeInfo=null)