Added timesheet-calendar view (#236)
This commit is contained in:
130
src/Controller/CalendarController.php
Normal file
130
src/Controller/CalendarController.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?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\Controller;
|
||||
|
||||
use App\Calendar\Service;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Repository\Query\TimesheetQuery;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Controller used to display calendars.
|
||||
*
|
||||
* @Route("/calendar")
|
||||
* @Security("is_granted('ROLE_USER')")
|
||||
*/
|
||||
class CalendarController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @var Service
|
||||
*/
|
||||
protected $calendar;
|
||||
|
||||
/**
|
||||
* @param Service $calendar
|
||||
*/
|
||||
public function __construct(Service $calendar)
|
||||
{
|
||||
$this->calendar = $calendar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/", name="calendar")
|
||||
* @Method("GET")
|
||||
* @Cache(smaxage="10")
|
||||
*/
|
||||
public function userCalendar()
|
||||
{
|
||||
return $this->render('calendar/user.html.twig', [
|
||||
'config' => $this->calendar->getConfig(),
|
||||
'google' => $this->calendar->getGoogle()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/user", name="calendar_entries")
|
||||
* @Method("GET")
|
||||
* @Cache(smaxage="10")
|
||||
*/
|
||||
public function calendarEntries(Request $request)
|
||||
{
|
||||
$start = $request->get('start');
|
||||
$end = $request->get('end');
|
||||
|
||||
$start = \DateTime::createFromFormat('Y-m-d', $start);
|
||||
if ($start === false) {
|
||||
$start = new \DateTime('first day of this month');
|
||||
}
|
||||
$start->setTime(0, 0, 0);
|
||||
|
||||
$end = \DateTime::createFromFormat('Y-m-d', $end);
|
||||
if ($end === false) {
|
||||
$end = clone $start;
|
||||
$end = $end->modify('last day of this month');
|
||||
}
|
||||
$end->setTime(23, 59, 59);
|
||||
|
||||
$query = new TimesheetQuery();
|
||||
$query
|
||||
->setBegin($start)
|
||||
->setUser($this->getUser())
|
||||
->setState(TimesheetQuery::STATE_ALL)
|
||||
->setResultType(TimesheetQuery::RESULT_TYPE_QUERYBUILDER)
|
||||
;
|
||||
|
||||
// running entries should only occur for the current month, but they won't
|
||||
// be found if we add the end to the query
|
||||
if ((new \DateTime())->getTimestamp() > $end->getTimestamp()) {
|
||||
$query->setEnd($end);
|
||||
}
|
||||
|
||||
$repository = $this->getDoctrine()->getRepository(Timesheet::class);
|
||||
|
||||
/* @var $entries Timesheet[] */
|
||||
$entries = $repository->findByQuery($query)->getQuery()->execute();
|
||||
$result = [];
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$result[] = $this->getTimesheetEntryForCalendar($entry);
|
||||
}
|
||||
|
||||
return $this->json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $entry
|
||||
* @return array
|
||||
*/
|
||||
protected function getTimesheetEntryForCalendar(Timesheet $entry)
|
||||
{
|
||||
$result = [
|
||||
'id' => $entry->getId(),
|
||||
'start' => $entry->getBegin(),
|
||||
'title' => $entry->getActivity()->getName(),
|
||||
'description' => $entry->getDescription(),
|
||||
'customer' => $entry->getActivity()->getProject()->getCustomer()->getName(),
|
||||
'project' => $entry->getActivity()->getProject()->getName(),
|
||||
'activity' => $entry->getActivity()->getName(),
|
||||
];
|
||||
|
||||
if (null === $entry->getEnd()) {
|
||||
$result['borderColor'] = '#f39c12';
|
||||
$result['backgroundColor'] = '#f39c12';
|
||||
} else {
|
||||
$result['end'] = $entry->getEnd() ?? new \DateTime();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Controller used to manage timesheet contents in the public part of the site.
|
||||
* Controller used to manage timesheets.
|
||||
*
|
||||
* @Route("/timesheet")
|
||||
* @Security("is_granted('ROLE_USER')")
|
||||
@@ -32,7 +32,6 @@ class TimesheetController extends AbstractController
|
||||
use TimesheetControllerTrait;
|
||||
|
||||
/**
|
||||
* TimesheetController constructor.
|
||||
* @param bool $durationOnly
|
||||
*/
|
||||
public function __construct(bool $durationOnly)
|
||||
@@ -138,7 +137,11 @@ class TimesheetController extends AbstractController
|
||||
*/
|
||||
public function editAction(Timesheet $entry, Request $request)
|
||||
{
|
||||
return $this->edit($entry, $request, 'timesheet_paginated', 'timesheet/edit.html.twig');
|
||||
if (null !== $request->get('page')) {
|
||||
return $this->edit($entry, $request, 'timesheet_paginated', 'timesheet/edit.html.twig');
|
||||
}
|
||||
|
||||
return $this->edit($entry, $request, 'timesheet', 'timesheet/edit.html.twig');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -121,6 +121,40 @@ trait TimesheetControllerTrait
|
||||
$entry->setUser($this->getUser());
|
||||
$entry->setBegin(new \DateTime());
|
||||
|
||||
$start = $request->get('begin');
|
||||
if ($start !== null) {
|
||||
$start = \DateTime::createFromFormat('Y-m-d', $start);
|
||||
if ($start !== false) {
|
||||
$start->setTime(10, 0, 0); // TODO make me configurable
|
||||
$entry->setBegin($start);
|
||||
}
|
||||
}
|
||||
|
||||
$end = $request->get('end');
|
||||
if ($end !== null) {
|
||||
$end = \DateTime::createFromFormat('Y-m-d', $end);
|
||||
if ($end !== false) {
|
||||
$end->setTime(18, 0, 0); // TODO make me configurable
|
||||
$entry->setEnd($end);
|
||||
}
|
||||
}
|
||||
|
||||
$from = $request->get('from');
|
||||
if ($from !== null) {
|
||||
$from = new \DateTime($from);
|
||||
if ($from !== false) {
|
||||
$entry->setBegin($from);
|
||||
}
|
||||
}
|
||||
|
||||
$to = $request->get('to');
|
||||
if ($to !== null) {
|
||||
$to = new \DateTime($to);
|
||||
if ($to !== false) {
|
||||
$entry->setEnd($to);
|
||||
}
|
||||
}
|
||||
|
||||
$createForm = $this->getCreateForm($entry);
|
||||
$createForm->handleRequest($request);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user