<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\File;
use App\Service\UrlHelper;
class AppExtension extends AbstractExtension
{
public function __construct(EntityManagerInterface $em, UrlHelper $urlHelper )
{
$this->em = $em;
$this->urlHelper = $urlHelper;
}
public function getFilters()
{
return [
new TwigFilter('documentUrl', [$this, 'documentUrl']),
new TwigFilter('privateDocumentUrl', [$this, 'privateDocumentUrl']),
];
}
//finds src and removes all ../ from it
public function documentUrl($record)
{
$doc = $this->em ->getRepository(File::class)->findOneBy(array('itemId' => $record->getId(), 'section' => 'generaldocument', 'type' => 'document'), ['sort' => 'ASC']);
$docUrl = ($doc) ? $this->urlHelper->createDocumentUrl('generaldocument', $record->getId(), $doc->getId(), $doc->getExtension()) : null;
return $docUrl;
}
public function privateDocumentUrl($record)
{
$doc = $this->em ->getRepository(File::class)->findOneBy(array('itemId' => $record->getId(), 'section' => 'privatedocument', 'type' => 'document'), ['sort' => 'ASC']);
$docUrl = ($doc) ? $this->urlHelper->createDocumentUrl('privatedocument', $record->getId(), $doc->getId(), $doc->getExtension()) : null;
return $docUrl;
}
}