Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
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 {
27  function CatalogBookHandler() {
28  parent::Handler();
29  }
30 
31 
32  //
33  // Overridden functions from PKPHandler
34  //
41  function authorize($request, &$args, $roleAssignments) {
42  import('classes.security.authorization.OmpPublishedMonographAccessPolicy');
43  $this->addPolicy(new OmpPublishedMonographAccessPolicy($request, $args, $roleAssignments));
44  return parent::authorize($request, $args, $roleAssignments);
45  }
46 
47 
48  //
49  // Public handler methods
50  //
56  function book($args, $request) {
57  $templateMgr = TemplateManager::getManager($request);
58  $this->setupTemplate($request);
59  AppLocale::requireComponents(LOCALE_COMPONENT_APP_SUBMISSION); // submission.synopsis
60 
61  $publishedMonograph = $this->getAuthorizedContextObject(ASSOC_TYPE_PUBLISHED_MONOGRAPH);
62  $templateMgr->assign('publishedMonograph', $publishedMonograph);
63 
64  // Get Social media blocks enabled for the catalog
65  $socialMediaDao = DAORegistry::getDAO('SocialMediaDAO');
66  $socialMedia = $socialMediaDao->getEnabledForContextByContextId($publishedMonograph->getContextId());
67  $blocks = array();
68  while ($media = $socialMedia->next()) {
69  $media->replaceCodeVars($publishedMonograph);
70  $blocks[] = $media->getCode();
71  }
72 
73  $templateMgr->assign_by_ref('blocks', $blocks);
74 
75  // add Chapters, if they exist.
76  if ($publishedMonograph->getWorkType() == WORK_TYPE_EDITED_VOLUME) {
77  $chapterDao = DAORegistry::getDAO('ChapterDAO');
78  $chapters = $chapterDao->getChapters($publishedMonograph->getId());
79  $templateMgr->assign_by_ref('chapters', $chapters->toAssociativeArray());
80  }
81  // determine which pubId plugins are enabled.
82  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
83  $enabledPubIdTypes = array();
84  foreach ((array) $pubIdPlugins as $plugin) {
85  if ($plugin->getEnabled()) {
86  $enabledPubIdTypes[] = $plugin->getPubIdType();
87  // check to see if the format has a pubId set. If not, generate one.
88  $publicationFormats = $publishedMonograph->getPublicationFormats(true);
89  foreach ($publicationFormats as $publicationFormat) {
90  if ($publicationFormat->getStoredPubId($plugin->getPubIdType()) == '') {
91  $plugin->getPubId($publicationFormat);
92  }
93  }
94  }
95  }
96  $templateMgr->assign('enabledPubIdTypes', $enabledPubIdTypes);
97 
98  // e-Commerce
99  import('classes.payment.omp.OMPPaymentManager');
100  $ompPaymentManager = new OMPPaymentManager($request);
101  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
102  if ($ompPaymentManager->isConfigured()) {
103  $availableFiles = array_filter(
104  $submissionFileDao->getLatestRevisions($publishedMonograph->getId()),
105  create_function('$a', 'return $a->getViewable() && $a->getDirectSalesPrice() !== null && $a->getAssocType() == ASSOC_TYPE_PUBLICATION_FORMAT;')
106  );
107  $availableFilesByPublicationFormat = array();
108  foreach ($availableFiles as $availableFile) {
109  $availableFilesByPublicationFormat[$availableFile->getAssocId()][] = $availableFile;
110  }
111 
112  // Determine whether or not to use the collapsed view.
113  $useCollapsedView = true;
114  foreach ($availableFilesByPublicationFormat as $publicationFormatId => $availableFiles) {
115  if (count($availableFiles)>1) {
116  $useCollapsedView = false;
117  break;
118  }
119  }
120 
121  // Expose variables to template
122  $templateMgr->assign('availableFiles', $availableFilesByPublicationFormat);
123  $templateMgr->assign('useCollapsedView', $useCollapsedView);
124  }
125 
126  if ($seriesId = $publishedMonograph->getSeriesId()) {
127  $seriesDao = DAORegistry::getDAO('SeriesDAO');
128  $series = $seriesDao->getById($seriesId, $publishedMonograph->getContextId());
129  $templateMgr->assign('series', $series);
130  }
131 
132  // Display
133  $templateMgr->display('catalog/book/book.tpl');
134  }
135 
142  function view($args, $request) {
143  $this->download($args, $request, true);
144  }
145 
152  function download($args, $request, $view = false) {
153  $this->setupTemplate($request);
154 
155  $monographId = (int) array_shift($args); // Validated thru auth
156  $publicationFormatId = (int) array_shift($args);
157  $fileIdAndRevision = array_shift($args);
158 
159  $publishedMonograph = $this->getAuthorizedContextObject(ASSOC_TYPE_PUBLISHED_MONOGRAPH);
160  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
161  $publicationFormat = $publicationFormatDao->getById($publicationFormatId, $publishedMonograph->getId());
162  if (!$publicationFormat || !$publicationFormat->getIsApproved() || !$publicationFormat->getIsAvailable()) fatalError('Invalid publication format specified.');
163 
164  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
165  list($fileId, $revision) = array_map(create_function('$a', 'return (int) $a;'), preg_split('/-/', $fileIdAndRevision));
166  import('classes.monograph.MonographFile'); // File constants
167  $submissionFile = $submissionFileDao->getRevision($fileId, $revision, SUBMISSION_FILE_PROOF, $monographId);
168  if (!$submissionFile || $submissionFile->getAssocType() != ASSOC_TYPE_PUBLICATION_FORMAT || $submissionFile->getAssocId() != $publicationFormatId || $submissionFile->getDirectSalesPrice() === null) {
169  fatalError('Invalid monograph file specified!');
170  }
171 
172  $ompCompletedPaymentDao = DAORegistry::getDAO('OMPCompletedPaymentDAO');
173  $user = $request->getUser();
174  if ($submissionFile->getDirectSalesPrice() === '0' || ($user && $ompCompletedPaymentDao->hasPaidPurchaseFile($user->getId(), $fileIdAndRevision))) {
175  // Paid purchase or open access. Allow download.
176 
177  // If inline viewing is requested, permit plugins to
178  // handle the document.
179  if ($view) {
180  PluginRegistry::loadCategory('viewableFiles', true);
181  if (HookRegistry::call('CatalogBookHandler::view', array(&$this, &$publishedMonograph, &$submissionFile))) {
182  // If the plugin handled the hook, prevent further default activity.
183  exit();
184  }
185  }
186 
187  // Inline viewer not available, or viewing not wanted.
188  // Download the file.
189  if (!HookRegistry::call('CatalogBookHandler::download', array(&$this, &$publishedMonograph, &$submissionFile))) {
190  import('lib.pkp.classes.file.SubmissionFileManager');
191  $monographFileManager = new SubmissionFileManager($publishedMonograph->getContextId(), $monographId);
192  return $monographFileManager->downloadFile($fileId, $revision);
193  }
194  }
195 
196  // Fall-through: user needs to pay for purchase.
197 
198  // Users that are not logged in need to register/login first.
199  if (!$user) return $request->redirect(null, 'login', null, null, array('source' => $request->url(null, null, null, array($monographId, $publicationFormatId, $fileIdAndRevision))));
200 
201  // They're logged in but need to pay to view.
202  import('classes.payment.omp.OMPPaymentManager');
203  $ompPaymentManager = new OMPPaymentManager($request);
204  if (!$ompPaymentManager->isConfigured()) {
205  $request->redirect(null, 'catalog');
206  }
207 
208  $press = $request->getPress();
209  $queuedPayment = $ompPaymentManager->createQueuedPayment(
210  $press->getId(),
211  PAYMENT_TYPE_PURCHASE_FILE,
212  $user->getId(),
213  $fileIdAndRevision,
214  $submissionFile->getDirectSalesPrice(),
215  $press->getSetting('currency')
216  );
217 
218  $ompPaymentManager->displayPaymentForm(
219  $ompPaymentManager->queuePayment($queuedPayment),
220  $queuedPayment
221  );
222  }
223 }
224 
225 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
static requireComponents()
setupTemplate($request)
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
Class to control access to published monographs in OMP.
authorize($request, &$args, $roleAssignments)
download($args, $request, $view=false)
static call($hookName, $args=null)
Provides payment management functions.
Handle requests for the book-specific part of the public-facing catalog.
Base request handler application class.
Definition: Handler.inc.php:20
& getAuthorizedContextObject($assocType)