<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Services\Search\SearchSelect;
use Doctrine\ORM\EntityManagerInterface;
class ContentBlockController extends AbstractController
{
private function fixBooleanValues(&$aParams)
{
array_walk_recursive($aParams, function(&$aParam){
if(is_string($aParam))
{
switch($aParam)
{
case 'true':
$aParam = true;
break;
case 'false':
$aParam = false;
break;
case 'null':
$aParam = null;
break;
}
}
});
}
/**
* @Route("/op/results/paginate", name="results_pagination", options={"expose"=true})
*/
public function renderResults(Request $request, RequestStack $request_stack, EntityManagerInterface $em, SearchSelect $search_select)
{
$master_request = $request_stack->getMainRequest();
$aParams = $request->get('aParams');
$aParams = $aParams ? $aParams : $request->request->get('aParams');
if($aParams && isset($aParams['static_set'])) // an array of items to be rendered
{
return $this->render($aParams['results_template'], [
'results' => $aParams['static_set'],
'pagination' => false,
'aParams' => $aParams,
'all_results' => count($aParams['static_set'])
]);
}
$contentType = strtolower($aParams['contentType']);
if($aParams)
{
$aParams = is_array($aParams) ? $aParams : json_decode($request->request->get('aParams'), true);
$this->fixBooleanValues($aParams);
// if not set, or true, will render results; otherwise it will return a json of the results (without pagination; useful for autocompleting)
$aParams['render'] = array_key_exists('render', $aParams) ? $aParams['render'] : true;
// IDs used on the same master request
$excluded_ids = $master_request->request->get('excluded_ids') ? $master_request->request->get('excluded_ids') : [$contentType => []];
if(!array_key_exists($contentType, $excluded_ids))
{
$excluded_ids[$contentType] = [];
}
//$excluded_ids = array($contentType => array());
if(!array_key_exists('allow_duplicates_in_same_master_request', $aParams) || !$aParams['allow_duplicates_in_same_master_request'])
{
//$excluded_ids = $master_request->request->get('excluded_ids');
//$excluded_ids = $excluded_ids ? $excluded_ids : array($contentType => array());
$aParams['aExcluded_ids'] = array_key_exists('aExcluded_ids', $aParams) && is_array($aParams['aExcluded_ids']) ? array_merge($aParams['aExcluded_ids'], $excluded_ids[$contentType]) : $excluded_ids[$contentType];
$aParams['aExcluded_ids'] = array_unique($aParams['aExcluded_ids']);
}
$search = $search_select->get($aParams);
$func = $aParams['func'] ?? 'get';
$search_results = $search->$func($aParams);
// Store new values to exclude them in the same master request
foreach($search_results['results'] as $result)
{
$excluded_ids[$contentType][] = $result['id'];
}
$master_request->request->set('excluded_ids', $excluded_ids);
// if no results, and suggestions requested, do it here (did you mean ...)
$search_results_suggested = false;
if(
empty($search_results['results']) &&
isset($aParams['show_suggestions']) &&
$aParams['show_suggestions'] &&
isset($aParams['search_terms']) &&
$aParams['search_terms'] &&
method_exists($search, 'getSuggested'))
{
$aParams_suggested = array_merge($aParams, [
'search_terms' => false,
'ids_collection' => $search->getSuggested($aParams),
'sorting' => 'field',
'max_results' => false
]);
$search_results_suggested = $search->get($aParams_suggested);
}
// Return only specific fields, useful for autocomplete plugins
$aFilteredResults = $search_results['results'];
if(array_key_exists('specific_fields', $aParams) && is_array($aParams['specific_fields']))
{
$aFilteredResults = [];
foreach($search_results['results'] as $result)
{
$aFields = [];
foreach($aParams['specific_fields'] as $field)
{
$aFields[$field] = $result[$field];
}
$aFilteredResults[] = $aFields;
}
}
if(array_key_exists('reverse_results', $aParams) && $aParams['reverse_results'])
{
$aFilteredResults = array_reverse($aFilteredResults);
}
return $aParams['render'] ? $this->render($aParams['results_template'], [
'results' => $aFilteredResults,
'pagination' => $search_results['pagination'],
'aParams' => $aParams,
'all_results' => $search_results,
'results_suggested' => $search_results_suggested['results'], // 'did you mean ...' results
]) : new Response(json_encode($aFilteredResults));
}
throw new NotFoundHttpException('Wrong parameters sent');
}
}