Open Journal Systems  3.3.0
CreateReviewerForm.inc.php
1 <?php
2 
16 import('lib.pkp.controllers.grid.users.reviewer.form.ReviewerForm');
17 
24  function __construct($submission, $reviewRound) {
25  parent::__construct($submission, $reviewRound);
26  $this->setTemplate('controllers/grid/users/reviewer/form/createReviewerForm.tpl');
27 
28  // the users register for the site, thus
29  // the site primary locale is the required default locale
30  $site = Application::get()->getRequest()->getSite();
31  $this->addSupportedFormLocale($site->getPrimaryLocale());
32 
33  $form = $this;
34  $this->addCheck(new FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $site->getPrimaryLocale()));
35  $this->addCheck(new FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function($familyName) use ($form) {
36  $givenNames = $form->getData('givenName');
37  foreach ($familyName as $locale => $value) {
38  if (!empty($value) && empty($givenNames[$locale])) {
39  return false;
40  }
41  }
42  return true;
43  }));
44  $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
45  $this->addCheck(new FormValidatorUsername($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
46  $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
47  $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
48  $this->addCheck(new FormValidator($this, 'userGroupId', 'required', 'user.profile.form.usergroupRequired'));
49  }
50 
51 
55  function fetch($request, $template = null, $display = false) {
56  $advancedSearchAction = $this->getAdvancedSearchAction($request);
57  $this->setReviewerFormAction($advancedSearchAction);
58  $site = $request->getSite();
59  $templateMgr = TemplateManager::getManager($request);
60  $templateMgr->assign('sitePrimaryLocale', $site->getPrimaryLocale());
61  return parent::fetch($request, $template, $display);
62  }
63 
68  function readInputData() {
69  parent::readInputData();
70 
71  $this->readUserVars(array(
72  'givenName',
73  'familyName',
74  'affiliation',
75  'interests',
76  'username',
77  'email',
78  'skipEmail',
79  'userGroupId',
80  ));
81  }
82 
86  function execute(...$functionArgs) {
87  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
88  $user = $userDao->newDataObject();
89 
90  $user->setGivenName($this->getData('givenName'), null);
91  $user->setFamilyName($this->getData('familyName'), null);
92  $user->setEmail($this->getData('email'));
93  $user->setAffiliation($this->getData('affiliation'), null); // Localized
94 
95  $authDao = DAORegistry::getDAO('AuthSourceDAO'); /* @var $authDao AuthSourceDAO */
96  $auth = $authDao->getDefaultPlugin();
97  $user->setAuthId($auth?$auth->getAuthId():0);
98  $user->setInlineHelp(1); // default new reviewers to having inline help visible
99 
100  $user->setUsername($this->getData('username'));
101  $password = Validation::generatePassword();
102 
103  if (isset($auth)) {
104  $user->setPassword($password);
105  // FIXME Check result and handle failures
106  $auth->doCreateUser($user);
107  $user->setAuthId($auth->authId);
108  $user->setPassword(Validation::encryptCredentials($user->getId(), Validation::generatePassword())); // Used for PW reset hash only
109  } else {
110  $user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
111  }
112  $user->setMustChangePassword(true); // Emailed P/W not safe
113 
114  $user->setDateRegistered(Core::getCurrentDate());
115  $reviewerId = $userDao->insertObject($user);
116 
117  // Set the reviewerId in the Form for the parent class to use
118  $this->setData('reviewerId', $reviewerId);
119 
120  // Insert the user interests
121  import('lib.pkp.classes.user.InterestManager');
122  $interestManager = new InterestManager();
123  $interestManager->setInterestsForUser($user, $this->getData('interests'));
124 
125  // Assign the selected user group ID to the user
126  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
127  $userGroupId = (int) $this->getData('userGroupId');
128  $userGroupDao->assignUserToGroup($reviewerId, $userGroupId);
129 
130  if (!$this->getData('skipEmail')) {
131  // Send welcome email to user
132  import('lib.pkp.classes.mail.MailTemplate');
133  $mail = new MailTemplate('REVIEWER_REGISTER');
134  if ($mail->isEnabled()) {
135  $request = Application::get()->getRequest();
136  $context = $request->getContext();
137  $mail->setReplyTo($context->getData('contactEmail'), $context->getData('contactName'));
138  $mail->assignParams(array('username' => $this->getData('username'), 'password' => $password, 'userFullName' => $user->getFullName()));
139  $mail->addRecipient($user->getEmail(), $user->getFullName());
140  if (!$mail->send($request)) {
141  import('classes.notification.NotificationManager');
142  $notificationMgr = new NotificationManager();
143  $notificationMgr->createTrivialNotification($request->getUser()->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('email.compose.error')));
144  }
145  }
146  }
147 
148  return parent::execute(...$functionArgs);
149  }
150 }
151 
152 
CreateReviewerForm\readInputData
readInputData()
Definition: CreateReviewerForm.inc.php:68
Validation\encryptCredentials
static encryptCredentials($username, $password, $encryption=false, $legacy=false)
Definition: Validation.inc.php:255
FormValidatorUsername
Form validation check for usernames (lowercase alphanumeric with interior dash/underscore.
Definition: FormValidatorUsername.inc.php:19
CreateReviewerForm
Form for creating and subsequently adding a reviewer to a submission.
Definition: CreateReviewerForm.inc.php:18
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\setData
setData($key, $value=null)
Definition: Form.inc.php:229
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
FormValidatorEmail
Form validation check for email addresses.
Definition: FormValidatorEmail.inc.php:20
Form\getData
getData($key)
Definition: Form.inc.php:220
Validation\generatePassword
static generatePassword($length=null)
Definition: Validation.inc.php:283
ReviewerForm\setReviewerFormAction
setReviewerFormAction($action)
Definition: ReviewerForm.inc.php:100
ReviewerForm\getAdvancedSearchAction
getAdvancedSearchAction($request)
Definition: ReviewerForm.inc.php:433
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
ReviewerForm
Base Form for adding a reviewer to a submission. N.B. Requires a subclass to implement the "reviewerI...
Definition: ReviewerForm.inc.php:19
CreateReviewerForm\execute
execute(... $functionArgs)
Definition: CreateReviewerForm.inc.php:86
Form\setTemplate
setTemplate($template)
Definition: Form.inc.php:129
Form\addSupportedFormLocale
addSupportedFormLocale($supportedLocale)
Definition: Form.inc.php:364
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
InterestManager
Handle user interest functions.
Definition: InterestManager.inc.php:16
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
CreateReviewerForm\fetch
fetch($request, $template=null, $display=false)
Definition: CreateReviewerForm.inc.php:55
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
NotificationManager
Definition: NotificationManager.inc.php:19
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
CreateReviewerForm\__construct
__construct($submission, $reviewRound)
Definition: CreateReviewerForm.inc.php:24
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18