<?php
namespace Appflix\Studygood\Storefront\Controller;
use Appflix\Studygood\Core\Service\VideoCourseService;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @Route(defaults={"_routeScope"={"storefront"}})
*/
class CourseViewController extends StorefrontController
{
use RenderStorefrontTrait;
private VideoCourseService $videoCourseService;
private AccountProfilePageLoader $profilePageLoader;
private GenericPageLoaderInterface $genericLoader;
public function __construct(
VideoCourseService $videoCourseService,
AccountProfilePageLoader $profilePageLoader,
GenericPageLoaderInterface $genericLoader
)
{
$this->videoCourseService = $videoCourseService;
$this->profilePageLoader = $profilePageLoader;
$this->genericLoader = $genericLoader;
}
/**
* @Route("/course/{productId}/{courseId}", name="studygood.course.index", methods={"GET"})
* @Route("/course/{productId}/{courseId}/{lessonId}", name="studygood.course.index", methods={"GET"}, defaults={"lessonId"=null})
*
* @throws PageNotFoundException
* @throws CustomerNotLoggedInException
*/
public function courseIndex(string $productId, string $courseId, ?string $lessonId = null, Request $request, SalesChannelContext $salesChannelContext): Response
{
$fromAccount = $request->get('fromAccount');
$this->videoCourseService->setContext($salesChannelContext->getContext());
$course = $this->videoCourseService->getCourse($courseId);
if (!$course || !$course->getChapters()->first()) {
throw new PageNotFoundException($courseId);
}
if ($lessonId) {
$currentLesson = $this->videoCourseService->getLesson($lessonId);
} else {
$currentLesson = $this->videoCourseService->getLesson($course->getChapters()->first()->getLessons()->first()->getId());
}
$esdSubscription = $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId, $currentLesson->getCourseId());
if (!$currentLesson->getIsPreview() && !$esdSubscription) {
throw new CustomerNotLoggedInException();
}
$this->videoCourseService->setProgress($esdSubscription, $courseId, $currentLesson->getId());
$page = $this->genericLoader->load($request, $salesChannelContext);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,follow');
}
return $this->renderStorefront('@Storefront/studygood/course/index.html.twig', [
'page' => $page,
'esdSubscription' => $esdSubscription,
'course' => $course,
'currentLesson' => $currentLesson,
'productId' => $productId,
'fromAccount' => $fromAccount
]);
}
/**
* @Route("/course-content/{productId}/{courseId}/{lessonId}", name="studygood.course.content", methods={"GET"}, defaults={"lessonId"=null, "XmlHttpRequest"=true})
* @throws PageNotFoundException
*/
public function courseContent(string $productId, string $courseId, ?string $lessonId = null, SalesChannelContext $salesChannelContext): JsonResponse
{
$this->videoCourseService->setContext($salesChannelContext->getContext());
$course = $this->videoCourseService->getCourse($courseId);
if (!$course || !$course->getChapters()->first()) {
throw new PageNotFoundException($courseId);
}
if ($lessonId) {
$currentLesson = $this->videoCourseService->getLesson($lessonId);
} else {
$currentLesson = $this->videoCourseService->getLesson($course->getChapters()->first()->getLessons()->first()->getId());
}
$esdSubscription = $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId, $currentLesson->getCourseId());
if (!$currentLesson->getIsPreview() && !$esdSubscription) {
throw new CustomerNotLoggedInException();
}
$this->videoCourseService->setProgress($esdSubscription, $courseId, $lessonId);
$viewData = [
'course' => $course,
'currentLesson' => $currentLesson,
'esdSubscription' => $esdSubscription,
'productId' => $productId,
'sgMedia' => $currentLesson->getSgMedia()
];
$return = ['html' => []];
$return['html']['#videoContainer'] = $this->renderWithSeoUrl('studygood/course/component/main-area.html.twig', $viewData);
$return['html']['#lessonFilesCount'] = $currentLesson->getFiles()->count() ?: null;
$return['html']['#lessonBoardCount'] = $currentLesson->getBoard()->count() ?: null;
$return['html']['#lessonMcTestCount'] = $currentLesson->getQuestions()->count() ?: null;
$return['html']['#lessonDescription'] = $this->renderWithSeoUrl('studygood/course/tab/description.html.twig', $viewData);
$return['html']['#check'.$currentLesson->getId()] = '<i class="fa-fw far fa-check-square"></i>';
$return['html']['#lessonBoard'] = $this->renderWithSeoUrl('studygood/course/tab/board.html.twig', $viewData);
$return['html']['#lessonFiles'] = $this->renderWithSeoUrl('studygood/course/tab/files.html.twig', $viewData);
$return['html']['#lessonMcTest'] = $this->renderWithSeoUrl('studygood/course/tab/test.html.twig', $viewData);
return new JsonResponse($return);
}
/**
* @Route("/course/lesson/{productId}/{lessonId}/board", name="studygood.board.modal.new", methods={"GET"}, defaults={"XmlHttpRequest"=true})
* @Route("/course/lesson/{productId}/{lessonId}/board/{boardId}/edit", name="studygood.board.modal.edit", methods={"GET"}, defaults={"XmlHttpRequest"=true, "boardId"=null})
* @Route("/course/lesson/{productId}/{lessonId}/board/{parentId}/reply", name="studygood.board.modal.reply", methods={"GET"}, defaults={"XmlHttpRequest"=true, "parentId"=null})
* @throws CustomerNotLoggedInException
*/
public function boardModal(string $productId, string $lessonId, ?string $boardId, ?string $parentId, SalesChannelContext $salesChannelContext): Response
{
$customer = $salesChannelContext->getCustomer();
if (!$customer) {
return $this->notFoundModal();
}
$this->videoCourseService->setContext($salesChannelContext->getContext());
$board = $this->videoCourseService->getBoard($boardId);
$currentLesson = $this->videoCourseService->getLesson($lessonId);
$esdSubscription = $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId, $currentLesson->getCourseId());
if (!$esdSubscription || ($board && $board->getCustomerId() !== $customer->getId())) {
return $this->notFoundModal();
}
return $this->renderStorefront('studygood/course/component/board-form-modal.html.twig', [
'modal' => [
'size' => 'lg',
],
'board' => $board,
'currentLesson' => $currentLesson,
'parentId' => $parentId,
'productId' => $productId
]);
}
/**
* @Route("/studygood/report/{courseId}", name="studygood.report.modal", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function reportModal(string $courseId, RequestDataBag $requestDataBag): Response
{
return $this->renderStorefront('studygood/course/component/report-form-modal.html.twig', [
'modal' => [
'size' => 'lg',
]
]);
}
/**
* @Route("/studygood/media/modal/{sgMediaId}", name="studygood.media.modal", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function mediaModal(string $sgMediaId, RequestDataBag $requestDataBag, SalesChannelContext $salesChannelContext): Response
{
$this->videoCourseService->setSalesChannelContext($salesChannelContext);
$sgMedia = $this->videoCourseService->getSgMedia($sgMediaId);
return $this->renderStorefront('studygood/component/media-modal.html.twig', [
'modal' => [
'size' => 'lg',
'centered' => true
],
'sgMedia' => $sgMedia
]);
}
/**
* @Route("/studygood/account/my-subscriptions", name="studygood.account.my-subscriptions", methods={"GET"})
*/
public function profileMySubscriptions(Request $request, SalesChannelContext $salesChannelContext): Response
{
$page = $this->profilePageLoader->load($request, $salesChannelContext);
return $this->renderStorefront('@Storefront/studygood/page/account/my-subscriptions/index.html.twig', [
'page' => $page,
]);
}
private function notFoundModal(): Response
{
return $this->renderStorefront('studygood/course/component/404-modal.html.twig');
}
}