Deprecated : Symfony\Component\Translation\t(): Implicitly marking parameter $domain as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/translation/Resources/functions.php on line 18
Deprecated : Symfony\Component\Dotenv\Dotenv::loadEnv(): Implicitly marking parameter $envKey as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/dotenv/Dotenv.php on line 110
Deprecated : Symfony\Component\Runtime\GenericRuntime::getResolver(): Implicitly marking parameter $reflector as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/runtime/GenericRuntime.php on line 89
Deprecated : Symfony\Component\Runtime\RuntimeInterface::getResolver(): Implicitly marking parameter $reflector as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/runtime/RuntimeInterface.php on line 26
Deprecated : Symfony\Component\Console\Input\ArgvInput::__construct(): Implicitly marking parameter $argv as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/ArgvInput.php on line 46
Deprecated : Symfony\Component\Console\Input\ArgvInput::__construct(): Implicitly marking parameter $definition as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/ArgvInput.php on line 46
Deprecated : Symfony\Component\Console\Input\Input::__construct(): Implicitly marking parameter $definition as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/Input.php on line 36
Deprecated : Constant E_STRICT is deprecated in /var/www/html/t/taurushr/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /var/www/html/t/taurushr/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\Controller ;
use App\Annotation\CmsComponent ;
use App\Entity\Page ;
use App\Service\ServiceController ;
use Doctrine\ORM\EntityManagerInterface ;
use Knp\Component\Pager\PaginatorInterface ;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\Routing\Annotation\Route ;
class SearchController extends AbstractController
{
/**
* @CmsComponent("Search Results", active=true, routeName="process_search")
*/
#[ Route ( path : '/pcgc-search' , name : 'process_search' )]
public function processSearch ( Request $request , EntityManagerInterface $em , ServiceController $serviceController , PaginatorInterface $paginator )
{
$resultsPerPage = 20 ;
$titleFields = array_reverse ([ 'title' , 'subtitle' , 'headline' , 'name' ]);
$descriptionFields = array_reverse ([ 'content' , 'content2' , 'content3' , 'description' , 'information' , 'taggedin' ]);
$searchableFields = array_merge ( $titleFields , $descriptionFields );
$criteria = $request -> get ( 'criteria' );
$string = $request -> get ( 'criteria' );
$totalResults = 0 ;
if ($criteria ) {
$criteriaArray = explode ( ' ' , (string) $criteria );
// print_r($criteriaArray);
foreach ( $criteriaArray as $key => $criteriaCheck ) {
if (strlen ( $criteriaCheck ) < 3 ) {
unset($criteriaArray [ $key ]);
}
if ('' == $criteriaCheck ) {
unset($criteriaArray [ $key ]);
}
}
$cmsComponentArray = $serviceController -> fetchCmsComponents ();
$pages = $em -> getRepository ( Page ::class)-> findBy ([ 'deleted' => false , 'active' => true ]);
// get only active pages with componets - to prevent fetching routes without any possible links
// 2 arrays - one for quick searching(activePageRoutes) and sluggedPages for full details - just ensure they have both have the same key
$activePageRoutes = [];
$sluggedPages = [];
foreach ($pages as $page ) {
$pageComps = $page -> getComponents ();
foreach ($pageComps as $pageComp ) {
if ('' != $pageComp [ 'route' ]) {
$sluggedPages [] = $page ;
$activePageRoutes [] = $pageComp [ 'route' ];
}
}
}
// echo "<pre>".print_r($activePageRoutes, true)."</pre>";
// get only componets which have the 'slugEntity' annotation
$activeRoutes = [];
$sluggedComponents = [];
foreach ($cmsComponentArray as $cmsComponent ) {
if ('' != $cmsComponent [ 'slugEntity' ]) {
$sluggedComponents [] = $cmsComponent ;
$activeRoutes [] = $cmsComponent [ 'route' ];
}
}
// echo "<pre>".print_r($sluggedComponents, true)."</pre>";
$hits = [];
// loop through all criteria
foreach ( $criteriaArray as $criteria ) {
// search page content first - as its easier ;)
foreach ( $pages as $page ) {
// loop through all searchable fields
foreach ( $searchableFields as $searchableField ) {
$getter = 'get' . ucwords ( $searchableField );
// check that the field exists
// search content - convert to lowercase
if ( method_exists ( $page , $getter ) && strstr ( strtolower ((string) $page ->{ $getter }()), strtolower ( $criteria ))) {
// we have a hit - added a field called hits - if a entity gets more than 1 hit
// increase the hits, i'll use this to calculate weighting
$key = 'page' . $page -> getId ();
if (!array_key_exists ( $key , $hits )) {
$hits [ $key ] = [
'hits' => 1 ,
'title' => $page -> getTitle (),
'description' => $page -> getContent (),
'link' => $page -> getSlug (),
'image' => $page -> getFullImagePath (),
];
} else {
// same entity hit - so increase hit count
++ $hits [ $key ][ 'hits' ];
}
++$totalResults ;
}
}
}
}
foreach ($criteriaArray as $criteria ) {
// search other bundles
// find entites to check - make sure they have a page to link to
foreach ( $sluggedComponents as $key => $sluggedComponent ) {
if (in_array ( $sluggedComponent [ 'route' ], $activePageRoutes )) {
// its an active page so search it
$checkComponent = $sluggedComponents [ $key ];
// get page info - so i know where to link the result
$pageKey = array_search ( $sluggedComponent [ 'route' ], $activePageRoutes );
$pageEntity = $sluggedPages [ $pageKey ];
// $queryEntity = str_replace('\\', '', $checkComponent['bundle'].':'.$checkComponent['slugEntity']);
// $queryEntity = str_replace('/', '', $queryEntity);
// dd($checkComponent);
$queryEntity = '\\' . $checkComponent [ 'bundle' ]. '\\Entity\\' . $checkComponent [ 'slugEntity' ];
$entities = $em -> getRepository ( $queryEntity )-> findBy ([ 'deleted' => 0 , 'active' => 1 ]);
foreach ($entities as $entity ) {
// loop through all searchable fields
foreach ( $searchableFields as $searchableField ) {
$getter = 'get' . ucwords ( $searchableField );
// check that the field exists
// search content - convert to lowercase
if ( method_exists ( $entity , $getter ) && strstr ( strtolower ((string) $entity ->{ $getter }()), strtolower ( $criteria ))) {
// we have a hit - added a field called hits - if a entity gets more than 1 hit
// increase the hits, i'll use this to calculate weighting
// used key as a way to uniquly reference to increase hits
$key = $checkComponent [ 'slugEntity' ]. $entity -> getId ();
if (!array_key_exists ( $key , $hits )) {
// as all entities are different - we need to homogenise
$title = '' ;
$description = '' ;
$link = '' ;
$image = '' ;
// get title field
foreach ( $titleFields as $titleField ) {
$getter = 'get' . ucwords ( $titleField );
if (method_exists ( $entity , $getter )) {
$newTitle = $entity ->{ $getter }();
if (strlen ( $newTitle ) > 0 ) {
$title = $newTitle ;
}
}
}
// get description field
foreach ( $descriptionFields as $descriptionField ) {
$getter = 'get' . ucwords ( $descriptionField );
if (method_exists ( $entity , $getter )) {
$newDescription = $entity ->{ $getter }();
if (strlen ( $newDescription ) > 0 ) {
$description = $newDescription ;
}
}
}
// use excerpt if exists
if ( method_exists ( $entity , 'getExcerpt' )) {
$description = $entity -> getExcerpt ();
}
// check if image exists
if ( method_exists ( $entity , 'getFullImagePath' )) {
$image = $entity -> getFullImagePath ();
}
// echo "<p>";
// echo $checkComponent['slug']."<br/>";
// echo $entity->getSlug()."<br/>";
// echo $pageEntity->getSlug()."<br/>";
// echo "</p>";
// get link
$link = str_replace (
$checkComponent [ 'slug' ],
$entity -> getSlug (),
$pageEntity -> getSlug ()
);
$hits [ $key ] = [
'hits' => 1 ,
'title' => $title ,
'description' => $description ,
'link' => $link ,
'image' => $image ,
];
} else {
// same entity hit - so increase hit count
++ $hits [ $key ][ 'hits' ];
}
++$totalResults ;
}
}
}
}
}
}
// echo "<pre>".print_r($hits, true)."</pre>";
$sortedHits = $this -> subvalSort ( $hits , 'hits' );
// dump($sortedHits);
$pageHits = $paginator -> paginate (
array_reverse ( $sortedHits ),
$request -> query -> getInt ( 'page' , 1 ),
$resultsPerPage
);
return $this -> render ( '@theme/common/search-results.html.twig' , [
'pageHits' => $pageHits ,
'criteriaArray' => $criteriaArray ,
'criteria' => $string ,
'totalResults' => count ( $sortedHits ),
]);
}
return new Response ( 'The search criteria is invalid' );
}
public function subvalSort ( $a , $subkey )
{
$c = [];
$b = [];
foreach ($a as $k => $v ) {
$b [ $k ] = strtolower ((string) $v [ $subkey ]);
}
if (isset($b ) && is_array ( $b )) {
asort ( $b );
foreach (array_keys ( $b ) as $key ) {
$c [] = $a [ $key ];
}
return $c ;
}
return [];
}
}