src/Utils/Uniqskills/CourseCatalog.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Uniqskills;
  3. use App\Entity\Gos\Category;
  4. use App\Entity\Gos\Country;
  5. use App\Entity\Gos\PortalSettings;
  6. use App\Entity\Gos\ProductVariant;
  7. use App\Entity\Gos\Uniqskills\Course;
  8. use App\Entity\Gos\Uniqskills\CourseKeyword;
  9. use App\Entity\Gos\User;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\Pagination\PaginationInterface;
  12. use Knp\Component\Pager\Pagination\SlidingPagination;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use App\Utils\Geolocation;
  18. class CourseCatalog
  19. {
  20.     private EntityManagerInterface $em;
  21.     private PaginatorInterface $knpPaginator;
  22.     private Geolocation $geoLocation;
  23.     private TokenStorageInterface $tokenStorage;
  24.     private ParameterBagInterface $parameterBag;
  25.     private $user;
  26.     public function __construct(
  27.         EntityManagerInterface  $em,
  28.         PaginatorInterface      $knpPaginator,
  29.         TokenStorageInterface   $tokenStorage,
  30.         Geolocation             $geoLocation,
  31.         ParameterBagInterface   $parameterBag
  32.     ) {
  33.         $this->em           $em;
  34.         $this->knpPaginator $knpPaginator;
  35.         $this->geoLocation  $geoLocation;
  36.         $this->tokenStorage $tokenStorage;
  37.         $this->user         $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  38.         $this->parameterBag $parameterBag;
  39.     }
  40.     public function getCountry(Request $request)
  41.     {
  42.         if ($request->getSession()->get('fixCountryCode'))
  43.         {
  44.             $country $this->em->getRepository(Country::class)->findOneByAlpha2(
  45.                 $request->getSession()->get('fixCountryCode')
  46.             );
  47.         }
  48.         else if ($request->getSession()->get('myCountryCode'))
  49.         {
  50.             $country $this->em->getRepository(Country::class)->findOneByAlpha2(
  51.                 $request->getSession()->get('myCountryCode')
  52.             );
  53.         }
  54.         else if (!is_object($this->user))
  55.         {
  56.             $country $this->em->getRepository(Country::class)->findOneByAlpha2(
  57.                 $this->geoLocation->geolocation()['countryCode']
  58.             );
  59.         }
  60.         else
  61.         {
  62.             $country $this->user->getCountry();
  63.         }
  64.         if (is_null($country))
  65.         {
  66.             $country $this->em->getRepository(Country::class)->findOneByAlpha2('PL');
  67.         }
  68.         return $country;
  69.     }
  70.     public function getUserCourse()
  71.     {
  72.         $userCourse = [];
  73.         if (is_object($this->user))
  74.         {
  75.             $userCourse $this->em->getRepository(Course::class)->findUserCourses($this->user->getId());
  76.         }
  77.         return $userCourse;
  78.     }
  79.     public function preparePagination(
  80.         array $routeParams,
  81.         array $userOrders,
  82.         Country $country,
  83.         PortalSettings $portalSettings
  84.     ): PaginationInterface
  85.     {
  86.         $courses $this->em
  87.             ->getRepository(Course::class)
  88.             ->findAllFiltered(
  89.                 null,
  90.                 null,
  91.                 [
  92.                     'language'        => $routeParams['locale'],
  93.                     'category'        => $routeParams['subCategory'] ?: $routeParams['category'],
  94.                     'isActive'        => true,
  95.                     'finishedAtCheck' => true,
  96.                     'keyword'         => $routeParams['keyword'],
  97.                     'duration'        => $routeParams['duration'],
  98.                     'priceRange'      => $routeParams['priceRange'],
  99.                     'portalSettings'  => $portalSettings->getId()
  100.                 ],
  101.                 $routeParams['searchQuery'],
  102.                 $userOrders
  103.             )
  104.             ->getQuery()->getResult();
  105.         $limit $portalSettings->getHash() != $this->parameterBag->get('pl_us_portal_settings') ? 30 5;
  106.         $pagination $this->knpPaginator->paginate(
  107.             $courses,
  108.             $routeParams['page'],
  109.             $limit
  110.         );
  111.         $pagination->setTemplate('/uniqskills/base/nano_pagination.html.twig');
  112.         //for course price
  113.         foreach ($pagination as $item)
  114.         {
  115.             /** @var Course $item */
  116.             $item->countryProduct $this->em->getRepository(ProductVariant::class)
  117.                 ->findAllByCourseAndCountry($item->getSlug(), $country->getName());
  118.         }
  119.         //set proper route for pagination
  120.         if (empty($routeParams['subCategory']))
  121.         {
  122.             if (empty($routeParams['category']))
  123.             {
  124.                 $pagination->setUsedRoute($routeParams['locale'] == 'pl'
  125.                     'fmUniqskillsFrontendPlCourseCataloguePage'
  126.                     'fmUniqskillsFrontendCourseCataloguePage');
  127.             }
  128.             else
  129.             {
  130.                 $pagination->setUsedRoute($routeParams['locale'] == 'pl'
  131.                     'fmUniqskillsFrontendPlCourseCatalogue'
  132.                     'fmUniqskillsFrontendCourseCatalogue');
  133.             }
  134.         }
  135.         else
  136.         {
  137.             $pagination->setUsedRoute($routeParams['locale'] == 'pl'
  138.                 'fmUniqskillsFrontendPlCatalogueSubCategory'
  139.                 'fmUniqskillsFrontendCatalogueSubCategory');
  140.         }
  141.         return $pagination;
  142.     }
  143.     public function prepareBreadcrumbs($category$subCategory): array
  144.     {
  145.         /** @var Category $categoryEntity */
  146.         /** @var Category $subCategoryEntity */
  147.         $breadcrumbs = [];
  148.         if ($category)
  149.         {
  150.             $categoryEntity $this->em->getRepository(Category::class)->findOneByCategorySlug($category);
  151.             ($categoryEntity)
  152.                 ? $breadcrumbs[] = $categoryEntity->getCategoryName()
  153.                 : $breadcrumbs[] = $category;
  154.         }
  155.         if ($subCategory)
  156.         {
  157.             $subCategoryEntity $this->em->getRepository(Category::class)->findOneByCategorySlug($category);
  158.             ($subCategoryEntity)
  159.                 ? $breadcrumbs[] = $subCategoryEntity->getCategoryName()
  160.                 : $breadcrumbs[] = $subCategory;
  161.         }
  162.         return $breadcrumbs;
  163.     }
  164.     public function prepareKeywords($category$subCategory$locale$userOrders)
  165.     {
  166.         $keywords $this->em
  167.             ->getRepository(CourseKeyword::class)
  168.             ->findAllForCategory($subCategory ?: $category$locale);
  169.         foreach ($keywords as $key => $keyword)
  170.         {
  171.             //counter for how many times keyword was used
  172.             $keywords[$key]['count'] = $this->em->getRepository(Course::class)
  173.                 ->countByKeywordAndLang($keyword['name'], $locale$userOrders);
  174.         }
  175.         return $keywords;
  176.     }
  177. }