|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Feedback Controller |
| 4 | + * |
| 5 | + * PHP version 7 |
| 6 | + * |
| 7 | + * @category VuFind |
| 8 | + * @package Controller |
| 9 | + * @author Johannes Schultze <[email protected]> |
| 10 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
| 11 | + * @link https://vufind.org Main Site |
| 12 | + */ |
| 13 | +namespace ResultFeedback\Controller; |
| 14 | + |
| 15 | +use VuFind\Exception\Mail as MailException; |
| 16 | +use Zend\Mail\Address; |
| 17 | + |
| 18 | +/** |
| 19 | + * Feedback Class |
| 20 | + * |
| 21 | + * Controls the Feedback |
| 22 | + * |
| 23 | + * @category VuFind |
| 24 | + * @package Controller |
| 25 | + * @author Johannes Schultze <[email protected]> |
| 26 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
| 27 | + * @link https://vufind.org/wiki/development Wiki |
| 28 | + */ |
| 29 | +class ResultFeedbackController extends \VuFind\Controller\AbstractBase |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Display Feedback home form. |
| 33 | + * |
| 34 | + * @return \Zend\View\Model\ViewModel |
| 35 | + */ |
| 36 | + public function homeAction() |
| 37 | + { |
| 38 | + return $this->forwardTo('ResultFeedback', 'Email'); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Receives input from the user and sends an email to the recipient set in |
| 43 | + * the resultFeedback.ini |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function emailAction() |
| 48 | + { |
| 49 | + $translator = $this->serviceLocator->get('Zend\Mvc\I18n\Translator'); |
| 50 | + |
| 51 | + $view = $this->createViewModel(); |
| 52 | + $view->useRecaptcha = $this->recaptcha()->active('feedback'); |
| 53 | + $view->name = $this->params()->fromPost('name'); |
| 54 | + $view->email = $this->params()->fromPost('email'); |
| 55 | + $view->comments = $this->params()->fromPost('comments'); |
| 56 | + $view->usertype = $this->params()->fromPost('usertype'); |
| 57 | + $view->recordid = $this->params()->fromPost('recordid'); |
| 58 | + $view->recordtitle = $this->params()->fromPost('recordtitle'); |
| 59 | + |
| 60 | + $id = $this->params()->fromRoute('id', $this->params()->fromQuery('id')); |
| 61 | + $view->id = $id; |
| 62 | + $searchClassId = $this->params()->fromRoute('searchclassid', $this->params()->fromQuery('searchclassid')); |
| 63 | + $view->searchClassId = $searchClassId; |
| 64 | + $recordLoader = $this->serviceLocator->get('VuFind\Record\Loader');; |
| 65 | + $driver = $recordLoader->load($id, $searchClassId, false); |
| 66 | + $view->driver = $driver; |
| 67 | + |
| 68 | + $resultFeedbackConfig = $this->serviceLocator->get('VuFind\Config\PluginManager')->get('resultFeedback')->toArray(); |
| 69 | + $resultUserTypes = []; |
| 70 | + if (isset($resultFeedbackConfig['resultFeedback']['user_types'])) { |
| 71 | + $resultUserTypes = $resultFeedbackConfig['resultFeedback']['user_types']; |
| 72 | + } |
| 73 | + $view->resultUserTypes = $resultUserTypes; |
| 74 | + |
| 75 | + // Process form submission: |
| 76 | + $view->hideForm = false; |
| 77 | + if ($this->formWasSubmitted('submit', $view->useRecaptcha)) { |
| 78 | + if (empty($view->email) || empty($view->comments)) { |
| 79 | + $this->flashMessenger()->addMessage('bulk_error_missing', 'error'); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $recipient_email = isset($resultFeedbackConfig['resultFeedback']['recipient_email']) ? $resultFeedbackConfig['resultFeedback']['recipient_email'] : null; |
| 84 | + $recipient_name = isset($resultFeedbackConfig['resultFeedback']['recipient_name']) ? $resultFeedbackConfig['resultFeedback']['recipient_name'] : 'Your Library'; |
| 85 | + $email_subject = isset($resultFeedbackConfig['resultFeedback']['email_subject']) ? $resultFeedbackConfig['resultFeedback']['email_subject'] : 'Result Feedback'; |
| 86 | + $sender_email = isset( $resultFeedbackConfig[ 'resultFeedback'][ 'sender_email']) ? $resultFeedbackConfig[ 'resultFeedback'][ 'sender_email'] : '[email protected]'; |
| 87 | + $sender_name = isset($resultFeedbackConfig['resultFeedback']['sender_name']) ? $resultFeedbackConfig['resultFeedback']['sender_name'] : 'Result Feedback'; |
| 88 | + if ($recipient_email == null) { |
| 89 | + throw new \Exception( |
| 90 | + 'Result Feedback Module Error: Recipient Email Unset (see resultFeedback.ini)' |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + $email_message = $translator->translate('resultfeedback_usertype') . ':' . "\n" . $translator->translate($view->usertype) . "\n\n"; |
| 95 | + $email_message .= empty($view->name) ? '' : 'Name:' . "\n" . $view->name . "\n\n"; |
| 96 | + $email_message .= $translator->translate('Email') . ':' . "\n" . $view->email . "\n\n"; |
| 97 | + $email_message .= $translator->translate('PPN') . ':' . "\n" . $view->recordid . "\n\n"; |
| 98 | + $email_message .= $translator->translate('Title') . ':' . "\n" . $view->recordtitle . "\n\n"; |
| 99 | + $email_message .= $translator->translate('Message') . ':' . "\n" . $view->comments . "\n\n"; |
| 100 | + |
| 101 | + // This sets up the email to be sent |
| 102 | + // Attempt to send the email and show an appropriate flash message: |
| 103 | + try { |
| 104 | + $mailer = $this->serviceLocator->get('VuFind\Mailer\Mailer'); |
| 105 | + $mailer->send( |
| 106 | + new Address($recipient_email, $recipient_name), |
| 107 | + new Address($sender_email, $sender_name), |
| 108 | + $email_subject, |
| 109 | + $email_message, |
| 110 | + null, |
| 111 | + $view->email |
| 112 | + ); |
| 113 | + $this->flashMessenger()->addMessage( |
| 114 | + 'Your result feedback has been send', 'success' |
| 115 | + ); |
| 116 | + $view->hideForm = true; |
| 117 | + } catch (MailException $e) { |
| 118 | + $this->flashMessenger()->addMessage($e->getMessage(), 'error'); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $view; |
| 123 | + } |
| 124 | +} |
0 commit comments