Open Monograph Press  3.3.0
CatalogBookHandler.inc.php
1 <?php
2 
17 import('classes.handler.Handler');
18 
19 // import UI base classes
20 import('lib.pkp.classes.linkAction.LinkAction');
21 import('lib.pkp.classes.core.JSONMessage');
22 
23 class CatalogBookHandler extends Handler {
25  public $publication;
26 
28  public $isVersionRequest = false;
29 
33  function __construct() {
34  parent::__construct();
35  }
36 
37 
38  //
39  // Overridden functions from PKPHandler
40  //
47  function authorize($request, &$args, $roleAssignments) {
48  import('classes.security.authorization.OmpPublishedSubmissionAccessPolicy');
49  $this->addPolicy(new OmpPublishedSubmissionAccessPolicy($request, $args, $roleAssignments));
50  return parent::authorize($request, $args, $roleAssignments);
51  }
52 
53 
54  //
55  // Public handler methods
56  //
62  function book($args, $request) {
63  $templateMgr = TemplateManager::getManager($request);
64  $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
65  $this->setupTemplate($request, $submission);
66  AppLocale::requireComponents(LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_PKP_SUBMISSION); // submission.synopsis; submission.copyrightStatement
67 
68  // Get the requested publication or default to the current publication
69  $submissionId = array_shift($args);
70  $subPath = empty($args) ? 0 : array_shift($args);
71  if ($subPath === 'version') {
72  $this->isVersionRequest = true;
73  $publicationId = (int) array_shift($args);
74  foreach ($submission->getData('publications') as $publication) {
75  if ($publication->getId() === $publicationId) {
76  $this->publication = $publication;
77  }
78  }
79  } else {
80  $this->publication = $submission->getCurrentPublication();
81  }
82 
83  if (!$this->publication || $this->publication->getData('status') !== STATUS_PUBLISHED) {
84  $request->getDispatcher()->handle404();
85  }
86 
87  // If the publication has been reached through an outdated
88  // urlPath, redirect to the latest version
89  if (!ctype_digit((string) $submissionId) && $submissionId !== $this->publication->getData('urlPath') && !$subPath) {
90  $newArgs = $args;
91  $newArgs = $this->publication->getData('urlPath')
92  ? $this->publication->getData('urlPath')
93  : $this->publication->getId();
94  $request->redirect(null, $request->getRequestedPage(), $request->getRequestedOp(), $newArgs);
95  }
96 
97  $templateMgr->assign([
98  'publishedSubmission' => $submission,
99  'publication' => $this->publication,
100  'firstPublication' => reset($submission->getData('publications')),
101  'currentPublication' => $submission->getCurrentPublication(),
102  'authorString' => $this->publication->getAuthorString(DAORegistry::getDAO('UserGroupDAO')->getByContextId($submission->getData('contextId'))->toArray()),
103  ]);
104 
105  // Provide the publication formats to the template
106  $availablePublicationFormats = [];
107  $availableRemotePublicationFormats = [];
108  foreach ($this->publication->getData('publicationFormats') as $format) {
109  if ($format->getIsAvailable()) {
110  $availablePublicationFormats[] = $format;
111  if ($format->getRemoteURL()) {
112  $availableRemotePublicationFormats[] = $format;
113  }
114  }
115  }
116  $templateMgr->assign(array(
117  'publicationFormats' => $availablePublicationFormats,
118  'remotePublicationFormats' => $availableRemotePublicationFormats,
119  ));
120 
121  // Assign chapters (if they exist)
122  $templateMgr->assign('chapters', DAORegistry::getDAO('ChapterDAO')->getByPublicationId($this->publication->getId())->toAssociativeArray());
123 
124  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
125  $templateMgr->assign(array(
126  'pubIdPlugins' => PluginRegistry::loadCategory('pubIds', true),
127  'ccLicenseBadge' => Application::getCCLicenseBadge($this->publication->getData('licenseUrl')),
128  ));
129 
130  // Categories
131  $templateMgr->assign([
132  'categories' => DAORegistry::getDAO('CategoryDAO')->getByPublicationId($this->publication->getId())->toArray(),
133  ]);
134 
135  // Citations
136  if ($this->publication->getData('citationsRaw')) {
137  $parsedCitations = DAORegistry::getDAO('CitationDAO')->getByPublicationId($this->publication->getId());
138  $templateMgr->assign([
139  'citations' => $parsedCitations->toArray(),
140  'parsedCitations' => $parsedCitations, // compatible with older themes
141  ]);
142  }
143 
144  // Retrieve editors for an edited volume
145  $editors = [];
146  if ($submission->getWorkType() == WORK_TYPE_EDITED_VOLUME) {
147  foreach ($this->publication->getData('authors') as $author) {
148  if ($author->getIsVolumeEditor()) {
149  $editors[] = $author;
150  }
151  }
152  }
153  $templateMgr->assign([
154  'editors' => $editors,
155  ]);
156 
157  // Consider public identifiers
158  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
159  $templateMgr->assign('pubIdPlugins', $pubIdPlugins);
160 
161  $availableFiles = array_filter(
162  DAORegistry::getDAO('SubmissionFileDAO')->getLatestRevisions($submission->getId(), null, null),
163  function($a) {
164  return $a->getDirectSalesPrice() !== null && $a->getAssocType() == ASSOC_TYPE_PUBLICATION_FORMAT;
165  }
166  );
167 
168  // Only pass files in pub formats that are also available
169  $filteredAvailableFiles = array();
170  foreach ($availableFiles as $file) {
171  foreach ($availablePublicationFormats as $format) {
172  if ($file->getAssocId() == $format->getId()) {
173  $filteredAvailableFiles[] = $file;
174  break;
175  }
176  }
177  }
178  $templateMgr->assign('availableFiles', $filteredAvailableFiles);
179 
180  // Provide the currency to the template, if configured.
181  if ($currencyCode = $request->getContext()->getData('currency')) {
182  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
183  $templateMgr->assign('currency', $isoCodes->getCurrencies()->getByLetterCode($currencyCode));
184  }
185 
186  // Add data for backwards compatibility
187  $templateMgr->assign([
188  'keywords' => $this->publication->getLocalizedData('keywords'),
189  'licenseUrl' => $this->publication->getData('licenseUrl'),
190  ]);
191 
192  // Ask robots not to index outdated versions and point to the canonical url for the latest version
193  if ($this->publication->getId() !== $submission->getCurrentPublication()->getId()) {
194  $templateMgr->addHeader('noindex', '<meta name="robots" content="noindex">');
195  $url = $request->getDispatcher()->url($request, ROUTE_PAGE, null, 'catalog', 'book', $submission->getBestId());
196  $templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
197  }
198 
199  // Display
200  if (!HookRegistry::call('CatalogBookHandler::book', array(&$request, &$submission))) {
201  return $templateMgr->display('frontend/pages/book.tpl');
202  }
203  }
204 
211  function view($args, $request) {
212  $this->download($args, $request, true);
213  }
214 
221  function download($args, $request, $view = false) {
222  $dispatcher = $request->getDispatcher();
223  $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
224  $this->setupTemplate($request, $submission);
225  $press = $request->getPress();
226  AppLocale::requireComponents(LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_PKP_SUBMISSION);
227 
228  $monographId = array_shift($args); // Validated thru auth
229  $subPath = array_shift($args);
230  if ($subPath === 'version') {
231  $publicationId = array_shift($args);
232  $representationId = array_shift($args);
233  $bestFileId = array_shift($args);
234  } else {
235  $publicationId = $submission->getCurrentPublication()->getId();
236  $representationId = $subPath;
237  $bestFileId = array_shift($args);
238  }
239 
240  $publicationFormat = Application::get()->getRepresentationDAO()->getByBestId($representationId, $publicationId);
241  if (!$publicationFormat || !$publicationFormat->getIsAvailable() || $remoteURL = $publicationFormat->getRemoteURL()) $dispatcher->handle404();
242 
243  $publication = null;
244  foreach ((array) $submission->getData('publications') as $iPublication) {
245  if ($iPublication->getId() == $publicationId) {
246  $publication = $iPublication;
247  break;
248  }
249  }
250 
251  if (empty($publication)
252  || $publication->getData('status') !== STATUS_PUBLISHED
253  || $publicationFormat->getData('publicationId') !== $publication->getId()) {
254  $dispatcher->handle404();
255  }
256 
257  import('lib.pkp.classes.submission.SubmissionFile'); // File constants
258  $submissionFile = DAORegistry::getDAO('SubmissionFileDAO')->getByBestId($bestFileId, $submission->getId());
259  if (!$submissionFile) $dispatcher->handle404();
260 
261  $fileIdAndRevision = $submissionFile->getFileIdAndRevision();
262  list($fileId, $revision) = array_map(function($a) {
263  return (int) $a;
264  }, preg_split('/-/', $fileIdAndRevision));
265  import('lib.pkp.classes.file.SubmissionFileManager');
266  $monographFileManager = new SubmissionFileManager($submission->getData('contextId'), $submission->getId());
267 
268  switch ($submissionFile->getAssocType()) {
269  case ASSOC_TYPE_PUBLICATION_FORMAT: // Publication format file
270  if ($submissionFile->getAssocId() != $publicationFormat->getId() || $submissionFile->getDirectSalesPrice() === null) $dispatcher->handle404();
271  break;
272  case ASSOC_TYPE_SUBMISSION_FILE: // Dependent file
273  $genreDao = DAORegistry::getDAO('GenreDAO'); /* @var $genreDao GenreDAO */
274  $genre = $genreDao->getById($submissionFile->getGenreId());
275  if (!$genre->getDependent()) $dispatcher->handle404();
276  return $monographFileManager->downloadById($fileId, $revision);
277  break;
278  default: $dispatcher->handle404();
279  }
280 
281  $urlPath = [$submission->getBestId()];
282  if ($publicationId !== $submission->getCurrentPublication()->getId()) {
283  $urlPath[] = 'version';
284  $urlPath[] = $publicationId;
285  }
286  $urlPath[] = $publicationFormat->getBestId();
287  $urlPath[] = $submissionFile->getBestId();
288 
289  $chapterDao = DAORegistry::getDAO('ChapterDAO'); /* @var $chapterDao ChapterDAO */
290  $templateMgr = TemplateManager::getManager($request);
291  $templateMgr->assign(array(
292  'publishedSubmission' => $submission,
293  'publicationFormat' => $publicationFormat,
294  'submissionFile' => $submissionFile,
295  'chapter' => $chapterDao->getChapter($submissionFile->getData('chapterId')),
296  'downloadUrl' => $dispatcher->url($request, ROUTE_PAGE, null, null, 'download', $urlPath, array('inline' => true)),
297  ));
298 
299  $ompCompletedPaymentDao = DAORegistry::getDAO('OMPCompletedPaymentDAO'); /* @var $ompCompletedPaymentDao OMPCompletedPaymentDAO */
300  $user = $request->getUser();
301  if ($submissionFile->getDirectSalesPrice() === '0' || ($user && $ompCompletedPaymentDao->hasPaidPurchaseFile($user->getId(), $fileIdAndRevision))) {
302  // Paid purchase or open access.
303  if (!$user && $press->getData('restrictMonographAccess')) {
304  // User needs to register first.
306  }
307 
308  if ($view) {
309  if (HookRegistry::call('CatalogBookHandler::view', array(&$this, &$submission, &$publicationFormat, &$submissionFile))) {
310  // If the plugin handled the hook, prevent further default activity.
311  exit();
312  }
313  }
314 
315  // Inline viewer not available, or viewing not wanted.
316  // Download or show the file.
317  $inline = $request->getUserVar('inline')?true:false;
318  if (HookRegistry::call('CatalogBookHandler::download', array(&$this, &$submission, &$publicationFormat, &$submissionFile, &$inline))) {
319  // If the plugin handled the hook, prevent further default activity.
320  exit();
321  }
322  return $monographFileManager->downloadById($fileId, $revision, $inline);
323  }
324 
325  // Fall-through: user needs to pay for purchase.
326 
327  // Users that are not logged in need to register/login first.
328  if (!$user) return $request->redirect(null, 'login', null, null, array('source' => $request->url(null, null, null, array($monographId, $representationId, $bestFileId))));
329 
330  // They're logged in but need to pay to view.
331  import('classes.payment.omp.OMPPaymentManager');
332  $paymentManager = new OMPPaymentManager($press);
333  if (!$paymentManager->isConfigured()) {
334  $request->redirect(null, 'catalog');
335  }
336 
337  $queuedPayment = $paymentManager->createQueuedPayment(
338  $request,
339  PAYMENT_TYPE_PURCHASE_FILE,
340  $user->getId(),
341  $fileIdAndRevision,
342  $submissionFile->getDirectSalesPrice(),
343  $press->getData('currency')
344  );
345  $paymentManager->queuePayment($queuedPayment);
346 
347  $paymentForm = $paymentManager->getPaymentForm($queuedPayment);
348  $paymentForm->display($request);
349  }
350 
356  function setupTemplate($request, $submission = null) {
357  $templateMgr = TemplateManager::getmanager($request);
358  if ($seriesId = $submission->getSeriesId()) {
359  $seriesDao = DAORegistry::getDAO('SeriesDAO'); /* @var $seriesDao SeriesDAO */
360  $series = $seriesDao->getById($seriesId, $submission->getData('contextId'));
361  $templateMgr->assign('series', $series);
362  }
363 
364  parent::setupTemplate($request);
365  }
366 }
367 
368 
SubmissionFileManager
Helper class for database-backed submission file management tasks.
Definition: SubmissionFileManager.inc.php:30
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
Validation\redirectLogin
static redirectLogin($message=null)
Definition: Validation.inc.php:168
CatalogBookHandler\book
book($args, $request)
Definition: CatalogBookHandler.inc.php:68
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
CatalogBookHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: CatalogBookHandler.inc.php:53
PKPApplication\getCCLicenseBadge
getCCLicenseBadge($ccLicenseURL, $locale=null)
Definition: PKPApplication.inc.php:704
OMPPaymentManager
Provides payment management functions.
Definition: OMPPaymentManager.inc.php:24
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
CatalogBookHandler\$isVersionRequest
$isVersionRequest
Definition: CatalogBookHandler.inc.php:34
CatalogBookHandler\download
download($args, $request, $view=false)
Definition: CatalogBookHandler.inc.php:227
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
CatalogBookHandler\view
view($args, $request)
Definition: CatalogBookHandler.inc.php:217
PKPHandler\getAuthorizedContextObject
& getAuthorizedContextObject($assocType)
Definition: PKPHandler.inc.php:174
CatalogBookHandler
Handle requests for the book-specific part of the public-facing catalog.
Definition: CatalogBookHandler.inc.php:23
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
Handler
Base request handler application class.
Definition: Handler.inc.php:18
CatalogBookHandler\setupTemplate
setupTemplate($request, $submission=null)
Definition: CatalogBookHandler.inc.php:362
CatalogBookHandler\$publication
$publication
Definition: CatalogBookHandler.inc.php:28
CatalogBookHandler\__construct
__construct()
Definition: CatalogBookHandler.inc.php:39
OmpPublishedSubmissionAccessPolicy
Class to control access to published submissions in OMP.
Definition: OmpPublishedSubmissionAccessPolicy.inc.php:17