Cleanup the pagecontroller and vote template a bit
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
dd12c9b3b0
commit
f368100a48
|
@ -27,43 +27,30 @@
|
||||||
|
|
||||||
namespace OCA\Forms\Controller;
|
namespace OCA\Forms\Controller;
|
||||||
|
|
||||||
|
use OCA\Forms\AppInfo\Application;
|
||||||
use OCA\Forms\Db\Event;
|
use OCA\Forms\Db\Event;
|
||||||
use OCA\Forms\Db\EventMapper;
|
use OCA\Forms\Db\EventMapper;
|
||||||
use OCA\Forms\Db\Notification;
|
|
||||||
use OCA\Forms\Db\NotificationMapper;
|
|
||||||
use OCA\Forms\Db\Vote;
|
use OCA\Forms\Db\Vote;
|
||||||
use OCA\Forms\Db\VoteMapper;
|
use OCA\Forms\Db\VoteMapper;
|
||||||
|
|
||||||
use OCA\Forms\Db\Answer;
|
|
||||||
use OCA\Forms\Db\AnswerMapper;
|
use OCA\Forms\Db\AnswerMapper;
|
||||||
use OCA\Forms\Db\Question;
|
|
||||||
use OCA\Forms\Db\QuestionMapper;
|
use OCA\Forms\Db\QuestionMapper;
|
||||||
|
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Db\DoesNotExistException;
|
use OCP\AppFramework\Db\DoesNotExistException;
|
||||||
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
|
||||||
use OCP\AppFramework\Http\RedirectResponse;
|
use OCP\AppFramework\Http\RedirectResponse;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\IAvatarManager;
|
|
||||||
use OCP\IConfig;
|
|
||||||
use OCP\IGroupManager;
|
use OCP\IGroupManager;
|
||||||
use OCP\IL10N;
|
|
||||||
use OCP\ILogger;
|
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
use OCP\IURLGenerator;
|
use OCP\IURLGenerator;
|
||||||
use OCP\IUser;
|
|
||||||
use OCP\IUserManager;
|
use OCP\IUserManager;
|
||||||
use OCP\L10N\IFactory;
|
|
||||||
use OCP\Mail\IMailer;
|
|
||||||
use OCP\Security\ISecureRandom;
|
|
||||||
use OCP\User; //To do: replace according to API
|
use OCP\User; //To do: replace according to API
|
||||||
use OCP\Util;
|
use OCP\Util;
|
||||||
|
|
||||||
class PageController extends Controller {
|
class PageController extends Controller {
|
||||||
|
|
||||||
private $userId;
|
private $userId;
|
||||||
private $config;
|
|
||||||
private $eventMapper;
|
private $eventMapper;
|
||||||
private $notificationMapper;
|
private $notificationMapper;
|
||||||
private $voteMapper;
|
private $voteMapper;
|
||||||
|
@ -73,75 +60,29 @@ class PageController extends Controller {
|
||||||
|
|
||||||
private $urlGenerator;
|
private $urlGenerator;
|
||||||
private $userMgr;
|
private $userMgr;
|
||||||
private $avatarManager;
|
|
||||||
private $logger;
|
|
||||||
private $trans;
|
|
||||||
private $transFactory;
|
|
||||||
private $groupManager;
|
private $groupManager;
|
||||||
private $mailer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PageController constructor.
|
|
||||||
* @param string $appName
|
|
||||||
* @param IRequest $request
|
|
||||||
* @param IConfig $config
|
|
||||||
* @param IUserManager $userMgr
|
|
||||||
* @param IGroupManager $groupManager
|
|
||||||
* @param IAvatarManager $avatarManager
|
|
||||||
* @param ILogger $logger
|
|
||||||
* @param IL10N $trans
|
|
||||||
* @param IFactory $transFactory
|
|
||||||
* @param IURLGenerator $urlGenerator
|
|
||||||
* @param string $userId
|
|
||||||
* @param EventMapper $eventMapper
|
|
||||||
*
|
|
||||||
* @param QuestionMapper $questionMapper
|
|
||||||
* @param AnswerMapper $answerMapper
|
|
||||||
*
|
|
||||||
* @param NotificationMapper $notificationMapper
|
|
||||||
* @param VoteMapper $VoteMapper
|
|
||||||
* @param IMailer $mailer
|
|
||||||
*/
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$appName,
|
|
||||||
IRequest $request,
|
IRequest $request,
|
||||||
IConfig $config,
|
|
||||||
IUserManager $userMgr,
|
IUserManager $userMgr,
|
||||||
IGroupManager $groupManager,
|
IGroupManager $groupManager,
|
||||||
IAvatarManager $avatarManager,
|
|
||||||
ILogger $logger,
|
|
||||||
IL10N $trans,
|
|
||||||
IFactory $transFactory,
|
|
||||||
IURLGenerator $urlGenerator,
|
IURLGenerator $urlGenerator,
|
||||||
$userId,
|
$userId,
|
||||||
EventMapper $eventMapper,
|
EventMapper $eventMapper,
|
||||||
|
|
||||||
QuestionMapper $questionMapper,
|
QuestionMapper $questionMapper,
|
||||||
AnswerMapper $answerMapper,
|
AnswerMapper $answerMapper,
|
||||||
|
VoteMapper $VoteMapper
|
||||||
NotificationMapper $notificationMapper,
|
|
||||||
VoteMapper $VoteMapper,
|
|
||||||
IMailer $mailer
|
|
||||||
) {
|
) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct(Application::APP_ID, $request);
|
||||||
$this->request = $request;
|
|
||||||
$this->config = $config;
|
|
||||||
$this->userMgr = $userMgr;
|
$this->userMgr = $userMgr;
|
||||||
$this->groupManager = $groupManager;
|
$this->groupManager = $groupManager;
|
||||||
$this->avatarManager = $avatarManager;
|
|
||||||
$this->logger = $logger;
|
|
||||||
$this->trans = $trans;
|
|
||||||
$this->transFactory = $transFactory;
|
|
||||||
$this->urlGenerator = $urlGenerator;
|
$this->urlGenerator = $urlGenerator;
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->eventMapper = $eventMapper;
|
$this->eventMapper = $eventMapper;
|
||||||
|
|
||||||
$this->questionMapper = $questionMapper;
|
$this->questionMapper = $questionMapper;
|
||||||
$this->answerMapper = $answerMapper;
|
$this->answerMapper = $answerMapper;
|
||||||
|
|
||||||
$this->notificationMapper = $notificationMapper;
|
|
||||||
$this->voteMapper = $VoteMapper;
|
$this->voteMapper = $VoteMapper;
|
||||||
$this->mailer = $mailer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -181,71 +122,6 @@ class PageController extends Controller {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $formId
|
|
||||||
* @param string $from
|
|
||||||
*/
|
|
||||||
private function sendNotifications($formId, $from) {
|
|
||||||
$form = $this->eventMapper->find($formId);
|
|
||||||
$notifications = $this->notificationMapper->findAllByForm($formId);
|
|
||||||
foreach ($notifications as $notification) {
|
|
||||||
if ($from === $notification->getUserId()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$recUser = $this->userMgr->get($notification->getUserId());
|
|
||||||
if (!$recUser instanceof IUser) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email');
|
|
||||||
if ($email === null || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$url = $this->urlGenerator->getAbsoluteURL(
|
|
||||||
$this->urlGenerator->linkToRoute('forms.page.goto_form',
|
|
||||||
array('hash' => $form->getHash()))
|
|
||||||
);
|
|
||||||
|
|
||||||
$sendUser = $this->userMgr->get($from);
|
|
||||||
$sender = $from;
|
|
||||||
if ($sendUser instanceof IUser) {
|
|
||||||
$sender = $sendUser->getDisplayName();
|
|
||||||
}
|
|
||||||
|
|
||||||
$lang = $this->config->getUserValue($notification->getUserId(), 'core', 'lang');
|
|
||||||
$trans = $this->transFactory->get('forms', $lang);
|
|
||||||
$emailTemplate = $this->mailer->createEMailTemplate('forms.Notification', [
|
|
||||||
'user' => $sender,
|
|
||||||
'title' => $form->getTitle(),
|
|
||||||
'link' => $url,
|
|
||||||
]);
|
|
||||||
$emailTemplate->setSubject($trans->t('Forms App - New Activity'));
|
|
||||||
$emailTemplate->addHeader();
|
|
||||||
$emailTemplate->addHeading($trans->t('Forms App - New Activity'), false);
|
|
||||||
|
|
||||||
$emailTemplate->addBodyText(str_replace(
|
|
||||||
['{user}', '{title}'],
|
|
||||||
[$sender, $form->getTitle()],
|
|
||||||
$trans->t('{user} participated in the form "{title}"')
|
|
||||||
));
|
|
||||||
|
|
||||||
$emailTemplate->addBodyButton(
|
|
||||||
htmlspecialchars($trans->t('Go to form')),
|
|
||||||
$url,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
$emailTemplate->addFooter();
|
|
||||||
try {
|
|
||||||
$message = $this->mailer->createMessage();
|
|
||||||
$message->setTo([$email => $recUser->getDisplayName()]);
|
|
||||||
$message->useTemplate($emailTemplate);
|
|
||||||
$this->mailer->send($message);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->logger->logException($e, ['app' => 'forms']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
* @NoCSRFRequired
|
* @NoCSRFRequired
|
||||||
|
@ -270,27 +146,11 @@ class PageController extends Controller {
|
||||||
return new TemplateResponse('forms', 'expired.tmpl');
|
return new TemplateResponse('forms', 'expired.tmpl');
|
||||||
}
|
}
|
||||||
|
|
||||||
$votes = $this->voteMapper->findByForm($form->getId());
|
|
||||||
$participants = $this->voteMapper->findParticipantsByForm($form->getId());
|
|
||||||
|
|
||||||
try {
|
|
||||||
$notification = $this->notificationMapper->findByUserAndForm($form->getId(), $this->userId);
|
|
||||||
} catch (DoesNotExistException $e) {
|
|
||||||
$notification = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->hasUserAccess($form)) {
|
if ($this->hasUserAccess($form)) {
|
||||||
$renderAs = $this->userId !== null ? 'user' : 'public';
|
$renderAs = $this->userId !== null ? 'user' : 'public';
|
||||||
$res = new TemplateResponse('forms', 'vote.tmpl', [
|
$res = new TemplateResponse('forms', 'vote.tmpl', [
|
||||||
'form' => $form,
|
'form' => $form,
|
||||||
'questions' => $this->getQuestions($form->getId()),
|
'questions' => $this->getQuestions($form->getId()),
|
||||||
'votes' => $votes,
|
|
||||||
'participant' => $participants,
|
|
||||||
'notification' => $notification,
|
|
||||||
'userId' => $this->userId,
|
|
||||||
'userMgr' => $this->userMgr,
|
|
||||||
'urlGenerator' => $this->urlGenerator,
|
|
||||||
'avatarManager' => $this->avatarManager
|
|
||||||
], $renderAs);
|
], $renderAs);
|
||||||
$csp = new ContentSecurityPolicy();
|
$csp = new ContentSecurityPolicy();
|
||||||
$csp->allowEvalScript(true);
|
$csp->allowEvalScript(true);
|
||||||
|
|
|
@ -21,18 +21,13 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use OCP\User; //To do: replace according to API
|
|
||||||
use OCP\Util;
|
use OCP\Util;
|
||||||
use OCP\Template;
|
|
||||||
|
|
||||||
Util::addStyle('forms', 'vote');
|
Util::addStyle('forms', 'vote');
|
||||||
|
|
||||||
Util::addScript('forms', 'vote');
|
Util::addScript('forms', 'vote');
|
||||||
Util::addScript('forms', 'survey.jquery.min');
|
Util::addScript('forms', 'survey.jquery.min');
|
||||||
|
|
||||||
$userId = $_['userId'];
|
|
||||||
/** @var \OCP\IUserManager $userMgr */
|
|
||||||
$userMgr = $_['userMgr'];
|
|
||||||
/** @var \OCA\Forms\Db\Event $form */
|
/** @var \OCA\Forms\Db\Event $form */
|
||||||
$form = $_['form'];
|
$form = $_['form'];
|
||||||
/** @var OCA\Forms\Db\Question[] $questions */
|
/** @var OCA\Forms\Db\Question[] $questions */
|
||||||
|
@ -48,4 +43,4 @@
|
||||||
<div id="surveyContainer"
|
<div id="surveyContainer"
|
||||||
form="<?php echo htmlentities(json_encode($form->read()))?>"
|
form="<?php echo htmlentities(json_encode($form->read()))?>"
|
||||||
questions="<?php echo htmlentities(json_encode($questions))?>"
|
questions="<?php echo htmlentities(json_encode($questions))?>"
|
||||||
></div>
|
></div>
|
||||||
|
|
Loading…
Reference in a new issue