2019-05-14 01:15:45 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
|
|
|
*
|
2020-04-29 11:42:39 +02:00
|
|
|
* @author affan98 <affan98@gmail.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2020-04-13 13:10:52 +02:00
|
|
|
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
|
2020-04-29 11:42:39 +02:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-05-14 01:15:45 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2020-04-29 11:42:39 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2019-05-14 01:15:45 +02:00
|
|
|
*
|
2020-04-29 11:42:39 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2019-05-14 01:15:45 +02:00
|
|
|
*
|
2020-04-29 11:42:39 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-05-14 01:15:45 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Forms\Controller;
|
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
use OCA\Forms\Db\Form;
|
|
|
|
use OCA\Forms\Db\FormMapper;
|
2020-04-28 21:14:40 +02:00
|
|
|
use OCA\Forms\Service\FormsService;
|
2019-05-14 01:15:45 +02:00
|
|
|
|
2020-04-29 19:06:37 +02:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2020-04-29 19:06:37 +02:00
|
|
|
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\IGroupManager;
|
2020-04-28 21:14:40 +02:00
|
|
|
use OCP\IInitialStateService;
|
2020-04-29 19:06:37 +02:00
|
|
|
use OCP\IL10N;
|
2020-05-27 13:52:59 +02:00
|
|
|
use OCP\ILogger;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCP\IRequest;
|
2020-04-29 19:06:37 +02:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2020-04-28 09:47:25 +02:00
|
|
|
use OCP\IUserSession;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCP\Util;
|
|
|
|
|
|
|
|
class PageController extends Controller {
|
2020-05-27 14:08:04 +02:00
|
|
|
private const TEMPLATE_EXPIRED = 'expired';
|
|
|
|
private const TEMPLATE_MAIN = 'main';
|
|
|
|
private const TEMPLATE_NOSUBMIT = 'nosubmit';
|
|
|
|
private const TEMPLATE_NOTFOUND = 'notfound';
|
|
|
|
|
2020-04-28 09:04:53 +02:00
|
|
|
protected $appName;
|
2020-04-28 09:47:25 +02:00
|
|
|
|
|
|
|
/** @var FormMapper */
|
2020-03-30 13:48:14 +02:00
|
|
|
private $formMapper;
|
2020-04-29 19:06:37 +02:00
|
|
|
|
|
|
|
/** @var FormsService */
|
|
|
|
private $formsService;
|
2020-04-28 09:47:25 +02:00
|
|
|
|
2020-04-29 19:06:37 +02:00
|
|
|
/** @var IAccountManager */
|
|
|
|
protected $accountManager;
|
2020-04-28 09:47:25 +02:00
|
|
|
|
|
|
|
/** @var IGroupManager */
|
2019-05-14 01:15:45 +02:00
|
|
|
private $groupManager;
|
2020-04-28 09:47:25 +02:00
|
|
|
|
2020-04-28 10:50:44 +02:00
|
|
|
/** @var IInitialStateService */
|
|
|
|
private $initialStateService;
|
2020-04-09 18:21:59 +02:00
|
|
|
|
2020-04-29 19:06:37 +02:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2020-05-27 13:52:59 +02:00
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2020-04-29 19:06:37 +02:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
2020-05-27 13:52:59 +02:00
|
|
|
|
2020-05-03 13:07:29 +02:00
|
|
|
/** @var Array
|
|
|
|
*
|
|
|
|
* Maximum String lengths, the database is set to store.
|
|
|
|
*/
|
|
|
|
private $maxStringLengths = [
|
|
|
|
'formTitle' => 256,
|
2020-07-29 23:42:41 +02:00
|
|
|
'formDescription' => 8192,
|
2020-05-03 13:07:29 +02:00
|
|
|
'questionText' => 2048,
|
|
|
|
'optionText' => 1024,
|
2020-07-29 23:42:41 +02:00
|
|
|
'answerText' => 4096,
|
2020-05-03 13:07:29 +02:00
|
|
|
];
|
|
|
|
|
2020-04-28 09:04:53 +02:00
|
|
|
public function __construct(string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
FormMapper $formMapper,
|
2020-04-29 19:06:37 +02:00
|
|
|
FormsService $formsService,
|
|
|
|
IAccountManager $accountManager,
|
|
|
|
IGroupManager $groupManager,
|
2020-04-28 21:14:40 +02:00
|
|
|
IInitialStateService $initialStateService,
|
2020-04-29 19:06:37 +02:00
|
|
|
IL10N $l10n,
|
2020-05-27 13:52:59 +02:00
|
|
|
ILogger $logger,
|
2020-04-29 19:06:37 +02:00
|
|
|
IUserManager $userManager,
|
|
|
|
IUserSession $userSession) {
|
2020-04-28 09:47:25 +02:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2020-04-28 09:04:53 +02:00
|
|
|
$this->appName = $appName;
|
2020-04-29 19:06:37 +02:00
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
$this->formMapper = $formMapper;
|
2020-04-28 21:14:40 +02:00
|
|
|
$this->formsService = $formsService;
|
2020-04-29 19:06:37 +02:00
|
|
|
|
|
|
|
$this->accountManager = $accountManager;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->initialStateService = $initialStateService;
|
|
|
|
$this->l10n = $l10n;
|
2020-05-27 13:52:59 +02:00
|
|
|
$this->logger = $logger;
|
2020-04-29 19:06:37 +02:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->userSession = $userSession;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-20 12:22:07 +01:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function index(): TemplateResponse {
|
2020-07-12 10:39:06 +02:00
|
|
|
Util::addScript($this->appName, 'forms-main');
|
2020-03-31 18:20:55 +02:00
|
|
|
Util::addStyle($this->appName, 'forms');
|
2020-05-10 13:25:57 +02:00
|
|
|
$this->initialStateService->provideInitialState($this->appName, 'maxStringLengths', $this->maxStringLengths);
|
2020-05-27 14:08:04 +02:00
|
|
|
return new TemplateResponse($this->appName, self::TEMPLATE_MAIN);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
* @param string $hash
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function gotoForm($hash): ?TemplateResponse {
|
2020-04-28 09:04:53 +02:00
|
|
|
// Inject style on all templates
|
|
|
|
Util::addStyle($this->appName, 'forms');
|
|
|
|
|
2019-05-14 01:15:45 +02:00
|
|
|
try {
|
2020-03-30 13:48:14 +02:00
|
|
|
$form = $this->formMapper->findByHash($hash);
|
2019-05-14 01:15:45 +02:00
|
|
|
} catch (DoesNotExistException $e) {
|
2020-05-27 14:08:04 +02:00
|
|
|
return $this->provideTemplate(self::TEMPLATE_NOTFOUND);
|
2020-05-04 19:06:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 14:21:05 +02:00
|
|
|
// Does the user have access to form
|
2020-05-04 19:06:53 +02:00
|
|
|
if (!$this->formsService->hasUserAccess($form->getId())) {
|
2020-05-27 14:08:04 +02:00
|
|
|
return $this->provideTemplate(self::TEMPLATE_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the user have permissions to submit (resp. submitOnce)
|
|
|
|
if (!$this->formsService->canSubmit($form->getId())) {
|
|
|
|
return $this->provideTemplate(self::TEMPLATE_NOSUBMIT, $form);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:04:53 +02:00
|
|
|
// Has form expired
|
|
|
|
if ($form->getExpires() !== 0 && time() > $form->getExpires()) {
|
2020-05-27 14:08:04 +02:00
|
|
|
return $this->provideTemplate(self::TEMPLATE_EXPIRED, $form);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
2020-03-19 21:44:06 +01:00
|
|
|
|
2020-05-27 14:08:04 +02:00
|
|
|
// Main Template to fill the form
|
2020-07-12 10:39:06 +02:00
|
|
|
Util::addScript($this->appName, 'forms-submit');
|
2020-05-04 19:06:53 +02:00
|
|
|
$this->initialStateService->provideInitialState($this->appName, 'form', $this->formsService->getPublicForm($form->getId()));
|
2020-05-03 13:07:29 +02:00
|
|
|
$this->initialStateService->provideInitialState($this->appName, 'maxStringLengths', $this->maxStringLengths);
|
2020-05-27 14:08:04 +02:00
|
|
|
return $this->provideTemplate(self::TEMPLATE_MAIN, $form);
|
|
|
|
}
|
2020-04-29 19:06:37 +02:00
|
|
|
|
2020-05-27 14:08:04 +02:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
* @param string $template
|
|
|
|
* @param Form $form Necessary to set header on public forms, not necessary for 'notfound'-template
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
|
|
|
public function provideTemplate(string $template, Form $form = null): ?TemplateResponse {
|
|
|
|
// If not logged in, use PublicTemplate
|
2020-04-29 19:06:37 +02:00
|
|
|
if (!$this->userSession->isLoggedIn()) {
|
|
|
|
Util::addStyle($this->appName, 'public');
|
2020-05-27 14:08:04 +02:00
|
|
|
$response = new PublicTemplateResponse($this->appName, $template);
|
|
|
|
|
|
|
|
// Set Header
|
|
|
|
$response->setHeaderTitle($this->l10n->t('Forms'));
|
|
|
|
if ($template !== self::TEMPLATE_NOTFOUND) {
|
|
|
|
$response->setHeaderTitle($form->getTitle());
|
2020-04-29 19:06:37 +02:00
|
|
|
|
2020-05-27 14:08:04 +02:00
|
|
|
// Get owner and check display name privacy settings
|
|
|
|
$owner = $this->userManager->get($form->getOwnerId());
|
|
|
|
if ($owner instanceof IUser) {
|
|
|
|
$ownerAccount = $this->accountManager->getAccount($owner);
|
2020-04-29 19:06:37 +02:00
|
|
|
|
2020-05-27 14:08:04 +02:00
|
|
|
$ownerName = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
|
|
|
|
if ($ownerName->getScope() === IAccountManager::VISIBILITY_PUBLIC) {
|
|
|
|
$response->setHeaderDetails($this->l10n->t('Shared by %s', [$ownerName->getValue()]));
|
|
|
|
}
|
2020-04-29 19:06:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:08:04 +02:00
|
|
|
return new TemplateResponse($this->appName, $template);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
}
|