src/Controller/DownloadsController.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller;
  7. use Exception;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Pimcore\Model\Asset\Document;
  10. use Pimcore\Model\Asset\Image;
  11. use Pimcore\Model\Asset\Video;
  12. use Pimcore\Model\DataObject\DownloadAsset;
  13. use Pimcore\Model\DataObject\DownloadCategory;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class DownloadsController extends AbstractController
  17. {
  18.     /**
  19.      * @param Request $request
  20.      * @param PaginatorInterface $paginator
  21.      *
  22.      * @return Response
  23.      *
  24.      * @throws Exception
  25.      */
  26.     public function downloadsAction(Request $requestPaginatorInterface $paginator): Response
  27.     {
  28.         $filters = [];
  29.         $downloadAssetsList = new DownloadAsset\Listing();
  30.         $downloadAssetsList->addConditionParam('asset__id > 0 OR localizedAsset__Id > 0');
  31.         $allFilterFileTypes $this->getFileTypes($downloadAssetsList->load());
  32.         $customSorting $this->getDocumentEditable('checkbox''useCustomSorting')->getData();
  33.         $categoryFilters $this->getDocumentEditable('relations''filterCategory')->getData();
  34.         $downloadCategoryList $categoryFilters ?: new DownloadCategory\Listing();
  35.         if ($categoryFilters) {
  36.             $categoryFilterCondition = [];
  37.             foreach ($categoryFilters as $categoryFilter) {
  38.                 $categoryFilterCondition[] = 'FIND_IN_SET(' intval($categoryFilter->getId()) . ', category)';
  39.             }
  40.             $downloadAssetsList->addConditionParam(implode(' OR '$categoryFilterCondition));
  41.         }
  42.         if ($categoryIds $request->get('categories')) {
  43.             $categoryIds array_filter($categoryIds);
  44.             if (!empty($categoryIds)) {
  45.                 $categoryCondition = [];
  46.                 foreach ($categoryIds as $category) {
  47.                     $categoryCondition[] = 'FIND_IN_SET(' intval($category) . ', category)';
  48.                 }
  49.                 $downloadAssetsList->addConditionParam(implode(' OR '$categoryCondition));
  50.             }
  51.             foreach ($downloadCategoryList as $downloadCategory) {
  52.                 if (in_array($downloadCategory->getId(), $categoryIds)) {
  53.                     $filters[] = [
  54.                         'name' => 'categories[]',
  55.                         'value' => $downloadCategory->getId(),
  56.                         'label' => $downloadCategory->getName(),
  57.                     ];
  58.                 }
  59.             }
  60.         }
  61.         if ($fileTypesIds $request->get('fileType')) {
  62.             $fileTypesIds array_filter($fileTypesIds);
  63.             if (!empty($fileTypesIds)) {
  64.                 $fileTypeCondition = [];
  65.                 foreach ($fileTypesIds as $fileType) {
  66.                     $fileTypeCondition[] = "fileType = '" $fileType "'";
  67.                 }
  68.                 $downloadAssetsList->addConditionParam(implode(' OR '$fileTypeCondition), []);
  69.                 foreach ($fileTypesIds as $key => $fileType) {
  70.                     $filters[] = [
  71.                         'name' => 'fileType[]',
  72.                         'value' => $key,
  73.                         'label' => $fileType,
  74.                     ];
  75.                 }
  76.             }
  77.         }
  78.         if ($searchQuery $request->get('search')) {
  79.             $downloadAssetsList->addConditionParam('(name LIKE :search OR description LIKE :search)', ['search' => '%' $searchQuery '%']);
  80.             $filters[] = [
  81.                 'name' => 'search',
  82.                 'value' => $searchQuery,
  83.                 'label' => $searchQuery,
  84.             ];
  85.         }
  86.         if ($customSorting) {
  87.             $downloadAssetsList->setOrderKey('sorting');
  88.             $downloadAssetsList->setOrder('ASC');
  89.         }
  90.         $paginator $paginator->paginate($downloadAssetsList->load(), (int)$request->get('page'1), 8);
  91.         $returnArray = [
  92.             'downloadAssets' => $paginator,
  93.             'downloadCategory' => $downloadCategoryList,
  94.             'fileTypeOptions' => $allFilterFileTypes,
  95.             'filters' => $filters,
  96.         ];
  97.         if ($request->get('ajax')) {
  98.             $template $this->renderTemplate('download/partials/teaser-grid-result.html.twig'$returnArray);
  99.             return $this->json(['success' => true'html' => $template->getContent()], 200);
  100.         }
  101.         return $this->render('download/downloads.html.twig'$returnArray);
  102.     }
  103.     /**
  104.      * @param array<mixed> $downloadAssetsList
  105.      *
  106.      * @return array<mixed>
  107.      */
  108.     public function getFileTypes(array $downloadAssetsList): array
  109.     {
  110.         $filesList = [];
  111.         foreach ($downloadAssetsList as $downloadAsset) {
  112.             $asset $downloadAsset->getAsset();
  113.             if ($asset instanceof Video) {
  114.                 $filesList['video'] = 'video';
  115.             } elseif ($asset instanceof Document) {
  116.                 $filesList['document'] = 'document';
  117.             } elseif ($asset instanceof Image) {
  118.                 $filesList['image'] = 'image';
  119.             }
  120.         }
  121.         return $filesList;
  122.     }
  123. }