<?php
namespace EADPlataforma\Controller\Website;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use EADPlataforma\Entity\Faq;
use EADPlataforma\Entity\Forum;
use EADPlataforma\Entity\Banner;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Course;
use EADPlataforma\Entity\CourseCertificate;
use EADPlataforma\Entity\CourseTestimonial;
use EADPlataforma\Entity\Product;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Entity\Whishlist;
use EADPlataforma\Entity\Category;
use EADPlataforma\Entity\Client;
use EADPlataforma\Entity\PageSection;
use EADPlataforma\Enum\AbstractEnum;
use EADPlataforma\Enum\FaqEnum;
use EADPlataforma\Enum\ClientEnum;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\ForumEnum;
use EADPlataforma\Enum\BannerEnum;
use EADPlataforma\Enum\CourseEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Enum\CourseCertificateEnum;
use EADPlataforma\Enum\CourseTestimonialEnum;
use EADPlataforma\Enum\WhishlistEnum;
use EADPlataforma\Enum\EnrollmentEnum;
use EADPlataforma\Enum\PageSectionEnum;
use EADPlataforma\Util\StringUtil;
use \setasign\FpdiProtection\FpdiProtection;
use \setasign\Fpdi\Fpdi;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class HomeController extends AbstractWebsiteController {
/**
* @Route(
* path = "/",
* name = "home",
* methods = {"GET"}
* )
*/
public function homePage(Request $request)
{
if(!$this->isPlatformActive()){
$this->data['platformStatus'] = $this->clientConfig->getPlatformStatus();
return $this->renderEAD('home/restricted.html.twig');
}
$productOfferRepository = $this->em->getRepository(ProductOffer::class);
$platformType = $this->client->getPlatformType();
if($platformType == ClientEnum::PLATFORM_TYPE_RESTRICTED){
$numberOffer = $productOfferRepository->countPublicProductOffers();
if(!$this->user || empty($numberOffer)){
return $this->redirectToRoute('resume');
}
}
$banners = null;
if($this->configuration->isModuleActive("banner_module")){
$bannerRepository = $this->em->getRepository(Banner::class);
$banners = $bannerRepository->findItems(true);
}
$faqs = null;
if($this->configuration->isModuleActive("faq_module")){
$faqRepository = $this->em->getRepository(Faq::class);
$faqs = $faqRepository->findByEAD([
"type" => FaqEnum::GENERAL,
"deleted" => FaqEnum::ITEM_NO_DELETED,
], [ "order" => "ASC" ]);
}
$courseTestimonialRepository = $this->em->getRepository(CourseTestimonial::class);
$courseTestimonials = $courseTestimonialRepository->getTestimonialApprovedRandom();
foreach ($courseTestimonials as $key => $value) {
$value['testimonial'] = StringUtil::encodeStringStatic($value['testimonial']);
$value['userName'] = StringUtil::encodeStringStatic($value['userName']);
$courseTestimonials[$key] = $value;
}
$categoryRepository = $this->em->getRepository(Category::class);
$productCategories = $categoryRepository->getCategories();
$userRepository = $this->em->getRepository(User::class);
$teachers = $userRepository->getUserTeacherSpotlightRandom(6);
$foumNumber = $this->em->getRepository(Forum::class)->countEAD([
"forum" => null,
"deleted" => ForumEnum::ITEM_NO_DELETED,
]);
$userNumber = $userRepository->countAllUsers(UserEnum::ACTIVE);
$courseNumber = $this->em->getRepository(Course::class)->countEAD([
"status" => CourseEnum::PUBLISHED,
"deleted" => CourseEnum::ITEM_NO_DELETED,
]);
$courseCertificateRepository = $this->em->getRepository(CourseCertificate::class);
$courseCertificateNumber = $courseCertificateRepository->countEAD([
"deleted" => CourseCertificateEnum::ITEM_NO_DELETED,
]);
$courseTestimonialNumber = $courseTestimonialRepository->countEAD([
"status" => CourseTestimonialEnum::APPROVED,
"deleted" => CourseTestimonialEnum::ITEM_NO_DELETED,
]);
$this->data['banners'] = $banners;
$this->data['faqs'] = $faqs;
$this->data['courseTestimonials'] = $courseTestimonials;
$this->data['productCategories'] = $productCategories;
$this->data['foumNumber'] = $foumNumber;
$this->data['userNumber'] = $userNumber;
$this->data['courseNumber'] = $courseNumber;
$this->data['courseCertificateNumber'] = $courseCertificateNumber;
$this->data['courseTestimonialNumber'] = $courseTestimonialNumber;
$sectionConfigHome = json_decode($this->configuration->get('section_config_home'));
$pageSectionRepository = $this->em->getRepository(PageSection::class);
$pageSections = $pageSectionRepository->findBy([
"status" => PageSectionEnum::PUBLISHED,
"deleted" => PageSectionEnum::ITEM_NO_DELETED
], [ "order" => "ASC"]);
$sections = [
"home_banner" => "bannerSection",
"search_bar" => "searchSection",
];
foreach ($pageSections as $key => $pageSection) {
$idx = "page-section-{$pageSection->getId()}";
$sections[$idx] = $pageSection;
if(!isset($sectionConfigHome->{$idx})){
$sectionConfigHome->{$idx} = (object)[
"active" => $pageSection->getStatus(),
"background" => $pageSection->getBackground(),
"order" => ($key + 1),
];
}
}
$sectionConfigHome = (array)$sectionConfigHome;
uksort($sectionConfigHome, function($a, $b) use($sectionConfigHome){
return $sectionConfigHome[$a]->order > $sectionConfigHome[$b]->order;
});
$sections["home_categories"] = "productCategorySection";
$sections["home_teachers"] = "teacherSection";
$sections["home_numbers"] = "schoolNumberSection";
$sections["home_faq"] = "faqSection";
$sections["home_testimonial"] = "testimonialSection";
$sections["home_newsletter"] = "newsSection";
$sections["home_social"] = "socialSection";
$sections["home_institutional"] = "institutionalSection";
$sections["stamps_section"] = "stampsSection";
$homeLayoutSections = [];
foreach ($sectionConfigHome as $key => $value) {
if(isset($sections[$key])){
if($sections[$key] instanceof PageSection){
$homeLayoutSections[$key] = $sections[$key];
}else{
$homeLayoutSections[$sections[$key]] = $value;
}
}
}
/*foreach ($sections as $key => $value) {
if(isset($sectionConfigHome->{$key})){
if($value instanceof PageSection){
$homeLayoutSections[$key] = $sections[$key];
}else{
$homeLayoutSections[$value] = $sectionConfigHome->{$key};
}
}else if($value instanceof PageSection){
$homeLayoutSections[$key] = $sections[$key];
}
}*/
$applyBkg = function(string $sectionName) use ($homeLayoutSections) {
if(isset($homeLayoutSections[$sectionName])){
if(isset($homeLayoutSections[$sectionName]->background)){
return $homeLayoutSections[$sectionName]->background == ClientEnum::YES;
}
}
return false;
};
$this->data['bannerSection'] = (object)[
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('bannerSection'),
];
$this->data['searchSection'] = (object)[
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('searchSection'),
];
$this->data['faqSection'] = (object)[
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('faqSection'),
];
$this->data['testimonialSection'] = (object)[
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('testimonialSection'),
];
$this->data['newsSection'] = (object)[
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('newsSection'),
];
$this->data['stampsSection'] = (object)[
"background" => $applyBkg('stampsSection'),
];
$this->data['productCategorySection'] = (object)[
"background" => $applyBkg('productCategorySection'),
];
$this->data['socialSection'] = (object)[
"background" => $applyBkg('socialSection'),
];
$this->data['institutionalSection'] = (object)[
"background" => $applyBkg('institutionalSection'),
];
$this->data['schoolNumberSection'] = (object)[
"background" => $applyBkg('schoolNumberSection'),
];
$this->data['teacherSection'] = (object)[
"title" => $this->configuration->getLanguage('featured_teachers', 'home'),
"subtitle" => $this->configuration->getLanguage('learn_from_the_best', 'home'),
"teachers" => $teachers,
"isCenterTitle" => true,
"showSubtitle" => true,
"background" => $applyBkg('teacherSection'),
"showBtnToAll" => !empty($teachers) ? true : false,
];
$sectionKeys = array_keys($homeLayoutSections);
$searchAfterBanner = (
$applyBkg('searchSection') == false &&
array_search('searchSection', $sectionKeys) == 1 + array_search('bannerSection', $sectionKeys)
);
$this->data['homeLayoutSections'] = $homeLayoutSections;
$this->data['searchAfterBanner'] = $searchAfterBanner;
$this->data['keyCaptcha'] = $this->createCaptchaKey($request);
return $this->renderEAD('home/home.html.twig');
}
/**
* @Route(
* path = "/mencached",
* name = "testRoute",
* methods = {"GET"}
* )
*/
public function testRoute(Request $request) {
print_r(date('Y-m-d H:i:s'));exit;
$memcacheService = $this->generalService->getService('MemcacheService');
$memcacheService->saveData('testRoute', 'teqrwaesfsdfsdf');
$data = $memcacheService->getData('testRoute');
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/front/helper",
* name = "frontHelperRoute",
* methods = {"GET"}
* )
*/
public function frontHelperRoute(Request $request) {
$this->checkUserSession($request);
if($this->user->getId() != 1){
return $this->redirectToRoute('notFound');
}
$images = [
"add2home.svg",
"bloco.png",
"camera.svg",
"carrinho_vazio.png",
"conclusion_fail_2.png",
"conclusion_fail_3.png",
"conclusion_fail.png",
"conclusion_success.png",
"conclusion_wait.png",
"conclusion_warning.png",
"cover_default.jpg",
"emoji_crying.png",
"favion.png",
"logo-ead.png",
"logo-ead.svg",
"logo-footer.jpg",
"logotipo-ead-plataforma.svg",
"medalha.svg",
"paper.svg",
"payment1.jpg",
"payment2.jpg",
"payment3.jpg",
"payment4.jpg",
"payment5.jpg",
"payment6.jpg",
"payment7.jpg",
"payment8.jpg",
"pb-eadplataforma.png",
"share.svg",
"stamp1.jpg",
"stamp2.jpg",
"stamp3.jpg",
"teste_cover_default.jpg",
"trofeu.svg",
"user.svg",
"without-about-icon.svg",
"without-comment-icon.svg",
"without-files-icon.svg",
"without-notes-icon.svg",
];
$cdn = "{$this->generalService->getContainer()->getParameter('ead-cdn')}assets/img/";
foreach ($images as $key => $image) {
$images[$key] = $cdn . $image;
}
$this->data['images'] = $images;
return $this->renderEAD('includes/front.helper.html.twig');
}
/**
* @Route(
* path = "/adm",
* name = "adminEad",
* methods = {"GET"}
* )
*/
public function adminEAD(Request $request) {
$this->checkUserSession($request);
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->redirectToRoute('notFound');
}
return $this->renderEAD('admin.html.twig', 'admin');
}
/**
* @Route(
* path = "/cancelar",
* name = "cancelarPlan",
* methods = {"GET"}
* )
*
* @Route(
* path = "/cancel",
* name = "cancelPlan",
* methods = {"GET"}
* )
*/
public function cancelPlan(Request $request) {
$this->checkUserSession($request);
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->redirectToRoute('notFound');
}
return $this->redirect("/adm/cancelar", 301);
}
/**
* @Route(
* path = "/gratis",
* name = "gratisPlan",
* methods = {"GET"}
* )
*
* @Route(
* path = "/free",
* name = "freePlan",
* methods = {"GET"}
* )
*/
public function freePlan(Request $request) {
$this->checkUserSession($request);
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->redirectToRoute('notFound');
}
return $this->redirect("/adm/gratis", 301);
}
/**
* @Route(
* path = "/adm/{path}",
* requirements = {"path"=".+"},
* defaults = {"path": null},
* name = "adminEadPath",
* methods = {"GET"}
* )
*/
public function adminEADPath(Request $request) {
$this->checkUserSession($request);
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->redirectToRoute('notFound');
}
return $this->renderEAD('admin.html.twig', 'admin');
}
/**
* @Route(
* path = "/not-found",
* name = "notFound",
* methods = {"GET"}
* )
*/
public function notFoundPage(Request $request) {
return $this->renderEAD('status/error-page.html.twig');
}
/**
* @Route(
* path = "/manifest.json",
* name = "manifest",
* methods = {"GET"}
* )
*/
public function getManifest(Request $request){
$domain = ($request ? $request->getHost() : $this->client->getDomainPrimary());
$upPath = AbstractEnum::PATH_UPLOAD;
$othersPath = AbstractEnum::PATH_OTHERS;
$favicon = $this->configuration->get("favicon");
$favicon = (!empty($favicon) ? "//{$domain}{$upPath}{$othersPath}/{$favicon}" : null);
$cdn = "{$this->generalService->getContainer()->getParameter('ead-cdn')}assets/img/";
$favicon192 = (!empty($favicon) ? "{$favicon}?option=manifest-1" : "{$cdn}favicon192x192.png");
$favicon512 = (!empty($favicon) ? "{$favicon}?option=manifest-2" : "{$cdn}favicon512x512.png");
$icons = [];
$icons[] = (object)[
"src" => $favicon192,
"type" => "image/png",
"sizes" => "192x192",
];
$icons[] = (object)[
"src" => $favicon512,
"type" => "image/png",
"sizes" => "512x512",
];
$primaryColor = $this->configuration->get("primary_color");
$secondColor = $this->configuration->get("second_color");
$data = (object)[
"short_name" => $this->client->getBrand(),
"name" => $this->client->getBrand(),
"description" => $this->client->getDescription(),
"id" => "./?utm_source=pwa",
"icons" => $icons,
"start_url" => "./?utm_source=pwa",
"background_color" => (!empty($primaryColor) ? $primaryColor : '#CCCCCC'),
"display" => "standalone",
"scope" => "/",
"theme_color" => (!empty($secondColor) ? $secondColor : '#BBBBBB'),
"gcm_sender_id" => "975354790337",
];
$json = json_encode($data);
return new JsonResponse($json, 200, [
'Content-Type' => 'application/json; charset=utf-8'
], true);
}
/**
* @Route(
* path = "/sitemap.xml",
* name = "sitemap",
* methods = {"GET"}
* )
*/
public function getSitemap(){
$xml = $this->generalService->getService('SiteMapService')->generateSiteMap();
return new Response($xml, 200, [
'Content-Type' => 'text/xml; charset=utf-8'
], true);
}
}