/** * Import the parsed users into the system. * @param $sendNotify boolean send an email notification to each imported user containing their username and password * @param $continueOnError boolean continue to import remaining users if a failure occurs * @return boolean success */ function importUsers($sendNotify = false, $continueOnError = false) { $success = true; $this->importedUsers = array(); $this->errors = array(); $userDao =& DAORegistry::getDAO('UserDAO'); $roleDao =& DAORegistry::getDAO('RoleDAO'); if ($sendNotify) { // Set up mail template to send to added users import('classes.mail.MailTemplate'); $mail = new MailTemplate('USER_REGISTER'); $journalDao =& DAORegistry::getDAO('JournalDAO'); $journal =& $journalDao->getById($this->journalId); $mail->setFrom($journal->getSetting('contactEmail'), $journal->getSetting('contactName')); } for ($i = 0, $count = count($this->usersToImport); $i < $count; $i++) { $user =& $this->usersToImport[$i]; // If the email address already exists in the system, // then assign the user the username associated with that email address. if ($user->getEmail() != null) { $emailExists = $userDao->getUserByEmail($user->getEmail(), true); if ($emailExists != null) { $user->setUsername($emailExists->getUsername()); } } if ($user->getUsername() == null) { $newUsername = true; $this->generateUsername($user); } else { $newUsername = false; } if ($user->getUnencryptedPassword() != null) { $user->setPassword(Validation::encryptCredentials($user->getUsername(), $user->getUnencryptedPassword())); } else { if ($user->getPassword() == null) { $this->generatePassword($user); } } if (!$newUsername) { // Check if user already exists $userExists = $userDao->getByUsername($user->getUsername(), true); if ($userExists != null) { $user->setId($userExists->getId()); } } else { $userExists = false; } if ($newUsername || !$userExists) { // Create new user account // If the user's username was specified in the data file and // the username already exists, only the new roles are added for that user if (!$userDao->insertUser($user)) { // Failed to add user! $this->errors[] = sprintf('%s: %s (%s)', __('manager.people.importUsers.failedToImportUser'), $user->getFullName(), $user->getUsername()); if ($continueOnError) { // Skip to next user $success = false; continue; } else { return false; } } } // Add reviewing interests to interests table $interestDao =& DAORegistry::getDAO('InterestDAO'); $interests = $user->getTemporaryInterests(); $interests = explode(',', $interests); $interests = array_map('trim', $interests); // Trim leading whitespace if (is_array($interests) && !empty($interests)) { $interestDao->setUserInterests($interests, $user->getId()); } // Enroll user in specified roles // If the user is already enrolled in a role, that role is skipped foreach ($user->getRoles() as $role) { $role->setUserId($user->getId()); $role->setJournalId($this->journalId); if (!$roleDao->userHasRole($role->getJournalId(), $role->getUserId(), $role->getRoleId())) { if (!$roleDao->insertRole($role)) { // Failed to add role! $this->errors[] = sprintf('%s: %s - %s (%s)', __('manager.people.importUsers.failedToImportRole'), $role->getRoleName(), $user->getFullName(), $user->getUsername()); if ($continueOnError) { // Continue to insert other roles for this user $success = false; continue; } else { return false; } } } } if ($sendNotify && !$userExists) { // Send email notification to user as if user just registered themselves $mail->addRecipient($user->getEmail(), $user->getFullName()); $mail->sendWithParams(array('journalName' => $journal->getTitle($journal->getPrimaryLocale()), 'username' => $user->getUsername(), 'password' => $user->getUnencryptedPassword() == null ? '-' : $user->getUnencryptedPassword(), 'userFullName' => $user->getFullName())); $mail->clearRecipients(); } array_push($this->importedUsers, $user); } return $success; }