src/Controller/ResourceController.php line 250

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\DataObject\ResourceItem;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use App\Website\LinkGenerator\ResourceLinkGenerator;
  10. use App\Website\Navigation\BreadcrumbHelperService;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Pimcore\Twig\Extension\Templating\HeadTitle;
  13. use Pimcore\Twig\Extension\Templating\Placeholder;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Elasticsearch\Client;
  17. class ResourceController extends BaseController
  18. {
  19.     const RESOURCE_DEFAULT_DOCUMENT_PROPERTY_NAME 'resource_default_document';
  20.     /**
  21.      * @param Request $request
  22.      *
  23.      * @return Response
  24.      *
  25.      * @throws \Exception
  26.      */
  27.     public function listingAction(Request $requestPaginatorInterface $paginator)
  28.     {
  29.         $topics = (array)$request->query->get('topics');
  30.         $audiences = (array)$request->query->get('audiences');
  31.         $filterquery "";
  32.         $filters = [];
  33.         $audiencesOptions = [];
  34.         $topicsquery "";
  35.         $audiencesquery "";
  36.         $typesquery "";
  37.         $stquery "";
  38.         $valuequery = [];
  39.         $sort $request->query->get('sort');
  40.         $st $request->query->get('st');
  41.         $keywords $request->query->get('keywords');
  42.         $type $request->query->get('type');
  43.         
  44. // STANDARD FILTER/SEARCH QUERIES
  45. $resourceListing = new ResourceItem\Listing();
  46. if ($sort == "date ASC") {
  47. $resourceListing->setOrderKey('date');
  48. $resourceListing->setOrder('ASC');
  49. } elseif ($sort == "title") {
  50.     $resourceListing->setOrderKey('title');
  51. $resourceListing->setOrder('ASC');
  52. } else {
  53.     $resourceListing->setOrderKey('date');
  54. $resourceListing->setOrder('DESC');
  55. }
  56.         if ($st) {
  57.             $valuequery[":st"] = "%" $st "%";
  58.             $stquery="(topics LIKE :st ";
  59.             $stquery.=" OR title LIKE :st";
  60.             $stquery.=" OR publisher LIKE :st";
  61.             $stquery.=" OR author LIKE :st";
  62.             $stquery.=" OR audience LIKE :st";
  63.             $stquery.=" OR discipline LIKE :st";
  64.             $stquery.=" OR keywords LIKE :st";
  65.             $stquery.=")";
  66.             $filters["st"]=$stquery;
  67.         }
  68.         if ($keywords) {
  69.             
  70.             //$valuequery[":kw"] = "%" . $keywords . "%";  
  71.             //$kwquery="(keywords LIKE :kw)";
  72.             $valuequery[":kw"] = "%;" $keywords ";%";  
  73.             $kwquery="(CONCAT(';',REPLACE(keywords, '; ', ';'),';') LIKE :kw)";
  74.             $filters["keywords"]=$kwquery;
  75.         }
  76.         if ($topics) {
  77.             foreach ($topics as $idx => $topic) {
  78.                 if ($topicsquery == "") {
  79.                     $topicsquery .= "(topics LIKE :topics" $idx 1;
  80.                 } else {
  81.                     $topicsquery .= " OR topics LIKE :topics" $idx 1;
  82.                 }
  83.                 $valuequery[":topics" $idx 1] = "%" $topic "%";
  84.             }
  85.             $topicsquery .= ")";
  86.             $filters["topics"]=$topicsquery;
  87.         }
  88.         if ($audiences) {
  89.             foreach ($audiences as $idx => $audience) {
  90.                 if ($audiencesquery == "") {
  91.                     $audiencesquery .= "(audience LIKE :audiences" $idx 1;
  92.                 } else {
  93.                     $audiencesquery .= " OR audience LIKE :audiences" $idx 1;
  94.                 }
  95.                 $valuequery[":audiences" $idx 1] = "%" $audience "%";
  96.             }
  97.             $audiencesquery .= ")";
  98.             $filters["audiences"]=$audiencesquery;
  99.         }
  100.         if ($type) {
  101.                     $typesquery .= "(resource_type LIKE :type)";
  102.                 
  103.                 $valuequery[":type"] = "%" $type "%";
  104.                 $filters["types"]=$typesquery;
  105.             }
  106.           
  107.          
  108.      
  109. $filterquery=implode(" AND "$filters);
  110.        // print_r($filterquery);
  111.        //print_r($valuequery);
  112.        // print_r($stquery);
  113.       //  $resourceListing->setCondition("topics LIKE :topics1 OR topics LIKE :topics2", ["topics1" => "%Data management%", "topics2" => "%Data discovery%"]);
  114.       if ($filterquery != "") {
  115.       $resourceListing->setCondition($filterquery$valuequery);
  116.       }
  117.         $paginator $paginator->paginate(
  118.             $resourceListing,
  119.             $request->get('page'1),
  120.             6
  121.         );
  122. // END STANDARD FILTER/SEARCH QUERIES
  123. // FILTER OPTIONS
  124. $sampleresource = new ResourceItem(); 
  125. $availableFilters=[];
  126. $topicsDef $sampleresource->getClass()->getFieldDefinition("topics");
  127. $topicsOptions $topicsDef->getOptions();
  128. $audiencesDef $sampleresource->getClass()->getFieldDefinition("audience");
  129. $audiencesOptions $audiencesDef->getOptions();
  130. $typesDef $sampleresource->getClass()->getFieldDefinition("resource_type");
  131. $typesOptions $typesDef->getOptions();
  132. $pqListing = new ResourceItem\Listing();
  133. $audiencesFinalOptions = [];
  134. $typesFinalOptions = [];
  135. foreach ($audiencesOptions as $audiencesOption) {
  136. $pqListing->setCondition("audience LIKE :audience", ["audience" => "%" $audiencesOption["key"] . "%"]);
  137. //print_r($audiencesOption["key"]);
  138. if (count($pqListing)) {
  139.     $audiencesFinalOptions[]=$audiencesOption;
  140. }
  141. }
  142. foreach ($typesOptions as $typesOption) {
  143.     $pqListing->setCondition("resource_type LIKE :type", ["type" => "%" $typesOption["key"] . "%"]);
  144.     //print_r($typesOption["key"]);
  145.     if (count($pqListing)) {
  146.         $typesFinalOptions[]=$typesOption;
  147.     }
  148.     
  149.     }
  150. // END FILTER OPTIONS
  151. // PREQUERIES FOR FACETTED SEARCH
  152. //$qb = $entityManager->createQueryBuilder();
  153. //$qb->select('count(account.id)');
  154. //$qb->from('ZaysoCoreBundle:Account','account');
  155. // END PREQUERIES FOR FACETTED SEARCH
  156.         return $this->render('resource/listing.html.twig', [
  157.             'resourceListing' => $paginator,
  158.             'paginationVariables' => $paginator->getPaginationData(),
  159.             'topics' => $request->query->get('topics'),
  160.             'keywords' => $request->query->get('keywords'),
  161.             'audiencesvalues' => $request->query->get('audiences'),
  162.             'typesvalues' => $request->query->get('type'),
  163.             'topicsvalues' => $request->query->get('topics'),
  164.             'st' => $request->query->get('st'),
  165.             'sort' => $sort,
  166.             'audiences_options' => $audiencesFinalOptions,
  167.             'types_options' => $typesFinalOptions,
  168.             'topics_options' => $topicsOptions,
  169.             'qs' => $_SERVER['QUERY_STRING']
  170.         ]);
  171.         //  return $this->render('resource/listing.html.twig', ['resourceListing' => $resourceListing]);
  172.     }
  173.     /**
  174.      * @Route("{path}/{resourcetitle}-crd-{resource}", name="resource-detail", defaults={"path"=""}, requirements={"path"=".*?", "resourcetitle"="[\w-]+", "resource"="\d+"})
  175.      *
  176.      * @param Request $request
  177.      * @param HeadTitle $headTitleHelper
  178.      * @param Placeholder $placeholderHelper
  179.      * @param ResourceLinkGenerator $resourceLinkGenerator
  180.      * @param BreadcrumbHelperService $breadcrumbHelperService
  181.      *
  182.      * @return Response
  183.      */
  184.     public function detailAction(Request $requestHeadTitle $headTitleHelperPlaceholder $placeholderHelperResourceLinkGenerator $resourceLinkGeneratorBreadcrumbHelperService $breadcrumbHelperService)
  185.     {
  186.         $resource ResourceItem::getById($request->get('resource'));
  187.         //$resource = ResourceItem::getById(4);
  188.         if (!($resource instanceof ResourceItem && ($resource->isPublished() || $this->verifyPreviewRequest($request$resource)))) {
  189.             throw new NotFoundHttpException('Resource not found.');
  190.         }
  191.         $def $resource->getClass()->getFieldDefinition("reverse_resources");
  192.         $refKey $def->getOwnerFieldName();
  193.         $refId $def->getOwnerClassId();
  194.         $nonOwnerRelations $resource->getRelationData($refKeyfalse$refId);
  195.         if ($resource->getParent()->getType() != "folder") {
  196.         if (!$nonOwnerRelations && $resource->getParent()->getClassName() == 'ResourceItem') {
  197.             $nonOwnerRelations $resource->getParent()->getRelationData($refKeyfalse$refId);
  198.         }
  199.     }
  200.         $breadcrumbHelperService->enrichResourcePage($resource);
  201.         $placeholderHelper('canonical')->set($resourceLinkGenerator->generate($resource, ['document' => $this->document->getProperty(self::RESOURCE_DEFAULT_DOCUMENT_PROPERTY_NAME)]));
  202.         $headTitleHelper($resource->getTitle());
  203.         return $this->render('resource/detail.html.twig', [
  204.             'resource' => $resource,
  205.             'collections' => $nonOwnerRelations,
  206.             'qs' => $_SERVER['QUERY_STRING']
  207.             
  208.         ]);
  209.     }
  210. }