Open Monograph Press  3.3.0
MarketForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class MarketForm extends Form {
21 
23  var $_market;
24 
28  public function __construct($submission, $publication, $market) {
29  parent::__construct('controllers/grid/catalogEntry/form/marketForm.tpl');
30  $this->setSubmission($submission);
31  $this->setPublication($publication);
32  $this->setMarket($market);
33 
34  // Validation checks for this form
35  $this->addCheck(new FormValidator($this, 'representationId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
36  $this->addCheck(new FormValidator($this, 'date', 'required', 'grid.catalogEntry.dateRequired'));
37  $this->addCheck(new FormValidator($this, 'price', 'required', 'grid.catalogEntry.priceRequired'));
38  $this->addCheck(new FormValidatorPost($this));
39  $this->addCheck(new FormValidatorCSRF($this));
40  }
41 
42  //
43  // Getters and Setters
44  //
49  public function getMarket() {
50  return $this->_market;
51  }
52 
57  public function setMarket($market) {
58  $this->_market = $market;
59  }
60 
65  public function getSubmission() {
66  return $this->_submission;
67  }
68 
73  public function setSubmission($submission) {
74  $this->_submission = $submission;
75  }
76 
81  public function getPublication() {
82  return $this->_publication;
83  }
84 
89  public function setPublication($publication) {
90  $this->_publication = $publication;
91  }
92 
93 
94  //
95  // Overridden template methods
96  //
100  public function initData() {
101  $market = $this->getMarket();
102 
103  if ($market) {
104  $this->_data = array(
105  'marketId' => $market->getId(),
106  'countriesIncluded' => $market->getCountriesIncluded(),
107  'countriesExcluded' => $market->getCountriesExcluded(),
108  'regionsIncluded' => $market->getRegionsIncluded(),
109  'regionsExcluded' => $market->getRegionsExcluded(),
110  'date' => $market->getDate(),
111  'dateFormat' => $market->getDateFormat(),
112  'discount' => $market->getDiscount(),
113  'dateRole' => $market->getDateRole(),
114  'agentId' => $market->getAgentId(),
115  'supplierId' => $market->getSupplierId(),
116  );
117  }
118  }
119 
123  public function fetch($request, $template = null, $display = false) {
124  $templateMgr = TemplateManager::getManager($request);
125  $submission = $this->getSubmission();
126  $templateMgr->assign('submissionId', $submission->getId());
127  $templateMgr->assign('publicationId', $this->getPublication()->getId());
128  $market = $this->getMarket();
129  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixCodelistItemDao ONIXCodelistItemDAO */
130  $templateMgr->assign(array(
131  'countryCodes' => $onixCodelistItemDao->getCodes('List91'), // countries (CA, US, GB, etc)
132  'regionCodes' => $onixCodelistItemDao->getCodes('List49'), // regions (British Columbia, England, etc)
133  'publicationDateFormats' => $onixCodelistItemDao->getCodes('List55'), // YYYYMMDD, YYMMDD, etc
134  'publicationDateRoles' => $onixCodelistItemDao->getCodes('List163'),
135  'currencyCodes' => $onixCodelistItemDao->getCodes('List96'), // GBP, USD, CAD, etc
136  'priceTypeCodes' => $onixCodelistItemDao->getCodes('List58'), // without tax, with tax, etc
137  'extentTypeCodes' => $onixCodelistItemDao->getCodes('List23'), // word count, FM page count, BM page count, main page count, etc
138  'taxRateCodes' => $onixCodelistItemDao->getCodes('List62'), // higher rate, standard rate, zero rate
139  'taxTypeCodes' => $onixCodelistItemDao->getCodes('List171'), // VAT, GST
140  ));
141 
142  $availableAgents = DAORegistry::getDAO('RepresentativeDAO')->getAgentsByMonographId($submission->getId());
143  $agentOptions = array();
144  while ($agent = $availableAgents->next()) {
145  $agentOptions[$agent->getId()] = $agent->getName();
146  }
147  $templateMgr->assign('availableAgents', $agentOptions);
148 
149  $availableSuppliers = DAORegistry::getDAO('RepresentativeDAO')->getSuppliersByMonographId($submission->getId());
150  $supplierOptions = array();
151  while ($supplier = $availableSuppliers->next()) {
152  $supplierOptions[$supplier->getId()] = $supplier->getName();
153  }
154  $templateMgr->assign('availableSuppliers', $supplierOptions);
155 
156  if ($market) {
157  $templateMgr->assign(array(
158  'marketId' => $market->getId(),
159  'countriesIncluded' => $market->getCountriesIncluded(),
160  'countriesExcluded' => $market->getCountriesExcluded(),
161  'regionsIncluded' => $market->getRegionsIncluded(),
162  'regionsExcluded' => $market->getRegionsExcluded(),
163  'date' => $market->getDate(),
164  'dateRole' => $market->getDateRole(),
165  'dateFormat' => $market->getDateFormat(),
166  'discount' => $market->getDiscount(),
167  'price' => $market->getPrice(),
168  'priceTypeCode' => $market->getPriceTypeCode(),
169  'currencyCode' => $market->getCurrencyCode() != '' ? $market->getCurrencyCode() : 'CAD',
170  'taxRateCode' => $market->getTaxRateCode(),
171  'taxTypeCode' => $market->getTaxTypeCode() != '' ? $market->getTaxTypeCode() : '02',
172  'agentId' => $market->getAgentId(),
173  'supplierId' => $market->getSupplierId(),
174  ));
175 
176  $representationId = $market->getPublicationFormatId();
177  } else { // loading a blank form
178  $representationId = (int) $request->getUserVar('representationId');
179  $templateMgr->assign(array(
180  'dateFormat' => '20', // YYYYMMDD Onix code as a default
181  'dateRole' => '01', // 'Date of Publication' as default
182  'currencyCode' => 'CAD',
183  ));
184  }
185 
186  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO'); /* @var $publicationFormatDao PublicationFormatDAO */
187  $publicationFormat = $publicationFormatDao->getById($representationId, $this->getPublication()->getId());
188 
189  if ($publicationFormat) { // the format exists for this submission
190  $templateMgr->assign('representationId', $representationId);
191  } else {
192  fatalError('Format not in authorized submission');
193  }
194 
195  return parent::fetch($request, $template, $display);
196  }
197 
202  public function readInputData() {
203  $this->readUserVars(array(
204  'marketId',
205  'representationId',
206  'countriesIncluded',
207  'countriesExcluded',
208  'regionsIncluded',
209  'regionsExcluded',
210  'date',
211  'dateFormat',
212  'dateRole',
213  'discount',
214  'price',
215  'priceTypeCode',
216  'currencyCode',
217  'taxRateCode',
218  'taxTypeCode',
219  'agentId',
220  'supplierId',
221  ));
222  }
223 
227  public function execute(...$functionArgs) {
228  parent::execute(...$functionArgs);
229  $marketDao = DAORegistry::getDAO('MarketDAO'); /* @var $marketDao MarketDAO */
230  $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO'); /* @var $publicationFormatDao PublicationFormatDAO */
231 
232  $submission = $this->getSubmission();
233  $market = $this->getMarket();
234  $publicationFormat = $publicationFormatDao->getById($this->getData('representationId'), $this->getPublication()->getId());
235 
236  if (!$market) {
237  // this is a new assigned format to this published submission
238  $market = $marketDao->newDataObject();
239  $existingFormat = false;
240  if ($publicationFormat != null) { // ensure this assigned format is in this submission
241  $market->setPublicationFormatId($publicationFormat->getId());
242  } else {
243  fatalError('This assigned format not in authorized submission context!');
244  }
245  } else {
246  $existingFormat = true;
247  if ($publicationFormat->getId() !== $market->getPublicationFormatId()) fatalError('Invalid format!');
248  }
249 
250  $market->setCountriesIncluded($this->getData('countriesIncluded') ? $this->getData('countriesIncluded') : array());
251  $market->setCountriesExcluded($this->getData('countriesExcluded') ? $this->getData('countriesExcluded') : array());
252  $market->setRegionsIncluded($this->getData('regionsIncluded') ? $this->getData('regionsIncluded') : array());
253  $market->setRegionsExcluded($this->getData('regionsExcluded') ? $this->getData('regionsExcluded') : array());
254  $market->setDate($this->getData('date'));
255  $market->setDateFormat($this->getData('dateFormat'));
256  $market->setDiscount($this->getData('discount'));
257  $market->setDateRole($this->getData('dateRole'));
258  $market->setPrice($this->getData('price'));
259  $market->setPriceTypeCode($this->getData('priceTypeCode'));
260  $market->setCurrencyCode($this->getData('currencyCode'));
261  $market->setTaxRateCode($this->getData('taxRateCode'));
262  $market->setTaxTypeCode($this->getData('taxTypeCode'));
263  $market->setAgentId($this->getData('agentId'));
264  $market->setSupplierId($this->getData('supplierId'));
265 
266  if ($existingFormat) {
267  $marketDao->updateObject($market);
268  $marketId = $market->getId();
269  } else {
270  $marketId = $marketDao->insertObject($market);
271  }
272 
273  return $marketId;
274  }
275 }
276 
277 
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
MarketForm\initData
initData()
Definition: MarketForm.inc.php:100
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
MarketForm\readInputData
readInputData()
Definition: MarketForm.inc.php:202
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
MarketForm\fetch
fetch($request, $template=null, $display=false)
Definition: MarketForm.inc.php:123
MarketForm\__construct
__construct($submission, $publication, $market)
Definition: MarketForm.inc.php:28
MarketForm\setPublication
setPublication($publication)
Definition: MarketForm.inc.php:89
MarketForm\execute
execute(... $functionArgs)
Definition: MarketForm.inc.php:227
MarketForm
Form for adding/editing a market region entry.
Definition: MarketForm.inc.php:18
MarketForm\$_submission
$_submission
Definition: MarketForm.inc.php:20
MarketForm\setSubmission
setSubmission($submission)
Definition: MarketForm.inc.php:73
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
MarketForm\setMarket
setMarket($market)
Definition: MarketForm.inc.php:57
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
MarketForm\getMarket
getMarket()
Definition: MarketForm.inc.php:49
MarketForm\$_market
$_market
Definition: MarketForm.inc.php:23
MarketForm\getPublication
getPublication()
Definition: MarketForm.inc.php:81
MarketForm\getSubmission
getSubmission()
Definition: MarketForm.inc.php:65
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
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