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

controllers/grid/catalogEntry/form/SalesRightsForm.inc.php

00001 <?php
00002 
00015 import('lib.pkp.classes.form.Form');
00016 
00017 class SalesRightsForm extends Form {
00019    var $_monograph;
00020 
00022    var $_salesRights;
00023 
00027    function SalesRightsForm($monograph, $salesRights) {
00028       parent::Form('controllers/grid/catalogEntry/form/salesRightsForm.tpl');
00029       $this->setMonograph($monograph);
00030       $this->setSalesRights($salesRights);
00031 
00032       // Validation checks for this form
00033       $this->addCheck(new FormValidator($this, 'type', 'required', 'grid.catalogEntry.typeRequired'));
00034       $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
00035       $this->addCheck(new FormValidatorCustom(
00036             $this, 'ROWSetting', 'optional', 'grid.catalogEntry.oneROWPerFormat',
00037             create_function(
00038                   '$ROWSetting, $form, $salesRightsDao, $salesRights',
00039                   '$pubFormatId = $form->getData(\'publicationFormatId\') ; return $ROWSetting == \'\' || $salesRightsDao->getROWByPublicationFormatId($pubFormatId) == null ||
00040                   ($salesRights != null && $salesRightsDao->getROWByPublicationFormatId($pubFormatId)->getId() == $salesRights->getId());'
00041             ), array(&$this, DAORegistry::getDAO('SalesRightsDAO'), $salesRights)
00042       ));
00043 
00044       $this->addCheck(new FormValidatorPost($this));
00045    }
00046 
00047    //
00048    // Getters and Setters
00049    //
00054    function &getSalesRights() {
00055       return $this->_salesRights;
00056    }
00057 
00062    function setSalesRights($salesRights) {
00063       $this->_salesRights =& $salesRights;
00064    }
00065 
00070    function &getMonograph() {
00071       return $this->_monograph;
00072    }
00073 
00078    function setMonograph($monograph) {
00079       $this->_monograph =& $monograph;
00080    }
00081 
00082 
00083    //
00084    // Overridden template methods
00085    //
00089    function initData() {
00090       $salesRights =& $this->getSalesRights();
00091 
00092       if ($salesRights) {
00093          $this->_data = array(
00094             'salesRightsId' => $salesRights->getId(),
00095             'type' => $salesRights->getType(),
00096             'ROWSetting' => $salesRights->getROWSetting(),
00097             'countriesIncluded' => $salesRights->getCountriesIncluded(),
00098             'countriesExcluded' => $salesRights->getCountriesExcluded(),
00099             'regionsIncluded' => $salesRights->getRegionsIncluded(),
00100             'regionsExcluded' => $salesRights->getRegionsExcluded(),
00101          );
00102       }
00103    }
00104 
00109    function fetch(&$request) {
00110 
00111       $templateMgr =& TemplateManager::getManager();
00112       $publicationFormatId = null;
00113 
00114       $monograph =& $this->getMonograph();
00115       $templateMgr->assign('monographId', $monograph->getId());
00116       $salesRights =& $this->getSalesRights();
00117       $onixCodelistItemDao =& DAORegistry::getDAO('ONIXCodelistItemDAO');
00118       $templateMgr->assign('countryCodes', $onixCodelistItemDao->getCodes('List91')); // countries (CA, US, GB, etc)
00119       $templateMgr->assign('regionCodes', $onixCodelistItemDao->getCodes('List49')); // regions (British Columbia, England, etc)
00120 
00121       if ($salesRights) {
00122          $publicationFormatId = $salesRights->getPublicationFormatId();
00123          $templateMgr->assign('salesRightsId', $salesRights->getId());
00124          $templateMgr->assign('type', $salesRights->getType());
00125          $templateMgr->assign('ROWSetting', $salesRights->getROWSetting());
00126          $templateMgr->assign('countriesIncluded', $salesRights->getCountriesIncluded());
00127          $templateMgr->assign('countriesExcluded', $salesRights->getCountriesExcluded());
00128          $templateMgr->assign('regionsIncluded', $salesRights->getRegionsIncluded());
00129          $templateMgr->assign('regionsExcluded', $salesRights->getRegionsExcluded());
00130 
00131          $publicationFormatId = $salesRights->getPublicationFormatId();
00132       } else { // loading a blank form
00133          $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
00134       }
00135 
00136       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00137       $publicationFormat =& $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
00138 
00139       if ($publicationFormat) { // the format exists for this monograph
00140          $templateMgr->assign('publicationFormatId', $publicationFormatId);
00141          // SalesRightsType values are not normally used more than once per PublishingDetail block, so filter used ones out.
00142          $assignedSalesRights = $publicationFormat->getSalesRights();
00143          $assignedTypes = array_keys($assignedSalesRights->toAssociativeArray('type')); // currently assigned types
00144 
00145          if ($salesRights) $assignedTypes = array_diff($assignedTypes, array($salesRights->getType())); // allow existing codes to keep their value
00146 
00147          $types =& $onixCodelistItemDao->getCodes('List46', $assignedTypes); // ONIX list for these
00148          $templateMgr->assign_by_ref('salesRights', $types);
00149       } else {
00150          fatalError('Format not in authorized monograph');
00151       }
00152 
00153       return parent::fetch($request);
00154    }
00155 
00160    function readInputData() {
00161       $this->readUserVars(array(
00162          'salesRightsId',
00163          'publicationFormatId',
00164          'type',
00165          'ROWSetting',
00166          'countriesIncluded',
00167          'countriesExcluded',
00168          'regionsIncluded',
00169          'regionsExcluded',
00170       ));
00171    }
00172 
00177    function execute() {
00178       $salesRightsDao =& DAORegistry::getDAO('SalesRightsDAO');
00179       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00180 
00181       $monograph = $this->getMonograph();
00182       $salesRights =& $this->getSalesRights();
00183       $publicationFormat =& $publicationFormatDao->getById($this->getData('publicationFormatId'), $monograph->getId());
00184 
00185       if (!$salesRights) {
00186          // this is a new assigned format to this published monograph
00187          $salesRights = $salesRightsDao->newDataObject();
00188          if ($publicationFormat != null) { // ensure this assigned format is in this monograph
00189             $salesRights->setPublicationFormatId($publicationFormat->getId());
00190             $existingFormat = false;
00191          } else {
00192             fatalError('This assigned format not in authorized monograph context!');
00193          }
00194       } else {
00195          $existingFormat = true;
00196          if ($publicationFormat->getId() !== $salesRights->getPublicationFormatId()) fatalError('Invalid format!');
00197       }
00198 
00199       $salesRights->setType($this->getData('type'));
00200       $salesRights->setROWSetting($this->getData('ROWSetting')?true:false);
00201       $salesRights->setCountriesIncluded($this->getData('countriesIncluded') ? $this->getData('countriesIncluded') : array());
00202       $salesRights->setCountriesExcluded($this->getData('countriesExcluded') ? $this->getData('countriesExcluded') : array());
00203       $salesRights->setRegionsIncluded($this->getData('regionsIncluded') ? $this->getData('regionsIncluded') : array());
00204       $salesRights->setRegionsExcluded($this->getData('regionsExcluded') ? $this->getData('regionsExcluded') : array());
00205 
00206       if ($existingFormat) {
00207          $salesRightsDao->updateObject($salesRights);
00208          $salesRightsId = $salesRights->getId();
00209       } else {
00210          $salesRightsId = $salesRightsDao->insertObject($salesRights);
00211       }
00212 
00213       return $salesRightsId;
00214    }
00215 }
00216 ?>

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