<?php
namespace App\Utils\Uniqskills;
use App\Entity\Gos\Category;
use App\Entity\Gos\Country;
use App\Entity\Gos\PortalSettings;
use App\Entity\Gos\ProductVariant;
use App\Entity\Gos\Uniqskills\Course;
use App\Entity\Gos\Uniqskills\CourseKeyword;
use App\Entity\Gos\User;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\Pagination\SlidingPagination;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Utils\Geolocation;
class CourseCatalog
{
private EntityManagerInterface $em;
private PaginatorInterface $knpPaginator;
private Geolocation $geoLocation;
private TokenStorageInterface $tokenStorage;
private ParameterBagInterface $parameterBag;
private $user;
public function __construct(
EntityManagerInterface $em,
PaginatorInterface $knpPaginator,
TokenStorageInterface $tokenStorage,
Geolocation $geoLocation,
ParameterBagInterface $parameterBag
) {
$this->em = $em;
$this->knpPaginator = $knpPaginator;
$this->geoLocation = $geoLocation;
$this->tokenStorage = $tokenStorage;
$this->user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$this->parameterBag = $parameterBag;
}
public function getCountry(Request $request)
{
if ($request->getSession()->get('fixCountryCode'))
{
$country = $this->em->getRepository(Country::class)->findOneByAlpha2(
$request->getSession()->get('fixCountryCode')
);
}
else if ($request->getSession()->get('myCountryCode'))
{
$country = $this->em->getRepository(Country::class)->findOneByAlpha2(
$request->getSession()->get('myCountryCode')
);
}
else if (!is_object($this->user))
{
$country = $this->em->getRepository(Country::class)->findOneByAlpha2(
$this->geoLocation->geolocation()['countryCode']
);
}
else
{
$country = $this->user->getCountry();
}
if (is_null($country))
{
$country = $this->em->getRepository(Country::class)->findOneByAlpha2('PL');
}
return $country;
}
public function getUserCourse()
{
$userCourse = [];
if (is_object($this->user))
{
$userCourse = $this->em->getRepository(Course::class)->findUserCourses($this->user->getId());
}
return $userCourse;
}
public function preparePagination(
array $routeParams,
array $userOrders,
Country $country,
PortalSettings $portalSettings
): PaginationInterface
{
$courses = $this->em
->getRepository(Course::class)
->findAllFiltered(
null,
null,
[
'language' => $routeParams['locale'],
'category' => $routeParams['subCategory'] ?: $routeParams['category'],
'isActive' => true,
'finishedAtCheck' => true,
'keyword' => $routeParams['keyword'],
'duration' => $routeParams['duration'],
'priceRange' => $routeParams['priceRange'],
'portalSettings' => $portalSettings->getId()
],
$routeParams['searchQuery'],
$userOrders
)
->getQuery()->getResult();
$limit = $portalSettings->getHash() != $this->parameterBag->get('pl_us_portal_settings') ? 30 : 5;
$pagination = $this->knpPaginator->paginate(
$courses,
$routeParams['page'],
$limit
);
$pagination->setTemplate('/uniqskills/base/nano_pagination.html.twig');
//for course price
foreach ($pagination as $item)
{
/** @var Course $item */
$item->countryProduct = $this->em->getRepository(ProductVariant::class)
->findAllByCourseAndCountry($item->getSlug(), $country->getName());
}
//set proper route for pagination
if (empty($routeParams['subCategory']))
{
if (empty($routeParams['category']))
{
$pagination->setUsedRoute($routeParams['locale'] == 'pl'
? 'fmUniqskillsFrontendPlCourseCataloguePage'
: 'fmUniqskillsFrontendCourseCataloguePage');
}
else
{
$pagination->setUsedRoute($routeParams['locale'] == 'pl'
? 'fmUniqskillsFrontendPlCourseCatalogue'
: 'fmUniqskillsFrontendCourseCatalogue');
}
}
else
{
$pagination->setUsedRoute($routeParams['locale'] == 'pl'
? 'fmUniqskillsFrontendPlCatalogueSubCategory'
: 'fmUniqskillsFrontendCatalogueSubCategory');
}
return $pagination;
}
public function prepareBreadcrumbs($category, $subCategory): array
{
/** @var Category $categoryEntity */
/** @var Category $subCategoryEntity */
$breadcrumbs = [];
if ($category)
{
$categoryEntity = $this->em->getRepository(Category::class)->findOneByCategorySlug($category);
($categoryEntity)
? $breadcrumbs[] = $categoryEntity->getCategoryName()
: $breadcrumbs[] = $category;
}
if ($subCategory)
{
$subCategoryEntity = $this->em->getRepository(Category::class)->findOneByCategorySlug($category);
($subCategoryEntity)
? $breadcrumbs[] = $subCategoryEntity->getCategoryName()
: $breadcrumbs[] = $subCategory;
}
return $breadcrumbs;
}
public function prepareKeywords($category, $subCategory, $locale, $userOrders)
{
$keywords = $this->em
->getRepository(CourseKeyword::class)
->findAllForCategory($subCategory ?: $category, $locale);
foreach ($keywords as $key => $keyword)
{
//counter for how many times keyword was used
$keywords[$key]['count'] = $this->em->getRepository(Course::class)
->countByKeywordAndLang($keyword['name'], $locale, $userOrders);
}
return $keywords;
}
}