<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller;
use Exception;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\Asset\Document;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Asset\Video;
use Pimcore\Model\DataObject\DownloadAsset;
use Pimcore\Model\DataObject\DownloadCategory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DownloadsController extends AbstractController
{
/**
* @param Request $request
* @param PaginatorInterface $paginator
*
* @return Response
*
* @throws Exception
*/
public function downloadsAction(Request $request, PaginatorInterface $paginator): Response
{
$filters = [];
$downloadAssetsList = new DownloadAsset\Listing();
$downloadAssetsList->addConditionParam('asset__id > 0 OR localizedAsset__Id > 0');
$allFilterFileTypes = $this->getFileTypes($downloadAssetsList->load());
$customSorting = $this->getDocumentEditable('checkbox', 'useCustomSorting')->getData();
$categoryFilters = $this->getDocumentEditable('relations', 'filterCategory')->getData();
$downloadCategoryList = $categoryFilters ?: new DownloadCategory\Listing();
if ($categoryFilters) {
$categoryFilterCondition = [];
foreach ($categoryFilters as $categoryFilter) {
$categoryFilterCondition[] = 'FIND_IN_SET(' . intval($categoryFilter->getId()) . ', category)';
}
$downloadAssetsList->addConditionParam(implode(' OR ', $categoryFilterCondition));
}
if ($categoryIds = $request->get('categories')) {
$categoryIds = array_filter($categoryIds);
if (!empty($categoryIds)) {
$categoryCondition = [];
foreach ($categoryIds as $category) {
$categoryCondition[] = 'FIND_IN_SET(' . intval($category) . ', category)';
}
$downloadAssetsList->addConditionParam(implode(' OR ', $categoryCondition));
}
foreach ($downloadCategoryList as $downloadCategory) {
if (in_array($downloadCategory->getId(), $categoryIds)) {
$filters[] = [
'name' => 'categories[]',
'value' => $downloadCategory->getId(),
'label' => $downloadCategory->getName(),
];
}
}
}
if ($fileTypesIds = $request->get('fileType')) {
$fileTypesIds = array_filter($fileTypesIds);
if (!empty($fileTypesIds)) {
$fileTypeCondition = [];
foreach ($fileTypesIds as $fileType) {
$fileTypeCondition[] = "fileType = '" . $fileType . "'";
}
$downloadAssetsList->addConditionParam(implode(' OR ', $fileTypeCondition), []);
foreach ($fileTypesIds as $key => $fileType) {
$filters[] = [
'name' => 'fileType[]',
'value' => $key,
'label' => $fileType,
];
}
}
}
if ($searchQuery = $request->get('search')) {
$downloadAssetsList->addConditionParam('(name LIKE :search OR description LIKE :search)', ['search' => '%' . $searchQuery . '%']);
$filters[] = [
'name' => 'search',
'value' => $searchQuery,
'label' => $searchQuery,
];
}
if ($customSorting) {
$downloadAssetsList->setOrderKey('sorting');
$downloadAssetsList->setOrder('ASC');
}
$paginator = $paginator->paginate($downloadAssetsList->load(), (int)$request->get('page', 1), 8);
$returnArray = [
'downloadAssets' => $paginator,
'downloadCategory' => $downloadCategoryList,
'fileTypeOptions' => $allFilterFileTypes,
'filters' => $filters,
];
if ($request->get('ajax')) {
$template = $this->renderTemplate('download/partials/teaser-grid-result.html.twig', $returnArray);
return $this->json(['success' => true, 'html' => $template->getContent()], 200);
}
return $this->render('download/downloads.html.twig', $returnArray);
}
/**
* @param array<mixed> $downloadAssetsList
*
* @return array<mixed>
*/
public function getFileTypes(array $downloadAssetsList): array
{
$filesList = [];
foreach ($downloadAssetsList as $downloadAsset) {
$asset = $downloadAsset->getAsset();
if ($asset instanceof Video) {
$filesList['video'] = 'video';
} elseif ($asset instanceof Document) {
$filesList['document'] = 'document';
} elseif ($asset instanceof Image) {
$filesList['image'] = 'image';
}
}
return $filesList;
}
}