• Main Page
  • Modules
  • Classes
  • Files
  • File List

pages/catalog/CatalogBookHandler.inc.php

00001 <?php
00002 
00016 import('classes.handler.Handler');
00017 
00018 // import UI base classes
00019 import('lib.pkp.classes.linkAction.LinkAction');
00020 import('lib.pkp.classes.core.JSONMessage');
00021 
00022 class CatalogBookHandler extends Handler {
00026    function CatalogBookHandler() {
00027       parent::Handler();
00028    }
00029 
00030 
00031    //
00032    // Overridden functions from PKPHandler
00033    //
00040    function authorize(&$request, $args, $roleAssignments) {
00041       import('classes.security.authorization.OmpPublishedMonographAccessPolicy');
00042       $this->addPolicy(new OmpPublishedMonographAccessPolicy($request, $args, $roleAssignments));
00043       return parent::authorize($request, $args, $roleAssignments);
00044    }
00045 
00046 
00047    //
00048    // Public handler methods
00049    //
00055    function book($args, &$request) {
00056       $templateMgr =& TemplateManager::getManager();
00057       $press =& $request->getPress();
00058       $this->setupTemplate($request);
00059       AppLocale::requireComponents(LOCALE_COMPONENT_OMP_SUBMISSION); // submission.synopsis
00060 
00061       $publishedMonograph =& $this->getAuthorizedContextObject(ASSOC_TYPE_PUBLISHED_MONOGRAPH);
00062       $templateMgr->assign('publishedMonograph', $publishedMonograph);
00063 
00064       // Get Social media blocks enabled for the catalog
00065       $socialMediaDao =& DAORegistry::getDAO('SocialMediaDAO');
00066       $socialMedia =& $socialMediaDao->getEnabledForCatalogByPressId($press->getId());
00067       $blocks = array();
00068       while ($media =& $socialMedia->next()) {
00069          $media->replaceCodeVars($publishedMonograph);
00070          $blocks[] = $media->getCode();
00071       }
00072 
00073       $templateMgr->assign_by_ref('blocks', $blocks);
00074 
00075       // e-Commerce
00076       import('classes.payment.omp.OMPPaymentManager');
00077       $ompPaymentManager = new OMPPaymentManager($request);
00078       $monographFileDao =& DAORegistry::getDAO('SubmissionFileDAO');
00079       if ($ompPaymentManager->isConfigured()) {
00080          $availableFiles = array_filter(
00081             $monographFileDao->getLatestRevisions($publishedMonograph->getId()),
00082             create_function('$a', 'return $a->getViewable() && $a->getDirectSalesPrice() !== null && $a->getAssocType() == ASSOC_TYPE_PUBLICATION_FORMAT;')       
00083          );
00084          $availableFilesByPublicationFormat = array();
00085          foreach ($availableFiles as $availableFile) {
00086             $availableFilesByPublicationFormat[$availableFile->getAssocId()][] = $availableFile;
00087          }
00088 
00089          // Determine whether or not to use the collapsed view.
00090          $useCollapsedView = true;
00091          foreach ($availableFilesByPublicationFormat as $publicationFormatId => $availableFiles) {
00092             if (count($availableFiles)>1) {
00093                $useCollapsedView = false;
00094                break;
00095             }
00096          }
00097 
00098          // Expose variables to template
00099          $templateMgr->assign('availableFiles', $availableFilesByPublicationFormat);
00100          $templateMgr->assign('useCollapsedView', $useCollapsedView);
00101       }
00102 
00103       // Display
00104       $templateMgr->display('catalog/book/book.tpl');
00105    }
00106 
00112    function download($args, &$request) {
00113       $press =& $request->getPress();
00114       $this->setupTemplate($request);
00115 
00116       $monographId = (int) array_shift($args); // Validated thru auth
00117       $publicationFormatId = (int) array_shift($args);
00118       $fileIdAndRevision = array_shift($args);
00119 
00120       $publishedMonograph =& $this->getAuthorizedContextObject(ASSOC_TYPE_PUBLISHED_MONOGRAPH);
00121       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00122       $publicationFormat =& $publicationFormatDao->getById($publicationFormatId, $publishedMonograph->getId());
00123       if (!$publicationFormat || !$publicationFormat->getIsApproved() || !$publicationFormat->getIsAvailable()) fatalError('Invalid publication format specified.');
00124 
00125       $submissionFileDao =& DAORegistry::getDAO('SubmissionFileDAO');
00126       list($fileId, $revision) = array_map(create_function('$a', 'return (int) $a;'), split('-', $fileIdAndRevision));
00127       import('classes.monograph.MonographFile'); // File constants
00128       $submissionFile =& $submissionFileDao->getRevision($fileId, $revision, MONOGRAPH_FILE_PROOF, $monographId);
00129       if (!$submissionFile || $submissionFile->getAssocType() != ASSOC_TYPE_PUBLICATION_FORMAT || $submissionFile->getAssocId() != $publicationFormatId || $submissionFile->getDirectSalesPrice() === null) {
00130          fatalError('Invalid monograph file specified!');
00131       }
00132 
00133       $ompCompletedPaymentDao =& DAORegistry::getDAO('OMPCompletedPaymentDAO');
00134       $user =& $request->getUser();
00135       if ($submissionFile->getDirectSalesPrice() === '0' || ($user && $ompCompletedPaymentDao->hasPaidPurchaseFile($user->getId(), $fileIdAndRevision))) {
00136          // Paid purchase or open access. Allow download.
00137          import('classes.file.MonographFileManager');
00138          $monographFileManager = new MonographFileManager($press->getId(), $monographId);
00139          return $monographFileManager->downloadFile($fileId, $revision);
00140       }
00141 
00142       // Fall-through: user needs to pay for purchase.
00143 
00144       // Check that they are registered and logged in.
00145       if (!$user) return $request->redirect(null, 'login', null, null, array('source' => $request->url(null, null, null, array($monographId, $publicationFormatId, $fileIdAndRevision))));
00146 
00147       import('classes.payment.omp.OMPPaymentManager');
00148       $ompPaymentManager = new OMPPaymentManager($request);
00149       if (!$ompPaymentManager->isConfigured()) {
00150          $request->redirect(null, 'catalog');
00151       }
00152 
00153       $queuedPayment = $ompPaymentManager->createQueuedPayment(
00154          $press->getId(),
00155          PAYMENT_TYPE_PURCHASE_FILE,
00156          $user->getId(),
00157          $fileIdAndRevision,
00158          $submissionFile->getDirectSalesPrice(),
00159          $press->getSetting('pressCurrency')
00160       );
00161 
00162       $queuedPaymentId = $ompPaymentManager->queuePayment($queuedPayment);
00163       $ompPaymentManager->displayPaymentForm($queuedPaymentId, $queuedPayment);
00164    }
00165 }
00166 
00167 ?>

Generated on Mon Sep 17 2012 13:58:56 for Open Monograph Press by  doxygen 1.7.1