Open Monograph Press  3.3.0
IdentificationCodeForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
21 
24 
27 
31  public function __construct($submission, $publication, $identificationCode) {
32  parent::__construct('controllers/grid/catalogEntry/form/codeForm.tpl');
33  $this->setSubmission($submission);
34  $this->setPublication($publication);
35  $this->setIdentificationCode($identificationCode);
36 
37  // Validation checks for this form
38  $this->addCheck(new FormValidator($this, 'code', 'required', 'grid.catalogEntry.codeRequired'));
39  $this->addCheck(new FormValidator($this, 'value', 'required', 'grid.catalogEntry.valueRequired'));
40  $this->addCheck(new FormValidator($this, 'representationId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
41  $this->addCheck(new FormValidatorPost($this));
42  $this->addCheck(new FormValidatorCSRF($this));
43  }
44 
45  //
46  // Getters and Setters
47  //
52  public function getIdentificationCode() {
54  }
55 
60  public function setIdentificationCode($identificationCode) {
61  $this->_identificationCode = $identificationCode;
62  }
63 
68  public function getSubmission() {
69  return $this->_submission;
70  }
71 
76  public function setSubmission($submission) {
77  $this->_submission = $submission;
78  }
79 
84  public function getPublication() {
85  return $this->_publication;
86  }
87 
92  public function setPublication($publication) {
93  $this->_publication = $publication;
94  }
95 
96 
97  //
98  // Overridden template methods
99  //
103  public function initData() {
104  $code = $this->getIdentificationCode();
105 
106  if ($code) {
107  $this->_data = array(
108  'identificationCodeId' => $code->getId(),
109  'code' => $code->getCode(),
110  'value' => $code->getValue(),
111  );
112  }
113  }
114 
118  public function fetch($request, $template = null, $display = false) {
119  $templateMgr = TemplateManager::getManager($request);
120  $submission = $this->getSubmission();
121  $templateMgr->assign('submissionId', $submission->getId());
122  $templateMgr->assign('publicationId', $this->getPublication()->getId());
123  $identificationCode = $this->getIdentificationCode();
124 
125  if ($identificationCode) {
126  $templateMgr->assign('identificationCodeId', $identificationCode->getId());
127  $templateMgr->assign('code', $identificationCode->getCode());
128  $templateMgr->assign('value', $identificationCode->getValue());
129  $representationId = $identificationCode->getPublicationFormatId();
130  } else { // loading a blank form
131  $representationId = (int) $request->getUserVar('representationId');
132  }
133 
134  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO'); /* @var $publicationFormatDao PublicationFormatDAO */
135  $publicationFormat = $publicationFormatDao->getById($representationId, $this->getPublication()->getId());
136 
137  if ($publicationFormat) { // the format exists for this submission
138  $templateMgr->assign('representationId', $representationId);
139  $identificationCodes = $publicationFormat->getIdentificationCodes();
140  $assignedCodes = array_keys($identificationCodes->toAssociativeArray('code')); // currently assigned codes
141  if ($identificationCode) $assignedCodes = array_diff($assignedCodes, array($identificationCode->getCode())); // allow existing codes to keep their value
142  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixCodelistItemDao ONIXCodelistItemDAO */
143 
144  // since the pubId DOI plugin may be enabled, we give that precedence and remove DOI from here if that is the case.
145  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true);
146  foreach ($pubIdPlugins as $plugin) {
147  if ($plugin->getEnabled() && $plugin->getPubIdType() == 'doi') {
148  $assignedCodes[] = '06'; // 06 is DOI in ONIX-speak.
149  }
150  }
151  $codes = $onixCodelistItemDao->getCodes('List5', $assignedCodes); // ONIX list for these
152  $templateMgr->assign('identificationCodes', $codes);
153  } else {
154  fatalError('Format not in authorized submission');
155  }
156 
157  return parent::fetch($request, $template, $display);
158  }
159 
164  public function readInputData() {
165  $this->readUserVars(array(
166  'identificationCodeId',
167  'representationId',
168  'code',
169  'value',
170  ));
171  }
172 
176  public function execute(...$functionArgs) {
177  parent::execute(...$functionArgs);
178  $identificationCodeDao = DAORegistry::getDAO('IdentificationCodeDAO'); /* @var $identificationCodeDao IdentificationCodeDAO */
179  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO'); /* @var $publicationFormatDao PublicationFormatDAO */
180 
181  $submission = $this->getSubmission();
182  $identificationCode = $this->getIdentificationCode();
183  $publicationFormat = $publicationFormatDao->getById($this->getData('representationId', $this->getPublication()->getId()));
184 
185  if (!$identificationCode) {
186  // this is a new code to this published submission
187  $identificationCode = $identificationCodeDao->newDataObject();
188  $existingFormat = false;
189  if ($publicationFormat != null) { // ensure this format is in this submission
190  $identificationCode->setPublicationFormatId($publicationFormat->getId());
191  } else {
192  fatalError('This format not in authorized submission context!');
193  }
194  } else {
195  $existingFormat = true;
196  if ($publicationFormat->getId() !== $identificationCode->getPublicationFormatId()) fatalError('Invalid format!');
197  }
198 
199  $identificationCode->setCode($this->getData('code'));
200  $identificationCode->setValue($this->getData('value'));
201 
202  if ($existingFormat) {
203  $identificationCodeDao->updateObject($identificationCode);
204  $identificationCodeId = $identificationCode->getId();
205  } else {
206  $identificationCodeId = $identificationCodeDao->insertObject($identificationCode);
207  }
208 
209  return $identificationCodeId;
210  }
211 }
212 
213 
IdentificationCodeForm\readInputData
readInputData()
Definition: IdentificationCodeForm.inc.php:164
IdentificationCodeForm\getIdentificationCode
getIdentificationCode()
Definition: IdentificationCodeForm.inc.php:52
IdentificationCodeForm
Form for adding/editing an identification code.
Definition: IdentificationCodeForm.inc.php:18
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
IdentificationCodeForm\__construct
__construct($submission, $publication, $identificationCode)
Definition: IdentificationCodeForm.inc.php:31
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
IdentificationCodeForm\getSubmission
getSubmission()
Definition: IdentificationCodeForm.inc.php:68
Form\getData
getData($key)
Definition: Form.inc.php:220
IdentificationCodeForm\execute
execute(... $functionArgs)
Definition: IdentificationCodeForm.inc.php:176
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
IdentificationCodeForm\fetch
fetch($request, $template=null, $display=false)
Definition: IdentificationCodeForm.inc.php:118
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
IdentificationCodeForm\$_identificationCode
$_identificationCode
Definition: IdentificationCodeForm.inc.php:26
IdentificationCodeForm\setIdentificationCode
setIdentificationCode($identificationCode)
Definition: IdentificationCodeForm.inc.php:60
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
IdentificationCodeForm\$_publication
$_publication
Definition: IdentificationCodeForm.inc.php:23
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
IdentificationCodeForm\setSubmission
setSubmission($submission)
Definition: IdentificationCodeForm.inc.php:76
IdentificationCodeForm\initData
initData()
Definition: IdentificationCodeForm.inc.php:103
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
IdentificationCodeForm\$_submission
$_submission
Definition: IdentificationCodeForm.inc.php:20
IdentificationCodeForm\getPublication
getPublication()
Definition: IdentificationCodeForm.inc.php:84
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
IdentificationCodeForm\setPublication
setPublication($publication)
Definition: IdentificationCodeForm.inc.php:92