示例#1
0
 /**
  * Send reminders for a user.
  *
  * @param \Finna\Db\Table\Row\User $user        User.
  * @param array                    $remindLoans Loans to be reminded.
  *
  * @return boolean success.
  */
 protected function sendReminder($user, $remindLoans)
 {
     if (!$user->email || trim($user->email) == '') {
         $this->msg("User {$user->username} (id {$user->id})" . ' does not have an email address, bypassing due date reminders');
         return false;
     }
     list($userInstitution, ) = explode(':', $user['username'], 2);
     if (!$this->currentInstitution || $userInstitution != $this->currentInstitution) {
         $templateDirs = ["{$this->baseDir}/themes/finna/templates"];
         if (!($viewPath = $this->resolveViewPath($userInstitution))) {
             $this->err("Could not resolve view path for user {$user->username}" . " (id {$user->id})");
             return false;
         } else {
             $templateDirs[] = "{$viewPath}/themes/custom/templates";
         }
         $this->currentInstitution = $userInstitution;
         $this->currentViewPath = $viewPath;
         $resolver = new AggregateResolver();
         $this->renderer->setResolver($resolver);
         $stack = new TemplatePathStack(['script_paths' => $templateDirs]);
         $resolver->attach($stack);
         $siteConfig = $viewPath . '/local/config/vufind/config.ini';
         $this->currentSiteConfig = parse_ini_file($siteConfig, true);
     }
     $language = isset($this->currentSiteConfig['Site']['language']) ? $this->currentSiteConfig['Site']['language'] : 'fi';
     $validLanguages = array_keys($this->currentSiteConfig['Languages']);
     if (!empty($user->finna_language) && in_array($user->finna_language, $validLanguages)) {
         $language = $user->finna_language;
     }
     $this->translator->addTranslationFile('ExtendedIni', null, 'default', $language)->setLocale($language);
     $key = $this->dueDateReminderTable->getUnsubscribeSecret($this->hmac, $user, $user->id);
     $params = ['id' => $user->id, 'type' => 'reminder', 'key' => $key];
     $unsubscribeUrl = $this->urlHelper->__invoke('myresearch-unsubscribe') . '?' . http_build_query($params);
     $urlParts = explode('/', $this->currentViewPath);
     $urlView = array_pop($urlParts);
     $urlInstitution = array_pop($urlParts);
     $baseUrl = 'https://' . $urlInstitution . '.finna.fi';
     if ($urlView != $this::DEFAULT_PATH) {
         $baseUrl .= "/{$urlView}";
     }
     $params = ['loans' => $remindLoans, 'url' => $baseUrl . $this->urlHelper->__invoke('myresearch-checkedout'), 'unsubscribeUrl' => $baseUrl . $unsubscribeUrl, 'baseUrl' => $baseUrl];
     $subject = $this->translator->translate('due_date_email_subject');
     $message = $this->renderer->render("Email/due-date-reminder.phtml", $params);
     try {
         $to = $user->email;
         $from = $this->currentSiteConfig['Site']['email'];
         $this->serviceManager->get('VuFind\\Mailer')->send($to, $from, $subject, $message);
     } catch (\Exception $e) {
         $this->err("Failed to send due date reminders to user {$user->username} " . " (id {$user->id})");
         $this->err('   ' . $e->getMessage());
         return false;
     }
     foreach ($remindLoans as $loan) {
         $params = ['user_id' => $user->id, 'loan_id' => $loan['loanId']];
         $this->dueDateReminderTable->delete($params);
         $dueDate = new \DateTime($loan['dueDate']);
         $params['due_date'] = $dueDate->format($this::DUE_DATE_FORMAT);
         $params['notification_date'] = gmdate($this::DUE_DATE_FORMAT, time());
         $this->dueDateReminderTable->insert($params);
     }
     return true;
 }