Skip to content

Commit

Permalink
Add new Ignore Origin rules tab, update ConfigController
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
  • Loading branch information
Kdecherf committed Jun 23, 2019
1 parent 3426afb commit 1751fbb
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
78 changes: 76 additions & 2 deletions src/Wallabag/CoreBundle/Controller/ConfigController.php
Expand Up @@ -11,10 +11,13 @@
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\IgnoreOriginRule;
use Wallabag\CoreBundle\Entity\RuleInterface;
use Wallabag\CoreBundle\Entity\TaggingRule;
use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
use Wallabag\CoreBundle\Form\Type\ConfigType;
use Wallabag\CoreBundle\Form\Type\FeedType;
use Wallabag\CoreBundle\Form\Type\IgnoreOriginRuleType;
use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
use Wallabag\CoreBundle\Form\Type\UserInformationType;
use Wallabag\CoreBundle\Tools\Utils;
Expand Down Expand Up @@ -140,13 +143,43 @@ public function indexAction(Request $request)
return $this->redirect($this->generateUrl('config') . '#set5');
}

$ignoreOriginRule = new IgnoreOriginRule();
$action = $this->generateUrl('config') . '#set6';

if ($request->query->has('ignore-origin-rule')) {
$ignoreOriginRule = $this->getDoctrine()
->getRepository('WallabagCorebundle:IgnoreOriginRule')
->find($request->query->get('ignore-origin-rule'));

if ($this->getUser()->getId() !== $ignoreOriginRule->getConfig()->getUser()->getId()) {
return $this->redirect($action);
}

$action = $this->generateUrl('config') . '?ignore-origin-rule=' . $ignoreOriginRule->getId() . '#set6';
}

$newIgnoreOriginRule = $this->createForm(IgnoreOriginRuleType::class, $ignoreOriginRule, ['action' => $action]);
$newIgnoreOriginRule->handleRequest($request);

if ($newIgnoreOriginRule->isSubmitted() && $newIgnoreOriginRule->isValid()) {
$ignoreOriginRule->setConfig($config);
$em->persist($ignoreOriginRule);
$em->flush();

$this->addFlash(
'notice',
'flashes.config.notice.ignore_origin_rule_updated'
);
}

return $this->render('WallabagCoreBundle:Config:index.html.twig', [
'form' => [
'config' => $configForm->createView(),
'feed' => $feedForm->createView(),
'pwd' => $pwdForm->createView(),
'user' => $userForm->createView(),
'new_tagging_rule' => $newTaggingRule->createView(),
'new_ignore_origin_rule' => $newIgnoreOriginRule->createView(),
],
'feed' => [
'username' => $user->getUsername(),
Expand Down Expand Up @@ -368,6 +401,47 @@ public function editTaggingRuleAction(TaggingRule $rule)
return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5');
}

/**
* Deletes a ignore origin rule and redirect to the config homepage.
*
* @param IgnoreOriginRule $rule
*
* @Route("/ignore-origin-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_ignore_origin_rule")
*
* @return RedirectResponse
*/
public function deleteIgnoreOriginRuleAction(IgnoreOriginRule $rule)
{
$this->validateRuleAction($rule);

$em = $this->getDoctrine()->getManager();
$em->remove($rule);
$em->flush();

$this->addFlash(
'notice',
'flashes.config.notice.ignore_origin_rules_deleted'
);

return $this->redirect($this->generateUrl('config') . '#set6');
}

/**
* Edit a tagging rule.
*
* @param IgnoreOriginRule $rule
*
* @Route("/ignore-origin-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_ignore_origin_rule")
*
* @return RedirectResponse
*/
public function editIgnoreOriginRuleAction(IgnoreOriginRule $rule)
{
$this->validateRuleAction($rule);

return $this->redirect($this->generateUrl('config') . '?ignore-origin-rule=' . $rule->getId() . '#set6');
}

/**
* Remove all annotations OR tags OR entries for the current user.
*
Expand Down Expand Up @@ -559,9 +633,9 @@ private function removeAnnotationsForArchivedByUserId($userId)
/**
* Validate that a rule can be edited/deleted by the current user.
*
* @param TaggingRule $rule
* @param RuleInterface $rule
*/
private function validateRuleAction(TaggingRule $rule)
private function validateRuleAction(RuleInterface $rule)
{
if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) {
throw $this->createAccessDeniedException('You can not access this tagging rule.');
Expand Down
37 changes: 37 additions & 0 deletions src/Wallabag/CoreBundle/Form/Type/IgnoreOriginRuleType.php
@@ -0,0 +1,37 @@
<?php

namespace Wallabag\CoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IgnoreOriginRuleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rule', TextType::class, [
'required' => true,
'label' => 'config.form_rules.rule_label',
])
->add('save', SubmitType::class, [
'label' => 'config.form.save',
])
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'Wallabag\CoreBundle\Entity\IgnoreOriginRule',
]);
}

public function getBlockPrefix()
{
return 'ignore_origin_rule';
}
}
Expand Up @@ -16,7 +16,8 @@
<li class="tab col s12 m6 l3"><a href="#set3">{{ 'config.tab_menu.user_info'|trans }}</a></li>
<li class="tab col s12 m6 l3"><a href="#set4">{{ 'config.tab_menu.password'|trans }}</a></li>
<li class="tab col s12 m6 l3"><a href="#set5">{{ 'config.tab_menu.rules'|trans }}</a></li>
<li class="tab col s12 m6 l3"><a href="#set6">{{ 'config.tab_menu.reset'|trans }}</a></li>
<li class="tab col s12 m6 l3"><a href="#set6">{{ 'config.tab_menu.ignore_origin'|trans }}</a></li>
<li class="tab col s12 m6 l3"><a href="#set7">{{ 'config.tab_menu.reset'|trans }}</a></li>
</ul>
</div>

Expand Down Expand Up @@ -417,6 +418,44 @@
</div>

<div id="set6" class="col s12">
{% if app.user.config.ignoreOriginRules is not empty %}
<div class="row">
<div class="input-field col s12">
<ul>
{% for ignore_origin_rule in app.user.config.ignoreOriginRules %}
<li>
{{ 'config.form_rules.if_label'|trans }}
« {{ ignore_origin_rule.rule }} »
<a href="{{ path('edit_ignore_origin_rule', {id: ignore_origin_rule.id}) }}" title="{{ 'config.form_rules.edit_rule_label'|trans }}">
<i class="tool grey-text mode_edit material-icons">mode_edit</i>
</a>
<a href="{{ path('delete_ignore_origin_rule', {id: ignore_origin_rule.id}) }}" title="{{ 'config.form_rules.delete_rule_label'|trans }}">
<i class="tool grey-text delete material-icons">delete</i>
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}

{{ form_start(form.new_ignore_origin_rule) }}
{{ form_errors(form.new_ignore_origin_rule) }}

<div class="row">
<div class="input-field col s12">
{{ form_label(form.new_ignore_origin_rule.rule) }}
{{ form_errors(form.new_ignore_origin_rule.rule) }}
{{ form_widget(form.new_ignore_origin_rule.rule) }}
</div>
</div>

{{ form_widget(form.new_ignore_origin_rule.save, {'attr': {'class': 'btn waves-effect waves-light'}}) }}
{{ form_rest(form.new_ignore_origin_rule) }}
</form>
</div>

<div id="set7" class="col s12">
<div class="row">
<h5>{{ 'config.reset.title'|trans }}</h5>
<p>{{ 'config.reset.description'|trans }}</p>
Expand Down

0 comments on commit 1751fbb

Please sign in to comment.