Files
sprawozdania/project/src/Twig/AppExtension.php
T
2025-03-29 20:35:54 +01:00

73 lines
2.2 KiB
PHP

<?php
// src/Twig/AppExtension.php
namespace App\Twig;
use App\Entity\Clients;
use App\Entity\Reports;
use App\Helper\MonthHelper;
use App\Services\AddReports;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('phone', [$this, 'formatPhone']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('enum', [$this, 'enumChecker']),
new TwigFunction('is_route_active', [$this, 'getActualRoute']),
new TwigFunction('getMailtoLink', [$this, 'getMailtoLink']),
];
}
public function formatPhone(?int $phoneNumber): string
{
if (!$phoneNumber) {
return "Brak";
}
return number_format($phoneNumber, 0,"."," ");
}
public function enumChecker(mixed $currentEnum, string $needEnumName): bool
{
return $currentEnum->name === $needEnumName;
}
public function getActualRoute(string $value, string $route): string
{
return $value === $route ? 'active' : '';
}
public function getMailtoLink(Reports $reports): string
{
$subject = rawurlencode("Sprawozdanie z wykonania układu nr {$reports->getNumber()}");
$dateBase = (clone $reports->getDateStart())->modify("-1 month");
$template = "email/report.html.twig";
$dateStart = (clone $dateBase)->modify("-".(AddReports::NEXT_REPORT_MONTH-1)." months")->modify("first day of this month");
if ($reports->getNumber() === 1){
$template = "email/firstReport.html.twig";
$dateStart = $reports->getClient()->getDateLegitimacy();
}
$view = sprintf(
file_get_contents($_SERVER['PWD']."/templates/".$template),
MonthHelper::takeDateFormatWithPolishMonths($dateStart),
MonthHelper::takeDateFormatWithPolishMonths($dateBase->modify("last day of this month"))
);
$content = rawurlencode($view);
/** @var Clients $client */
$client = $reports->getClient();
return "mailto:{$client->getStrEmail()}?subject=$subject&body=$content";
}
}