|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Slub\DigasFeManagement\Command; |
| 4 | + |
| 5 | +/*************************************************************** |
| 6 | + * Copyright notice |
| 7 | + * |
| 8 | + * (c) 2022 SLUB Dresden <[email protected]> |
| 9 | + * All rights reserved |
| 10 | + * |
| 11 | + * This script is part of the TYPO3 project. The TYPO3 project is |
| 12 | + * free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 3 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * The GNU General Public License can be found at |
| 18 | + * http://www.gnu.org/copyleft/gpl.html. |
| 19 | + * |
| 20 | + * This script is distributed in the hope that it will be useful, |
| 21 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | + * GNU General Public License for more details. |
| 24 | + * |
| 25 | + * This copyright notice MUST APPEAR in all copies of the script! |
| 26 | + ***************************************************************/ |
| 27 | + |
| 28 | +use Slub\DigasFeManagement\Domain\Model\Access; |
| 29 | +use Slub\DigasFeManagement\Domain\Model\User; |
| 30 | +use Symfony\Component\Console\Input\InputArgument; |
| 31 | +use Symfony\Component\Console\Input\InputInterface; |
| 32 | +use Symfony\Component\Console\Output\OutputInterface; |
| 33 | +use TYPO3\CMS\Core\Mail\MailMessage; |
| 34 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 35 | +use TYPO3\CMS\Core\Utility\MathUtility; |
| 36 | +use TYPO3\CMS\Extbase\Utility\LocalizationUtility as ExtbaseLocalizationUtility; |
| 37 | + |
| 38 | +/** |
| 39 | + * Class KitodoAccessExpirationNotification |
| 40 | + */ |
| 41 | +class KitodoAccessExpirationNotification extends DigasBaseCommand |
| 42 | +{ |
| 43 | + /** |
| 44 | + * @var int Expiration timespan in days. |
| 45 | + */ |
| 46 | + protected $expirationTimestamp; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param InputInterface $input |
| 50 | + * @param OutputInterface $output |
| 51 | + */ |
| 52 | + public function initialize(InputInterface $input, OutputInterface $output) |
| 53 | + { |
| 54 | + parent::initialize($input, $output); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Configure the command by defining the name, options and arguments |
| 59 | + */ |
| 60 | + protected function configure() |
| 61 | + { |
| 62 | + $this->setDescription('[DiGAS FE Management] Notify fe_users about expiring kitodo documents.') |
| 63 | + ->addArgument( |
| 64 | + 'expirationTimestamp', |
| 65 | + InputArgument::REQUIRED, |
| 66 | + 'Add a timespan in days (i.e. "365"). fe_users with expiring document access will get an information email.' |
| 67 | + )->setHelp( |
| 68 | + 'This command informs fe_users with expiring kitodo document access.' |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Executes the command |
| 74 | + * |
| 75 | + * @param InputInterface $input |
| 76 | + * @param OutputInterface $output |
| 77 | + * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException |
| 78 | + */ |
| 79 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 80 | + { |
| 81 | + // Initialize IO |
| 82 | + parent::execute($input, $output); |
| 83 | + |
| 84 | + if (MathUtility::canBeInterpretedAsInteger($input->getArgument('expirationTimestamp'))) { |
| 85 | + $this->expirationTimestamp = MathUtility::forceIntegerInRange((int)$input->getArgument('expirationTimestamp'), 0); |
| 86 | + } |
| 87 | + |
| 88 | + if ($this->expirationTimestamp <= 0) { |
| 89 | + $this->io->error('"expirationTimestamp" has to a positive integer value. Abort.'); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $time = new \DateTime(); |
| 94 | + $expirationTimestamp = $time->getTimestamp() + ((60 * 60 * 24) * $this->expirationTimestamp); |
| 95 | + |
| 96 | + $this->io->text('Get fe_users with nearly expiration documents.'); |
| 97 | + // get fe users with requests for access loop |
| 98 | + $expireAccessUsers = $this->AccessRepository->findExpirationUsers($expirationTimestamp); |
| 99 | + $this->io->text(count($expireAccessUsers) . ' fe_users with expiring documents were found.'); |
| 100 | + |
| 101 | + if (!empty($expireAccessUsers)) { |
| 102 | + foreach ($expireAccessUsers as $expireUser) { |
| 103 | + /** @var User $feUser */ |
| 104 | + $feUser = $this->UserRepository->findByUid($expireUser->getFeUser()); |
| 105 | + $expiringAccessEntries = $this->AccessRepository->findExpiringEntriesByUser($expireUser->getFeUser(), $expirationTimestamp); |
| 106 | + |
| 107 | + if (!empty($expiringAccessEntries)) { |
| 108 | + $this->io->text(sprintf('Notify fe_user (UID: %s) with %s expiring documents.', $expireUser->getFeUser(), count($expiringAccessEntries))); |
| 109 | + |
| 110 | + $this->notifyUser($feUser, $expiringAccessEntries); |
| 111 | + $this->persistenceManager->persistAll(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + $this->io->success('Task finished successfully.'); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Update access model object and set accessGrantedNotification |
| 122 | + * |
| 123 | + * @param Access $accessEntry |
| 124 | + * @param string $notificationTimestamp |
| 125 | + * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
| 126 | + * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException |
| 127 | + */ |
| 128 | + protected function updateAccessEntry(Access $accessEntry, int $notificationTimestamp) |
| 129 | + { |
| 130 | + // update access entry with notification time |
| 131 | + $accessEntry->setExpireNotification($notificationTimestamp); |
| 132 | + $this->AccessRepository->update($accessEntry); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Send email to fe_users with expiring kitodo documents |
| 137 | + * |
| 138 | + * @param User $feUser |
| 139 | + * @param array $documentsList |
| 140 | + */ |
| 141 | + protected function sendNotificationEmail(User $feUser, array $documentsList) |
| 142 | + { |
| 143 | + $this->initUserLocal($feUser); |
| 144 | + $userEmail = $feUser->getEmail(); |
| 145 | + $userFullName = $feUser->getFullName(); |
| 146 | + if (!GeneralUtility::validEmail($userEmail)) { |
| 147 | + $this->io->warning(sprintf('[DiGAS FE Management] Expiration notification warning to user (UID: %s) could not be sent. No valid email address.', $feUser->getUid())); |
| 148 | + return; |
| 149 | + } |
| 150 | + $email = GeneralUtility::makeInstance(MailMessage::class); |
| 151 | + |
| 152 | + $textEmail = $this->generateNotificationEmail( |
| 153 | + $documentsList, |
| 154 | + 'EXT:digas_fe_management/Resources/Private/Templates/Email/Text/KitodoAccessExpirationNotification.html' |
| 155 | + ); |
| 156 | + $htmlEmail = $this->generateNotificationEmail( |
| 157 | + $documentsList, |
| 158 | + 'EXT:digas_fe_management/Resources/Private/Templates/Email/Html/KitodoAccessExpirationNotification.html', |
| 159 | + 'html' |
| 160 | + ); |
| 161 | + $emailSubject = ExtbaseLocalizationUtility::translate('kitodoAccessExpirationNotification.email.subject', 'DigasFeManagement'); |
| 162 | + |
| 163 | + // Prepare and send the message |
| 164 | + $email->setSubject($emailSubject) |
| 165 | + ->setFrom([ |
| 166 | + $this->settings['adminEmail'] => $this->settings['adminName'] |
| 167 | + ]) |
| 168 | + ->setTo([ |
| 169 | + $userEmail => $userFullName |
| 170 | + ]) |
| 171 | + ->setBody($textEmail) |
| 172 | + ->addPart($htmlEmail, 'text/html') |
| 173 | + ->send(); |
| 174 | + } |
| 175 | +} |
0 commit comments