Added timesheet-calendar view (#236)

This commit is contained in:
Kevin Papst
2018-07-27 20:19:56 +02:00
committed by GitHub
parent 5ccc1c991b
commit e6f8bc8ae2
40 changed files with 1286 additions and 49 deletions

66
src/Calendar/Config.php Normal file
View File

@@ -0,0 +1,66 @@
<?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\Calendar;
class Config
{
/**
* @var array
*/
protected $config = [];
/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* @return array
*/
public function getBusinessDays()
{
return $this->config['businessHours']['days'];
}
/**
* @return string
*/
public function getBusinessTimeBegin()
{
return $this->config['businessHours']['begin'];
}
/**
* @return string
*/
public function getBusinessTimeEnd()
{
return $this->config['businessHours']['end'];
}
/**
* @return int
*/
public function getDayLimit()
{
return $this->config['day_limit'];
}
/**
* @return bool
*/
public function isShowWeekNumbers()
{
return $this->config['week_numbers'];
}
}

48
src/Calendar/Google.php Normal file
View File

@@ -0,0 +1,48 @@
<?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\Calendar;
class Google
{
/**
* @var Source[]
*/
protected $sources = [];
/**
* @var string
*/
protected $apiKey = null;
/**
* @param string $apiKey
* @param Source[] $sources
*/
public function __construct($apiKey, $sources = [])
{
$this->apiKey = $apiKey;
$this->sources = $sources;
}
/**
* @return Source[]
*/
public function getSources()
{
return $this->sources;
}
/**
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
}
}

59
src/Calendar/Service.php Normal file
View File

@@ -0,0 +1,59 @@
<?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\Calendar;
class Service
{
/**
* @var array
*/
protected $config;
/**
* Service constructor.
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* @return Config
*/
public function getConfig()
{
return new Config($this->config);
}
/**
* @return Google
*/
public function getGoogle()
{
$apiKey = $this->config['google']['api_key'] ?? null;
$sources = [];
if (isset($this->config['google']['sources'])) {
foreach ($this->config['google']['sources'] as $name => $config) {
$source = new Source();
$source
->setColor($config['color'])
->setUri($config['id'])
->setId($name)
;
$sources[] = $source;
}
}
return new Google($apiKey, $sources);
}
}

83
src/Calendar/Source.php Normal file
View File

@@ -0,0 +1,83 @@
<?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\Calendar;
class Source
{
/**
* @var string
*/
protected $id;
/**
* @var string
*/
protected $uri;
/**
* @var string
*/
protected $color;
/**
* @return string
*/
public function getId(): ?string
{
return $this->id;
}
/**
* @param string $id
* @return Source
*/
public function setId(string $id)
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getUri(): ?string
{
return $this->uri;
}
/**
* @param string $uri
* @return Source
*/
public function setUri(string $uri)
{
$this->uri = $uri;
return $this;
}
/**
* @return string
*/
public function getColor(): ?string
{
return $this->color;
}
/**
* @param string $color
* @return Source
*/
public function setColor(string $color)
{
$this->color = $color;
return $this;
}
}

View 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;
}
}

View File

@@ -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');
}
/**

View File

@@ -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);

View File

@@ -70,7 +70,7 @@ class CustomerFixtures extends Fixture
/**
* @param Generator $faker
* @param boolean $visible
* @param bool $visible
* @return Customer
*/
private function createCustomer(Generator $faker, $visible)
@@ -93,7 +93,7 @@ class CustomerFixtures extends Fixture
/**
* @param Generator $faker
* @param Customer $customer
* @param boolean $visible
* @param bool $visible
* @return Project
*/
private function createProject(Generator $faker, Customer $customer, $visible)
@@ -114,7 +114,7 @@ class CustomerFixtures extends Fixture
/**
* @param Generator $faker
* @param Project $project
* @param boolean $visible
* @param bool $visible
* @return Activity
*/
private function createActivity(Generator $faker, Project $project, $visible)

View File

@@ -40,10 +40,10 @@ class TimesheetFixtures extends Fixture implements DependentFixtureInterface
*/
public function getDependencies()
{
return array(
return [
UserFixtures::class,
CustomerFixtures::class,
);
];
}
/**

View File

@@ -118,7 +118,7 @@ class UserFixtures extends Fixture
$passwordEncoder = $this->encoder;
$faker = Factory::create();
for($i = 1; $i <= self::AMOUNT_EXTRA_USER; $i++) {
for ($i = 1; $i <= self::AMOUNT_EXTRA_USER; $i++) {
$user = new User();
$user
->setAlias($faker->name)

View File

@@ -34,6 +34,7 @@ class AppExtension extends Extension implements PrependExtensionInterface
}
$container->setParameter('kimai.languages', $config['languages']);
$container->setParameter('kimai.calendar', $config['calendar']);
$this->createTimesheetParameter($config, $container);
$this->createInvoiceParameter($config, $container);

View File

@@ -118,6 +118,40 @@ class Configuration implements ConfigurationInterface
->end()
->end()
->end()
->arrayNode('calendar')
->children()
->booleanNode('week_numbers')->defaultTrue()->end()
->integerNode('day_limit')->defaultValue(4)->end()
->arrayNode('businessHours')
->addDefaultsIfNotSet()
->children()
->arrayNode('days')
->requiresAtLeastOneElement()
->prototype('integer')->end()
->defaultValue([1, 2, 3, 4, 5])
->end()
->scalarNode('begin')->defaultValue('08:00')->end()
->scalarNode('end')->defaultValue('20:00')->end()
->end()
->end()
->arrayNode('google')
->addDefaultsIfNotSet()
->children()
->scalarNode('api_key')->defaultNull()->end()
->arrayNode('sources')
->requiresAtLeastOneElement()
->useAttributeAsKey('key')
->arrayPrototype()
->children()
->scalarNode('id')->isRequired()->end()
->scalarNode('color')->defaultValue('#ccc')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end();

View File

@@ -55,29 +55,30 @@ class Extensions extends \Twig_Extension
* @var string[]
*/
protected static $icons = [
'user' => 'fas fa-user',
'customer' => 'fas fa-users',
'project' => 'fas fa-project-diagram',
'activity' => 'fas fa-tasks',
'admin' => 'fas fa-wrench',
'invoice' => 'fas fa-file-invoice',
'timesheet' => 'far fa-clock',
'calendar' => 'far fa-calendar-alt',
'customer' => 'fas fa-users',
'create' => 'far fa-plus-square',
'dashboard' => 'fas fa-tachometer-alt',
'logout' => 'fas fa-sign-out-alt',
'trash' => 'far fa-trash-alt',
'delete' => 'far fa-trash-alt',
'repeat' => 'fas fa-redo-alt',
'edit' => 'far fa-edit',
'manual' => 'fas fa-book',
'filter' => 'fas fa-filter',
'help' => 'far fa-question-circle',
'invoice' => 'fas fa-file-invoice',
'list' => 'fas fa-list',
'logout' => 'fas fa-sign-out-alt',
'manual' => 'fas fa-book',
'print' => 'fas fa-print',
'project' => 'fas fa-project-diagram',
'repeat' => 'fas fa-redo-alt',
'start' => 'fas fa-play-circle',
'start-small' => 'fas fa-play-circle',
'stop' => 'fas fa-stop',
'stop-small' => 'far fa-stop-circle',
'filter' => 'fas fa-filter',
'create' => 'far fa-plus-square',
'list' => 'fas fa-list',
'print' => 'fas fa-print',
'timesheet' => 'far fa-clock',
'trash' => 'far fa-trash-alt',
'user' => 'fas fa-user',
'visibility' => 'far fa-eye',
];