From f19f9f62d13c62f18884e8bd0fa67403e8cad8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 11 Oct 2015 17:30:58 +0200 Subject: Add a form to create tagging rules --- .../CoreBundle/Controller/ConfigController.php | 21 ++++++++++ .../DataTransformer/StringToListTransformer.php | 49 ++++++++++++++++++++++ .../CoreBundle/Form/Type/TaggingRuleType.php | 38 +++++++++++++++++ .../views/themes/material/Config/index.html.twig | 31 +++++++++++++- 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php create mode 100644 src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 8bbe4ca0..24b86344 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -7,9 +7,11 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Wallabag\CoreBundle\Entity\Config; +use Wallabag\CoreBundle\Entity\TaggingRule; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Form\Type\ChangePasswordType; use Wallabag\CoreBundle\Form\Type\UserInformationType; +use Wallabag\CoreBundle\Form\Type\TaggingRuleType; use Wallabag\CoreBundle\Form\Type\NewUserType; use Wallabag\CoreBundle\Form\Type\RssType; use Wallabag\CoreBundle\Tools\Utils; @@ -98,6 +100,24 @@ class ConfigController extends Controller return $this->redirect($this->generateUrl('config')); } + // handle tagging rule + $taggingRule = new TaggingRule(); + $newTaggingRule = $this->createForm(new TaggingRuleType(), $taggingRule); + $newTaggingRule->handleRequest($request); + + if ($newTaggingRule->isValid()) { + $taggingRule->setConfig($config); + $em->persist($taggingRule); + $em->flush(); + + $this->get('session')->getFlashBag()->add( + 'notice', + 'Tagging rules updated' + ); + + return $this->redirect($this->generateUrl('config')); + } + // handle adding new user $newUser = $userManager->createUser(); // enable created user by default @@ -136,6 +156,7 @@ class ConfigController extends Controller 'pwd' => $pwdForm->createView(), 'user' => $userForm->createView(), 'new_user' => $newUserForm->createView(), + 'new_tagging_rule' => $newTaggingRule->createView(), ), 'rss' => array( 'username' => $user->getUsername(), diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php new file mode 100644 index 00000000..332a91b8 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php @@ -0,0 +1,49 @@ +separator = $separator; + } + + /** + * Transforms a list to a string. + * + * @param array|null $list + * + * @return string + */ + public function transform($list) + { + if (null === $list) { + return ''; + } + + return implode($this->separator, $list); + } + + /** + * Transforms a string to a list. + * + * @param string $string + * + * @return array|null + */ + public function reverseTransform($string) + { + if (!$string) { + return null; + } + + return array_filter(array_map('trim', explode($this->separator, $string))); + } +} diff --git a/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php b/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php new file mode 100644 index 00000000..7fbba38a --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php @@ -0,0 +1,38 @@ +add('rule', 'text', array('required' => true)) + ->add('save', 'submit') + ; + + $tagsField = $builder + ->create('tags', 'text') + ->addModelTransformer(new StringToListTransformer(',')); + + $builder->add($tagsField); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\CoreBundle\Entity\TaggingRule', + )); + } + + public function getName() + { + return 'tagging_rule'; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig index 8f121a2b..d27a8ca6 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig @@ -15,8 +15,9 @@
  • {% trans %}RSS{% endtrans %}
  • {% trans %}User information{% endtrans %}
  • {% trans %}Password{% endtrans %}
  • +
  • {% trans %}Tags{% endtrans %}
  • {% if is_granted('ROLE_SUPER_ADMIN') %} -
  • {% trans %}Add a user{% endtrans %}
  • +
  • {% trans %}Add a user{% endtrans %}
  • {% endif %} @@ -183,6 +184,34 @@ +
    +
    + {{ form_errors(form.pwd) }} + +
    +
    + {{ form_label(form.new_tagging_rule.rule) }} + {{ form_errors(form.new_tagging_rule.rule) }} + {{ form_widget(form.new_tagging_rule.rule) }} +
    +
    + +
    +
    + {{ form_label(form.new_tagging_rule.tags) }} + {{ form_errors(form.new_tagging_rule.tags) }} + {{ form_widget(form.new_tagging_rule.tags) }} +
    +
    + + + + +
    +
    + {% if is_granted('ROLE_SUPER_ADMIN') %}
    {{ form_start(form.new_user) }} -- cgit v1.2.3