aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Controller
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2019-06-26 22:31:47 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2019-07-08 09:38:32 +0200
commit34be2d5de44ade2a78be73decc0b90a2c1bca720 (patch)
treed635438784c4b02e1d182cfbc3d47b62e5032f1d /src/Wallabag/CoreBundle/Controller
parent92cd51aa2c29e23f137cde9b9732ced33ff38e59 (diff)
downloadwallabag-34be2d5de44ade2a78be73decc0b90a2c1bca720.tar.gz
wallabag-34be2d5de44ade2a78be73decc0b90a2c1bca720.tar.zst
wallabag-34be2d5de44ade2a78be73decc0b90a2c1bca720.zip
Add ability to import/export tagging rules
- Add missing translations - Add some tests - Add `/api/taggingrule/export` API endpoint - Add baggy theme - Add error message when importing tagging rules failed - Also fix all translations (I think we are good now)
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller')
-rw-r--r--src/Wallabag/CoreBundle/Controller/ConfigController.php62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php
index cea41303..0db90ba4 100644
--- a/src/Wallabag/CoreBundle/Controller/ConfigController.php
+++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php
@@ -2,11 +2,14 @@
2 2
3namespace Wallabag\CoreBundle\Controller; 3namespace Wallabag\CoreBundle\Controller;
4 4
5use JMS\Serializer\SerializationContext;
6use JMS\Serializer\SerializerBuilder;
5use PragmaRX\Recovery\Recovery as BackupCodes; 7use PragmaRX\Recovery\Recovery as BackupCodes;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller; 8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\JsonResponse; 9use Symfony\Component\HttpFoundation\JsonResponse;
8use Symfony\Component\HttpFoundation\RedirectResponse; 10use Symfony\Component\HttpFoundation\RedirectResponse;
9use Symfony\Component\HttpFoundation\Request; 11use Symfony\Component\HttpFoundation\Request;
12use Symfony\Component\HttpFoundation\Response;
10use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 13use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
11use Symfony\Component\Routing\Annotation\Route; 14use Symfony\Component\Routing\Annotation\Route;
12use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; 15use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
@@ -15,6 +18,7 @@ use Wallabag\CoreBundle\Entity\TaggingRule;
15use Wallabag\CoreBundle\Form\Type\ChangePasswordType; 18use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
16use Wallabag\CoreBundle\Form\Type\ConfigType; 19use Wallabag\CoreBundle\Form\Type\ConfigType;
17use Wallabag\CoreBundle\Form\Type\FeedType; 20use Wallabag\CoreBundle\Form\Type\FeedType;
21use Wallabag\CoreBundle\Form\Type\TaggingRuleImportType;
18use Wallabag\CoreBundle\Form\Type\TaggingRuleType; 22use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
19use Wallabag\CoreBundle\Form\Type\UserInformationType; 23use Wallabag\CoreBundle\Form\Type\UserInformationType;
20use Wallabag\CoreBundle\Tools\Utils; 24use Wallabag\CoreBundle\Tools\Utils;
@@ -140,6 +144,37 @@ class ConfigController extends Controller
140 return $this->redirect($this->generateUrl('config') . '#set5'); 144 return $this->redirect($this->generateUrl('config') . '#set5');
141 } 145 }
142 146
147 // handle tagging rules import
148 $taggingRulesImportform = $this->createForm(TaggingRuleImportType::class);
149 $taggingRulesImportform->handleRequest($request);
150
151 if ($taggingRulesImportform->isSubmitted() && $taggingRulesImportform->isValid()) {
152 $message = 'flashes.config.notice.tagging_rules_not_imported';
153 $file = $taggingRulesImportform->get('file')->getData();
154
155 if (null !== $file && $file->isValid() && \in_array($file->getClientMimeType(), ['application/json', 'application/octet-stream'], true)) {
156 $content = json_decode(file_get_contents($file->getPathname()), true);
157
158 if (\is_array($content)) {
159 foreach ($content as $rule) {
160 $taggingRule = new TaggingRule();
161 $taggingRule->setRule($rule['rule']);
162 $taggingRule->setTags($rule['tags']);
163 $taggingRule->setConfig($config);
164 $em->persist($taggingRule);
165 }
166
167 $em->flush();
168
169 $message = 'flashes.config.notice.tagging_rules_imported';
170 }
171 }
172
173 $this->addFlash('notice', $message);
174
175 return $this->redirect($this->generateUrl('config') . '#set5');
176 }
177
143 return $this->render('WallabagCoreBundle:Config:index.html.twig', [ 178 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
144 'form' => [ 179 'form' => [
145 'config' => $configForm->createView(), 180 'config' => $configForm->createView(),
@@ -147,6 +182,7 @@ class ConfigController extends Controller
147 'pwd' => $pwdForm->createView(), 182 'pwd' => $pwdForm->createView(),
148 'user' => $userForm->createView(), 183 'user' => $userForm->createView(),
149 'new_tagging_rule' => $newTaggingRule->createView(), 184 'new_tagging_rule' => $newTaggingRule->createView(),
185 'import_tagging_rule' => $taggingRulesImportform->createView(),
150 ], 186 ],
151 'feed' => [ 187 'feed' => [
152 'username' => $user->getUsername(), 188 'username' => $user->getUsername(),
@@ -493,6 +529,32 @@ class ConfigController extends Controller
493 } 529 }
494 530
495 /** 531 /**
532 * Export tagging rules for the logged in user.
533 *
534 * @Route("/tagging-rule/export", name="export_tagging_rule")
535 *
536 * @return Response
537 */
538 public function exportTaggingRulesAction()
539 {
540 $data = SerializerBuilder::create()->build()->serialize(
541 $this->getUser()->getConfig()->getTaggingRules(),
542 'json',
543 SerializationContext::create()->setGroups(['export_tagging_rule'])
544 );
545
546 return Response::create(
547 $data,
548 200,
549 [
550 'Content-type' => 'application/json',
551 'Content-Disposition' => 'attachment; filename="tagging_rules_' . $this->getUser()->getUsername() . '.json"',
552 'Content-Transfer-Encoding' => 'UTF-8',
553 ]
554 );
555 }
556
557 /**
496 * Remove all tags for given tags and a given user and cleanup orphan tags. 558 * Remove all tags for given tags and a given user and cleanup orphan tags.
497 * 559 *
498 * @param array $tags 560 * @param array $tags