<?php
namespace EADPlataforma\Controller\Website;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\Country;
use EADPlataforma\Entity\UserCard;
use EADPlataforma\Entity\UserCustomField;
use EADPlataforma\Entity\UserCheckoutInfo;
use EADPlataforma\Entity\CourseCertificate;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Forum;
use EADPlataforma\Entity\LikeControl;
use EADPlataforma\Entity\Receiver;
use EADPlataforma\Enum\ReceiverEnum;
use EADPlataforma\Enum\ForumEnum;
use EADPlataforma\Enum\LikeControlEnum;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\GeoDataEnum;
use EADPlataforma\Enum\UserCardEnum;
use EADPlataforma\Enum\UserCustomFieldEnum;
use EADPlataforma\Enum\UserCheckoutInfoEnum;
use EADPlataforma\Enum\CourseCertificateEnum;
use EADPlataforma\Enum\EnrollmentEnum;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class UserController extends AbstractWebsiteController {
/**
* @Route(
* path = "/profile/",
* name = "userProfile",
* methods = {"GET"}
* )
*/
public function getUserProfile(Request $request) {
$this->checkUserSession($request);
$cardTotal = $this->em->getRepository(UserCard::class)->count([
"user" => $this->user->getId(),
"deleted" => UserCardEnum::ITEM_NO_DELETED
]);
$userCheckoutTotal = $this->em->getRepository(UserCheckoutInfo::class)->count([
"user" => $this->user->getId(),
"deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
]);
$fields = [];
if($this->configuration->checkModuleIsAbleOnPlan('customFormModule')){
$fields = $this->em->getRepository(UserCustomField::class)->findBy([
"deleted" => UserCustomFieldEnum::ITEM_NO_DELETED,
"status" => UserCustomFieldEnum::PUBLISHED,
],
[ "order" => "ASC" ]);
$fieldsValues = json_decode($this->user->getCustomField());
foreach ($fields as $key => $field) {
$field->value = null;
$field->optionsObj = null;
$options = $field->getOptions();
if(!empty($options)){
$field->optionsObj = json_decode($options);
}
if(!empty($fieldsValues->{$field->getId()})){
$objSave = $fieldsValues->{$field->getId()};
if(!empty($objSave->value)){
$field->value = $objSave->value;
}
}
if($field->getType() == UserCustomFieldEnum::FIELD_MULTIPLE){
foreach ($field->optionsObj as $keyOp => $option) {
$option->checked = false;
if(!empty($field->value)){
foreach ($field->value as $keyVal => $valueOp) {
if($option->value == $valueOp){
$option->checked = true;
}
}
}
$field->optionsObj[$keyOp] = $option;
}
}
$fields[$key] = $field;
}
}
$receiverRepository = $this->em->getRepository(Receiver::class);
$pagarMeBankAccount = $this->generalService->getService(
'PagarMe\\PagarMeBankAccount'
);
$banks = $pagarMeBankAccount->getBanks();
$banksData = [];
foreach ($banks as $key => $bank) {
$banksData[] = (object)[
"id" => (string)$key,
"name" => $bank
];
}
if($this->user){
$googleAuthenticatorService = $this->generalService->getService('GoogleAuthenticatorService');
$googleAuthenticatorService->setEmail($this->user->getEmail());
$dataAuthentication = $googleAuthenticatorService->generateQrCodeAuthentication();
$this->data['secretKey'] = $dataAuthentication['secretKey'];
$this->data['qrCode'] = $dataAuthentication['qrCode'];
}
$this->data['banksData'] = $banksData;
$this->data['totalReceiver'] = $receiverRepository->countByType(
ReceiverEnum::EAD_CHECKOUT,
$this->user->getId()
);
$this->data['cardTotal'] = $cardTotal;
$this->data['userCheckoutTotal'] = $userCheckoutTotal;
$this->data['fields'] = $fields;
return $this->renderEAD('user/profile/user-profile-board.html.twig');
}
/**
* @Route(
* path = "/p/{username}",
* name = "userProfilePublic",
* methods = {"GET"}
* )
*/
public function getUserProfilePublic(Request $request) {
$this->checkUserSession($request);
$username = $request->get('username');
$userRepository = $this->em->getRepository(User::class);
$user = $userRepository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"username" => $username,
]);
if(!$user){
return $this->redirectToRoute('notFound');
}
$forumRepository = $this->em->getRepository(Forum::class);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$likeControlRepository = $this->em->getRepository(LikeControl::class);
$certificateRepository = $this->em->getRepository(CourseCertificate::class);
$enrollments = $enrollmentRepository->getResumeEnrollment(
$user->getId()
);
$totalCoursesCompleted = $enrollmentRepository->countResumeEnrollment(
$user->getId(),
[ EnrollmentEnum::SITUATION_COMPLETED ]
);
$lastCertificate = $certificateRepository->findOneBy(
[
"user" => $user->getId(),
"deleted" => CourseCertificateEnum::ITEM_NO_DELETED,
],
[ "id" => "DESC"]
);
$totalForum = $forumRepository->count([
"user" => $user->getId(),
"forum" => null,
"deleted" => ForumEnum::ITEM_NO_DELETED,
]);
$totalLike = $likeControlRepository->count([
"userFrom" => $user->getId(),
]);
$this->data['pUser'] = $user;
$this->data['enrollments'] = $enrollments;
$this->data['lastCertificate'] = $lastCertificate;
$this->data['totalCoursesCompleted'] = $totalCoursesCompleted;
$this->data['totalForum'] = $totalForum;
$this->data['totalLike'] = $totalLike;
return $this->renderEAD('user/profile/user-profile.html.twig');
}
/**
* @Route(
* path = "/p/{userId}/courses",
* name = "userProfileCourses",
* methods = {"GET"}
* )
*/
public function getUserProfileCourses(Request $request) {
$userId = $request->get('userId');
$userRepository = $this->em->getRepository(User::class);
$user = $userRepository->findOneBy([
"deleted" => UserEnum::ITEM_NO_DELETED,
"id" => $userId,
]);
if(!$user){
return $this->redirectToRoute('notFound');
}
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollments = $enrollmentRepository->getResumeEnrollment(
$this->user->getId(),
[],
null,
null,
null,
null,
2
);
$this->data['enrollments'] = $enrollments;
return $this->renderEAD('user/profile/user-profile-courses.html.twig');
}
}