<?php
namespace EADPlataforma\Twig;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFunction;
use EADPlataforma\Services\GeneralService;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Entity\Category;
use EADPlataforma\Entity\Page;
use EADPlataforma\Entity\Notification;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\CourseCertificate;
use EADPlataforma\Entity\Configuration;
use EADPlataforma\Entity\User;
use EADPlataforma\Enum\AbstractEnum;
use EADPlataforma\Enum\Eadmin\ClientEnum;
use EADPlataforma\Enum\ClientEnum AS CliEnum;
use EADPlataforma\Enum\NotificationEnum;
use EADPlataforma\Enum\PageEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Enum\EnrollmentEnum;
class VariablesExtension extends AbstractExtension implements GlobalsInterface
{
/**
* @var GeneralService
*/
private $generalService;
/**
* @var SchoolEntityManager
*/
private $em;
/**
* @var SchoolEntityManager
*/
private $emEadmin;
/**
* @var string
*/
private $cdn;
/**
* @var SchoolEntityManager
*/
private $userSessionService;
/**
* @var SchoolEntityManager
*/
private $configuration;
/**
* @var SchoolEntityManager
*/
private $userPermissionUtil;
/**
* Constructor
*
* @param GeneralService $generalService
*/
public function __construct(GeneralService $generalService)
{
$this->generalService = $generalService;
$this->em = $this->generalService->getService('SchoolEntityManager');
$this->userSessionService = $this->generalService->getService('UserSessionService');
$this->configuration = $this->generalService->getService('ConfigurationService');
$container = $this->generalService->getContainer();
$this->cdn = "{$container->getParameter('ead-cdn')}assets/";
}
public function getUserPermissionUtil()
{
if(!$this->userPermissionUtil){
$this->userPermissionUtil = $this->generalService->getUtil('UserPermissionUtil');
}
return $this->userPermissionUtil;
}
public function getGlobals(): array
{
$user = $this->userSessionService->getUser();
$session = $this->userSessionService->getSession();
$client = $this->configuration->getClient();
$domain = $this->configuration->getActiveDomain(true);
$poRepository = $this->em->getRepository(ProductOffer::class);
$categoryRepository = $this->em->getRepository(Category::class);
$notificationRepository = $this->em->getRepository(Notification::class);
$pageRepository = $this->em->getRepository(Page::class);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$certificateRepository = $this->em->getRepository(CourseCertificate::class);
$configurationRepository = $this->em->getRepository(Configuration::class);
$userRepository = $this->em->getRepository(User::class);
$countEnrollment = null;
$countCertificate = null;
$countWish = null;
$whatsapp = preg_replace("/[^0-9]/", "", $this->configuration->get("whatsapp"));
$categories = $categoryRepository->getCategories();
$userTeacherSpotlight = $userRepository->getUserTeacherSpotlight();
$pages = [];
$pagesNavbar = count($pages);
$pagesFooter = count($pages);
if($this->configuration->isModuleActive('pages_module')){
$pages = $pageRepository->findBy(
[
"deleted" => PageEnum::ITEM_NO_DELETED,
"status" => PageEnum::PUBLISHED
],
[ "order" => "ASC"]
);
$pagesNavbar = $pageRepository->count([
"deleted" => PageEnum::ITEM_NO_DELETED,
"status" => PageEnum::PUBLISHED,
"showType" => PageEnum::NAVBAR
]);
$pagesFooter = $pageRepository->count([
"deleted" => PageEnum::ITEM_NO_DELETED,
"status" => PageEnum::PUBLISHED,
"showType" => PageEnum::FOOTER
]);
}
$platformType = $client->getPlatformType();
$isRestricted = ($platformType == CliEnum::PLATFORM_TYPE_RESTRICTED);
$isRestrictedUser = (($isRestricted && $user) || !$isRestricted);
$eadDomain = null;
if($client){
$eadDomain = $client->getDomainPrimary();
}
$menuResume = (object)[
"text" => $this->configuration->getLanguage('resume', 'head'),
"link" => $this->generalService->generateUrl('resume'),
"show" => ( $user && $isRestrictedUser ),
"class" => "resume"
];
$menuPages = (object)[
"text" => $this->configuration->getLanguage('pages', 'head'),
"dropdown" => true,
"show" => (!empty($pagesNavbar) && $isRestrictedUser),
"class" => "pages"
];
$infoOffer = $poRepository->getPublicStoreInfo();
$offerNumber = $infoOffer->offerNumber;
$offerSubNumber = $infoOffer->offerSubNumber;
$offerComboNumber = $infoOffer->offerComboNumber;
$subModule = $infoOffer->subModule;
$hasProducts = $infoOffer->hasProducts;
//check sell course
$showCourses = false;
$showPlans = false;
$showCombos = false;
$showForuns = false;
if($isRestricted){
$showCourses = ($user && $offerNumber > 0);
$showPlans = ($user && ($offerSubNumber > 0 && $subModule));
$showCombos = ($user && $offerComboNumber > 0);
$showForuns = (
$user && $this->configuration->showForum(( $user ? true : false ))
);
}else{
$showCourses = ($offerNumber > 0);
$showPlans = ($offerSubNumber > 0 && $subModule);
$showCombos = ($offerComboNumber > 0);
$showForuns = $this->configuration->showForum(( $user ? true : false ));
}
$menuCourse = (object)[
"text" => $this->configuration->getLanguage('courses', 'head'),
"link" => $this->generalService->generateUrl(
'productListCourses',
[ "type" => "courses" ]
),
"show" => $showCourses && $isRestrictedUser,
"class" => "courses"
];
$menuPlan = (object)[
"text" => $this->configuration->getLanguage('plans', 'head'),
"link" => $this->generalService->generateUrl(
'productListPlans',
[ "type" => "plans" ]
),
"show" => $showPlans && $isRestrictedUser,
"class" => "plans"
];
$menuCombo = (object)[
"text" => $this->configuration->getLanguage('combos', 'head'),
"link" => $this->generalService->generateUrl('productListCombos'),
"show" => $showCombos && $isRestrictedUser,
"class" => "combos"
];
$menuForum = (object)[
"text" => $this->configuration->getLanguage('forum', 'head'),
"link" => $this->generalService->generateUrl('forum'),
"show" => $showForuns && $isRestrictedUser,
"class" => "forum"
];
// MENU MOBILE
$menuMobilePages = (object)[
"text" => $this->configuration->getLanguage('pages', 'head'),
"link" => 'pages',
"slide" => true,
"show" => (!empty($pagesNavbar) && $isRestrictedUser),
"class" => "pages"
];
$menuMobileCategorias = (object)[
"text" => $this->configuration->getLanguage('categories', 'head'),
"link" => 'categorias',
"slide" => true,
"show" => (!empty($categories) && $isRestrictedUser),
"class" => "categories"
];
$menuMobileNotificacoes = (object)[
"text" => $this->configuration->getLanguage('notifications', 'head'),
"link" => 'notificacoes',
"slide" => true,
"show" => ( $user && $isRestrictedUser ),
];
$notificationNumber = 0;
if($user){
$notificationNumber = $notificationRepository->count([
"userTo" => $user->getId(),
"status" => NotificationEnum::NOT_VIEWED,
]);
$countEnrollment = $enrollmentRepository->countResumeEnrollment(
$user->getId(),
[],
null
);
$countCertificate = $certificateRepository->getCountResumeCourseCertificate(
$user->getId()
);
$countWish = $poRepository->getCountResumeWhishList($user->getId());
}
$countSocial = $configurationRepository->countSocial();
$request = $this->generalService->getRequest();
$isAppDomain = (strpos($request->getHost(), ".eadplataforma.app") !== false);
$actualDomain = ($request ? $request->getHost() : $domain);
$userId = $user ? $user->getId() : null;
$upPath = AbstractEnum::PATH_UPLOAD;
$othersPath = AbstractEnum::PATH_OTHERS;
$clientConfig = $this->configuration->getClientConfiguration();
$clientVersion = $clientConfig->getClientVersion();
$activeMetrics = $clientConfig->getActiveMetrics();
$favicon = $this->configuration->get('favicon');
$favicon = (
!empty($favicon) ? "//{$actualDomain}{$upPath}{$othersPath}/{$favicon}" : null
);
$globalConfig = $this->configuration->getGlobalConfiguration();
$platformType = $client->getPlatformType();
$isRestricted = ($platformType == CliEnum::PLATFORM_TYPE_RESTRICTED);
$clientPathVersion = ClientEnum::CLIENT_VERSION_PATH[$clientVersion];
$assetsVersion = $globalConfig->getAssetsVersion();
$paymentConfig = $this->configuration->getPaymentConfig();
$userToken = null;
if($session){
$userToken = md5($clientConfig->getId()) . md5(
$session->getId() . $session->getToken()
);
}
$csrfToken = $this->generalService->verifySignedCookie('csrf_token');
if(!$csrfToken){
$csrfToken = bin2hex(random_bytes(32));
$this->generalService->setCookieSign('csrf_token', $csrfToken, time() + 3600);
}
$swToken = "BN85y68oIhTmtxsq18XAn_8VtQ4IN8cx6En2tMXIjsT5veiXwJdFAvln5CClS7ptKc4vEIGOnqiwZjE4Fy8yIXU";
return [
"csrfToken" => $csrfToken,
"swToken" => $swToken,
"activeMetrics" => $activeMetrics,
"menu" => [
$menuResume,
$menuPages,
$menuCourse,
$menuPlan,
$menuCombo,
$menuForum,
],
"menuMobile" => [
$menuResume,
$menuMobilePages,
$menuMobileCategorias,
$menuCourse,
$menuPlan,
$menuCombo,
$menuForum,
],
"user" => $user,
"userId" => $user ? $user->getId() : null,
"userPermissionApi" => $this->getUserPermissionUtil()->getPermission(
"appStore",
"api"
),
"session" => $session,
"userToken" => $userToken,
"canAccessAdm" => $this->getUserPermissionUtil()->canAccessAdm(),
"categories" => $categories,
"pages" => $pages,
"client" => $client,
"clientVersion" => $clientPathVersion,
"domain" => $domain,
"eadDomain" => $eadDomain,
"actualDomain" => $actualDomain,
"configuration" => $this->configuration->getAllConfig(),
"year" => date('Y'),
"totalNotifications" => $notificationNumber,
"countEnrollment" => $countEnrollment,
"countCertificate" => $countCertificate,
"countWish" => $countWish,
"versionSite" => $assetsVersion->{"site_{$clientPathVersion}"},
"versionAdmin" => $assetsVersion->{"admin_{$clientPathVersion}"},
"versionLesson" => $assetsVersion->{"lesson_{$clientPathVersion}"},
"countSocial" => $countSocial,
"cdnVersion" => strtotime(date('Y-m-d H:i:s')),
"cdn" => $this->cdn,
"cdnLivestream" => "//livestream.com/assets/plugins/referrer_tracking.js",
"cdnSambatech" => "//player.sambatech.com.br/stable/js/samba.player.api.js",
"cdnGoogleFonts" => "//fonts.googleapis.com/css2",
"favicon" => $favicon,
"faviconCdn" => "{$this->cdn}img/favicon.ico",
"whatsapp" => $whatsapp,
"scHash" => md5($userId . $actualDomain . strtotime(date('Y-m-d H:i:s'))),
"menuMobileNotificacoes" => $menuMobileNotificacoes,
"userTeacherSpotlight" => $userTeacherSpotlight,
"pagesFooter" => $pagesFooter,
"hasProducts" => $hasProducts,
"isRestricted" => $isRestricted,
"isAppDomain" => $isAppDomain,
"isBrazilDefault" => true,
"isForumStatusDefault" => true,
"isDarkTheme" => ($this->configuration->get('website_theme') == 'dark'),
"isLocal" => $this->configuration->isLocal() && $this->configuration->isDebug(),
"debug" => $this->configuration->isDebug(),
"hideBgDeco" => (bool)($this->configuration->get('hide_background_decoration')),
"feeInstallmentInterest" => $paymentConfig->installmentInterest,
"isTrial" => $this->configuration->isTrial(),
"cdnFontawesome" => "https://kit.fontawesome.com/171d5efe0c.js",
];
}
}