Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
1
Indexable
Never
<?php

namespace App\Controller;


use ErdnaxelaWeb\IbexaDesignIntegration\Pager\PagerBuilder;
use ErdnaxelaWeb\IbexaDesignIntegration\Transformer\ContentTransformer;
use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
use Ibexa\Core\MVC\Symfony\Controller\Controller;
use Ibexa\Core\MVC\Symfony\View\ContentView;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Core\Pagination\Pagerfanta\ContentSearchAdapter;
use Ibexa\Core\Pagination\Pagerfanta\Pagerfanta;
use Ibexa\Core\Repository\Repository;

class NeedController extends Controller
{
    public function __construct(
        protected ContentTransformer $contentTransformer,
        protected PagerBuilder       $pagerBuilder,
        protected Repository $repository
    ) {
    }

    public function viewAction(ContentView $view): ContentView
    {
        $content = $view->getContent();
        $location = $view->getLocation();
        $sortClauses = $location->getSortClauses();
        $contentDecorator = ($this->contentTransformer)($content, $location);

        $query = new LocationQuery();
        $filters[] = new Criterion\Subtree($location->pathString);
        $filters[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
        $filters[] = new Criterion\ContentTypeIdentifier('need');
        $query->filter = new LogicalAnd($filters);
        $query->sortClauses = $sortClauses;
        $view->addParameters([
            'pager' => $this->buildContentPager($query, 1, 9)
        ]);

        $view->setContent($contentDecorator);

        return $view;
    }

    public function buildContentPager(
        Query $query,
        int $currentPage = 1,
        int $maxPerPage = 12,
              $languages = []
    ): Pagerfanta {
        $languageFilter = [
            'languages' => $languages ?? ['fre-FR'],
            'useAlwaysAvailable' => true,
        ];

        $pager = new Pagerfanta(
            new ContentSearchAdapter($query, $this->repository->getSearchService(), $languageFilter)
        );
        $pager->setMaxPerPage($maxPerPage);
        $pager->setCurrentPage($currentPage);

        return $pager;
    }
}