<?php
namespace App\Controller;
use App\Listing\DataObjectListing;
use App\Model\ProductCategory;
use App\Model\Sector;
use Exception;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Db;
use Pimcore\Model\DataObject\Project;
use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ProjectController extends DefaultFrontendController
{
/**
* @Template
* @param Request $request
* @return array
* @throws Exception
*/
public function singleAction(Request $request): array
{
$project = $this->getDocumentEditable('relation', 'project')->getElement();
$data = parent::data($request);
if ($project && $project->getDefaultFeaturedImage()) {
$data['page']['og']['image'] = $project->getDefaultFeaturedImage()->getFullPath();
}
return $data;
}
/**
* @Template
* @param Request $request
* @param PaginatorInterface $paginator
* @return array
* @throws Exception
*/
public function archiveAction(Request $request, PaginatorInterface $paginator): array
{
$objectListing = new Project\Listing;
$objectListing->setLocale($request->getLocale());
$objectListing = DataObjectListing::belongsToPageInCurrentLocaleQuery($objectListing, 'project');
$objectListing->setOrderKey('index');
$objectListing->setOrder('ASC');
if ($productCategoryId = $request->query->get('product_category')) {
$objectListing->filterByGroups($productCategoryId);
}
if ($branchId = $request->query->get('branch')) {
$objectListing->filterBySectors($branchId);
}
/** Based on https://pimcore.com/docs/pimcore/current/Development_Documentation/Objects/Working_with_PHP_API.html#page_Working-with-Knp-Component-Pager-Paginator */
$pagination = $paginator->paginate(
$objectListing,
// Minimal page index is 1
max(1, (int) $request->get('page', 1)),
// Max page size is 100.
min(100, max(10, (int) $request->get('pageSize', 10))),
);
return $this->data($request, [
'pagination' => $pagination,
'paginationVariables' => $pagination->getPaginationData(),
'productCategoryFilter' => $this->productCategoryFilter(),
'branchFilter' => $this->branchFilter(),
]);
}
protected function productCategoryFilter(): array
{
$sql = <<< SQL
SELECT DISTINCT odest.o_id
FROM `object_relations_3` or3
INNER JOIN `objects` osrc ON or3.`src_id` = osrc.`o_id` AND osrc.o_className = 'Project' AND osrc.o_published = 1
INNER JOIN `objects` odest ON or3.`dest_id` = odest.`o_id` AND odest.o_className = 'ProductCategory' AND osrc.o_published = 1
WHERE or3.fieldname = 'groups'
SQL;
$categories = Db::get()->fetchAll($sql);
if (!$categories) {
return [];
}
$listing = new DataObject\Listing();
$listing->setCondition("o_id IN (?)", [array_column($categories, 'o_id')]);
/** @var ProductCategory[] $categories */
$categories = $listing->getData();
$return = [];
foreach ($categories as $category) {
$return[$category->getId()] = $category->getName() ?: $category->getKey();
}
natcasesort($return);
return $return;
}
protected function branchFilter(): array
{
$sql = <<< SQL
SELECT DISTINCT odest.o_id
FROM `object_relations_3` or3
INNER JOIN `objects` osrc ON or3.`src_id` = osrc.`o_id` AND osrc.o_className = 'Project' AND osrc.o_published = 1
INNER JOIN `objects` odest ON or3.`dest_id` = odest.`o_id` AND odest.o_className = 'Sector' AND osrc.o_published = 1
WHERE or3.fieldname = 'sectors'
SQL;
$sectors = Db::get()->fetchAll($sql);
if (!$sectors) {
return [];
}
$listing = new DataObject\Listing();
$listing->setCondition("o_id IN (?)", [array_column($sectors, 'o_id')]);
/** @var Sector[] $sectors */
$sectors = $listing->getData();
$return = [];
foreach ($sectors as $sector) {
$return[$sector->getId()] = $sector->getBranchename() ?: $sector->getKey();
}
natcasesort($return);
return $return;
}
}