src/Controller/ProjectController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Listing\DataObjectListing;
  4. use App\Model\ProductCategory;
  5. use App\Model\Sector;
  6. use Exception;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. use Pimcore\Db;
  9. use Pimcore\Model\DataObject\Project;
  10. use Pimcore\Model\DataObject;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  13. class ProjectController extends DefaultFrontendController
  14. {
  15.     /**
  16.      * @Template
  17.      * @param Request $request
  18.      * @return array
  19.      * @throws Exception
  20.      */
  21.     public function singleAction(Request $request): array
  22.     {
  23.         $project $this->getDocumentEditable('relation''project')->getElement();
  24.         $data parent::data($request);
  25.         if ($project && $project->getDefaultFeaturedImage()) {
  26.             $data['page']['og']['image'] = $project->getDefaultFeaturedImage()->getFullPath();
  27.         }
  28.         return $data;
  29.     }
  30.     /**
  31.      * @Template
  32.      * @param Request $request
  33.      * @param PaginatorInterface $paginator
  34.      * @return array
  35.      * @throws Exception
  36.      */
  37.     public function archiveAction(Request $requestPaginatorInterface $paginator): array
  38.     {
  39.         $objectListing = new Project\Listing;
  40.         $objectListing->setLocale($request->getLocale());
  41.         $objectListing DataObjectListing::belongsToPageInCurrentLocaleQuery($objectListing'project');
  42.         $objectListing->setOrderKey('index');
  43.         $objectListing->setOrder('ASC');
  44.         if ($productCategoryId $request->query->get('product_category')) {
  45.             $objectListing->filterByGroups($productCategoryId);
  46.         }
  47.         if ($branchId $request->query->get('branch')) {
  48.             $objectListing->filterBySectors($branchId);
  49.         }
  50.         /** Based on https://pimcore.com/docs/pimcore/current/Development_Documentation/Objects/Working_with_PHP_API.html#page_Working-with-Knp-Component-Pager-Paginator */
  51.         $pagination $paginator->paginate(
  52.             $objectListing,
  53.             // Minimal page index is 1
  54.             max(1, (int) $request->get('page'1)),
  55.             // Max page size is 100.
  56.             min(100max(10, (int) $request->get('pageSize'10))),
  57.         );
  58.         return $this->data($request, [
  59.             'pagination' => $pagination,
  60.             'paginationVariables' => $pagination->getPaginationData(),
  61.             'productCategoryFilter' => $this->productCategoryFilter(),
  62.             'branchFilter' => $this->branchFilter(),
  63.         ]);
  64.     }
  65.     protected function productCategoryFilter(): array
  66.     {
  67.         $sql = <<< SQL
  68.             SELECT DISTINCT odest.o_id
  69.             FROM `object_relations_3` or3
  70.             INNER JOIN `objects` osrc ON or3.`src_id` = osrc.`o_id` AND osrc.o_className = 'Project' AND osrc.o_published = 1
  71.             INNER JOIN `objects` odest ON or3.`dest_id` = odest.`o_id` AND odest.o_className = 'ProductCategory' AND osrc.o_published = 1
  72.             WHERE or3.fieldname = 'groups'
  73.             SQL;
  74.         $categories Db::get()->fetchAll($sql);
  75.         if (!$categories) {
  76.             return [];
  77.         }
  78.         $listing = new DataObject\Listing();
  79.         $listing->setCondition("o_id IN (?)", [array_column($categories'o_id')]);
  80.         /** @var ProductCategory[] $categories */
  81.         $categories $listing->getData();
  82.         $return = [];
  83.         foreach ($categories as $category) {
  84.             $return[$category->getId()] = $category->getName() ?: $category->getKey();
  85.         }
  86.         natcasesort($return);
  87.         return $return;
  88.     }
  89.     protected function branchFilter(): array
  90.     {
  91.         $sql = <<< SQL
  92.             SELECT DISTINCT odest.o_id
  93.             FROM `object_relations_3` or3
  94.             INNER JOIN `objects` osrc ON or3.`src_id` = osrc.`o_id` AND osrc.o_className = 'Project' AND osrc.o_published = 1
  95.             INNER JOIN `objects` odest ON or3.`dest_id` = odest.`o_id` AND odest.o_className = 'Sector' AND osrc.o_published = 1
  96.             WHERE or3.fieldname = 'sectors'
  97.             SQL;
  98.         $sectors Db::get()->fetchAll($sql);
  99.         if (!$sectors) {
  100.             return [];
  101.         }
  102.         $listing = new DataObject\Listing();
  103.         $listing->setCondition("o_id IN (?)", [array_column($sectors'o_id')]);
  104.         /** @var Sector[] $sectors */
  105.         $sectors $listing->getData();
  106.         $return = [];
  107.         foreach ($sectors as $sector) {
  108.             $return[$sector->getId()] = $sector->getBranchename() ?: $sector->getKey();
  109.         }
  110.         natcasesort($return);
  111.         return $return;
  112.     }
  113. }