added tags for timesheets (#604)

This commit is contained in:
Mathias
2019-05-12 01:40:04 +02:00
committed by Kevin Papst
parent f31118292c
commit e29e183e84
84 changed files with 2132 additions and 158 deletions

115
src/API/TagController.php Normal file
View File

@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
/*
* 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\API;
use App\Repository\TagRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface;
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\Response;
/**
* @RouteResource("Tag")
*/
class TagController extends BaseApiController
{
/**
* @var TagRepository
*/
protected $repository;
/**
* @var ViewHandlerInterface
*/
protected $viewHandler;
/**
* @param ViewHandlerInterface $viewHandler
* @param TagRepository $repository
*/
public function __construct(ViewHandlerInterface $viewHandler, TagRepository $repository)
{
$this->viewHandler = $viewHandler;
$this->repository = $repository;
}
/**
* @SWG\Response(
* response=200,
* description="Returns the collection of all existing tags as string array",
* @SWG\Schema(
* type="array",
* @SWG\Items(type="string")
* )
* )
*
* @Rest\QueryParam(name="name", requirements="[a-zA-Z0-9 -\.]+", strict=true, nullable=true, description="Search term to filter tag list")
*
* @return Response
*/
public function cgetAction(ParamFetcherInterface $paramFetcher)
{
$filter = $paramFetcher->get('name');
$data = $this->repository->findAllTagNames($filter);
if (null === $data) {
$data = [];
}
$view = new View($data, 200);
$view->getContext()->setGroups(['Default', 'Collection']);
return $this->viewHandler->handle($view);
}
/**
* Delete an existing tag
*
* @SWG\Delete(
* @SWG\Response(
* response=204,
* description="Delete one tag"
* ),
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="Tag ID to delete",
* required=true,
* )
*
* @Security("is_granted('delete_tag')")
*
* @param int $id
* @return Response
*/
public function deleteAction($id)
{
$tag = $this->repository->find($id);
if (null === $tag) {
throw new NotFoundException();
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($tag);
$entityManager->flush();
$view = new View(null, Response::HTTP_NO_CONTENT);
return $this->viewHandler->handle($view);
}
}

View File

@@ -15,8 +15,10 @@ use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Form\TimesheetEditForm;
use App\Repository\Query\TimesheetQuery;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Timesheet\UserDateTimeFactory;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface;
@@ -49,24 +51,29 @@ class TimesheetController extends BaseApiController
* @var TimesheetConfiguration
*/
protected $configuration;
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
/**
* @var TagRepository
*/
protected $tagRepository;
/**
* @param ViewHandlerInterface $viewHandler
* @param TimesheetRepository $repository
* @param UserDateTimeFactory $dateTime
* @param TimesheetConfiguration $configuration
* @param TagRepository $tagRepository
*/
public function __construct(ViewHandlerInterface $viewHandler, TimesheetRepository $repository, UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
public function __construct(ViewHandlerInterface $viewHandler, TimesheetRepository $repository, UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration, TagRepository $tagRepository)
{
$this->viewHandler = $viewHandler;
$this->repository = $repository;
$this->configuration = $configuration;
$this->dateTime = $dateTime;
$this->tagRepository = $tagRepository;
}
/**
@@ -87,6 +94,7 @@ class TimesheetController extends BaseApiController
* @Rest\QueryParam(name="activity", requirements="\d+", strict=true, nullable=true, description="Activity ID to filter timesheets")
* @Rest\QueryParam(name="page", requirements="\d+", strict=true, nullable=true, description="The page to display, renders a 404 if not found (default: 1)")
* @Rest\QueryParam(name="size", requirements="\d+", strict=true, nullable=true, description="The amount of entries for each page (default: 25)")
* @Rest\QueryParam(name="tags", requirements="[a-zA-Z0-9 -,]+", strict=true, nullable=true, description="The name of tags which are in the datasets")
* @Rest\QueryParam(name="orderBy", requirements="id|begin|end|rate", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, begin, end, rate (default: begin)")
* @Rest\QueryParam(name="order", requirements="ASC|DESC", strict=true, nullable=true, description="The result order. Allowed values: ASC, DESC (default: DESC)")
* @Rest\QueryParam(name="begin", requirements=@Constraints\DateTime, strict=true, nullable=true, description="Only records after this date will be included (format: ISO 8601)")
@@ -133,6 +141,13 @@ class TimesheetController extends BaseApiController
$query->setPageSize($size);
}
if (null !== ($tags = $paramFetcher->get('tags'))) {
$ids = $this->tagRepository->findIdsByTagNameList($tags);
if ($ids !== null && sizeof($ids) > 0) {
$query->setTags(new ArrayCollection($ids));
}
}
if (null !== ($order = $paramFetcher->get('order'))) {
$query->setOrder($order);
}