Open Journal Systems  3.3.0
PKPAuthorForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class PKPAuthorForm extends Form {
21 
23  var $_author;
24 
28  function __construct($publication, $author) {
29  parent::__construct('controllers/grid/users/author/form/authorForm.tpl');
30  $this->setPublication($publication);
31  $this->setAuthor($author);
32 
33  // the publication locale should be the default/required locale
34  $this->setDefaultFormLocale($publication->getData('locale'));
35 
36  // Validation checks for this form
37  $form = $this;
38  $this->addCheck(new FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $this->defaultLocale));
39  $this->addCheck(new FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function($familyName) use ($form) {
40  $givenNames = $form->getData('givenName');
41  foreach ($familyName as $locale => $value) {
42  if (!empty($value) && empty($givenNames[$locale])) {
43  return false;
44  }
45  }
46  return true;
47  }));
48  $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'form.emailRequired'));
49  $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
50  $this->addCheck(new FormValidator($this, 'userGroupId', 'required', 'submission.submit.form.contributorRoleRequired'));
51  $this->addCheck(new FormValidatorORCID($this, 'orcid', 'optional', 'user.orcid.orcidInvalid'));
52  $this->addCheck(new FormValidatorPost($this));
53  $this->addCheck(new FormValidatorCSRF($this));
54  }
55 
56  //
57  // Getters and Setters
58  //
63  function getAuthor() {
64  return $this->_author;
65  }
66 
71  function setAuthor($author) {
72  $this->_author = $author;
73  }
74 
79  function getPublication() {
80  return $this->_publication;
81  }
82 
87  function setPublication($publication) {
88  $this->_publication = $publication;
89  }
90 
91 
92  //
93  // Overridden template methods
94  //
98  function initData() {
99  $author = $this->getAuthor();
100 
101  if ($author) {
102  $this->_data = array(
103  'authorId' => $author->getId(),
104  'givenName' => $author->getGivenName(null),
105  'familyName' => $author->getFamilyName(null),
106  'preferredPublicName' => $author->getPreferredPublicName(null),
107  'affiliation' => $author->getAffiliation(null),
108  'country' => $author->getCountry(),
109  'email' => $author->getEmail(),
110  'userUrl' => $author->getUrl(),
111  'orcid' => $author->getOrcid(),
112  'userGroupId' => $author->getUserGroupId(),
113  'biography' => $author->getBiography(null),
114  'primaryContact' => $this->getPublication()->getData('primaryContactId') === $author->getId(),
115  'includeInBrowse' => $author->getIncludeInBrowse(),
116  );
117  } else {
118  // assume authors should be listed unless otherwise specified.
119  $this->_data = array('includeInBrowse' => true);
120  }
121  // in order to be able to use the hook
122  return parent::initData();
123  }
124 
128  function fetch($request, $template = null, $display = false) {
129  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
130  $authorUserGroups = $userGroupDao->getByRoleId($request->getContext()->getId(), ROLE_ID_AUTHOR);
131  $publication = $this->getPublication();
132  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
133  $countries = array();
134  foreach ($isoCodes->getCountries() as $country) {
135  $countries[$country->getAlpha2()] = $country->getLocalName();
136  }
137  asort($countries);
138  $templateMgr = TemplateManager::getManager($request);
139  $templateMgr->assign(array(
140  'submissionId' => $publication->getData('submissionId'),
141  'publicationId' => $publication->getId(),
142  'countries' => $countries,
143  'authorUserGroups' => $authorUserGroups,
144  ));
145 
146  return parent::fetch($request, $template, $display);
147  }
148 
153  function readInputData() {
154  $this->readUserVars(array(
155  'authorId',
156  'givenName',
157  'familyName',
158  'preferredPublicName',
159  'affiliation',
160  'country',
161  'email',
162  'userUrl',
163  'orcid',
164  'userGroupId',
165  'biography',
166  'primaryContact',
167  'includeInBrowse',
168  ));
169  }
170 
175  function execute(...$functionParams) {
176  $authorDao = DAORegistry::getDAO('AuthorDAO'); /* @var $authorDao AuthorDAO */
177  $publication = $this->getPublication();
178 
179  $author = $this->getAuthor();
180  if (!$author) {
181  // this is a new submission contributor
182  $this->_author = $authorDao->newDataObject();
183  $author = $this->getAuthor();
184  $author->setData('publicationId', $publication->getId());
185  $author->setData('seq', count($publication->getData('authors')));
186  $existingAuthor = false;
187  } else {
188  $existingAuthor = true;
189  if ($publication->getId() !== $author->getData('publicationId')) fatalError('Invalid author!');
190  }
191 
192  $author->setGivenName($this->getData('givenName'), null);
193  $author->setFamilyName($this->getData('familyName'), null);
194  $author->setPreferredPublicName($this->getData('preferredPublicName'), null);
195  $author->setAffiliation($this->getData('affiliation'), null); // localized
196  $author->setCountry($this->getData('country'));
197  $author->setEmail($this->getData('email'));
198  $author->setUrl($this->getData('userUrl'));
199  $author->setOrcid($this->getData('orcid'));
200  $author->setUserGroupId($this->getData('userGroupId'));
201  $author->setBiography($this->getData('biography'), null); // localized
202  $author->setIncludeInBrowse(($this->getData('includeInBrowse') ? true : false));
203 
204  // in order to be able to use the hook
205  parent::execute(...$functionParams);
206 
207  if ($existingAuthor) {
208  $authorDao->updateObject($author);
209  $authorId = $author->getId();
210  } else {
211  $authorId = $authorDao->insertObject($author);
212  }
213 
214  if ($this->getData('primaryContact')) {
215  $submission = Services::get('submission')->get($publication->getData('submissionId'));
216  $context = Services::get('context')->get($submission->getData('contextId'));
217  $params = ['primaryContactId' => $authorId];
218  $errors = Services::get('publication')->validate(
219  VALIDATE_ACTION_EDIT,
220  $params,
221  $context->getData('supportedLocales'),
222  $publication->getData('locale')
223  );
224  if (!empty($errors)) {
225  throw new Exception('Invalid primary contact ID. This author can not be a primary contact.');
226  }
227  $publication = Services::get('publication')->edit($publication, $params, Application::get()->getRequest());
228  }
229 
230  return $authorId;
231  }
232 }
233 
234 
PKPAuthorForm\readInputData
readInputData()
Definition: PKPAuthorForm.inc.php:153
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
FormValidatorLocale
Class to represent a form validation check for localized fields.
Definition: FormValidatorLocale.inc.php:16
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
PKPAuthorForm
Form for adding/editing a author.
Definition: PKPAuthorForm.inc.php:18
FormValidatorEmail
Form validation check for email addresses.
Definition: FormValidatorEmail.inc.php:20
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorORCID
Form validation check for ORCID iDs.
Definition: FormValidatorORCID.inc.php:18
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
Form\setDefaultFormLocale
setDefaultFormLocale($defaultLocale)
Definition: Form.inc.php:356
PKPAuthorForm\execute
execute(... $functionParams)
Definition: PKPAuthorForm.inc.php:175
PKPAuthorForm\setPublication
setPublication($publication)
Definition: PKPAuthorForm.inc.php:87
PKPAuthorForm\fetch
fetch($request, $template=null, $display=false)
Definition: PKPAuthorForm.inc.php:128
PKPAuthorForm\$_author
$_author
Definition: PKPAuthorForm.inc.php:23
PKPAuthorForm\$_publication
$_publication
Definition: PKPAuthorForm.inc.php:20
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
PKPAuthorForm\initData
initData()
Definition: PKPAuthorForm.inc.php:98
PKPAuthorForm\setAuthor
setAuthor($author)
Definition: PKPAuthorForm.inc.php:71
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
FormValidatorUrl
Form validation check for URLs.
Definition: FormValidatorUrl.inc.php:20
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
PKPAuthorForm\__construct
__construct($publication, $author)
Definition: PKPAuthorForm.inc.php:28
PKPAuthorForm\getAuthor
getAuthor()
Definition: PKPAuthorForm.inc.php:63
PKPAuthorForm\getPublication
getPublication()
Definition: PKPAuthorForm.inc.php:79
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49