Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
RepresentativeForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class RepresentativeForm extends Form {
21 
24 
28  function RepresentativeForm($monograph, $representative) {
29  parent::Form('controllers/grid/catalogEntry/form/representativeForm.tpl');
30  $this->setMonograph($monograph);
31  $this->setRepresentative($representative);
32 
33  // Validation checks for this form
34  $this->addCheck(new FormValidatorCustom(
35  $this, 'isSupplier', 'required', 'grid.catalogEntry.roleRequired',
36  create_function(
37  '$isSupplier, $form, $onixDao, $agentRole, $supplierRole',
38  'return (!$isSupplier && $onixDao->codeExistsInList($agentRole, \'List69\')) || ($isSupplier && $onixDao->codeExistsInList($supplierRole, \'List93\'));'
39  ), array(&$this, DAORegistry::getDAO('ONIXCodelistItemDAO'), Request::getUserVar('agentRole'), Request::getUserVar('supplierRole'))
40  ));
41  $this->addCheck(new FormValidatorPost($this));
42  }
43 
44  //
45  // Getters and Setters
46  //
51  function &getRepresentative() {
53  }
54 
59  function setRepresentative($representative) {
60  $this->_representative = $representative;
61  }
62 
67  function getMonograph() {
68  return $this->_monograph;
69  }
70 
75  function setMonograph($monograph) {
76  $this->_monograph = $monograph;
77  }
78 
79 
80  //
81  // Overridden template methods
82  //
86  function initData() {
87  $representative = $this->getRepresentative();
88 
89  if ($representative) {
90  $this->_data = array(
91  'representativeId' => $representative->getId(),
92  'role' => $representative->getRole(),
93  'representativeIdType' => $representative->getRepresentativeIdType(),
94  'representativeIdValue' => $representative->getRepresentativeIdValue(),
95  'name' => $representative->getName(),
96  'phone' => $representative->getPhone(),
97  'fax' => $representative->getFax(),
98  'email' => $representative->getEmail(),
99  'url' =>$representative->getUrl(),
100  'isSupplier' => $representative->getIsSupplier(),
101  );
102  }
103  }
104 
109  function fetch($request) {
110 
111  $templateMgr = TemplateManager::getManager($request);
112 
113  $monograph = $this->getMonograph();
114  $templateMgr->assign('submissionId', $monograph->getId());
115  $representative = $this->getRepresentative();
116  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO');
117  $templateMgr->assign('idTypeCodes', $onixCodelistItemDao->getCodes('List92')); // GLN, etc
118  $templateMgr->assign('agentRoleCodes', $onixCodelistItemDao->getCodes('List69')); // Sales Agent, etc
119  $templateMgr->assign('supplierRoleCodes', $onixCodelistItemDao->getCodes('List93')); // wholesaler, publisher to retailer, etc
120  $templateMgr->assign('isSupplier', true); // default to 'supplier' on the form.
121 
122  if ($representative) {
123  $templateMgr->assign('representativeId', $representative->getId());
124  $templateMgr->assign('role', $representative->getRole());
125  $templateMgr->assign('representativeIdType', $representative->getRepresentativeIdType());
126  $templateMgr->assign('representativeIdValue', $representative->getRepresentativeIdValue());
127  $templateMgr->assign('name', $representative->getName());
128  $templateMgr->assign('phone', $representative->getPhone());
129  $templateMgr->assign('fax', $representative->getFax());
130  $templateMgr->assign('email', $representative->getEmail());
131  $templateMgr->assign('url', $representative->getUrl());
132  $templateMgr->assign('isSupplier', $representative->getIsSupplier() ? true : false);
133  } else { // loading a blank form
134  $templateMgr->assign('representativeIdType', '06'); // pre-populate new forms with GLN as it is recommended
135  }
136 
137  return parent::fetch($request);
138  }
139 
144  function readInputData() {
145  $this->readUserVars(array(
146  'representativeId',
147  'agentRole',
148  'supplierRole',
149  'representativeIdType',
150  'representativeIdValue',
151  'name',
152  'phone',
153  'fax',
154  'email',
155  'url',
156  'isSupplier',
157  ));
158  }
159 
164  function execute() {
165  $representativeDao = DAORegistry::getDAO('RepresentativeDAO');
166  $monograph = $this->getMonograph();
167  $representative = $this->getRepresentative();
168 
169  if (!$representative) {
170  // this is a new representative for this monograph
171  $representative = $representativeDao->newDataObject();
172  $representative->setMonographId($monograph->getId());
173  $existingRepresentative = false;
174 
175  } else {
176  $existingRepresentative = true;
177  // verify that this representative is in this monograph's context
178  if ($representativeDao->getById($representative->getId(), $monograph->getId()) == null) fatalError('Invalid representative!');
179  }
180 
181  if ($this->getData('isSupplier')) { // supplier
182  $representative->setRole($this->getData('supplierRole'));
183  } else {
184  $representative->setRole($this->getData('agentRole'));
185  }
186 
187  $representative->setRepresentativeIdType($this->getData('representativeIdType'));
188  $representative->setRepresentativeIdValue($this->getData('representativeIdValue'));
189  $representative->setName($this->getData('name'));
190  $representative->setPhone($this->getData('phone'));
191  $representative->setFax($this->getData('fax'));
192  $representative->setEmail($this->getData('email'));
193  $representative->setUrl($this->getData('url'));
194  $representative->setIsSupplier($this->getData('isSupplier') ? true : false);
195 
196  if ($existingRepresentative) {
197  $representativeDao->updateObject($representative);
198  $representativeId = $representative->getId();
199  } else {
200  $representativeId = $representativeDao->insertObject($representative);
201  }
202 
203  return $representativeId;
204  }
205 }
206 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
Form for adding/editing a representative entry.
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
getData($key)
Definition: Form.inc.php:210
setRepresentative($representative)
RepresentativeForm($monograph, $representative)