<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\ResourceItem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Website\LinkGenerator\ResourceLinkGenerator;
use App\Website\Navigation\BreadcrumbHelperService;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Twig\Extension\Templating\HeadTitle;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Elasticsearch\Client;
class ResourceController extends BaseController
{
const RESOURCE_DEFAULT_DOCUMENT_PROPERTY_NAME = 'resource_default_document';
/**
* @param Request $request
*
* @return Response
*
* @throws \Exception
*/
public function listingAction(Request $request, PaginatorInterface $paginator)
{
$topics = (array)$request->query->get('topics');
$audiences = (array)$request->query->get('audiences');
$filterquery = "";
$filters = [];
$audiencesOptions = [];
$topicsquery = "";
$audiencesquery = "";
$typesquery = "";
$stquery = "";
$valuequery = [];
$sort = $request->query->get('sort');
$st = $request->query->get('st');
$keywords = $request->query->get('keywords');
$type = $request->query->get('type');
// STANDARD FILTER/SEARCH QUERIES
$resourceListing = new ResourceItem\Listing();
if ($sort == "date ASC") {
$resourceListing->setOrderKey('date');
$resourceListing->setOrder('ASC');
} elseif ($sort == "title") {
$resourceListing->setOrderKey('title');
$resourceListing->setOrder('ASC');
} else {
$resourceListing->setOrderKey('date');
$resourceListing->setOrder('DESC');
}
if ($st) {
$valuequery[":st"] = "%" . $st . "%";
$stquery="(topics LIKE :st ";
$stquery.=" OR title LIKE :st";
$stquery.=" OR publisher LIKE :st";
$stquery.=" OR author LIKE :st";
$stquery.=" OR audience LIKE :st";
$stquery.=" OR discipline LIKE :st";
$stquery.=" OR keywords LIKE :st";
$stquery.=")";
$filters["st"]=$stquery;
}
if ($keywords) {
//$valuequery[":kw"] = "%" . $keywords . "%";
//$kwquery="(keywords LIKE :kw)";
$valuequery[":kw"] = "%;" . $keywords . ";%";
$kwquery="(CONCAT(';',REPLACE(keywords, '; ', ';'),';') LIKE :kw)";
$filters["keywords"]=$kwquery;
}
if ($topics) {
foreach ($topics as $idx => $topic) {
if ($topicsquery == "") {
$topicsquery .= "(topics LIKE :topics" . $idx + 1;
} else {
$topicsquery .= " OR topics LIKE :topics" . $idx + 1;
}
$valuequery[":topics" . $idx + 1] = "%" . $topic . "%";
}
$topicsquery .= ")";
$filters["topics"]=$topicsquery;
}
if ($audiences) {
foreach ($audiences as $idx => $audience) {
if ($audiencesquery == "") {
$audiencesquery .= "(audience LIKE :audiences" . $idx + 1;
} else {
$audiencesquery .= " OR audience LIKE :audiences" . $idx + 1;
}
$valuequery[":audiences" . $idx + 1] = "%" . $audience . "%";
}
$audiencesquery .= ")";
$filters["audiences"]=$audiencesquery;
}
if ($type) {
$typesquery .= "(resource_type LIKE :type)";
$valuequery[":type"] = "%" . $type . "%";
$filters["types"]=$typesquery;
}
$filterquery=implode(" AND ", $filters);
// print_r($filterquery);
//print_r($valuequery);
// print_r($stquery);
// $resourceListing->setCondition("topics LIKE :topics1 OR topics LIKE :topics2", ["topics1" => "%Data management%", "topics2" => "%Data discovery%"]);
if ($filterquery != "") {
$resourceListing->setCondition($filterquery, $valuequery);
}
$paginator = $paginator->paginate(
$resourceListing,
$request->get('page', 1),
6
);
// END STANDARD FILTER/SEARCH QUERIES
// FILTER OPTIONS
$sampleresource = new ResourceItem();
$availableFilters=[];
$topicsDef = $sampleresource->getClass()->getFieldDefinition("topics");
$topicsOptions = $topicsDef->getOptions();
$audiencesDef = $sampleresource->getClass()->getFieldDefinition("audience");
$audiencesOptions = $audiencesDef->getOptions();
$typesDef = $sampleresource->getClass()->getFieldDefinition("resource_type");
$typesOptions = $typesDef->getOptions();
$pqListing = new ResourceItem\Listing();
$audiencesFinalOptions = [];
$typesFinalOptions = [];
foreach ($audiencesOptions as $audiencesOption) {
$pqListing->setCondition("audience LIKE :audience", ["audience" => "%" . $audiencesOption["key"] . "%"]);
//print_r($audiencesOption["key"]);
if (count($pqListing)) {
$audiencesFinalOptions[]=$audiencesOption;
}
}
foreach ($typesOptions as $typesOption) {
$pqListing->setCondition("resource_type LIKE :type", ["type" => "%" . $typesOption["key"] . "%"]);
//print_r($typesOption["key"]);
if (count($pqListing)) {
$typesFinalOptions[]=$typesOption;
}
}
// END FILTER OPTIONS
// PREQUERIES FOR FACETTED SEARCH
//$qb = $entityManager->createQueryBuilder();
//$qb->select('count(account.id)');
//$qb->from('ZaysoCoreBundle:Account','account');
// END PREQUERIES FOR FACETTED SEARCH
return $this->render('resource/listing.html.twig', [
'resourceListing' => $paginator,
'paginationVariables' => $paginator->getPaginationData(),
'topics' => $request->query->get('topics'),
'keywords' => $request->query->get('keywords'),
'audiencesvalues' => $request->query->get('audiences'),
'typesvalues' => $request->query->get('type'),
'topicsvalues' => $request->query->get('topics'),
'st' => $request->query->get('st'),
'sort' => $sort,
'audiences_options' => $audiencesFinalOptions,
'types_options' => $typesFinalOptions,
'topics_options' => $topicsOptions,
'qs' => $_SERVER['QUERY_STRING']
]);
// return $this->render('resource/listing.html.twig', ['resourceListing' => $resourceListing]);
}
/**
* @Route("{path}/{resourcetitle}-crd-{resource}", name="resource-detail", defaults={"path"=""}, requirements={"path"=".*?", "resourcetitle"="[\w-]+", "resource"="\d+"})
*
* @param Request $request
* @param HeadTitle $headTitleHelper
* @param Placeholder $placeholderHelper
* @param ResourceLinkGenerator $resourceLinkGenerator
* @param BreadcrumbHelperService $breadcrumbHelperService
*
* @return Response
*/
public function detailAction(Request $request, HeadTitle $headTitleHelper, Placeholder $placeholderHelper, ResourceLinkGenerator $resourceLinkGenerator, BreadcrumbHelperService $breadcrumbHelperService)
{
$resource = ResourceItem::getById($request->get('resource'));
//$resource = ResourceItem::getById(4);
if (!($resource instanceof ResourceItem && ($resource->isPublished() || $this->verifyPreviewRequest($request, $resource)))) {
throw new NotFoundHttpException('Resource not found.');
}
$def = $resource->getClass()->getFieldDefinition("reverse_resources");
$refKey = $def->getOwnerFieldName();
$refId = $def->getOwnerClassId();
$nonOwnerRelations = $resource->getRelationData($refKey, false, $refId);
if ($resource->getParent()->getType() != "folder") {
if (!$nonOwnerRelations && $resource->getParent()->getClassName() == 'ResourceItem') {
$nonOwnerRelations = $resource->getParent()->getRelationData($refKey, false, $refId);
}
}
$breadcrumbHelperService->enrichResourcePage($resource);
$placeholderHelper('canonical')->set($resourceLinkGenerator->generate($resource, ['document' => $this->document->getProperty(self::RESOURCE_DEFAULT_DOCUMENT_PROPERTY_NAME)]));
$headTitleHelper($resource->getTitle());
return $this->render('resource/detail.html.twig', [
'resource' => $resource,
'collections' => $nonOwnerRelations,
'qs' => $_SERVER['QUERY_STRING']
]);
}
}