Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
IdentificationCodeForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
21 
24 
28  function IdentificationCodeForm($monograph, $identificationCode) {
29  parent::Form('controllers/grid/catalogEntry/form/codeForm.tpl');
30  $this->setMonograph($monograph);
31  $this->setIdentificationCode($identificationCode);
32 
33  // Validation checks for this form
34  $this->addCheck(new FormValidator($this, 'code', 'required', 'grid.catalogEntry.codeRequired'));
35  $this->addCheck(new FormValidator($this, 'value', 'required', 'grid.catalogEntry.valueRequired'));
36  $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
37  $this->addCheck(new FormValidatorPost($this));
38  }
39 
40  //
41  // Getters and Setters
42  //
47  function &getIdentificationCode() {
49  }
50 
55  function setIdentificationCode($identificationCode) {
56  $this->_identificationCode = $identificationCode;
57  }
58 
63  function getMonograph() {
64  return $this->_monograph;
65  }
66 
71  function setMonograph($monograph) {
72  $this->_monograph = $monograph;
73  }
74 
75 
76  //
77  // Overridden template methods
78  //
82  function initData() {
83  $code = $this->getIdentificationCode();
84 
85  if ($code) {
86  $this->_data = array(
87  'identificationCodeId' => $code->getId(),
88  'code' => $code->getCode(),
89  'value' => $code->getValue(),
90  );
91  }
92  }
93 
98  function fetch($request) {
99 
100  $templateMgr = TemplateManager::getManager($request);
101  $publicationFormatId = null;
102 
103  $monograph = $this->getMonograph();
104  $templateMgr->assign('submissionId', $monograph->getId());
105  $identificationCode = $this->getIdentificationCode();
106 
107  if ($identificationCode) {
108  $publicationFormatId = $identificationCode->getPublicationFormatId();
109  $templateMgr->assign('identificationCodeId', $identificationCode->getId());
110  $templateMgr->assign('code', $identificationCode->getCode());
111  $templateMgr->assign('value', $identificationCode->getValue());
112  $publicationFormatId = $identificationCode->getPublicationFormatId();
113  } else { // loading a blank form
114  $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
115  }
116 
117  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
118  $publicationFormat = $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
119 
120  if ($publicationFormat) { // the format exists for this monograph
121  $templateMgr->assign('publicationFormatId', $publicationFormatId);
122  $identificationCodes = $publicationFormat->getIdentificationCodes();
123  $assignedCodes = array_keys($identificationCodes->toAssociativeArray('code')); // currently assigned codes
124  if ($identificationCode) $assignedCodes = array_diff($assignedCodes, array($identificationCode->getCode())); // allow existing codes to keep their value
125  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO');
126 
127  // since the pubId DOI plugin may be enabled, we give that precedence and remove DOI from here if that is the case.
128  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
129  foreach ($pubIdPlugins as $plugin) {
130  if ($plugin->getEnabled() && $plugin->getPubIdType() == 'doi') {
131  $assignedCodes[] = '06'; // 06 is DOI in ONIX-speak.
132  }
133  }
134  $codes = $onixCodelistItemDao->getCodes('List5', $assignedCodes); // ONIX list for these
135  $templateMgr->assign_by_ref('identificationCodes', $codes);
136  } else {
137  fatalError('Format not in authorized monograph');
138  }
139 
140  return parent::fetch($request);
141  }
142 
147  function readInputData() {
148  $this->readUserVars(array(
149  'identificationCodeId',
150  'publicationFormatId',
151  'code',
152  'value',
153  ));
154  }
155 
160  function execute() {
161  $identificationCodeDao = DAORegistry::getDAO('IdentificationCodeDAO');
162  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
163 
164  $monograph = $this->getMonograph();
165  $identificationCode = $this->getIdentificationCode();
166  $publicationFormat = $publicationFormatDao->getById($this->getData('publicationFormatId', $monograph->getId()));
167 
168  if (!$identificationCode) {
169  // this is a new code to this published monograph
170  $identificationCode = $identificationCodeDao->newDataObject();
171  if ($publicationFormat != null) { // ensure this format is in this monograph
172  $identificationCode->setPublicationFormatId($publicationFormat->getId());
173  $existingFormat = false;
174  } else {
175  fatalError('This format not in authorized monograph context!');
176  }
177  } else {
178  $existingFormat = true;
179  if ($publicationFormat->getId() !== $identificationCode->getPublicationFormatId()) fatalError('Invalid format!');
180  }
181 
182  $identificationCode->setCode($this->getData('code'));
183  $identificationCode->setValue($this->getData('value'));
184 
185  if ($existingFormat) {
186  $identificationCodeDao->updateObject($identificationCode);
187  $identificationCodeId = $identificationCode->getId();
188  } else {
189  $identificationCodeId = $identificationCodeDao->insertObject($identificationCode);
190  }
191 
192  return $identificationCodeId;
193  }
194 }
195 
196 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
Form validation check to make sure the form is POSTed.
Form for adding/editing an identification code.
setIdentificationCode($identificationCode)
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
getData($key)
Definition: Form.inc.php:210
IdentificationCodeForm($monograph, $identificationCode)
Class to represent a form validation check.