<?php
namespace App\Controller;
use App\Entity\Valtozat;
use App\Model\ArchiveAttachment;
use App\Model\ArchiveLegislation;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Tools\RendeletListaParams;
use App\Entity\Wadmin;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Temakor;
use Symfony\Component\HttpFoundation\JsonResponse;
class PJAController extends AbstractController
{
/**
* @Route("/restapi/temakorok", name="temakorlista")
*/
public function temakorok(Request $request)
{
$temas = $this->getDoctrine()->getRepository(Temakor::class)->getRestapiTemakor();
return new JsonResponse($temas);
}
/**
* @Route("/restapi/dokumentumok/lista{token}", defaults={"_format"="xml"}, name="rendeletlista")
*/
public function lista($token, Request $request)
{
$params = new RendeletListaParams();
$rquery = $request->query;
if (!$rquery->has('kod') && !($rquery->has('kibocsato_kod') && $params->setKibocsatoKod($rquery->get('kibocsato_kod')))) {
return $this->render('pja/lista.xml.twig', [
'wcount' => 0,
'wadmin' => array()
]);
}
if ($rquery->has('tipus_kod')) {
$params->setTipusKod($rquery->get('tipus_kod'));
}
if ($rquery->has('include_archive')) {
$params->setIncArchive($rquery->get('include_archive'));
}
if ($rquery->has('include_hatkiv')) {
$params->setIncHatkiv($rquery->get('include_hatkiv'));
}
if ($rquery->has('fromoffset')) {
$params->setFromoffset($rquery->get('fromoffset'));
}
if ($rquery->has('tolimit')) {
$params->setTolimit($rquery->get('tolimit'));
}
if ($rquery->has('evszam')) {
$params->setEvszam($rquery->get('evszam'));
}
if ($rquery->has('sorszam')) {
$params->setSorszam($rquery->get('sorszam'));
}
if ($rquery->has('cim_part')) {
$params->setCimPart($rquery->get('cim_part'));
}
if ($rquery->has('vis')) {
$params->setVis($rquery->get('vis'));
}
if ($rquery->has('kod')) {
$params->setKod($rquery->get('kod'));
}
if (null !== $params->getKod()) {
$wcount = $this->getDoctrine()->getRepository(Wadmin::class)->countOfPubWKodByPja($params);
$wadmin = $this->getDoctrine()->getRepository(Wadmin::class)->getPubWKodByPja($params);
} else {
$wcount = $this->getDoctrine()->getRepository(Wadmin::class)->countOfPublicatedByPja($params);
$wadmin = $this->getDoctrine()->getRepository(Wadmin::class)->getPublicatedByPja($params);
}
$response = $this->render('pja/lista.xml.twig', [
'wcount' => $wcount,
'wadmin' => $wadmin,
]);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
/**
* @Route("/api/v1/njt/hivatkozasok", defaults={"_format"="xml"}, name="hivatkozasok")
*/
public function hivatkozasValidator(Request $request, LoggerInterface $logger)
{
$content = $request->getContent();
if (strlen($content) == 0 || null === $content) {
throw $this->createNotFoundException();
}
$xml = simplexml_load_string($content);
if (false === $xml) {
throw $this->createNotFoundException();
}
// Egyelőre csak WADMINT ellenőriz.
$wids = array();
foreach ($xml->children() as $hiv) {
if (!in_array((string)($hiv['wAdminId']), $wids)) {
$wids[] = (string)($hiv['wAdminId']);
}
}
$result = $this->getDoctrine()->getRepository(Wadmin::class)->checkWadminKeys($wids);
foreach ($xml->children() as $hiv) {
$logger->debug('XML_ATTR: ' . (string)($hiv['wAdminId']));
$hiv->addAttribute('valid', (in_array((string)($hiv['wAdminId']), array_column($result, 'jogszabalyId'))) ? 'true' : 'false');
}
$strxml = $xml->asXML();
$response = new Response($strxml);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
/**
* @Route("/restapi/migrate/download/archive/html/{legislationId}/{archiveId}", name="migrate_archive_html", methods={"GET"})
*/
public function migrateArchiveHtml($legislationId, $archiveId): StreamedResponse
{
/** @var Valtozat $legislation */
$legislation = $this->getDoctrine()->getRepository(Valtozat::class)->findArchiveVersion($legislationId, $archiveId);
if (null === $legislation) {
throw $this->createNotFoundException('Nincs ilyen változat!');
}
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$legislationId . '.html'
);
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'text/html');
$response->headers->set('Content-Disposition', $disposition);
$response->setCallback(function () use ($legislation) {
if (is_resource($legislation->getHtml())) {
fpassthru($legislation->getHtml());
} else {
echo $legislation->getHtml();
}
});
return $response->send();
}
/**
* @Route("/restapi/migrate/download/archive/attachments/list/{legislationId}/{archiveId}", name="migrate_archive_attachment_list", methods={"GET"})
*/
public function migrateArchiveAttachmentsList($legislationId, $archiveId): JsonResponse
{
/** @var Valtozat $legislation */
$legislation = $this->getDoctrine()->getRepository(Valtozat::class)->findArchiveVersionAttachments($legislationId, $archiveId);
if (null === $legislation) {
throw $this->createNotFoundException('Legislation not found!');
}
$result = new ArchiveLegislation();
$result->archiveId = $archiveId;
$result->legislationId = $legislationId;
foreach ($legislation->getDocuments() as $document) {
$attachment = new ArchiveAttachment();
$attachment->title = $document->getTitle();
$attachment->filename = $document->getFilename();
$attachment->filesize = $document->getFilesize();
$attachment->filehash = $document->getFilehash();
$attachment->downloadID = implode('_', array($attachment->filehash, $document->getId()));
$result->attachments[] = $attachment;
}
return new JsonResponse($result);
}
}