forked from wallabag/wallabag
Merge branch 'master' into feat_referer_to_session_redirect
This commit is contained in:
@ -4,13 +4,13 @@ namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Craue\ConfigBundle\Util\Config;
|
||||
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use FOS\UserBundle\Model\UserManagerInterface;
|
||||
use JMS\Serializer\SerializationContext;
|
||||
use JMS\Serializer\SerializerBuilder;
|
||||
use PragmaRX\Recovery\Recovery as BackupCodes;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@ -20,7 +20,7 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Wallabag\AnnotationBundle\Entity\Annotation;
|
||||
use Wallabag\AnnotationBundle\Repository\AnnotationRepository;
|
||||
use Wallabag\CoreBundle\Entity\Config as ConfigEntity;
|
||||
use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule;
|
||||
use Wallabag\CoreBundle\Entity\RuleInterface;
|
||||
@ -32,21 +32,39 @@ use Wallabag\CoreBundle\Form\Type\IgnoreOriginUserRuleType;
|
||||
use Wallabag\CoreBundle\Form\Type\TaggingRuleImportType;
|
||||
use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
|
||||
use Wallabag\CoreBundle\Form\Type\UserInformationType;
|
||||
use Wallabag\CoreBundle\Repository\ConfigRepository;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\CoreBundle\Repository\IgnoreOriginUserRuleRepository;
|
||||
use Wallabag\CoreBundle\Repository\TaggingRuleRepository;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
use Wallabag\CoreBundle\Tools\Utils;
|
||||
use Wallabag\UserBundle\Repository\UserRepository;
|
||||
|
||||
class ConfigController extends Controller
|
||||
class ConfigController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private UserManagerInterface $userManager;
|
||||
private EntryRepository $entryRepository;
|
||||
private TagRepository $tagRepository;
|
||||
private AnnotationRepository $annotationRepository;
|
||||
private ConfigRepository $configRepository;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, UserManagerInterface $userManager, EntryRepository $entryRepository, TagRepository $tagRepository, AnnotationRepository $annotationRepository, ConfigRepository $configRepository)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->userManager = $userManager;
|
||||
$this->entryRepository = $entryRepository;
|
||||
$this->tagRepository = $tagRepository;
|
||||
$this->annotationRepository = $annotationRepository;
|
||||
$this->configRepository = $configRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/config", name="config")
|
||||
*/
|
||||
public function indexAction(Request $request)
|
||||
public function indexAction(Request $request, Config $craueConfig, TaggingRuleRepository $taggingRuleRepository, IgnoreOriginUserRuleRepository $ignoreOriginUserRuleRepository, UserRepository $userRepository)
|
||||
{
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$config = $this->getConfig();
|
||||
$userManager = $this->container->get(UserManagerInterface::class);
|
||||
$user = $this->getUser();
|
||||
|
||||
// handle basic config detail (this form is defined as a service)
|
||||
@ -54,8 +72,8 @@ class ConfigController extends Controller
|
||||
$configForm->handleRequest($request);
|
||||
|
||||
if ($configForm->isSubmitted() && $configForm->isValid()) {
|
||||
$em->persist($config);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($config);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$request->getSession()->set('_locale', $config->getLanguage());
|
||||
|
||||
@ -72,13 +90,13 @@ class ConfigController extends Controller
|
||||
$pwdForm->handleRequest($request);
|
||||
|
||||
if ($pwdForm->isSubmitted() && $pwdForm->isValid()) {
|
||||
if ($this->get(Config::class)->get('demo_mode_enabled') && $this->get(Config::class)->get('demo_mode_username') === $user->getUsername()) {
|
||||
if ($craueConfig->get('demo_mode_enabled') && $craueConfig->get('demo_mode_username') === $user->getUsername()) {
|
||||
$message = 'flashes.config.notice.password_not_updated_demo';
|
||||
} else {
|
||||
$message = 'flashes.config.notice.password_updated';
|
||||
|
||||
$user->setPlainPassword($pwdForm->get('new_password')->getData());
|
||||
$userManager->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
}
|
||||
|
||||
$this->addFlash('notice', $message);
|
||||
@ -94,7 +112,7 @@ class ConfigController extends Controller
|
||||
$userForm->handleRequest($request);
|
||||
|
||||
if ($userForm->isSubmitted() && $userForm->isValid()) {
|
||||
$userManager->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -109,8 +127,8 @@ class ConfigController extends Controller
|
||||
$feedForm->handleRequest($request);
|
||||
|
||||
if ($feedForm->isSubmitted() && $feedForm->isValid()) {
|
||||
$em->persist($config);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($config);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -125,9 +143,7 @@ class ConfigController extends Controller
|
||||
$action = $this->generateUrl('config') . '#set5';
|
||||
|
||||
if ($request->query->has('tagging-rule')) {
|
||||
$taggingRule = $this->get('doctrine')
|
||||
->getRepository(TaggingRule::class)
|
||||
->find($request->query->get('tagging-rule'));
|
||||
$taggingRule = $taggingRuleRepository->find($request->query->get('tagging-rule'));
|
||||
|
||||
if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
|
||||
return $this->redirect($action);
|
||||
@ -141,8 +157,8 @@ class ConfigController extends Controller
|
||||
|
||||
if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) {
|
||||
$taggingRule->setConfig($config);
|
||||
$em->persist($taggingRule);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($taggingRule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -169,10 +185,10 @@ class ConfigController extends Controller
|
||||
$taggingRule->setRule($rule['rule']);
|
||||
$taggingRule->setTags($rule['tags']);
|
||||
$taggingRule->setConfig($config);
|
||||
$em->persist($taggingRule);
|
||||
$this->entityManager->persist($taggingRule);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
$message = 'flashes.config.notice.tagging_rules_imported';
|
||||
}
|
||||
@ -188,8 +204,7 @@ class ConfigController extends Controller
|
||||
$action = $this->generateUrl('config') . '#set6';
|
||||
|
||||
if ($request->query->has('ignore-origin-user-rule')) {
|
||||
$ignoreOriginUserRule = $this->get('doctrine')
|
||||
->getRepository(IgnoreOriginUserRule::class)
|
||||
$ignoreOriginUserRule = $ignoreOriginUserRuleRepository
|
||||
->find($request->query->get('ignore-origin-user-rule'));
|
||||
|
||||
if ($this->getUser()->getId() !== $ignoreOriginUserRule->getConfig()->getUser()->getId()) {
|
||||
@ -206,8 +221,8 @@ class ConfigController extends Controller
|
||||
|
||||
if ($newIgnoreOriginUserRule->isSubmitted() && $newIgnoreOriginUserRule->isValid()) {
|
||||
$ignoreOriginUserRule->setConfig($config);
|
||||
$em->persist($ignoreOriginUserRule);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($ignoreOriginUserRule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -231,9 +246,8 @@ class ConfigController extends Controller
|
||||
'username' => $user->getUsername(),
|
||||
'token' => $config->getFeedToken(),
|
||||
],
|
||||
'twofactor_auth' => $this->getParameter('twofactor_auth'),
|
||||
'wallabag_url' => $this->getParameter('domain_name'),
|
||||
'enabled_users' => $this->get(UserRepository::class)->getSumEnabledUsers(),
|
||||
'enabled_users' => $userRepository->getSumEnabledUsers(),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -244,14 +258,10 @@ class ConfigController extends Controller
|
||||
*/
|
||||
public function disableOtpEmailAction()
|
||||
{
|
||||
if (!$this->getParameter('twofactor_auth')) {
|
||||
return $this->createNotFoundException('two_factor not enabled');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
$user->setEmailTwoFactor(false);
|
||||
|
||||
$this->container->get(UserManagerInterface::class)->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -268,17 +278,13 @@ class ConfigController extends Controller
|
||||
*/
|
||||
public function otpEmailAction()
|
||||
{
|
||||
if (!$this->getParameter('twofactor_auth')) {
|
||||
return $this->createNotFoundException('two_factor not enabled');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
|
||||
$user->setGoogleAuthenticatorSecret(null);
|
||||
$user->setBackupCodes(null);
|
||||
$user->setEmailTwoFactor(true);
|
||||
|
||||
$this->container->get(UserManagerInterface::class)->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -295,16 +301,12 @@ class ConfigController extends Controller
|
||||
*/
|
||||
public function disableOtpAppAction()
|
||||
{
|
||||
if (!$this->getParameter('twofactor_auth')) {
|
||||
return $this->createNotFoundException('two_factor not enabled');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
|
||||
$user->setGoogleAuthenticatorSecret('');
|
||||
$user->setBackupCodes(null);
|
||||
|
||||
$this->container->get(UserManagerInterface::class)->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -319,14 +321,10 @@ class ConfigController extends Controller
|
||||
*
|
||||
* @Route("/config/otp/app", name="config_otp_app")
|
||||
*/
|
||||
public function otpAppAction()
|
||||
public function otpAppAction(GoogleAuthenticatorInterface $googleAuthenticator)
|
||||
{
|
||||
if (!$this->getParameter('twofactor_auth')) {
|
||||
return $this->createNotFoundException('two_factor not enabled');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
$secret = $this->get(GoogleAuthenticatorInterface::class)->generateSecret();
|
||||
$secret = $googleAuthenticator->generateSecret();
|
||||
|
||||
$user->setGoogleAuthenticatorSecret($secret);
|
||||
$user->setEmailTwoFactor(false);
|
||||
@ -341,7 +339,7 @@ class ConfigController extends Controller
|
||||
|
||||
$user->setBackupCodes($backupCodesHashed);
|
||||
|
||||
$this->container->get(UserManagerInterface::class)->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -350,7 +348,7 @@ class ConfigController extends Controller
|
||||
|
||||
return $this->render('@WallabagCore/Config/otp_app.html.twig', [
|
||||
'backupCodes' => $backupCodes,
|
||||
'qr_code' => $this->get(GoogleAuthenticatorInterface::class)->getQRContent($user),
|
||||
'qr_code' => $googleAuthenticator->getQRContent($user),
|
||||
'secret' => $secret,
|
||||
]);
|
||||
}
|
||||
@ -362,15 +360,11 @@ class ConfigController extends Controller
|
||||
*/
|
||||
public function otpAppCancelAction()
|
||||
{
|
||||
if (!$this->getParameter('twofactor_auth')) {
|
||||
return $this->createNotFoundException('two_factor not enabled');
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
$user->setGoogleAuthenticatorSecret(null);
|
||||
$user->setBackupCodes(null);
|
||||
|
||||
$this->container->get(UserManagerInterface::class)->updateUser($user, true);
|
||||
$this->userManager->updateUser($user, true);
|
||||
|
||||
return $this->redirect($this->generateUrl('config') . '#set3');
|
||||
}
|
||||
@ -380,9 +374,9 @@ class ConfigController extends Controller
|
||||
*
|
||||
* @Route("/config/otp/app/check", name="config_otp_app_check")
|
||||
*/
|
||||
public function otpAppCheckAction(Request $request)
|
||||
public function otpAppCheckAction(Request $request, GoogleAuthenticatorInterface $googleAuthenticator)
|
||||
{
|
||||
$isValid = $this->get(GoogleAuthenticatorInterface::class)->checkCode(
|
||||
$isValid = $googleAuthenticator->checkCode(
|
||||
$this->getUser(),
|
||||
$request->get('_auth_code')
|
||||
);
|
||||
@ -414,9 +408,8 @@ class ConfigController extends Controller
|
||||
$config = $this->getConfig();
|
||||
$config->setFeedToken(Utils::generateToken());
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($config);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($config);
|
||||
$this->entityManager->flush();
|
||||
|
||||
if ($request->isXmlHttpRequest()) {
|
||||
return new JsonResponse(['token' => $config->getFeedToken()]);
|
||||
@ -440,9 +433,8 @@ class ConfigController extends Controller
|
||||
$config = $this->getConfig();
|
||||
$config->setFeedToken(null);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($config);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($config);
|
||||
$this->entityManager->flush();
|
||||
|
||||
if ($request->isXmlHttpRequest()) {
|
||||
return new JsonResponse();
|
||||
@ -467,9 +459,8 @@ class ConfigController extends Controller
|
||||
{
|
||||
$this->validateRuleAction($rule);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($rule);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($rule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -504,9 +495,8 @@ class ConfigController extends Controller
|
||||
{
|
||||
$this->validateRuleAction($rule);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($rule);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($rule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@ -537,13 +527,11 @@ class ConfigController extends Controller
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function resetAction($type)
|
||||
public function resetAction(string $type, AnnotationRepository $annotationRepository, EntryRepository $entryRepository)
|
||||
{
|
||||
switch ($type) {
|
||||
case 'annotations':
|
||||
$this->get('doctrine')
|
||||
->getRepository(Annotation::class)
|
||||
->removeAllByUserId($this->getUser()->getId());
|
||||
$annotationRepository->removeAllByUserId($this->getUser()->getId());
|
||||
break;
|
||||
case 'tags':
|
||||
$this->removeAllTagsByUserId($this->getUser()->getId());
|
||||
@ -551,24 +539,24 @@ class ConfigController extends Controller
|
||||
case 'entries':
|
||||
// SQLite doesn't care about cascading remove, so we need to manually remove associated stuff
|
||||
// otherwise they won't be removed ...
|
||||
if ($this->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
|
||||
$this->get('doctrine')->getRepository(Annotation::class)->removeAllByUserId($this->getUser()->getId());
|
||||
if ($this->entityManager->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
|
||||
$annotationRepository->removeAllByUserId($this->getUser()->getId());
|
||||
}
|
||||
|
||||
// manually remove tags to avoid orphan tag
|
||||
$this->removeAllTagsByUserId($this->getUser()->getId());
|
||||
|
||||
$this->get(EntryRepository::class)->removeAllByUserId($this->getUser()->getId());
|
||||
$entryRepository->removeAllByUserId($this->getUser()->getId());
|
||||
break;
|
||||
case 'archived':
|
||||
if ($this->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
|
||||
if ($this->entityManager->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
|
||||
$this->removeAnnotationsForArchivedByUserId($this->getUser()->getId());
|
||||
}
|
||||
|
||||
// manually remove tags to avoid orphan tag
|
||||
$this->removeTagsForArchivedByUserId($this->getUser()->getId());
|
||||
|
||||
$this->get(EntryRepository::class)->removeArchivedByUserId($this->getUser()->getId());
|
||||
$entryRepository->removeArchivedByUserId($this->getUser()->getId());
|
||||
break;
|
||||
}
|
||||
|
||||
@ -583,16 +571,19 @@ class ConfigController extends Controller
|
||||
/**
|
||||
* Delete account for current user.
|
||||
*
|
||||
* @Route("/account/delete", name="delete_account")
|
||||
* @Route("/account/delete", name="delete_account", methods={"POST"})
|
||||
*
|
||||
* @throws AccessDeniedHttpException
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function deleteAccountAction(Request $request)
|
||||
public function deleteAccountAction(Request $request, UserRepository $userRepository, TokenStorageInterface $tokenStorage)
|
||||
{
|
||||
$enabledUsers = $this->get(UserRepository::class)
|
||||
->getSumEnabledUsers();
|
||||
if (!$this->isCsrfTokenValid('delete-account', $request->request->get('token'))) {
|
||||
throw $this->createAccessDeniedException('Bad CSRF token.');
|
||||
}
|
||||
|
||||
$enabledUsers = $userRepository->getSumEnabledUsers();
|
||||
|
||||
if ($enabledUsers <= 1) {
|
||||
throw new AccessDeniedHttpException();
|
||||
@ -601,11 +592,10 @@ class ConfigController extends Controller
|
||||
$user = $this->getUser();
|
||||
|
||||
// logout current user
|
||||
$this->get(TokenStorageInterface::class)->setToken(null);
|
||||
$tokenStorage->setToken(null);
|
||||
$request->getSession()->invalidate();
|
||||
|
||||
$em = $this->get(UserManagerInterface::class);
|
||||
$em->deleteUser($user);
|
||||
$this->userManager->deleteUser($user);
|
||||
|
||||
return $this->redirect($this->generateUrl('fos_user_security_login'));
|
||||
}
|
||||
@ -622,9 +612,8 @@ class ConfigController extends Controller
|
||||
$user = $this->getUser();
|
||||
$user->getConfig()->setListMode(!$user->getConfig()->getListMode());
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($user);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirect($request->getSession()->get('prevUrl'));
|
||||
}
|
||||
@ -638,9 +627,9 @@ class ConfigController extends Controller
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function setLocaleAction(Request $request, $language = null)
|
||||
public function setLocaleAction(Request $request, ValidatorInterface $validator, $language = null)
|
||||
{
|
||||
$errors = $this->get(ValidatorInterface::class)->validate($language, (new LocaleConstraint()));
|
||||
$errors = $validator->validate($language, (new LocaleConstraint()));
|
||||
|
||||
if (0 === \count($errors)) {
|
||||
$request->getSession()->set('_locale', $language);
|
||||
@ -687,19 +676,16 @@ class ConfigController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
$this->get(EntryRepository::class)
|
||||
->removeTags($userId, $tags);
|
||||
$this->entryRepository->removeTags($userId, $tags);
|
||||
|
||||
// cleanup orphan tags
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (0 === \count($tag->getEntries())) {
|
||||
$em->remove($tag);
|
||||
$this->entityManager->remove($tag);
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -709,7 +695,7 @@ class ConfigController extends Controller
|
||||
*/
|
||||
private function removeAllTagsByUserId($userId)
|
||||
{
|
||||
$tags = $this->get(TagRepository::class)->findAllTags($userId);
|
||||
$tags = $this->tagRepository->findAllTags($userId);
|
||||
$this->removeAllTagsByStatusAndUserId($tags, $userId);
|
||||
}
|
||||
|
||||
@ -720,23 +706,20 @@ class ConfigController extends Controller
|
||||
*/
|
||||
private function removeTagsForArchivedByUserId($userId)
|
||||
{
|
||||
$tags = $this->get(TagRepository::class)->findForArchivedArticlesByUser($userId);
|
||||
$tags = $this->tagRepository->findForArchivedArticlesByUser($userId);
|
||||
$this->removeAllTagsByStatusAndUserId($tags, $userId);
|
||||
}
|
||||
|
||||
private function removeAnnotationsForArchivedByUserId($userId)
|
||||
{
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
|
||||
$archivedEntriesAnnotations = $this->get('doctrine')
|
||||
->getRepository(Annotation::class)
|
||||
$archivedEntriesAnnotations = $this->annotationRepository
|
||||
->findAllArchivedEntriesByUser($userId);
|
||||
|
||||
foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) {
|
||||
$em->remove($archivedEntriesAnnotation);
|
||||
$this->entityManager->remove($archivedEntriesAnnotation);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -757,9 +740,7 @@ class ConfigController extends Controller
|
||||
*/
|
||||
private function getConfig()
|
||||
{
|
||||
$config = $this->get('doctrine')
|
||||
->getRepository(ConfigEntity::class)
|
||||
->findOneByUser($this->getUser());
|
||||
$config = $this->configRepository->findOneByUser($this->getUser());
|
||||
|
||||
// should NEVER HAPPEN ...
|
||||
if (!$config) {
|
||||
|
||||
@ -3,20 +3,20 @@
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Craue\ConfigBundle\Util\Config;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\NoResultException;
|
||||
use Lexik\Bundle\FormFilterBundle\Filter\FilterBuilderUpdaterInterface;
|
||||
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
||||
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\Tag;
|
||||
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
|
||||
@ -31,16 +31,34 @@ use Wallabag\CoreBundle\Helper\Redirect;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
|
||||
class EntryController extends Controller
|
||||
class EntryController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private EventDispatcherInterface $eventDispatcher;
|
||||
private EntryRepository $entryRepository;
|
||||
private Redirect $redirectHelper;
|
||||
private PreparePagerForEntries $preparePagerForEntriesHelper;
|
||||
private FilterBuilderUpdaterInterface $filterBuilderUpdater;
|
||||
private ContentProxy $contentProxy;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher, EntryRepository $entryRepository, Redirect $redirectHelper, PreparePagerForEntries $preparePagerForEntriesHelper, FilterBuilderUpdaterInterface $filterBuilderUpdater, ContentProxy $contentProxy)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
$this->entryRepository = $entryRepository;
|
||||
$this->redirectHelper = $redirectHelper;
|
||||
$this->preparePagerForEntriesHelper = $preparePagerForEntriesHelper;
|
||||
$this->filterBuilderUpdater = $filterBuilderUpdater;
|
||||
$this->contentProxy = $contentProxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/mass", name="mass_action")
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function massAction(Request $request)
|
||||
public function massAction(Request $request, TagRepository $tagRepository)
|
||||
{
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$values = $request->request->all();
|
||||
|
||||
$tagsToAdd = [];
|
||||
@ -67,7 +85,7 @@ class EntryController extends Controller
|
||||
$label = substr($label, 1);
|
||||
$remove = true;
|
||||
}
|
||||
$tag = $this->get(TagRepository::class)->findOneByLabel($label);
|
||||
$tag = $tagRepository->findOneByLabel($label);
|
||||
if ($remove) {
|
||||
if (null !== $tag) {
|
||||
$tagsToRemove[] = $tag;
|
||||
@ -86,7 +104,7 @@ class EntryController extends Controller
|
||||
if (isset($values['entry-checkbox'])) {
|
||||
foreach ($values['entry-checkbox'] as $id) {
|
||||
/** @var Entry * */
|
||||
$entry = $this->get(EntryRepository::class)->findById((int) $id)[0];
|
||||
$entry = $this->entryRepository->findById((int) $id)[0];
|
||||
|
||||
$this->checkUserAction($entry);
|
||||
|
||||
@ -102,15 +120,17 @@ class EntryController extends Controller
|
||||
$entry->removeTag($tag);
|
||||
}
|
||||
} elseif ('delete' === $action) {
|
||||
$this->get(EventDispatcherInterface::class)->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
||||
$em->remove($entry);
|
||||
$this->eventDispatcher->dispatch(new EntryDeletedEvent($entry), EntryDeletedEvent::NAME);
|
||||
$this->entityManager->remove($entry);
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirect($this->get(Redirect::class)->to($request->getSession()->get('prevUrl')));
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'));
|
||||
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,7 +169,7 @@ class EntryController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function addEntryFormAction(Request $request)
|
||||
public function addEntryFormAction(Request $request, TranslatorInterface $translator)
|
||||
{
|
||||
$entry = new Entry($this->getUser());
|
||||
|
||||
@ -161,9 +181,9 @@ class EntryController extends Controller
|
||||
$existingEntry = $this->checkIfEntryAlreadyExists($entry);
|
||||
|
||||
if (false !== $existingEntry) {
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
|
||||
$translator->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
|
||||
);
|
||||
|
||||
return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
|
||||
@ -171,12 +191,11 @@ class EntryController extends Controller
|
||||
|
||||
$this->updateEntry($entry);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
// entry saved, dispatch event about it!
|
||||
$this->get(EventDispatcherInterface::class)->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
|
||||
|
||||
return $this->redirect($this->generateUrl('homepage'));
|
||||
}
|
||||
@ -199,12 +218,11 @@ class EntryController extends Controller
|
||||
if (false === $this->checkIfEntryAlreadyExists($entry)) {
|
||||
$this->updateEntry($entry);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
// entry saved, dispatch event about it!
|
||||
$this->get(EventDispatcherInterface::class)->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
|
||||
}
|
||||
|
||||
return $this->redirect($this->generateUrl('homepage'));
|
||||
@ -236,11 +254,10 @@ class EntryController extends Controller
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
'flashes.entry.notice.entry_updated'
|
||||
);
|
||||
@ -279,7 +296,7 @@ class EntryController extends Controller
|
||||
public function showUnreadAction(Request $request, $page)
|
||||
{
|
||||
// load the quickstart if no entry in database
|
||||
if (1 === (int) $page && 0 === $this->get(EntryRepository::class)->countAllEntriesByUser($this->getUser()->getId())) {
|
||||
if (1 === (int) $page && 0 === $this->entryRepository->countAllEntriesByUser($this->getUser()->getId())) {
|
||||
return $this->redirect($this->generateUrl('quickstart'));
|
||||
}
|
||||
|
||||
@ -345,21 +362,17 @@ class EntryController extends Controller
|
||||
/**
|
||||
* Shows random entry depending on the given type.
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @Route("/{type}/random", name="random_entry", requirements={"type": "unread|starred|archive|untagged|annotated|all"})
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function redirectRandomEntryAction($type = 'all')
|
||||
public function redirectRandomEntryAction(string $type = 'all')
|
||||
{
|
||||
try {
|
||||
$entry = $this->get(EntryRepository::class)
|
||||
$entry = $this->entryRepository
|
||||
->getRandomEntry($this->getUser()->getId(), $type);
|
||||
} catch (NoResultException $e) {
|
||||
$bag = $this->get(SessionInterface::class)->getFlashBag();
|
||||
$bag->clear();
|
||||
$bag->add('notice', 'flashes.entry.notice.no_random_entry');
|
||||
$this->addFlash('notice', 'flashes.entry.notice.no_random_entry');
|
||||
|
||||
return $this->redirect($this->generateUrl($type));
|
||||
}
|
||||
@ -401,19 +414,16 @@ class EntryController extends Controller
|
||||
|
||||
// if refreshing entry failed, don't save it
|
||||
if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
|
||||
$bag = $this->get(SessionInterface::class)->getFlashBag();
|
||||
$bag->clear();
|
||||
$bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
|
||||
$this->addFlash('notice', 'flashes.entry.notice.entry_reloaded_failed');
|
||||
|
||||
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
||||
}
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
// entry saved, dispatch event about it!
|
||||
$this->get(EventDispatcherInterface::class)->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
|
||||
|
||||
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
||||
}
|
||||
@ -430,19 +440,22 @@ class EntryController extends Controller
|
||||
$this->checkUserAction($entry);
|
||||
|
||||
$entry->toggleArchive();
|
||||
$this->get('doctrine')->getManager()->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
$message = 'flashes.entry.notice.entry_unarchived';
|
||||
if ($entry->isArchived()) {
|
||||
$message = 'flashes.entry.notice.entry_archived';
|
||||
}
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$message
|
||||
);
|
||||
|
||||
return $this->redirect($this->get(Redirect::class)->to($request->getSession()->get('prevUrl')));
|
||||
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'));
|
||||
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -458,19 +471,21 @@ class EntryController extends Controller
|
||||
|
||||
$entry->toggleStar();
|
||||
$entry->updateStar($entry->isStarred());
|
||||
$this->get('doctrine')->getManager()->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
$message = 'flashes.entry.notice.entry_unstarred';
|
||||
if ($entry->isStarred()) {
|
||||
$message = 'flashes.entry.notice.entry_starred';
|
||||
}
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$message
|
||||
);
|
||||
|
||||
return $this->redirect($this->get(Redirect::class)->to($request->getSession()->get('prevUrl')));
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'));
|
||||
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -493,21 +508,22 @@ class EntryController extends Controller
|
||||
);
|
||||
|
||||
// entry deleted, dispatch event about it!
|
||||
$this->get(EventDispatcherInterface::class)->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
||||
$this->eventDispatcher->dispatch(new EntryDeletedEvent($entry), EntryDeletedEvent::NAME);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
'flashes.entry.notice.entry_deleted'
|
||||
);
|
||||
|
||||
// don't redirect user to the deleted entry
|
||||
$prev = $request->getSession()->get('prevUrl');
|
||||
|
||||
// don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
|
||||
$prev = $request->headers->get('prevUrl');
|
||||
$to = (1 !== preg_match('#' . $url . '$#i', $prev) ? $prev : null);
|
||||
$redirectUrl = $this->get(Redirect::class)->to($to);
|
||||
|
||||
$redirectUrl = $this->redirectHelper->to($to);
|
||||
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
@ -526,9 +542,8 @@ class EntryController extends Controller
|
||||
if (null === $entry->getUid()) {
|
||||
$entry->generateUid();
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirect($this->generateUrl('share_entry', [
|
||||
@ -549,9 +564,8 @@ class EntryController extends Controller
|
||||
|
||||
$entry->cleanUid();
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirect($this->generateUrl('view', [
|
||||
'id' => $entry->getId(),
|
||||
@ -566,9 +580,9 @@ class EntryController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function shareEntryAction(Entry $entry)
|
||||
public function shareEntryAction(Entry $entry, Config $craueConfig)
|
||||
{
|
||||
if (!$this->get(Config::class)->get('share_public')) {
|
||||
if (!$craueConfig->get('share_public')) {
|
||||
throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
|
||||
}
|
||||
|
||||
@ -603,7 +617,6 @@ class EntryController extends Controller
|
||||
*/
|
||||
private function showEntries($type, Request $request, $page)
|
||||
{
|
||||
$repository = $this->get(EntryRepository::class);
|
||||
$searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
|
||||
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
|
||||
$request->getSession()->set('prevUrl', $request->getRequestUri());
|
||||
@ -612,31 +625,31 @@ class EntryController extends Controller
|
||||
|
||||
switch ($type) {
|
||||
case 'search':
|
||||
$qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
|
||||
$qb = $this->entryRepository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
|
||||
break;
|
||||
case 'untagged':
|
||||
$qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForUntaggedByUser($this->getUser()->getId());
|
||||
break;
|
||||
case 'starred':
|
||||
$qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForStarredByUser($this->getUser()->getId());
|
||||
$formOptions['filter_starred'] = true;
|
||||
break;
|
||||
case 'archive':
|
||||
$qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForArchiveByUser($this->getUser()->getId());
|
||||
$formOptions['filter_archived'] = true;
|
||||
break;
|
||||
case 'annotated':
|
||||
$qb = $repository->getBuilderForAnnotationsByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForAnnotationsByUser($this->getUser()->getId());
|
||||
break;
|
||||
case 'unread':
|
||||
$qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForUnreadByUser($this->getUser()->getId());
|
||||
$formOptions['filter_unread'] = true;
|
||||
break;
|
||||
case 'same-domain':
|
||||
$qb = $repository->getBuilderForSameDomainByUser($this->getUser()->getId(), $request->get('id'));
|
||||
$qb = $this->entryRepository->getBuilderForSameDomainByUser($this->getUser()->getId(), $request->get('id'));
|
||||
break;
|
||||
case 'all':
|
||||
$qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
|
||||
$qb = $this->entryRepository->getBuilderForAllByUser($this->getUser()->getId());
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||
@ -649,12 +662,12 @@ class EntryController extends Controller
|
||||
$form->submit($request->query->get($form->getName()));
|
||||
|
||||
// build the query from the given form object
|
||||
$this->get(FilterBuilderUpdaterInterface::class)->addFilterConditions($form, $qb);
|
||||
$this->filterBuilderUpdater->addFilterConditions($form, $qb);
|
||||
}
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
|
||||
|
||||
$entries = $this->get(PreparePagerForEntries::class)->prepare($pagerAdapter);
|
||||
$entries = $this->preparePagerForEntriesHelper->prepare($pagerAdapter);
|
||||
|
||||
try {
|
||||
$entries->setCurrentPage($page);
|
||||
@ -664,7 +677,7 @@ class EntryController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$nbEntriesUntagged = $this->get(EntryRepository::class)
|
||||
$nbEntriesUntagged = $this->entryRepository
|
||||
->countUntaggedEntriesByUser($this->getUser()->getId());
|
||||
|
||||
return $this->render(
|
||||
@ -690,7 +703,7 @@ class EntryController extends Controller
|
||||
$message = 'flashes.entry.notice.' . $prefixMessage;
|
||||
|
||||
try {
|
||||
$this->get(ContentProxy::class)->updateEntry($entry, $entry->getUrl());
|
||||
$this->contentProxy->updateEntry($entry, $entry->getUrl());
|
||||
} catch (\Exception $e) {
|
||||
// $this->logger->error('Error while saving an entry', [
|
||||
// 'exception' => $e,
|
||||
@ -701,14 +714,14 @@ class EntryController extends Controller
|
||||
}
|
||||
|
||||
if (empty($entry->getDomainName())) {
|
||||
$this->get(ContentProxy::class)->setEntryDomainName($entry);
|
||||
$this->contentProxy->setEntryDomainName($entry);
|
||||
}
|
||||
|
||||
if (empty($entry->getTitle())) {
|
||||
$this->get(ContentProxy::class)->setDefaultEntryTitle($entry);
|
||||
$this->contentProxy->setDefaultEntryTitle($entry);
|
||||
}
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add('notice', $message);
|
||||
$this->addFlash('notice', $message);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -728,6 +741,6 @@ class EntryController extends Controller
|
||||
*/
|
||||
private function checkIfEntryAlreadyExists(Entry $entry)
|
||||
{
|
||||
return $this->get(EntryRepository::class)->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
|
||||
return $this->entryRepository->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,12 +2,11 @@
|
||||
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Helper\EntriesExport;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
@ -16,13 +15,11 @@ use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
* The try/catch can be removed once all formats will be implemented.
|
||||
* Still need implementation: txt.
|
||||
*/
|
||||
class ExportController extends Controller
|
||||
class ExportController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Gets one entry content.
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @Route("/export/{id}.{format}", name="export_entry", requirements={
|
||||
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
||||
* "id": "\d+"
|
||||
@ -30,10 +27,21 @@ class ExportController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function downloadEntryAction(Entry $entry, $format)
|
||||
public function downloadEntryAction(Request $request, EntryRepository $entryRepository, EntriesExport $entriesExport, string $format, int $id)
|
||||
{
|
||||
try {
|
||||
return $this->get(EntriesExport::class)
|
||||
$entry = $entryRepository->find($id);
|
||||
|
||||
/*
|
||||
* We duplicate EntryController::checkUserAction here as a quick fix for an improper authorization vulnerability
|
||||
*
|
||||
* This should be eventually rewritten
|
||||
*/
|
||||
if (null === $entry || null === $this->getUser() || $this->getUser()->getId() !== $entry->getUser()->getId()) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
return $entriesExport
|
||||
->setEntries($entry)
|
||||
->updateTitle('entry')
|
||||
->updateAuthor('entry')
|
||||
@ -46,9 +54,6 @@ class ExportController extends Controller
|
||||
/**
|
||||
* Export all entries for current user.
|
||||
*
|
||||
* @param string $format
|
||||
* @param string $category
|
||||
*
|
||||
* @Route("/export/{category}.{format}", name="export_entries", requirements={
|
||||
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
||||
* "category": "all|unread|starred|archive|tag_entries|untagged|search|annotated|same_domain"
|
||||
@ -56,17 +61,24 @@ class ExportController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function downloadEntriesAction(Request $request, $format, $category)
|
||||
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category, int $entry = 0)
|
||||
{
|
||||
$method = ucfirst($category);
|
||||
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
|
||||
$repository = $this->get(EntryRepository::class);
|
||||
$title = $method;
|
||||
|
||||
if ('tag_entries' === $category) {
|
||||
$tag = $this->get(TagRepository::class)->findOneBySlug($request->query->get('tag'));
|
||||
if ('same_domain' === $category) {
|
||||
$entries = $entryRepository->getBuilderForSameDomainByUser(
|
||||
$this->getUser()->getId(),
|
||||
$request->get('entry')
|
||||
)->getQuery()
|
||||
->getResult();
|
||||
|
||||
$entries = $repository->findAllByTagId(
|
||||
$title = 'Same domain';
|
||||
} elseif ('tag_entries' === $category) {
|
||||
$tag = $tagRepository->findOneBySlug($request->query->get('tag'));
|
||||
|
||||
$entries = $entryRepository->findAllByTagId(
|
||||
$this->getUser()->getId(),
|
||||
$tag->getId()
|
||||
);
|
||||
@ -76,7 +88,7 @@ class ExportController extends Controller
|
||||
$searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
|
||||
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
|
||||
|
||||
$entries = $repository->getBuilderForSearchByUser(
|
||||
$entries = $entryRepository->getBuilderForSearchByUser(
|
||||
$this->getUser()->getId(),
|
||||
$searchTerm,
|
||||
$currentRoute
|
||||
@ -85,21 +97,21 @@ class ExportController extends Controller
|
||||
|
||||
$title = 'Search ' . $searchTerm;
|
||||
} elseif ('annotated' === $category) {
|
||||
$entries = $repository->getBuilderForAnnotationsByUser(
|
||||
$entries = $entryRepository->getBuilderForAnnotationsByUser(
|
||||
$this->getUser()->getId()
|
||||
)->getQuery()
|
||||
->getResult();
|
||||
|
||||
$title = 'With annotations';
|
||||
} else {
|
||||
$entries = $repository
|
||||
$entries = $entryRepository
|
||||
->$methodBuilder($this->getUser()->getId())
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->get(EntriesExport::class)
|
||||
return $entriesExport
|
||||
->setEntries($entries)
|
||||
->updateTitle($title)
|
||||
->updateAuthor($method)
|
||||
|
||||
@ -7,7 +7,7 @@ use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
||||
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
@ -18,8 +18,15 @@ use Wallabag\CoreBundle\Helper\PreparePagerForEntries;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\UserBundle\Entity\User;
|
||||
|
||||
class FeedController extends Controller
|
||||
class FeedController extends AbstractController
|
||||
{
|
||||
private EntryRepository $entryRepository;
|
||||
|
||||
public function __construct(EntryRepository $entryRepository)
|
||||
{
|
||||
$this->entryRepository = $entryRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows unread entries for current user.
|
||||
*
|
||||
@ -92,7 +99,7 @@ class FeedController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function showTagsFeedAction(Request $request, User $user, Tag $tag, $page)
|
||||
public function showTagsFeedAction(Request $request, User $user, Tag $tag, PreparePagerForEntries $preparePagerForEntries, $page)
|
||||
{
|
||||
$sort = $request->query->get('sort', 'created');
|
||||
|
||||
@ -115,7 +122,7 @@ class FeedController extends Controller
|
||||
UrlGeneratorInterface::ABSOLUTE_URL
|
||||
);
|
||||
|
||||
$entriesByTag = $this->get(EntryRepository::class)->findAllByTagId(
|
||||
$entriesByTag = $this->entryRepository->findAllByTagId(
|
||||
$user->getId(),
|
||||
$tag->getId(),
|
||||
$sorts[$sort]
|
||||
@ -123,7 +130,7 @@ class FeedController extends Controller
|
||||
|
||||
$pagerAdapter = new ArrayAdapter($entriesByTag);
|
||||
|
||||
$entries = $this->get(PreparePagerForEntries::class)->prepare(
|
||||
$entries = $preparePagerForEntries->prepare(
|
||||
$pagerAdapter,
|
||||
$user
|
||||
);
|
||||
@ -184,22 +191,20 @@ class FeedController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
private function showEntries($type, User $user, $page = 1)
|
||||
private function showEntries(string $type, User $user, $page = 1)
|
||||
{
|
||||
$repository = $this->get(EntryRepository::class);
|
||||
|
||||
switch ($type) {
|
||||
case 'starred':
|
||||
$qb = $repository->getBuilderForStarredByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
|
||||
break;
|
||||
case 'archive':
|
||||
$qb = $repository->getBuilderForArchiveByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
|
||||
break;
|
||||
case 'unread':
|
||||
$qb = $repository->getBuilderForUnreadByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
|
||||
break;
|
||||
case 'all':
|
||||
$qb = $repository->getBuilderForAllByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||
|
||||
@ -2,14 +2,14 @@
|
||||
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
|
||||
use Wallabag\CoreBundle\Form\Type\IgnoreOriginInstanceRuleType;
|
||||
use Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository;
|
||||
@ -19,16 +19,25 @@ use Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository;
|
||||
*
|
||||
* @Route("/ignore-origin-instance-rules")
|
||||
*/
|
||||
class IgnoreOriginInstanceRuleController extends Controller
|
||||
class IgnoreOriginInstanceRuleController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all IgnoreOriginInstanceRule entities.
|
||||
*
|
||||
* @Route("/", name="ignore_origin_instance_rules_index", methods={"GET"})
|
||||
*/
|
||||
public function indexAction()
|
||||
public function indexAction(IgnoreOriginInstanceRuleRepository $repository)
|
||||
{
|
||||
$rules = $this->get(IgnoreOriginInstanceRuleRepository::class)->findAll();
|
||||
$rules = $repository->findAll();
|
||||
|
||||
return $this->render('@WallabagCore/IgnoreOriginInstanceRule/index.html.twig', [
|
||||
'rules' => $rules,
|
||||
@ -50,13 +59,12 @@ class IgnoreOriginInstanceRuleController extends Controller
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($ignoreOriginInstanceRule);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($ignoreOriginInstanceRule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.ignore_origin_instance_rule.notice.added')
|
||||
$this->translator->trans('flashes.ignore_origin_instance_rule.notice.added')
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('ignore_origin_instance_rules_index');
|
||||
@ -82,13 +90,12 @@ class IgnoreOriginInstanceRuleController extends Controller
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($ignoreOriginInstanceRule);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($ignoreOriginInstanceRule);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.ignore_origin_instance_rule.notice.updated')
|
||||
$this->translator->trans('flashes.ignore_origin_instance_rule.notice.updated')
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('ignore_origin_instance_rules_index');
|
||||
@ -114,14 +121,13 @@ class IgnoreOriginInstanceRuleController extends Controller
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.ignore_origin_instance_rule.notice.deleted')
|
||||
$this->translator->trans('flashes.ignore_origin_instance_rule.notice.deleted')
|
||||
);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($ignoreOriginInstanceRule);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($ignoreOriginInstanceRule);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('ignore_origin_instance_rules_index');
|
||||
|
||||
@ -3,14 +3,14 @@
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Craue\ConfigBundle\Util\Config;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Wallabag\CoreBundle\Entity\SiteCredential;
|
||||
use Wallabag\CoreBundle\Form\Type\SiteCredentialType;
|
||||
use Wallabag\CoreBundle\Helper\CryptoProxy;
|
||||
@ -22,18 +22,31 @@ use Wallabag\UserBundle\Entity\User;
|
||||
*
|
||||
* @Route("/site-credentials")
|
||||
*/
|
||||
class SiteCredentialController extends Controller
|
||||
class SiteCredentialController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private TranslatorInterface $translator;
|
||||
private CryptoProxy $cryptoProxy;
|
||||
private Config $craueConfig;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator, CryptoProxy $cryptoProxy, Config $craueConfig)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->translator = $translator;
|
||||
$this->cryptoProxy = $cryptoProxy;
|
||||
$this->craueConfig = $craueConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all User entities.
|
||||
*
|
||||
* @Route("/", name="site_credentials_index", methods={"GET"})
|
||||
*/
|
||||
public function indexAction()
|
||||
public function indexAction(SiteCredentialRepository $repository)
|
||||
{
|
||||
$this->isSiteCredentialsEnabled();
|
||||
|
||||
$credentials = $this->get(SiteCredentialRepository::class)->findByUser($this->getUser());
|
||||
$credentials = $repository->findByUser($this->getUser());
|
||||
|
||||
return $this->render('@WallabagCore/SiteCredential/index.html.twig', [
|
||||
'credentials' => $credentials,
|
||||
@ -57,16 +70,15 @@ class SiteCredentialController extends Controller
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$credential->setUsername($this->get(CryptoProxy::class)->crypt($credential->getUsername()));
|
||||
$credential->setPassword($this->get(CryptoProxy::class)->crypt($credential->getPassword()));
|
||||
$credential->setUsername($this->cryptoProxy->crypt($credential->getUsername()));
|
||||
$credential->setPassword($this->cryptoProxy->crypt($credential->getPassword()));
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($credential);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($credential);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.site_credential.notice.added', ['%host%' => $credential->getHost()])
|
||||
$this->translator->trans('flashes.site_credential.notice.added', ['%host%' => $credential->getHost()])
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('site_credentials_index');
|
||||
@ -96,16 +108,15 @@ class SiteCredentialController extends Controller
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$siteCredential->setUsername($this->get(CryptoProxy::class)->crypt($siteCredential->getUsername()));
|
||||
$siteCredential->setPassword($this->get(CryptoProxy::class)->crypt($siteCredential->getPassword()));
|
||||
$siteCredential->setUsername($this->cryptoProxy->crypt($siteCredential->getUsername()));
|
||||
$siteCredential->setPassword($this->cryptoProxy->crypt($siteCredential->getPassword()));
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($siteCredential);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($siteCredential);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.site_credential.notice.updated', ['%host%' => $siteCredential->getHost()])
|
||||
$this->translator->trans('flashes.site_credential.notice.updated', ['%host%' => $siteCredential->getHost()])
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('site_credentials_index');
|
||||
@ -135,14 +146,13 @@ class SiteCredentialController extends Controller
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
$this->get(TranslatorInterface::class)->trans('flashes.site_credential.notice.deleted', ['%host%' => $siteCredential->getHost()])
|
||||
$this->translator->trans('flashes.site_credential.notice.deleted', ['%host%' => $siteCredential->getHost()])
|
||||
);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($siteCredential);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($siteCredential);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('site_credentials_index');
|
||||
@ -153,7 +163,7 @@ class SiteCredentialController extends Controller
|
||||
*/
|
||||
private function isSiteCredentialsEnabled()
|
||||
{
|
||||
if (!$this->get(Config::class)->get('restricted_access')) {
|
||||
if (!$this->craueConfig->get('restricted_access')) {
|
||||
throw $this->createNotFoundException('Feature "restricted_access" is disabled, controllers too.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,17 +2,17 @@
|
||||
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class StaticController extends Controller
|
||||
class StaticController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Route("/howto", name="howto")
|
||||
*/
|
||||
public function howtoAction()
|
||||
{
|
||||
$addonsUrl = $this->container->getParameter('addons_url');
|
||||
$addonsUrl = $this->getParameter('addons_url');
|
||||
|
||||
return $this->render(
|
||||
'@WallabagCore/Static/howto.html.twig',
|
||||
|
||||
@ -2,15 +2,16 @@
|
||||
|
||||
namespace Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Pagerfanta\Adapter\ArrayAdapter;
|
||||
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\Tag;
|
||||
use Wallabag\CoreBundle\Form\Type\NewTagType;
|
||||
@ -21,29 +22,55 @@ use Wallabag\CoreBundle\Helper\TagsAssigner;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
|
||||
class TagController extends Controller
|
||||
class TagController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private TagsAssigner $tagsAssigner;
|
||||
private Redirect $redirectHelper;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, TagsAssigner $tagsAssigner, Redirect $redirectHelper)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->tagsAssigner = $tagsAssigner;
|
||||
$this->redirectHelper = $redirectHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
|
||||
* @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag", methods={"POST"})
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function addTagFormAction(Request $request, Entry $entry)
|
||||
public function addTagFormAction(Request $request, Entry $entry, TranslatorInterface $translator)
|
||||
{
|
||||
$form = $this->createForm(NewTagType::class, new Tag());
|
||||
$form->handleRequest($request);
|
||||
|
||||
$tags = $form->get('label')->getData();
|
||||
$tagsExploded = explode(',', $tags);
|
||||
|
||||
// avoid too much tag to be added
|
||||
if (\count($tagsExploded) >= NewTagType::MAX_TAGS || \strlen($tags) >= NewTagType::MAX_LENGTH) {
|
||||
$message = $translator->trans('flashes.tag.notice.too_much_tags', [
|
||||
'%tags%' => NewTagType::MAX_TAGS,
|
||||
'%characters%' => NewTagType::MAX_LENGTH,
|
||||
]);
|
||||
$this->addFlash('notice', $message);
|
||||
|
||||
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->get(TagsAssigner::class)->assignTagsToEntry(
|
||||
$this->checkUserAction($entry);
|
||||
|
||||
$this->tagsAssigner->assignTagsToEntry(
|
||||
$entry,
|
||||
$form->get('label')->getData()
|
||||
);
|
||||
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->persist($entry);
|
||||
$em->flush();
|
||||
$this->entityManager->persist($entry);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
'flashes.tag.notice.tag_added'
|
||||
);
|
||||
@ -66,17 +93,18 @@ class TagController extends Controller
|
||||
*/
|
||||
public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
|
||||
{
|
||||
$this->checkUserAction($entry);
|
||||
|
||||
$entry->removeTag($tag);
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
// remove orphan tag in case no entries are associated to it
|
||||
if (0 === \count($tag->getEntries())) {
|
||||
$em->remove($tag);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($tag);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$redirectUrl = $this->get(Redirect::class)->to($request->getSession()->get('prevUrl'), '', true);
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'), '', true);
|
||||
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
@ -88,12 +116,10 @@ class TagController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function showTagAction()
|
||||
public function showTagAction(TagRepository $tagRepository, EntryRepository $entryRepository)
|
||||
{
|
||||
$tags = $this->get(TagRepository::class)
|
||||
->findAllFlatTagsWithNbEntries($this->getUser()->getId());
|
||||
$nbEntriesUntagged = $this->get(EntryRepository::class)
|
||||
->countUntaggedEntriesByUser($this->getUser()->getId());
|
||||
$tags = $tagRepository->findAllFlatTagsWithNbEntries($this->getUser()->getId());
|
||||
$nbEntriesUntagged = $entryRepository->countUntaggedEntriesByUser($this->getUser()->getId());
|
||||
|
||||
$renameForms = [];
|
||||
foreach ($tags as $tag) {
|
||||
@ -115,16 +141,16 @@ class TagController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function showEntriesForTagAction(Tag $tag, $page, Request $request)
|
||||
public function showEntriesForTagAction(Tag $tag, EntryRepository $entryRepository, PreparePagerForEntries $preparePagerForEntries, $page, Request $request)
|
||||
{
|
||||
$entriesByTag = $this->get(EntryRepository::class)->findAllByTagId(
|
||||
$entriesByTag = $entryRepository->findAllByTagId(
|
||||
$this->getUser()->getId(),
|
||||
$tag->getId()
|
||||
);
|
||||
|
||||
$pagerAdapter = new ArrayAdapter($entriesByTag);
|
||||
|
||||
$entries = $this->get(PreparePagerForEntries::class)->prepare($pagerAdapter);
|
||||
$entries = $preparePagerForEntries->prepare($pagerAdapter);
|
||||
|
||||
try {
|
||||
$entries->setCurrentPage($page);
|
||||
@ -154,12 +180,12 @@ class TagController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function renameTagAction(Tag $tag, Request $request)
|
||||
public function renameTagAction(Tag $tag, Request $request, TagRepository $tagRepository, EntryRepository $entryRepository)
|
||||
{
|
||||
$form = $this->createForm(RenameTagType::class, new Tag());
|
||||
$form->handleRequest($request);
|
||||
|
||||
$redirectUrl = $this->get(Redirect::class)->to($request->getSession()->get('prevUrl'), '', true);
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'), '', true);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$newTag = new Tag();
|
||||
@ -169,18 +195,18 @@ class TagController extends Controller
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
|
||||
$tagFromRepo = $this->get(TagRepository::class)->findOneByLabel($newTag->getLabel());
|
||||
$tagFromRepo = $tagRepository->findOneByLabel($newTag->getLabel());
|
||||
|
||||
if (null !== $tagFromRepo) {
|
||||
$newTag = $tagFromRepo;
|
||||
}
|
||||
|
||||
$entries = $this->get(EntryRepository::class)->findAllByTagId(
|
||||
$entries = $entryRepository->findAllByTagId(
|
||||
$this->getUser()->getId(),
|
||||
$tag->getId()
|
||||
);
|
||||
foreach ($entries as $entry) {
|
||||
$this->get(TagsAssigner::class)->assignTagsToEntry(
|
||||
$this->tagsAssigner->assignTagsToEntry(
|
||||
$entry,
|
||||
$newTag->getLabel(),
|
||||
[$newTag]
|
||||
@ -188,9 +214,9 @@ class TagController extends Controller
|
||||
$entry->removeTag($tag);
|
||||
}
|
||||
|
||||
$this->get('doctrine')->getManager()->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->get(SessionInterface::class)->getFlashBag()->add(
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
'flashes.tag.notice.tag_renamed'
|
||||
);
|
||||
@ -206,28 +232,32 @@ class TagController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function tagThisSearchAction($filter, Request $request)
|
||||
public function tagThisSearchAction($filter, Request $request, EntryRepository $entryRepository)
|
||||
{
|
||||
$currentRoute = $request->query->has('currentRoute') ? $request->query->get('currentRoute') : '';
|
||||
|
||||
/** @var QueryBuilder $qb */
|
||||
$qb = $this->get(EntryRepository::class)->getBuilderForSearchByUser($this->getUser()->getId(), $filter, $currentRoute);
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$qb = $entryRepository->getBuilderForSearchByUser($this->getUser()->getId(), $filter, $currentRoute);
|
||||
|
||||
$entries = $qb->getQuery()->getResult();
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$this->get(TagsAssigner::class)->assignTagsToEntry(
|
||||
$this->tagsAssigner->assignTagsToEntry(
|
||||
$entry,
|
||||
$filter
|
||||
);
|
||||
|
||||
$em->persist($entry);
|
||||
// check to avoid duplicate tags creation
|
||||
foreach ($this->entityManager->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
|
||||
if ($entity instanceof Tag && strtolower($entity->getLabel()) === strtolower($filter)) {
|
||||
continue 2;
|
||||
}
|
||||
$this->entityManager->persist($entry);
|
||||
}
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
return $this->redirect($this->get(Redirect::class)->to($request->getSession()->get('prevUrl'), '', true));
|
||||
return $this->redirect($this->redirectHelper->to($request->headers->get('prevUrl'), '', true));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -238,19 +268,29 @@ class TagController extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function removeTagAction(Tag $tag, Request $request)
|
||||
public function removeTagAction(Tag $tag, Request $request, EntryRepository $entryRepository)
|
||||
{
|
||||
foreach ($tag->getEntriesByUserId($this->getUser()->getId()) as $entry) {
|
||||
$this->get(EntryRepository::class)->removeTag($this->getUser()->getId(), $tag);
|
||||
$entryRepository->removeTag($this->getUser()->getId(), $tag);
|
||||
}
|
||||
|
||||
// remove orphan tag in case no entries are associated to it
|
||||
if (0 === \count($tag->getEntries())) {
|
||||
$em = $this->get('doctrine')->getManager();
|
||||
$em->remove($tag);
|
||||
$em->flush();
|
||||
$this->entityManager->remove($tag);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
$redirectUrl = $this->redirectHelper->to($request->headers->get('prevUrl'), '', true);
|
||||
|
||||
return $this->redirect($this->get(Redirect::class)->to($request->getSession()->get('prevUrl'), '', true));
|
||||
return $this->redirect($redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the logged user can manage the given entry.
|
||||
*/
|
||||
private function checkUserAction(Entry $entry)
|
||||
{
|
||||
if (null === $this->getUser() || $this->getUser()->getId() !== $entry->getUser()->getId()) {
|
||||
throw $this->createAccessDeniedException('You can not access this entry.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user