Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
BookForReviewDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.db.DAO');
18 
19 /* These constants are used for user-selectable search fields. */
20 define('BFR_FIELD_PUBLISHER', 'publisher');
21 define('BFR_FIELD_YEAR', 'year');
22 define('BFR_FIELD_ISBN', 'isbn');
23 define('BFR_FIELD_TITLE', 'title');
24 define('BFR_FIELD_DESCRIPTION', 'description');
25 define('BFR_FIELD_NONE', null);
26 
27 
28 class BookForReviewDAO extends DAO {
30  var $parentPluginName;
31 
33  var $bookForReviewAuthorDao;
34 
38  function BookForReviewDAO($parentPluginName) {
39  parent::DAO();
40  $this->parentPluginName = $parentPluginName;
41  $this->bookForReviewAuthorDao =& DAORegistry::getDAO('BookForReviewAuthorDAO');
42  }
43 
48  function getLocaleFieldNames() {
49  return array(
50  'title',
51  'description',
52  'coverPageAltText',
53  'originalFileName',
54  'fileName',
55  'width',
56  'height'
57  );
58  }
59 
64  function updateLocaleFields(&$book) {
65  $this->updateDataObjectSettings('books_for_review_settings', $book, array(
66  'book_id' => $book->getId()
67  ));
68  }
69 
75  function &getBookForReview($bookId) {
76  $result =& $this->retrieve(
77  'SELECT * FROM books_for_review WHERE book_id = ?', $bookId
78  );
79 
80  $returner = null;
81  if ($result->RecordCount() != 0) {
82  $returner =& $this->_returnBookForReviewFromRow($result->GetRowAssoc(false));
83  }
84  $result->Close();
85  return $returner;
86  }
87 
93  function getBookForReviewJournalId($bookId) {
94  $result =& $this->retrieve(
95  'SELECT journal_id FROM books_for_review WHERE book_id = ?', $bookId
96  );
97 
98  return isset($result->fields[0]) ? $result->fields[0] : 0;
99  }
100 
106  function &_returnBookForReviewFromRow(&$row) {
107  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
108  $bfrPlugin->import('classes.BookForReview');
109 
110  $book = new BookForReview();
111  $book->setId($row['book_id']);
112  $book->setJournalId($row['journal_id']);
113  $book->setStatus($row['status']);
114  $book->setUserId($row['user_id']);
115  $book->setEditorId($row['editor_id']);
116  $book->setAuthorType($row['author_type']);
117  $book->setPublisher($row['publisher']);
118  $book->setUrl($row['url']);
119  $book->setYear($row['year']);
120  $book->setLanguage($row['language']);
121  $book->setCopy($row['copy']);
122  $book->setEdition($row['edition']);
123  $book->setPages($row['pages']);
124  $book->setISBN($row['isbn']);
125  $book->setArticleId($row['article_id']);
126  $book->setNotes($row['notes']);
127  $book->setDateCreated($row['date_created']);
128  $book->setDateRequested($row['date_requested']);
129  $book->setDateAssigned($row['date_assigned']);
130  $book->setDateMailed($row['date_mailed']);
131  $book->setDateDue($row['date_due']);
132  $book->setDateSubmitted($row['date_submitted']);
133 
134  $book->setAuthors($this->bookForReviewAuthorDao->getAuthorsByBookForReview($row['book_id']));
135 
136  $this->getDataObjectSettings('books_for_review_settings', 'book_id', $row['book_id'], $book);
137 
138  HookRegistry::call('BookForReviewDAO::_returnBookForReviewFromRow', array(&$book, &$row));
139 
140  return $book;
141  }
142 
148  function insertObject(&$book) {
149  $ret = $this->update(
150  sprintf('
151  INSERT INTO books_for_review
152  (journal_id,
153  status,
154  user_id,
155  editor_id,
156  author_type,
157  publisher,
158  url,
159  year,
160  language,
161  copy,
162  edition,
163  pages,
164  isbn,
165  article_id,
166  notes,
167  date_created,
168  date_requested,
169  date_assigned,
170  date_mailed,
171  date_due,
172  date_submitted)
173  VALUES
174  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, %s, %s, %s, %s)',
175  $this->datetimeToDB($book->getDateCreated()),
176  $this->datetimeToDB($book->getDateRequested()),
177  $this->datetimeToDB($book->getDateAssigned()),
178  $this->datetimeToDB($book->getDateMailed()),
179  $this->datetimeToDB($book->getDateDue()),
180  $this->datetimeToDB($book->getDateSubmitted())
181  ),
182  array(
183  $book->getJournalId(),
184  $book->getStatus(),
185  $book->getUserId(),
186  $book->getEditorId(),
187  $book->getAuthorType(),
188  $book->getPublisher(),
189  $book->getUrl(),
190  $book->getYear(),
191  $book->getLanguage(),
192  $book->getCopy(),
193  $book->getEdition(),
194  $book->getPages(),
195  $book->getISBN(),
196  $book->getArticleId(),
197  $book->getNotes()
198  )
199  );
200  $book->setId($this->getInsertBookForReviewId());
201  $this->updateLocaleFields($book);
202 
203  // Insert authors for this book for review
204  $authors =& $book->getAuthors();
205  for ($i=0, $count=count($authors); $i < $count; $i++) {
206  $authors[$i]->setBookId($book->getId());
207  $this->bookForReviewAuthorDao->insertAuthor($authors[$i]);
208  }
209 
210  return $book->getId();
211  }
212 
218  function updateObject(&$book) {
219  $this->update(
220  sprintf('UPDATE books_for_review
221  SET
222  journal_id = ?,
223  status = ?,
224  user_id = ?,
225  editor_id = ?,
226  author_type = ?,
227  publisher = ?,
228  url = ?,
229  year = ?,
230  language = ?,
231  copy = ?,
232  edition = ?,
233  pages = ?,
234  isbn = ?,
235  article_id = ?,
236  notes = ?,
237  date_created = %s,
238  date_requested = %s,
239  date_assigned = %s,
240  date_mailed = %s,
241  date_due = %s,
242  date_submitted = %s
243  WHERE book_id = ?',
244  $this->datetimeToDB($book->getDateCreated()),
245  $this->datetimeToDB($book->getDateRequested()),
246  $this->datetimeToDB($book->getDateAssigned()),
247  $this->datetimeToDB($book->getDateMailed()),
248  $this->datetimeToDB($book->getDateDue()),
249  $this->datetimeToDB($book->getDateSubmitted())
250  ),
251  array(
252  $book->getJournalId(),
253  $book->getStatus(),
254  $this->nullOrInt($book->getUserId()),
255  $this->nullOrInt($book->getEditorId()),
256  $book->getAuthorType(),
257  $book->getPublisher(),
258  $book->getUrl(),
259  $book->getYear(),
260  $book->getLanguage(),
261  $book->getCopy(),
262  $this->nullOrInt($book->getEdition()),
263  $this->nullOrInt($book->getPages()),
264  $book->getISBN(),
265  $this->nullOrInt($book->getArticleId()),
266  $book->getNotes(),
267  $book->getId()
268  )
269  );
270 
271  $this->updateLocaleFields($book);
272 
273  // Update authors for this book for review
274  $authors =& $book->getAuthors();
275  for ($i=0, $count=count($authors); $i < $count; $i++) {
276  if ($authors[$i]->getId() > 0) {
277  $this->bookForReviewAuthorDao->updateAuthor($authors[$i]);
278  } else {
279  $this->bookForReviewAuthorDao->insertAuthor($authors[$i]);
280  }
281  }
282 
283  // Remove deleted authors
284  $removedAuthors = $book->getRemovedAuthors();
285  for ($i=0, $count=count($removedAuthors); $i < $count; $i++) {
286  $this->bookForReviewAuthorDao->deleteAuthorById($removedAuthors[$i], $book->getId());
287  }
288 
289  // Update author sequence numbers
290  $this->bookForReviewAuthorDao->resequenceAuthors($book->getId());
291  }
292 
297  function deleteObject($book) {
298  $this->deleteBookForReviewById($book->getId());
299  }
300 
305  function deleteBookForReviewById($bookId) {
306  $book =& $this->getBookForReview($bookId);
307 
308  if ($book) {
309  // Delete authors
310  $this->bookForReviewAuthorDao->deleteAuthorsByBookForReview($bookId);
311 
312  // Delete cover image files (for all locales) from the filesystem
313  import('classes.file.PublicFileManager');
314  $publicFileManager = new PublicFileManager();
315  $locales = AppLocale::getSupportedLocales();
316  foreach ($locales as $locale) {
317  $publicFileManager->removeJournalFile($book->getJournalId(), $book->getFileName($locale));
318  }
319 
320  // Delete settings
321  $this->update('DELETE FROM books_for_review_settings WHERE book_id = ?', $bookId);
322 
323  // Delete book
324  $this->update('DELETE FROM books_for_review WHERE book_id = ?', $bookId);
325  }
326  }
327 
332  function deleteBooksForReviewByJournal($journalId) {
333  $books = $this->getBooksForReviewByJournalId($journalId);
334 
335  while (!$books->eof()) {
336  $book =& $books->next();
337  $this->deleteBookForReviewById($book->getId());
338  }
339  }
340 
348  function &getBooksForReviewByAuthor($journalId, $userId, $rangeInfo = null) {
349  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
350  $bfrPlugin->import('classes.BookForReview');
351 
352  $sql = 'SELECT DISTINCT bfr.*
353  FROM books_for_review bfr
354  WHERE bfr.journal_id = ?
355  AND bfr.user_id = ?
356  ORDER BY bfr.book_id DESC';
357 
358  $paramArray = array(
359  (int) $journalId,
360  (int) $userId
361  );
362 
363  $result =& $this->retrieveRange($sql, $paramArray, $rangeInfo);
364  $returner = new DAOResultFactory($result, $this, '_returnBookForReviewFromRow');
365  return $returner;
366  }
367 
375  function &getBooksForReviewAssignedByAuthor($journalId, $userId, $rangeInfo = null) {
376  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
377  $bfrPlugin->import('classes.BookForReview');
378 
379  $sql = 'SELECT DISTINCT bfr.*
380  FROM books_for_review bfr
381  WHERE (bfr.status = ? OR bfr.status = ?)
382  AND bfr.journal_id = ?
383  AND bfr.user_id = ?
384  ORDER BY bfr.book_id DESC';
385 
386  $paramArray = array(
387  BFR_STATUS_ASSIGNED,
388  BFR_STATUS_MAILED,
389  (int) $journalId,
390  (int) $userId
391  );
392 
393  $result =& $this->retrieveRange($sql, $paramArray, $rangeInfo);
394  $returner = new DAOResultFactory($result, $this, '_returnBookForReviewFromRow');
395  return $returner;
396  }
397 
405  function &getBooksForReviewByDateDue($journalId, $dateDue, $rangeInfo = null) {
406  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
407  $bfrPlugin->import('classes.BookForReview');
408 
409  $sql = sprintf(
410  'SELECT DISTINCT bfr.*
411  FROM books_for_review bfr
412  WHERE (bfr.status = ? OR bfr.status = ?)
413  AND bfr.journal_id = ?
414  AND DATE(bfr.date_due) = %s
415  ORDER BY bfr.book_id',
416  $this->dateToDB($dateDue));
417 
418  $paramArray = array(
419  BFR_STATUS_ASSIGNED,
420  BFR_STATUS_MAILED,
421  (int) $journalId
422  );
423 
424  $result =& $this->retrieveRange($sql, $paramArray, $rangeInfo);
425  $returner = new DAOResultFactory($result, $this, '_returnBookForReviewFromRow');
426  return $returner;
427  }
428 
441  function &getBooksForReviewByJournalId($journalId, $searchType = null, $search = null, $searchMatch = null, $status = null, $userId = null, $editorId = null, $rangeInfo = null) {
442  $sql = 'SELECT DISTINCT bfr.* FROM books_for_review bfr';
443  $paramArray = array();
444 
445  switch ($searchType) {
446  case BFR_FIELD_PUBLISHER:
447  $sql .= ' WHERE LOWER(bfr.publisher) ' . ($searchMatch=='is'?'=':'LIKE') . ' LOWER(?)';
448  $paramArray[] = $searchMatch == 'is' ? $search : "%$search%";
449  break;
450  case BFR_FIELD_YEAR:
451  $sql .= ' WHERE bfr.year = ?';
452  $paramArray[] = (int) $search;
453  break;
454  case BFR_FIELD_ISBN:
455  $sql .= ' WHERE LOWER(bfr.isbn) ' . ($searchMatch=='is'?'=':'LIKE') . ' LOWER(?)';
456  $paramArray[] = $searchMatch == 'is' ? $search : "%$search%";
457  break;
458  case BFR_FIELD_TITLE:
459  $sql .= ', books_for_review_settings bfrs WHERE bfrs.book_id = bfr.book_id AND bfrs.setting_name = \'title\' AND LOWER(bfrs.setting_value) ' . ($searchMatch == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
460  $paramArray[] = $searchMatch == 'is' ? $search : "%$search%";
461  break;
462  case BFR_FIELD_DESCRIPTION:
463  $sql .= ', books_for_review_settings bfrs WHERE bfrs.book_id = bfr.book_id AND bfrs.setting_name = \'description\' AND LOWER(bfrs.setting_value) ' . ($searchMatch == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
464  $paramArray[] = $searchMatch == 'is' ? $search : "%$search%";
465  break;
466  default:
467  $searchType = null;
468  }
469 
470  if (empty($searchType)) {
471  $sql .= ' WHERE';
472  } else {
473  $sql .= ' AND';
474  }
475 
476  if (!empty($status)) {
477  $sql .= ' bfr.status = ? AND';
478  $paramArray[] = (int) $status;
479  }
480 
481  if (!empty($userId)) {
482  $sql .= ' bfr.user_id = ? AND';
483  $paramArray[] = (int) $userId;
484  }
485 
486  if (!empty($editorId)) {
487  $sql .= ' bfr.editor_id = ? AND';
488  $paramArray[] = (int) $editorId;
489  }
490 
491  $sql .= ' bfr.journal_id = ? ORDER BY bfr.book_id DESC';
492  $paramArray[] = (int) $journalId;
493 
494  $result =& $this->retrieveRange($sql, $paramArray, $rangeInfo);
495  $returner = new DAOResultFactory($result, $this, '_returnBookForReviewFromRow');
496  return $returner;
497  }
498 
504  function &getSubmittedBookForReviewByArticle($journalId, $articleId) {
505  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
506  $bfrPlugin->import('classes.BookForReview');
507 
508  $result =& $this->retrieve(
509  'SELECT *
510  FROM books_for_review
511  WHERE article_id = ?
512  AND status = ?
513  AND journal_id = ?',
514  array(
515  $articleId,
516  BFR_STATUS_SUBMITTED,
517  $journalId
518  )
519  );
520 
521  $returner = null;
522  if ($result->RecordCount() != 0) {
523  $returner =& $this->_returnBookForReviewFromRow($result->GetRowAssoc(false));
524  }
525  $result->Close();
526  return $returner;
527  }
528 
535  function getSubmittedBookForReviewIdByArticle($journalId, $articleId) {
536  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
537  $bfrPlugin->import('classes.BookForReview');
538 
539  $result =& $this->retrieve(
540  'SELECT book_id
541  FROM books_for_review
542  WHERE article_id = ?
543  AND status = ?
544  AND journal_id = ?',
545  array(
546  $articleId,
547  BFR_STATUS_SUBMITTED,
548  $journalId
549  )
550  );
551 
552  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? $result->fields[0] : null;
553 
554  $result->Close();
555  unset($result);
556 
557  return $returner;
558  }
559 
567  function getBooksForReviewStatusCount($journalId, $status = null, $userId = null) {
568  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
569  $bfrPlugin->import('classes.BookForReview');
570 
571  $sql = 'SELECT COUNT(*)
572  FROM books_for_review bfr
573  WHERE bfr.journal_id = ?';
574  $paramArray = array((int)$journalId);
575 
576  if ($status) {
577  $sql .= ' AND bfr.status = ?';
578  $paramArray[] = (int) $status;
579  }
580 
581  if ($userId) {
582  $sql .= ' AND bfr.user_id = ?';
583  $paramArray[] = (int) $userId;
584  }
585 
586  $result =& $this->retrieve($sql, $paramArray);
587  return isset($result->fields[0]) ? $result->fields[0] : 0;
588  }
589 
596  function &getStatusCounts($journalId, $userId = null) {
597  $bfrPlugin =& PluginRegistry::getPlugin('generic', $this->parentPluginName);
598  $bfrPlugin->import('classes.BookForReview');
599  $counts = array();
600 
601  $counts[BFR_STATUS_AVAILABLE] = $this->getBooksForReviewStatusCount($journalId, BFR_STATUS_AVAILABLE, $userId);
602  $counts[BFR_STATUS_REQUESTED] = $this->getBooksForReviewStatusCount($journalId, BFR_STATUS_REQUESTED, $userId);
603  $counts[BFR_STATUS_ASSIGNED] = $this->getBooksForReviewStatusCount($journalId, BFR_STATUS_ASSIGNED, $userId);
604  $counts[BFR_STATUS_MAILED] = $this->getBooksForReviewStatusCount($journalId, BFR_STATUS_MAILED, $userId);
605  $counts[BFR_STATUS_SUBMITTED] = $this->getBooksForReviewStatusCount($journalId, BFR_STATUS_SUBMITTED, $userId);
606 
607  return $counts;
608  }
609 
614  function removeCoverPage($bookId, $locale) {
615  $book =& $this->getBookForReview($bookId);
616 
617  if ($book) {
618  // Delete cover image file from the filesystem and settings
619  import('classes.file.PublicFileManager');
620  $publicFileManager = new PublicFileManager();
621  $publicFileManager->removeJournalFile($book->getJournalId(), $book->getFileName($locale));
622 
623  $book->setFileName(null, $locale);
624  $book->setWidth(null, $locale);
625  $book->setHeight(null, $locale);
626  $book->setOriginalFileName(null, $locale);
627  $book->setCoverPageAltText(null, $locale);
628 
629  $this->updateObject($book);
630  }
631  }
632 
638  function changeBookForReviewStatus($bookId, $status) {
639  $this->update(
640  'UPDATE books_for_review SET status = ? WHERE book_id = ?', array($status, $bookId)
641  );
642  }
643 
649  return $this->getInsertId('books_for_review', 'book_id');
650  }
651 }
652 
653 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& _returnBookForReviewFromRow(&$row)
Wrapper class for uploading files to a site/journal&#39;s public directory.
& getPlugin($category, $name)
changeBookForReviewStatus($bookId, $status)
BookForReviewDAO($parentPluginName)
deleteBooksForReviewByJournal($journalId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& getBooksForReviewByJournalId($journalId, $searchType=null, $search=null, $searchMatch=null, $status=null, $userId=null, $editorId=null, $rangeInfo=null)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:460
& getBooksForReviewByAuthor($journalId, $userId, $rangeInfo=null)
dateToDB($d)
Definition: DAO.inc.php:306
& getBooksForReviewAssignedByAuthor($journalId, $userId, $rangeInfo=null)
removeCoverPage($bookId, $locale)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
getSubmittedBookForReviewIdByArticle($journalId, $articleId)
Basic class describing a book for review.
getBooksForReviewStatusCount($journalId, $status=null, $userId=null)
getBookForReviewJournalId($bookId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
& getStatusCounts($journalId, $userId=null)
& getDAO($name, $dbconn=null)
getSupportedLocales()
Operations for retrieving and modifying BookForReview objects.
call($hookName, $args=null)
& getSubmittedBookForReviewByArticle($journalId, $articleId)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getBooksForReviewByDateDue($journalId, $dateDue, $rangeInfo=null)