src/DataProvider/SearchCollectionDataProvider.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
  4. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
  6. use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
  7. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  8. use ApiPlatform\Core\Exception\RuntimeException;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Api\Search as Search;
  12. use DateTime;
  13. use Score\BaseBundle\Services\SeoUrl;
  14. final class SearchCollectionDataProvider implements ContextAwareCollectionDataProviderInterfaceRestrictedDataProviderInterface
  15. {
  16.     private $collectionExtensions;
  17.     private $managerRegistry;
  18.    
  19.     public function __construct(ManagerRegistry $managerRegistryiterable $collectionExtensions = [])
  20.     {
  21.         $this->managerRegistry $managerRegistry;
  22.         $this->collectionExtensions $collectionExtensions;
  23.     }
  24.     
  25.     public function supports(string $resourceClassstring $operationName null, array $context = []): bool
  26.     {
  27.         return Search::class === $resourceClass;
  28.     }
  29.     public function getCollection(string $resourceClassstring $operationName null, array $context = []): iterable
  30.     {
  31.         $seoUrlManager = new SeoUrl();
  32.         $slug =$context['filters']['keyword'];
  33.         $siteQuery '';
  34.         if(array_key_exists('site',$context['filters']))
  35.         {
  36.             $siteQuery ' AND s.site_id = '.intval($context['filters']['site']);
  37.         }
  38.         $types = ['event','article','page','document'];
  39.         if(array_key_exists('type',$context['filters']))
  40.         {
  41.             $types str_replace(' ','',$context['filters']['type']);
  42.             $types explode(',',$types);
  43.         }
  44.         $result = [];
  45.         $ratedResult = [];
  46.         $con $this->managerRegistry->getManagerForClass($resourceClass)->getConnection();
  47.         if ($slug && $seoUrlManager->createSlug($slug)) {
  48.             
  49.             $seoSlug $seoUrlManager->createSlug($slug);
  50.             //$con = $this->getDoctrine()->getManager()->getConnection();
  51.             $rawData = [];
  52.             //$rawData['page'] = $con->fetchAllAssociative('SELECT p.title as name , p.content , p.seo_id as slug, p.icon as icon, p.edited_at as edit FROM cms_page p');
  53.             $rawData['page'] = $con->fetchAllAssociative('SELECT p.created_at, p.id,s.site_id,  p.title as name , p.content as content , substring(p.content,0,200) as teaser, p.seo_id as slug, p.icon as icon, p.edited_at as edit FROM cms_page p left join cms_site_items s on p.id =s.item_id and s.item_type = \'page\'  WHERE p.seo_id NOT IN (?, ?, ?)'.$siteQuery, ["as""error""homepage"]);
  54.             $rawData['article'] = $con->fetchAllAssociative('SELECT a.created_at, a.id,s.site_id, a.name as name, a.teaser, a.body as content, a.author, a.slug as slug, a.icon as icon, a.edited_at as edit FROM cms_article a  left join cms_site_items s on a.id =s.item_id and s.item_type = \'article\'');
  55.             $rawData['event'] = $con->fetchAllAssociative('SELECT e.created_at, e.id,s.site_id, e.name as name, e.content as content, e.teaser, e.organizer, e.contact_person, e.slug as slug, e.icon as icon, e.edited_at as edit FROM cms_event e  left join cms_site_items s on e.id =s.item_id and s.item_type = \'event\'');
  56.             $rawData['document'] = $con->fetchAllAssociative('SELECT d.id,  d.name as name, d.description as content,  \'\' as teaser, \'\' as slug, d.created_at as created_at FROM document d');
  57.             $skip false;
  58.             foreach ($rawData as $type => $rows) {
  59.                if(in_array($type,$types))
  60.                {
  61.                 foreach ($rows as $row) {
  62.                     $skip false;
  63.                     if(strpos($seoUrlManager->createSlug($row['name']), $seoSlug) !== false or strpos($seoUrlManager->createSlug($row['content']), $seoSlug) !== false  or strpos($seoUrlManager->createSlug($row['teaser']), $seoSlug) !== false)
  64.                     {
  65.                         $ratingCriteria = ['name' =>100000,'teaser' =>10000'content' => 1000];
  66.                         $rating 0;
  67.                         if(strpos($seoUrlManager->createSlug($row['name']), $seoSlug) !== false)
  68.                         {
  69.                             $rating += $ratingCriteria['name'];
  70.                         }
  71.                         if(strpos($seoUrlManager->createSlug($row['teaser']), $seoSlug) !== false)
  72.                         {
  73.                             $rating += $ratingCriteria['teaser'];
  74.                         }
  75.                         if(strpos($seoUrlManager->createSlug($row['content']), $seoSlug) !== false)
  76.                         {
  77.                             $rating += $ratingCriteria['content'];
  78.                         }
  79.                         $index $rating;
  80.                         if(array_key_exists($index,$result))
  81.                         {
  82.                             $index += $row['id'];
  83.                         }
  84.                         
  85.                         $result[$index] = [
  86.                             "id" => $type.'_'.$row['id'],
  87.                             "type" => $type,
  88.                             "data" => [
  89.                                 "id" => $row['id'],
  90.                                 "title" => $row['name'],
  91.                                 'teaser' => $row['teaser'],
  92.                                 "slug" => $row['slug'],
  93.                                 "rating" => $rating,
  94.                                 'created_at' => $row['created_at']
  95.                             ]
  96.                         ];
  97.                         //var_dump( $result);
  98.                         krsort($result);
  99.                         
  100.                         $ratedResult array_values($result);
  101.                         //var_dump( $result);
  102.                         /*
  103.                         $ratedResult = $result;
  104.                         $ratedResult = [];
  105.                         foreach($result as $r)
  106.                         {
  107.                             //echo  $r['id'];
  108.                             $ratedResult[] = $r;
  109.                         }
  110.                         */
  111.                         
  112.                         /*
  113.                         foreach($result as $res)
  114.                         {
  115.                             $ratedResult[] = [
  116.                                 "id" => $type.'_'.$row['id'],
  117.                                 "type" => $type,
  118.                                 "data" => [
  119.                                     "id" => $row['id'],
  120.                                     "title" => $row['name'],
  121.                                     'teaser' => $row['teaser'],
  122.                                     "slug" => $row['slug'],
  123.                                     "rating" => $rating,
  124.                                     'created_at' => $row['created_at']
  125.                                 ]
  126.                             ];
  127.                         }
  128.                         */
  129.                     }
  130.                    
  131.                     /*
  132.                     foreach($row as $col => $value) {
  133.                         if ( $skip === false && $value && $col !== "edit" && strpos($seoUrlManager->createSlug($value), $seoSlug) !== false) {
  134.                             
  135.                             $rating = 
  136.                             
  137.                             
  138.                             $skip = true;
  139.                             var_dump($col);
  140.                             $ratingCriteria = ['title' =>1,'teaser' => 0.7, 'body' => 0.5];
  141.                             //$rating
  142.                             //$rating = 
  143.                             $result[] = [
  144.                                 "id" => $type.'_'.$row['id'],
  145.                                 "type" => $type,
  146.                                 "data" => [
  147.                                     "id" => $row['id'],
  148.                                     "title" => $row['name'],
  149.                                     'teaser' => $row['teaser'],
  150.                                     "slug" => $row['slug']
  151.                                 ]
  152.                                
  153.                             ];
  154.                         }
  155.                     }
  156.                     */
  157.                 }
  158.                }
  159.                
  160.             }
  161.             /*
  162.             usort($result, function ($item1, $item2) {
  163.                 return $item2['edited'] <=> $item1['edited'];
  164.             });
  165.             */
  166.         }
  167.         return $ratedResult;
  168.     }
  169.    
  170.     
  171. }