improved tag auto-completion and added tag management (#1083)

This commit is contained in:
Kevin Papst
2019-09-04 23:47:33 +02:00
committed by GitHub
parent 3ea05cb705
commit d041a3f4f9
25 changed files with 368 additions and 17 deletions

View File

@@ -11,6 +11,9 @@ declare(strict_types=1);
namespace App\API;
use App\Entity\Tag;
use App\Form\API\TagApiEditForm;
use App\Form\TagEditForm;
use App\Repository\TagRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
@@ -19,7 +22,9 @@ use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* @RouteResource("Tag")
@@ -58,7 +63,7 @@ class TagController extends BaseApiController
* )
* )
*
* @Rest\QueryParam(name="name", requirements="[a-zA-Z0-9 -\.]+", strict=true, nullable=true, description="Search term to filter tag list")
* @Rest\QueryParam(name="name", strict=true, nullable=true, description="Search term to filter tag list")
*
* @return Response
*/
@@ -69,7 +74,58 @@ class TagController extends BaseApiController
$data = $this->repository->findAllTagNames($filter);
$view = new View($data, 200);
$view->getContext()->setGroups(['Default', 'Collection']);
$view->getContext()->setGroups(['Default', 'Collection', 'Tag']);
return $this->viewHandler->handle($view);
}
/**
* Creates a new tag
*
* @SWG\Post(
* description="Creates a new tag and returns it afterwards",
* @SWG\Response(
* response=200,
* description="Returns the new created tag",
* @SWG\Schema(ref="#/definitions/TagEntity"),
* )
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/TagEditForm")
* )
*
* @param Request $request
* @return Response
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function postAction(Request $request)
{
if (!$this->isGranted('manage_tag')) {
throw new AccessDeniedHttpException('User cannot create tags');
}
$tag = new Tag();
$form = $this->createForm(TagApiEditForm::class, $tag);
$form->submit($request->request->all());
if ($form->isValid()) {
$this->repository->saveTag($tag);
$view = new View($tag, 200);
$view->getContext()->setGroups(['Default', 'Entity', 'Tag']);
return $this->viewHandler->handle($view);
}
$view = new View($form);
$view->getContext()->setGroups(['Default', 'Entity', 'Tag']);
return $this->viewHandler->handle($view);
}

View File

@@ -266,6 +266,11 @@ class SystemConfigurationController extends AbstractController
->setLabel('theme.markdown_content')
->setType(CheckboxType::class)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('theme.autocomplete_chars')
->setLabel('theme.autocomplete_chars')
->setType(IntegerType::class)
->setTranslationDomain('system-configuration'),
// FIXME should that be configurable per user?
/*
(new Configuration())

View File

@@ -9,9 +9,12 @@
namespace App\Controller;
use App\Entity\Tag;
use App\Form\TagEditForm;
use App\Form\Toolbar\TagToolbarForm;
use App\Repository\Query\TagQuery;
use App\Repository\TagRepository;
use Doctrine\ORM\ORMException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -51,6 +54,68 @@ class TagController extends AbstractController
]);
}
/**
* @Route(path="/{id}/edit", name="tags_edit", methods={"GET", "POST"})
* @Security("is_granted('manage_tag')")
*/
public function editAction(Tag $tag, TagRepository $repository, Request $request)
{
$editForm = $this->createForm(TagEditForm::class, $tag, [
'action' => $this->generateUrl('tags_edit', ['id' => $tag->getId()]),
'method' => 'POST',
]);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
try {
$repository->saveTag($tag);
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('tags');
} catch (ORMException $ex) {
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
}
}
return $this->render('tags/edit.html.twig', [
'tag' => $tag,
'form' => $editForm->createView()
]);
}
/**
* @Route(path="/create", name="tags_create", methods={"GET", "POST"})
* @Security("is_granted('manage_tag')")
*/
public function createAction(TagRepository $repository, Request $request)
{
$tag = new Tag();
$editForm = $this->createForm(TagEditForm::class, $tag, [
'action' => $this->generateUrl('tags_create'),
'method' => 'POST',
]);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
try {
$repository->saveTag($tag);
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('tags');
} catch (ORMException $ex) {
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
}
}
return $this->render('tags/edit.html.twig', [
'tag' => $tag,
'form' => $editForm->createView()
]);
}
/**
* @param TagQuery $query
* @return \Symfony\Component\Form\FormInterface

View File

@@ -346,6 +346,9 @@ class Configuration implements ConfigurationInterface
->end()
->end()
->end()
->integerNode('autocomplete_chars')
->defaultValue(3)
->end()
->end()
;

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\API;
use App\Form\TagEditForm;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TagApiEditForm extends TagEditForm
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults([
'csrf_protection' => false,
]);
}
}

50
src/Form/TagEditForm.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form;
use App\Entity\Tag;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TagEditForm extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'label' => 'label.name',
'attr' => [
'autofocus' => 'autofocus'
],
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Tag::class,
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'tags_edit',
'attr' => [
'data-form-event' => 'kimai.tagUpdate'
],
]);
}
}

View File

@@ -18,6 +18,18 @@ use Pagerfanta\Pagerfanta;
class TagRepository extends EntityRepository
{
/**
* @param Tag $tag
* @throws ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function saveTag(Tag $tag)
{
$entityManager = $this->getEntityManager();
$entityManager->persist($tag);
$entityManager->flush();
}
/**
* @param Tag $tag
* @throws ORMException