<?php
namespace App\Package\Openform\Front\Controller;
use Gedmo\Sluggable\Util\Urlizer;
use App\Package\Openform\Entity\Page;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use App\Package\Openform\Front\Controller\BaseController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class PageController extends BaseController
{
private $zip_path = '';
private $em;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$entity = $this->em->getRepository(Page::class)->getVisibleBySlug($request->get('slug'), $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
// Przekierowanie
$redirectLink = $entity->getTranslation()[$locale]->getLink();
if (strlen(trim($redirectLink)) > 0) {
$parsedUrl = parse_url($redirectLink);
if (!isset($parsedUrl['host'])) {
$domain = $request->getSchemeAndHttpHost();
return $this->redirect($domain . '/' . ltrim($redirectLink, '/'));
} else {
return $this->redirect($redirectLink);
}
}
// HOME
if ($entity->getIdname() == 'home') {
$routeName = 'index' . ($locale == 'pl' ? '_default' : '');
return $this->redirectToRoute($routeName);
}
if ($entity->getIdname() == 'page_collections') {
return $this->forward('App\Package\Openform\Front\Controller\CollectionsController::indexAction', [
'request' => $request,
'entity' => $entity
]);
}
if ($entity->getIdname() == 'page_sitemap') {
return $this->forward('App\Package\Openform\Front\Controller\SitemapController::indexAction', [
'request' => $request,
'entity' => $entity
]);
}
return $this->render('@openform_front_templates/Page/index.html.twig', [
'entity' => $entity,
// 'langLinks' => $this->generateLangLinksPage($this->em, $entity),
]);
}
/**
* Get file attached to blockPage or blockNews filesets
* @Route("/plik-do-pobrania/{id}/{hash}", name="page_file_download_default", requirements={"id"="\d+"})
* @Route("/{locale}/file-to-download/{id}/{hash}", name="page_file_download", requirements={"locale"="%app.front.locale.rest%", "id"="\d+"})
* @param Request $request
* @param int $id
* @throws NotFoundHttpException
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function downloadFileAction(Request $request, int $id)
{
$file = $this->getDoctrine()->getManager()->getRepository('Openform:FilesetsFile')->find($id);
if (!$file) {
throw new NotFoundHttpException();
}
if (!$file->getStat() || ($file->getLang() != null && $file->getLang() != $request->getLocale())) {
throw new NotFoundHttpException();
}
if ($file->getFilePath() && $file->getParent() && $file->getParent()->getStat()) {
if (is_file($file->getProjectDir() . $file->getFilePath())) {
$title = $file->getTranslation()[$request->getLocale()]->getTitle();
if ($title != '') {
$title = Urlizer::urlize($title);
} else {
$title = Urlizer::urlize($file->getFilePath());
}
if ($title != '') {
$title = $title . '.' . strtolower(pathinfo($file->getFilePath(), PATHINFO_EXTENSION));
} else {
$title = $file->getTitle();
}
$respons = new BinaryFileResponse($file->getProjectDir() . $file->getFilePath());
$respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $title);
return $respons;
} else
throw new NotFoundHttpException();
}
return $this->redirect(
$this->routeLocalizer->generate('index', [], $request->getLocale())
);
}
/**
* User click download button in box
* similar to function UserController:downloadebookAction
* @param Request $request
* @param int $id BookVariant.id
* @throws NotFoundHttpException
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*
* @Route("/{locale}/download-packit/{id}/{hash}", name="page_file_download_packit", requirements={"locale"="%app.front.locale.rest%", "hash"="[a-zA-Z0-9\-_]+", "id"="\d+" } )
* @Route("/pobierz-pakiet/{id}/{hash}", name="page_file_download_packit_default", requirements={ "hash"="[a-zA-Z0-9\-_]+", "id"="\d+" } )
*/
public function downloadeFilePackitAction(Request $request, $id, $hash)
{
$locale = $request->getLocale();
/** @var \App\Package\Openform\Entity\Filesets */
$fileset = $this->getDoctrine()->getManager()->getRepository('Openform:Filesets')->find($id);
if (!$fileset || $fileset->makeHash() !== $hash) {
throw new NotFoundHttpException();
}
if ($fileset->getStat() && isset($fileset->getTranslation()[$locale]) && $fileset->getTranslation()[$locale]->getTitle()) {
$files = $fileset->getVisibleFiles($locale);
$this->setZipPath();
if (count($files)) {
//if many files zippin
if (count($files) > 1) {
$hasFiles = false;
$zip = new \ZipArchive();
$filename = 'zip-packit-files-' . $fileset->getId() . '.zip';
if ($zip->open($this->zip_path . $filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== TRUE) {
throw new NotFoundHttpException();
}
foreach ($files as $file) {
if ($file->getStat() && $file->getFilePath()) {
if (is_file($file->getProjectDir() . $file->getFilePath())) {
$zip->addFile($file->getProjectDir() . $file->getFilePath(), \pathinfo($file->getFilePath(), PATHINFO_BASENAME));
$hasFiles = true;
} else {
throw new NotFoundHttpException();
}
}
}
$zip->close();
if ($hasFiles && is_file($this->zip_path . $filename)) {
$book_title = Urlizer::urlize(trim($fileset->getTranslation()[$locale]->getTitle()));
if ($book_title == '') {
$book_title = $filename;
}
$respons = new BinaryFileResponse($this->zip_path . $filename);
$respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $book_title . '.zip');
return $respons;
} else
throw new NotFoundHttpException();
}
//if one file
else {
foreach ($files as $file) {
if ($file->getStat() && $file->getFilePath()) {
if (is_file($file->getProjectDir() . $file->getFilePath())) {
$respons = new BinaryFileResponse($file->getProjectDir() . $file->getFilePath());
$respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $respons;
} else {
throw new NotFoundHttpException();
}
}
}
}
} else {
throw new NotFoundHttpException();
}
}
throw new NotFoundHttpException();
// return $this->redirect(
// $this->routeLocalizer->generate('index', [], $request->getLocale() )
// );
}
/**
* helper, make folder if not exists and set path to zip folder files to download
* @return void
*/
private function setZipPath(): void
{
$this->zip_path = str_replace('public_html/media', 'media_secured', __MEDIA_DIR__) . '/zip_tmp/';
if (!is_dir($this->zip_path)) {
$f = new Filesystem();
$f->mkdir($this->zip_path);
}
}
}