<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Knp\Component\Pager\PaginatorInterface;
class ProjekteController extends FrontendController {
// -----------------------------------
// GET DETAIL
// -----------------------------------
/**
* @Route(
* "/{path}/{title}_p{id}",
* name="projekt",
* requirements={
* "path"=".*",
* "title"="[\w-]+",
* "id"="\d+"
* }
* )
*/
public function getDetail(Request $request, TranslatorInterface $translator): Response
{
$documentId = $this->document->getId();
// preview
$preview = $request->get('preview');
// parameters
$routeParameters = $request->attributes->get('_route_params');
$id = $routeParameters['id'];
//$title = $routeParameters['title'];
// news object
$detail = DataObject\Projekt::getById($id);
// view
if ($detail && ($preview == 'true' || $detail->getPublished())) {
$title = HelperController::toUrl($detail->getMaintitle());
$detailLink = $this->generateUrl('projekt', [
'id' => $id,
'title' => $title,
'path' => $translator->trans('projekt-url-path')
]);
return $this->render('projekte/detail.html.twig', [
'projektDetail' => $detail,
'projektDetailLink' => $detailLink,
'projektTeaserList' => $this->getList($request),
'documentId' => $documentId
]);
} else {
throw new NotFoundHttpException("Projekte object not found");
}
}
// -----------------------------------
// GET LIST
// -----------------------------------
public static function getList(Request $request): DataObject\Projekt\Listing
{
$list = new DataObject\Projekt\Listing();
$list->setOrderKey("date");
$list->setOrder("desc");
//$list->setLimit(2);
if ($request->get('filter')) {
$list->setCondition('category__id LIKE ?', [$request->get('filter')]);
}
$limit = NULL;
if ($limit) {
$list->setLimit($limit);
}
return $list;
}
// --------------------------------
// GET LIST AJAX
// --------------------------------
/**
* @Route(
* "/{_locale}/projekt-list",
* name="projekt-list"
* )
*/
public function listAjax(Request $request, PaginatorInterface $paginator): Response
{
$list = new DataObject\Projekt\Listing();
$list->setOrderKey('date');
$list->setOrder('desc');
//$list->load();
if ($request->get('filter')) {
$list->setCondition('category__id LIKE ?', [$request->get('filter')]);
}
// paginator
$paginator = $paginator->paginate($list, $request->get('page', 1), 2);
return $this->render('projekte/projekt-list-ajax.html.twig', [
'projektList' => $paginator,
'paginationVariables' => $paginator->getPaginationData()
]);
}
// -----------------------------------
// GET FILTER
// -----------------------------------
public static function getFilter(): array
{
$list = new DataObject\Projekt\Listing();
$categories = [];
foreach ($list as $detail) {
foreach ($detail->getCategory() as $category) {
$categories[$category->getId()] = $category->getName();
}
}
return $categories;
}
/**
* @Route("/api/projekt-list-api", name="projekt-api-list")
* @param Request $request
* @param TranslatorInterface $translator
* @return JsonResponse
*/
public function projektApiAction(Request $request, TranslatorInterface $translator): JsonResponse
{
// set empty json array
$json = [];
// get a list of news objects and order them by date
$apiList = new DataObject\Projekt\Listing();
$apiList->setOrderKey('date');
$apiList->setOrder('DESC');
$apiList->setOffset($request->get('offset', 0));
$apiList->setLimit($request->get('limit', 500));
foreach ($apiList as $item) {
$categories = [];
foreach ($item->getCategory() as $category) {
$categories[] = $category->getName();
}
$title = HelperController::toUrl($item->getMaintitle());
$detailLink = $this->generateUrl('projekt', [
'id' => $item->getId(),
'title' => $title,
'path' => $translator->trans('projekt-url-path')
]);
$subjectVal = ["ö","-","/"," ","ä","&","ü"];
$toReplace = ["oe","","","","ae","","ue"];
$formatedCategories = ["project-teaser"];
foreach ($categories as $category ) {
$formatedCategories[] = strtolower(str_replace($subjectVal, $toReplace, $category));
}
$json[] = [
'id' => $item->getId(),
'categories' => $categories,
'formatedCategories' => $formatedCategories,
'key' => $item->getKey(),
'maintitle' => $item->getMaintitle(),
'image' => $item->getImage()->getThumbnail('ProjectListTeaser')->getPath(),
'detailLink' => $detailLink,
//'objectSelect' => $item->getObjektSelect(),
];
}
return $this->json($json);
}
}