Add a form to create tagging rules

This commit is contained in:
Kévin Gomez 2015-10-11 17:30:58 +02:00
parent ac9fec610a
commit f19f9f62d1
4 changed files with 138 additions and 1 deletions

View 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(),

View File

@ -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)));
}
}

View File

@ -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';
}
}

View 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>
@ -183,6 +184,34 @@
</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) }}