Skip to content

Commit

Permalink
added demo report
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed May 3, 2024
1 parent 73beaf8 commit a3e7d1e
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## Version 2.4.0

Compatibility: requires minimum Kimai 2.15.0

- Added demo report

## Version 2.3.0

Compatibility: requires minimum Kimai 2.15.0

- unify API access permission check
- Unify API access permission check

## Version 2.2.0

Expand Down
52 changes: 52 additions & 0 deletions Controller/DemoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use App\Utils\PageSetup;
use KimaiPlugin\DemoBundle\Configuration\DemoConfiguration;
use KimaiPlugin\DemoBundle\Form\DemoType;
use KimaiPlugin\DemoBundle\Report\DemoReportForm;
use KimaiPlugin\DemoBundle\Report\DemoReportQuery;
use KimaiPlugin\DemoBundle\Repository\DemoRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
Expand Down Expand Up @@ -79,4 +82,53 @@ public function error(string $code): Response

throw new \Exception('Error 500');
}

#[Route(path: '/report', name: 'demo_report', methods: ['GET', 'POST'])]
public function report(Request $request): Response
{
$dateTimeFactory = $this->getDateTimeFactory();

$values = new DemoReportQuery($dateTimeFactory->getStartOfMonth());

$form = $this->createFormForGetRequest(DemoReportForm::class, $values, [
'timezone' => $dateTimeFactory->getTimezone()->getName(),
]);

$form->submit($request->query->all(), false);

if ($form->isSubmitted()) {
if (!$form->isValid()) {
$values->setMonth($dateTimeFactory->getStartOfMonth());
}
}

if ($values->getMonth() === null) {

Check failure on line 105 in Controller/DemoController.php

View workflow job for this annotation

GitHub Actions / Linting - PHP 8.1

Strict comparison using === between DateTimeInterface and null will always evaluate to false.

Check failure on line 105 in Controller/DemoController.php

View workflow job for this annotation

GitHub Actions / Linting - PHP 8.3

Strict comparison using === between DateTimeInterface and null will always evaluate to false.
$values->setMonth($dateTimeFactory->getStartOfMonth());
}

/** @var \DateTime $start */
$start = $values->getMonth();
$start->modify('first day of 00:00:00');

$end = clone $start;
$end->modify('last day of 23:59:59');

$previous = clone $start;
$previous->modify('-1 month');

$next = clone $start;
$next->modify('+1 month');

$data = [
'report_title' => 'Demo report',
'form' => $form->createView(),
'current' => $start,
'next' => $next,
'previous' => $previous,
'hasData' => false,
'box_id' => 'demo_box_id',
];

return $this->render('@Demo/report.html.twig', $data);
}
}
41 changes: 41 additions & 0 deletions EventSubscriber/ReportingSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the "DemoBundle" for Kimai.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\DemoBundle\EventSubscriber;

use App\Event\ReportingEvent;
use App\Reporting\Report;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final class ReportingSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly AuthorizationCheckerInterface $security)
{
}

public static function getSubscribedEvents(): array
{
return [
ReportingEvent::class => ['onReporting'],
];
}

public function onReporting(ReportingEvent $event): void
{
$auth = $this->security;

if (!$auth->isGranted('view_reporting')) {
return;
}

$event->addReport(new Report('demo_report', 'demo_report', 'Demo Report', 'fas fa-snowman'));
}
}
42 changes: 42 additions & 0 deletions Report/DemoReportForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the "DemoBundle" for Kimai.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\DemoBundle\Report;

use App\Form\Type\MonthPickerType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractType<DemoReportQuery>
*/
final class DemoReportForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('month', MonthPickerType::class, [
'required' => true,
'label' => false,
'view_timezone' => $options['timezone'],
'model_timezone' => $options['timezone'],
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => DemoReportQuery::class,
'timezone' => date_default_timezone_get(),
'csrf_protection' => false,
'method' => 'GET',
]);
}
}
28 changes: 28 additions & 0 deletions Report/DemoReportQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the "DemoBundle" for Kimai.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\DemoBundle\Report;

final class DemoReportQuery
{
public function __construct(private \DateTimeInterface $month)
{
}

public function getMonth(): \DateTimeInterface
{
return $this->month;
}

public function setMonth(?\DateTime $month): void
{
$this->month = $month;

Check failure on line 26 in Report/DemoReportQuery.php

View workflow job for this annotation

GitHub Actions / Linting - PHP 8.1

Property KimaiPlugin\DemoBundle\Report\DemoReportQuery::$month (DateTimeInterface) does not accept DateTime|null.

Check failure on line 26 in Report/DemoReportQuery.php

View workflow job for this annotation

GitHub Actions / Linting - PHP 8.3

Property KimaiPlugin\DemoBundle\Report\DemoReportQuery::$month (DateTimeInterface) does not accept DateTime|null.
}
}
19 changes: 19 additions & 0 deletions Resources/views/report.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'reporting/layout.html.twig' %}

{% block report_form_layout %}
{{ form_widget(form.month, {'label': false}) }}
{% endblock %}

{% block report %}
{% embed '@theme/embeds/card.html.twig' %}
{% block box_body_class %}{{ box_id }} table-responsive m-0{% endblock %}
{% block box_body %}
{% if not hasData %}
{% from "macros/widgets.html.twig" import nothing_found %}
{{ nothing_found() }}
{% else %}
<p>Put your report data here</p>
{% endif %}
{% endblock %}
{% endembed %}
{% endblock %}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Showcase plugin for Kimai, with demos for many extension points within the core",
"homepage": "https://github.com/Keleo/DemoBundle",
"type": "kimai-plugin",
"version": "2.3.0",
"version": "2.4.0",
"keywords": [
"kimai",
"kimai-plugin"
Expand Down

0 comments on commit a3e7d1e

Please sign in to comment.