]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add a form to create tagging rules
authorKévin Gomez <contact@kevingomez.fr>
Sun, 11 Oct 2015 15:30:58 +0000 (17:30 +0200)
committerKévin Gomez <contact@kevingomez.fr>
Wed, 11 Nov 2015 15:23:49 +0000 (16:23 +0100)
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig

index 8bbe4ca06e5fac841566fc3676335d45f419457c..24b86344b25aa8073442a91fcc86df51677bb4cd 100644 (file)
@@ -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 (file)
index 0000000..332a91b
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace Wallabag\CoreBundle\Form\DataTransformer;
+
+use Doctrine\Common\Persistence\ObjectManager;
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
+
+class StringToListTransformer implements DataTransformerInterface
+{
+    private $separator;
+
+    public function __construct($separator = ',')
+    {
+        $this->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 (file)
index 0000000..7fbba38
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace Wallabag\CoreBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+use Wallabag\CoreBundle\Form\DataTransformer\StringToListTransformer;
+
+class TaggingRuleType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->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';
+    }
+}
index 8f121a2b756c594bee8e0da6b21af560fac22e03..d27a8ca6ffbf69def148e4c79bead361d1b580c0 100644 (file)
@@ -15,8 +15,9 @@
                         <li class="tab col s3"><a href="#set2">{% trans %}RSS{% endtrans %}</a></li>
                         <li class="tab col s3"><a href="#set3">{% trans %}User information{% endtrans %}</a></li>
                         <li class="tab col s3"><a href="#set4">{% trans %}Password{% endtrans %}</a></li>
+                        <li class="tab col s3"><a href="#set5">{% trans %}Tags{% endtrans %}</a></li>
                         {% if is_granted('ROLE_SUPER_ADMIN') %}
-                        <li class="tab col s3"><a href="#set5">{% trans %}Add a user{% endtrans %}</a></li>
+                        <li class="tab col s3"><a href="#set6">{% trans %}Add a user{% endtrans %}</a></li>
                         {% endif %}
                         </ul>
                     </div>
                         </form>
                     </div>
 
+                    <div id="set5" class="col s12">
+                        <form action="{{ path('config') }}#set5" method="post" {{ form_enctype(form.pwd) }}>
+                            {{ form_errors(form.pwd) }}
+
+                            <div class="row">
+                                <div class="input-field col s12">
+                                    {{ form_label(form.new_tagging_rule.rule) }}
+                                    {{ form_errors(form.new_tagging_rule.rule) }}
+                                    {{ form_widget(form.new_tagging_rule.rule) }}
+                                </div>
+                            </div>
+
+                            <div class="row">
+                                <div class="input-field col s12">
+                                    {{ form_label(form.new_tagging_rule.tags) }}
+                                    {{ form_errors(form.new_tagging_rule.tags) }}
+                                    {{ form_widget(form.new_tagging_rule.tags) }}
+                                </div>
+                            </div>
+
+                            <div class="hidden">{{ form_rest(form.new_tagging_rule) }}</div>
+                            <button class="btn waves-effect waves-light" type="submit" name="action">
+                                {% trans %}Save{% endtrans %}
+                            </button>
+
+                        </form>
+                    </div>
+
                     {% if is_granted('ROLE_SUPER_ADMIN') %}
                     <div id="set5" class="col s12">
                         {{ form_start(form.new_user) }}