Open Monograph Press  3.3.0
RepresentativeForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class RepresentativeForm extends Form {
21 
24 
28  public function __construct($monograph, $representative) {
29  parent::__construct('controllers/grid/catalogEntry/form/representativeForm.tpl');
30  $this->setMonograph($monograph);
31  $this->setRepresentative($representative);
32 
33  // Validation checks for this form
34  $form = $this;
35  $this->addCheck(new FormValidatorCustom(
36  $this, 'isSupplier', 'required', 'grid.catalogEntry.roleRequired',
37  function($isSupplier) use ($form) {
38  $request = Application::get()->getRequest();
39  $agentRole = $request->getUserVar('agentRole');
40  $supplierRole = $request->getUserVar('supplierRole');
41  $onixDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixDao ONIXCodelistItemDAO */
42  return (!$isSupplier && $onixDao->codeExistsInList($agentRole, 'List69')) || ($isSupplier && $onixDao->codeExistsInList($supplierRole, 'List93'));
43  }
44  ));
45  $this->addCheck(new FormValidatorPost($this));
46  $this->addCheck(new FormValidatorCSRF($this));
47  }
48 
49  //
50  // Getters and Setters
51  //
56  public function &getRepresentative() {
58  }
59 
64  public function setRepresentative($representative) {
65  $this->_representative = $representative;
66  }
67 
72  public function getMonograph() {
73  return $this->_monograph;
74  }
75 
80  public function setMonograph($monograph) {
81  $this->_monograph = $monograph;
82  }
83 
84 
85  //
86  // Overridden template methods
87  //
91  public function initData() {
92  $representative = $this->getRepresentative();
93 
94  if ($representative) {
95  $this->_data = array(
96  'representativeId' => $representative->getId(),
97  'role' => $representative->getRole(),
98  'representativeIdType' => $representative->getRepresentativeIdType(),
99  'representativeIdValue' => $representative->getRepresentativeIdValue(),
100  'name' => $representative->getName(),
101  'phone' => $representative->getPhone(),
102  'email' => $representative->getEmail(),
103  'url' =>$representative->getUrl(),
104  'isSupplier' => $representative->getIsSupplier(),
105  );
106  }
107  }
108 
112  public function fetch($request, $template = null, $display = false) {
113  $templateMgr = TemplateManager::getManager($request);
114 
115  $monograph = $this->getMonograph();
116  $templateMgr->assign('submissionId', $monograph->getId());
117  $representative = $this->getRepresentative();
118  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixCodelistItemDao ONIXCodelistItemDAO */
119  $templateMgr->assign(array(
120  'idTypeCodes' => $onixCodelistItemDao->getCodes('List92'), // GLN, etc
121  'agentRoleCodes' => $onixCodelistItemDao->getCodes('List69'), // Sales Agent, etc
122  'supplierRoleCodes' => $onixCodelistItemDao->getCodes('List93'), // wholesaler, publisher to retailer, etc
123  'isSupplier' => true,
124  )); // default to 'supplier' on the form.
125 
126  if ($representative) $templateMgr->assign(array(
127  'representativeId' => $representative->getId(),
128  'role' => $representative->getRole(),
129  'representativeIdType' => $representative->getRepresentativeIdType(),
130  'representativeIdValue' => $representative->getRepresentativeIdValue(),
131  'name' => $representative->getName(),
132  'phone' => $representative->getPhone(),
133  'email' => $representative->getEmail(),
134  'url' => $representative->getUrl(),
135  'isSupplier' => $representative->getIsSupplier() ? true : false,
136  ));
137  else $templateMgr->assign('representativeIdType', '06'); // pre-populate new forms with GLN as it is recommended
138 
139  return parent::fetch($request, $template, $display);
140  }
141 
146  public function readInputData() {
147  $this->readUserVars(array(
148  'representativeId',
149  'agentRole',
150  'supplierRole',
151  'representativeIdType',
152  'representativeIdValue',
153  'name',
154  'phone',
155  'email',
156  'url',
157  'isSupplier',
158  ));
159  }
160 
164  public function execute(...$functionArgs) {
165  parent::execute(...$functionArgs);
166  $representativeDao = DAORegistry::getDAO('RepresentativeDAO'); /* @var $representativeDao RepresentativeDAO */
167  $monograph = $this->getMonograph();
168  $representative = $this->getRepresentative();
169 
170  if (!$representative) {
171  // this is a new representative for this monograph
172  $representative = $representativeDao->newDataObject();
173  $representative->setMonographId($monograph->getId());
174  $existingRepresentative = false;
175 
176  } else {
177  $existingRepresentative = true;
178  // verify that this representative is in this monograph's context
179  if ($representativeDao->getById($representative->getId(), $monograph->getId()) == null) fatalError('Invalid representative!');
180  }
181 
182  if ($this->getData('isSupplier')) { // supplier
183  $representative->setRole($this->getData('supplierRole'));
184  } else {
185  $representative->setRole($this->getData('agentRole'));
186  }
187 
188  $representative->setRepresentativeIdType($this->getData('representativeIdType'));
189  $representative->setRepresentativeIdValue($this->getData('representativeIdValue'));
190  $representative->setName($this->getData('name'));
191  $representative->setPhone($this->getData('phone'));
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 
RepresentativeForm
Form for adding/editing a representative entry.
Definition: RepresentativeForm.inc.php:18
RepresentativeForm\readInputData
readInputData()
Definition: RepresentativeForm.inc.php:146
RepresentativeForm\getMonograph
getMonograph()
Definition: RepresentativeForm.inc.php:72
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
RepresentativeForm\setRepresentative
setRepresentative($representative)
Definition: RepresentativeForm.inc.php:64
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
RepresentativeForm\initData
initData()
Definition: RepresentativeForm.inc.php:91
RepresentativeForm\execute
execute(... $functionArgs)
Definition: RepresentativeForm.inc.php:164
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
RepresentativeForm\setMonograph
setMonograph($monograph)
Definition: RepresentativeForm.inc.php:80
RepresentativeForm\getRepresentative
& getRepresentative()
Definition: RepresentativeForm.inc.php:56
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
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18
RepresentativeForm\fetch
fetch($request, $template=null, $display=false)
Definition: RepresentativeForm.inc.php:112
RepresentativeForm\__construct
__construct($monograph, $representative)
Definition: RepresentativeForm.inc.php:28
RepresentativeForm\$_representative
$_representative
Definition: RepresentativeForm.inc.php:23
RepresentativeForm\$_monograph
$_monograph
Definition: RepresentativeForm.inc.php:20