Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PublicationDateForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class PublicationDateForm extends Form {
21 
24 
28  function PublicationDateForm($monograph, $publicationDate) {
29  parent::Form('controllers/grid/catalogEntry/form/pubDateForm.tpl');
30  $this->setMonograph($monograph);
31  $this->setPublicationDate($publicationDate);
32 
33  // Validation checks for this form
34  $this->addCheck(new FormValidator($this, 'role', 'required', 'grid.catalogEntry.roleRequired'));
35  $this->addCheck(new FormValidator($this, 'dateFormat', 'required', 'grid.catalogEntry.dateFormatRequired'));
36 
37  $this->addCheck(new FormValidatorCustom(
38  $this, 'date', 'required', 'grid.catalogEntry.dateRequired',
39  create_function(
40  '$date, $form, $onixCodelistItemDao',
41  '$dateFormat = $form->getData(\'dateFormat\');
42  if (!$dateFormat) return false;
43  $dateFormats = $onixCodelistItemDao->getCodes(\'List55\');
44  $format = $dateFormats[$dateFormat];
45  if (stristr($format, \'string\') && $date != \'\') return true;
46  $format = preg_replace(\'/\s*\(H\)/i\', \'\', $format);
47  if (count(str_split($date)) == count(str_split($format))) return true;
48  return false;'), array(&$this, DAORegistry::getDAO('ONIXCodelistItemDAO')
49  )
50  ));
51 
52  $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
53  $this->addCheck(new FormValidatorPost($this));
54  }
55 
56  //
57  // Getters and Setters
58  //
63  function getPublicationDate() {
65  }
66 
71  function setPublicationDate($publicationDate) {
72  $this->_publicationDate = $publicationDate;
73  }
74 
79  function getMonograph() {
80  return $this->_monograph;
81  }
82 
87  function setMonograph($monograph) {
88  $this->_monograph = $monograph;
89  }
90 
91 
92  //
93  // Overridden template methods
94  //
98  function initData() {
99  $date = $this->getPublicationDate();
100 
101  if ($date) {
102  $this->_data = array(
103  'publicationDateId' => $date->getId(),
104  'role' => $date->getRole(),
105  'dateFormat' => $date->getDateFormat(),
106  'date' => $date->getDate(),
107  );
108  }
109  }
110 
115  function fetch($request) {
116 
117  $templateMgr = TemplateManager::getManager($request);
118  $publicationFormatId = null;
119 
120  $monograph = $this->getMonograph();
121  $templateMgr->assign('submissionId', $monograph->getId());
122  $publicationDate = $this->getPublicationDate();
123 
124  if ($publicationDate) {
125  $publicationFormatId = $publicationDate->getPublicationFormatId();
126  $templateMgr->assign('publicationDateId', $publicationDate->getId());
127  $templateMgr->assign('role', $publicationDate->getRole());
128  $templateMgr->assign('dateFormat', $publicationDate->getDateFormat());
129  $templateMgr->assign('date', $publicationDate->getDate());
130  $publicationFormatId = $publicationDate->getPublicationFormatId();
131  } else { // loading a blank form
132  $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
133  $templateMgr->assign('dateFormat', '20'); // YYYYMMDD Onix code as a default
134  }
135 
136  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
137  $publicationFormat = $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
138 
139  if ($publicationFormat) { // the format exists for this monograph
140  $templateMgr->assign('publicationFormatId', $publicationFormatId);
141  $publicationDates = $publicationFormat->getPublicationDates();
142  $assignedRoles = array_keys($publicationDates->toAssociativeArray('role')); // currently assigned roles
143  if ($publicationDate) $assignedRoles = array_diff($assignedRoles, array($publicationDate->getRole())); // allow existing roles to keep their value
144  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO');
145  $roles = $onixCodelistItemDao->getCodes('List163', $assignedRoles); // ONIX list for these
146  $templateMgr->assign('publicationDateRoles', $roles);
147 
148  //load our date formats
149  $dateFormats = $onixCodelistItemDao->getCodes('List55');
150  $templateMgr->assign('publicationDateFormats', $dateFormats);
151  } else {
152  fatalError('Format not in authorized monograph');
153  }
154 
155  return parent::fetch($request);
156  }
157 
162  function readInputData() {
163  $this->readUserVars(array(
164  'publicationDateId',
165  'publicationFormatId',
166  'role',
167  'dateFormat',
168  'date',
169  ));
170  }
171 
176  function execute() {
177  $publicationDateDao = DAORegistry::getDAO('PublicationDateDAO');
178  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
179 
180  $monograph = $this->getMonograph();
181  $publicationDate = $this->getPublicationDate();
182  $publicationFormat = $publicationFormatDao->getById($this->getData('publicationFormatId'), $monograph->getId());
183 
184  if (!$publicationDate) {
185  // this is a new publication date for this published monograph
186  $publicationDate = $publicationDateDao->newDataObject();
187  if ($publicationFormat != null) { // ensure this assigned format is in this monograph
188  $publicationDate->setPublicationFormatId($publicationFormat->getId());
189  $existingFormat = false;
190  } else {
191  fatalError('This assigned format not in authorized monograph context!');
192  }
193  } else {
194  $existingFormat = true;
195  if ($publicationFormat->getId() !== $publicationDate->getPublicationFormatId()) fatalError('Invalid format!');
196  }
197 
198  $publicationDate->setRole($this->getData('role'));
199  $publicationDate->setDateFormat($this->getData('dateFormat'));
200  $publicationDate->setDate($this->getData('date'));
201 
202  if ($existingFormat) {
203  $publicationDateDao->updateObject($publicationDate);
204  $publicationDateId = $publicationDate->getId();
205  } else {
206  $publicationDateId = $publicationDateDao->insertObject($publicationDate);
207  }
208 
209  return $publicationDateId;
210  }
211 }
212 
213 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
Form validation check with a custom user function performing the validation check.
Form validation check to make sure the form is POSTed.
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
PublicationDateForm($monograph, $publicationDate)
getData($key)
Definition: Form.inc.php:210
Form for adding/editing a publication date.
Class to represent a form validation check.
setPublicationDate($publicationDate)