Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SalesRightsForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class SalesRightsForm extends Form {
21 
24 
28  function SalesRightsForm($monograph, $salesRights) {
29  parent::Form('controllers/grid/catalogEntry/form/salesRightsForm.tpl');
30  $this->setMonograph($monograph);
31  $this->setSalesRights($salesRights);
32 
33  // Validation checks for this form
34  $this->addCheck(new FormValidator($this, 'type', 'required', 'grid.catalogEntry.typeRequired'));
35  $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
36  $this->addCheck(new FormValidatorCustom(
37  $this, 'ROWSetting', 'optional', 'grid.catalogEntry.oneROWPerFormat',
38  create_function(
39  '$ROWSetting, $form, $salesRightsDao, $salesRights',
40  '$pubFormatId = $form->getData(\'publicationFormatId\') ; return $ROWSetting == \'\' || $salesRightsDao->getROWByPublicationFormatId($pubFormatId) == null ||
41  ($salesRights != null && $salesRightsDao->getROWByPublicationFormatId($pubFormatId)->getId() == $salesRights->getId());'
42  ), array(&$this, DAORegistry::getDAO('SalesRightsDAO'), $salesRights)
43  ));
44 
45  $this->addCheck(new FormValidatorPost($this));
46  }
47 
48  //
49  // Getters and Setters
50  //
55  function getSalesRights() {
56  return $this->_salesRights;
57  }
58 
63  function setSalesRights($salesRights) {
64  $this->_salesRights = $salesRights;
65  }
66 
71  function getMonograph() {
72  return $this->_monograph;
73  }
74 
79  function setMonograph($monograph) {
80  $this->_monograph = $monograph;
81  }
82 
83 
84  //
85  // Overridden template methods
86  //
90  function initData() {
91  $salesRights = $this->getSalesRights();
92 
93  if ($salesRights) {
94  $this->_data = array(
95  'salesRightsId' => $salesRights->getId(),
96  'type' => $salesRights->getType(),
97  'ROWSetting' => $salesRights->getROWSetting(),
98  'countriesIncluded' => $salesRights->getCountriesIncluded(),
99  'countriesExcluded' => $salesRights->getCountriesExcluded(),
100  'regionsIncluded' => $salesRights->getRegionsIncluded(),
101  'regionsExcluded' => $salesRights->getRegionsExcluded(),
102  );
103  }
104  }
105 
110  function fetch($request) {
111 
112  $templateMgr = TemplateManager::getManager($request);
113  $publicationFormatId = null;
114 
115  $monograph = $this->getMonograph();
116  $templateMgr->assign('submissionId', $monograph->getId());
117  $salesRights = $this->getSalesRights();
118  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO');
119  $templateMgr->assign('countryCodes', $onixCodelistItemDao->getCodes('List91')); // countries (CA, US, GB, etc)
120  $templateMgr->assign('regionCodes', $onixCodelistItemDao->getCodes('List49')); // regions (British Columbia, England, etc)
121 
122  if ($salesRights) {
123  $publicationFormatId = $salesRights->getPublicationFormatId();
124  $templateMgr->assign('salesRightsId', $salesRights->getId());
125  $templateMgr->assign('type', $salesRights->getType());
126  $templateMgr->assign('ROWSetting', $salesRights->getROWSetting());
127  $templateMgr->assign('countriesIncluded', $salesRights->getCountriesIncluded());
128  $templateMgr->assign('countriesExcluded', $salesRights->getCountriesExcluded());
129  $templateMgr->assign('regionsIncluded', $salesRights->getRegionsIncluded());
130  $templateMgr->assign('regionsExcluded', $salesRights->getRegionsExcluded());
131 
132  $publicationFormatId = $salesRights->getPublicationFormatId();
133  } else { // loading a blank form
134  $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
135  }
136 
137  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
138  $publicationFormat = $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
139 
140  if ($publicationFormat) { // the format exists for this monograph
141  $templateMgr->assign('publicationFormatId', $publicationFormatId);
142  // SalesRightsType values are not normally used more than once per PublishingDetail block, so filter used ones out.
143  $assignedSalesRights = $publicationFormat->getSalesRights();
144  $assignedTypes = array_keys($assignedSalesRights->toAssociativeArray('type')); // currently assigned types
145 
146  if ($salesRights) $assignedTypes = array_diff($assignedTypes, array($salesRights->getType())); // allow existing codes to keep their value
147 
148  $types = $onixCodelistItemDao->getCodes('List46', $assignedTypes); // ONIX list for these
149  $templateMgr->assign('salesRights', $types);
150  } else {
151  fatalError('Format not in authorized monograph');
152  }
153 
154  return parent::fetch($request);
155  }
156 
161  function readInputData() {
162  $this->readUserVars(array(
163  'salesRightsId',
164  'publicationFormatId',
165  'type',
166  'ROWSetting',
167  'countriesIncluded',
168  'countriesExcluded',
169  'regionsIncluded',
170  'regionsExcluded',
171  ));
172  }
173 
178  function execute() {
179  $salesRightsDao = DAORegistry::getDAO('SalesRightsDAO');
180  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
181 
182  $monograph = $this->getMonograph();
183  $salesRights = $this->getSalesRights();
184  $publicationFormat = $publicationFormatDao->getById($this->getData('publicationFormatId'), $monograph->getId());
185 
186  if (!$salesRights) {
187  // this is a new assigned format to this published monograph
188  $salesRights = $salesRightsDao->newDataObject();
189  if ($publicationFormat != null) { // ensure this assigned format is in this monograph
190  $salesRights->setPublicationFormatId($publicationFormat->getId());
191  $existingFormat = false;
192  } else {
193  fatalError('This assigned format not in authorized monograph context!');
194  }
195  } else {
196  $existingFormat = true;
197  if ($publicationFormat->getId() !== $salesRights->getPublicationFormatId()) fatalError('Invalid format!');
198  }
199 
200  $salesRights->setType($this->getData('type'));
201  $salesRights->setROWSetting($this->getData('ROWSetting')?true:false);
202  $salesRights->setCountriesIncluded($this->getData('countriesIncluded') ? $this->getData('countriesIncluded') : array());
203  $salesRights->setCountriesExcluded($this->getData('countriesExcluded') ? $this->getData('countriesExcluded') : array());
204  $salesRights->setRegionsIncluded($this->getData('regionsIncluded') ? $this->getData('regionsIncluded') : array());
205  $salesRights->setRegionsExcluded($this->getData('regionsExcluded') ? $this->getData('regionsExcluded') : array());
206 
207  if ($existingFormat) {
208  $salesRightsDao->updateObject($salesRights);
209  $salesRightsId = $salesRights->getId();
210  } else {
211  $salesRightsId = $salesRightsDao->insertObject($salesRights);
212  }
213 
214  return $salesRightsId;
215  }
216 }
217 ?>
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
SalesRightsForm($monograph, $salesRights)
setMonograph($monograph)
getData($key)
Definition: Form.inc.php:210
Form for adding/editing a sales rights entry.
Class to represent a form validation check.
setSalesRights($salesRights)