Add support of mass action to tag entries

Closes #3118

Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
This commit is contained in:
Kevin Decherf 2021-04-27 23:11:03 +02:00
parent d510dc6c98
commit 08eb190c95
6 changed files with 88 additions and 2 deletions

View file

@ -222,4 +222,10 @@ $(document).ready(() => {
});
});
}
$('form[name="form_mass_action"] input[name="tags"]').on('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
$('form[name="form_mass_action"] button[name="tag"]').trigger('click');
}
});
});

View file

@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
use Wallabag\CoreBundle\Event\EntrySavedEvent;
use Wallabag\CoreBundle\Form\Type\EditEntryType;
@ -30,11 +31,44 @@ class EntryController extends Controller
$em = $this->getDoctrine()->getManager();
$values = $request->request->all();
$tagsToAdd = [];
$tagsToRemove = [];
$action = 'toggle-read';
if (isset($values['toggle-star'])) {
$action = 'toggle-star';
} elseif (isset($values['delete'])) {
$action = 'delete';
} elseif (isset($values['tag'])) {
$action = 'tag';
if (isset($values['tags'])) {
$labels = array_filter(explode(',', $values['tags']),
function ($v) {
$v = trim($v);
return '' !== $v;
});
foreach ($labels as $label) {
$remove = false;
if (0 === strpos($label, '-')) {
$label = substr($label, 1);
$remove = true;
}
$tag = $this->get('wallabag_core.tag_repository')->findOneByLabel($label);
if ($remove) {
if (null !== $tag) {
$tagsToRemove[] = $tag;
}
} else {
if (null === $tag) {
$tag = new Tag();
$tag->setLabel($label);
}
$tagsToAdd[] = $tag;
}
}
}
}
if (isset($values['entry-checkbox'])) {
@ -48,6 +82,13 @@ class EntryController extends Controller
$entry->toggleArchive();
} elseif ('toggle-star' === $action) {
$entry->toggleStar();
} elseif ('tag' === $action) {
foreach ($tagsToAdd as $tag) {
$entry->addTag($tag);
}
foreach ($tagsToRemove as $tag) {
$entry->removeTag($tag);
}
} elseif ('delete' === $action) {
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
$em->remove($entry);

View file

@ -51,6 +51,11 @@
<button class="btn cyan darken-1" type="submit" name="toggle-read" title="{{ 'entry.list.toogle_as_read'|trans }}"><i class="material-icons">done</i></button>
<button class="btn cyan darken-1" type="submit" name="toggle-star" title="{{ 'entry.list.toogle_as_star'|trans }}" ><i class="material-icons">star</i></button>
<button class="btn cyan darken-1" type="submit" name="delete" onclick="return confirm('{{ 'entry.confirm.delete_entries'|trans|escape('js') }}')" title="{{ 'entry.list.delete'|trans }}"><i class="material-icons">delete</i></button>
<button class="btn cyan darken-1" type="submit" name="tag"><i class="material-icons">label</i></button>
</span>
<span class="input-field inline">
<input type="text" name="tags" />
</span>
{% endif %}
</div>

View file

@ -1698,6 +1698,10 @@ class EntryControllerTest extends WallabagCoreTestCase
$entry2->setUrl($this->url);
$this->getEntityManager()->persist($entry2);
$entry3 = new Entry($this->getLoggedInUser());
$entry3->setUrl($this->url);
$this->getEntityManager()->persist($entry3);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
@ -1749,6 +1753,36 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertSame(1, $res->isStarred());
// Mass actions : tag
$client->request('POST', '/mass', [
'tag' => '',
'tags' => 'foo',
'entry-checkbox' => $entries,
]);
$this->assertSame(302, $client->getResponse()->getStatusCode());
$res = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->find($entry1->getId());
$this->assertContains('foo', $res->getTagsLabel());
$res = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->find($entry2->getId());
$this->assertContains('foo', $res->getTagsLabel());
$res = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->find($entry3->getId());
$this->assertNotContains('foo', $res->getTagsLabel());
// Mass actions : delete
$client->request('POST', '/mass', [
'delete' => '',

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long