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

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

00001 <?php
00002 
00015 import('lib.pkp.classes.form.Form');
00016 
00017 class IdentificationCodeForm extends Form {
00019    var $_monograph;
00020 
00022    var $_identificationCode;
00023 
00027    function IdentificationCodeForm($monograph, $identificationCode) {
00028       parent::Form('controllers/grid/catalogEntry/form/codeForm.tpl');
00029       $this->setMonograph($monograph);
00030       $this->setIdentificationCode($identificationCode);
00031 
00032       // Validation checks for this form
00033       $this->addCheck(new FormValidator($this, 'code', 'required', 'grid.catalogEntry.codeRequired'));
00034       $this->addCheck(new FormValidator($this, 'value', 'required', 'grid.catalogEntry.valueRequired'));
00035       $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
00036       $this->addCheck(new FormValidatorPost($this));
00037    }
00038 
00039    //
00040    // Getters and Setters
00041    //
00046    function &getIdentificationCode() {
00047       return $this->_identificationCode;
00048    }
00049 
00054    function setIdentificationCode($identificationCode) {
00055       $this->_identificationCode =& $identificationCode;
00056    }
00057 
00062    function &getMonograph() {
00063       return $this->_monograph;
00064    }
00065 
00070    function setMonograph($monograph) {
00071       $this->_monograph =& $monograph;
00072    }
00073 
00074 
00075    //
00076    // Overridden template methods
00077    //
00081    function initData() {
00082       $code =& $this->getIdentificationCode();
00083 
00084       if ($code) {
00085          $this->_data = array(
00086             'identificationCodeId' => $code->getId(),
00087             'code' => $code->getCode(),
00088             'value' => $code->getValue(),
00089          );
00090       }
00091    }
00092 
00097    function fetch(&$request) {
00098 
00099       $templateMgr =& TemplateManager::getManager();
00100       $publicationFormatId = null;
00101 
00102       $monograph =& $this->getMonograph();
00103       $templateMgr->assign('monographId', $monograph->getId());
00104       $identificationCode =& $this->getIdentificationCode();
00105 
00106       if ($identificationCode) {
00107          $publicationFormatId = $identificationCode->getPublicationFormatId();
00108          $templateMgr->assign('identificationCodeId', $identificationCode->getId());
00109          $templateMgr->assign('code', $identificationCode->getCode());
00110          $templateMgr->assign('value', $identificationCode->getValue());
00111          $publicationFormatId = $identificationCode->getPublicationFormatId();
00112       } else { // loading a blank form
00113          $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
00114       }
00115 
00116       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00117       $publicationFormat =& $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
00118 
00119       if ($publicationFormat) { // the format exists for this monograph
00120          $templateMgr->assign('publicationFormatId', $publicationFormatId);
00121          $identificationCodes = $publicationFormat->getIdentificationCodes();
00122          $assignedCodes = array_keys($identificationCodes->toAssociativeArray('code')); // currently assigned codes
00123          if ($identificationCode) $assignedCodes = array_diff($assignedCodes, array($identificationCode->getCode())); // allow existing codes to keep their value
00124          $onixCodelistItemDao =& DAORegistry::getDAO('ONIXCodelistItemDAO');
00125          $codes =& $onixCodelistItemDao->getCodes('List5', $assignedCodes); // ONIX list for these
00126          $templateMgr->assign_by_ref('identificationCodes', $codes);
00127       } else {
00128          fatalError('Format not in authorized monograph');
00129       }
00130 
00131       return parent::fetch($request);
00132    }
00133 
00138    function readInputData() {
00139       $this->readUserVars(array(
00140          'identificationCodeId',
00141          'publicationFormatId',
00142          'code',
00143          'value',
00144       ));
00145    }
00146 
00151    function execute() {
00152       $identificationCodeDao =& DAORegistry::getDAO('IdentificationCodeDAO');
00153       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00154 
00155       $monograph = $this->getMonograph();
00156       $identificationCode =& $this->getIdentificationCode();
00157       $publicationFormat =& $publicationFormatDao->getById($this->getData('publicationFormatId', $monograph->getId()));
00158 
00159       if (!$identificationCode) {
00160          // this is a new code to this published monograph
00161          $identificationCode = $identificationCodeDao->newDataObject();
00162          if ($publicationFormat != null) { // ensure this format is in this monograph
00163             $identificationCode->setPublicationFormatId($publicationFormat->getId());
00164             $existingFormat = false;
00165          } else {
00166             fatalError('This format not in authorized monograph context!');
00167          }
00168       } else {
00169          $existingFormat = true;
00170          if ($publicationFormat->getId() !== $identificationCode->getPublicationFormatId()) fatalError('Invalid format!');
00171       }
00172 
00173       $identificationCode->setCode($this->getData('code'));
00174       $identificationCode->setValue($this->getData('value'));
00175 
00176       if ($existingFormat) {
00177          $identificationCodeDao->updateObject($identificationCode);
00178          $identificationCodeId = $identificationCode->getId();
00179       } else {
00180          $identificationCodeId = $identificationCodeDao->insertObject($identificationCode);
00181       }
00182 
00183       return $identificationCodeId;
00184    }
00185 }
00186 
00187 ?>

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