custom/plugins/AppflixStudygood/src/Storefront/Controller/CourseViewController.php line 210

Open in your IDE?
  1. <?php
  2. namespace Appflix\Studygood\Storefront\Controller;
  3. use Appflix\Studygood\Core\Service\VideoCourseService;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Controller\StorefrontController;
  10. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
  11. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. /**
  19.  * @Route(defaults={"_routeScope"={"storefront"}})
  20.  */
  21. class CourseViewController extends StorefrontController
  22. {
  23.     use RenderStorefrontTrait;
  24.     private VideoCourseService $videoCourseService;
  25.     private AccountProfilePageLoader $profilePageLoader;
  26.     private GenericPageLoaderInterface $genericLoader;
  27.     public function __construct(
  28.         VideoCourseService $videoCourseService,
  29.         AccountProfilePageLoader $profilePageLoader,
  30.         GenericPageLoaderInterface $genericLoader
  31.     )
  32.     {
  33.         $this->videoCourseService $videoCourseService;
  34.         $this->profilePageLoader $profilePageLoader;
  35.         $this->genericLoader $genericLoader;
  36.     }
  37.     /**
  38.      * @Route("/course/{productId}/{courseId}", name="studygood.course.index", methods={"GET"})
  39.      * @Route("/course/{productId}/{courseId}/{lessonId}", name="studygood.course.index", methods={"GET"}, defaults={"lessonId"=null})
  40.      *
  41.      * @throws PageNotFoundException
  42.      * @throws CustomerNotLoggedInException
  43.      */
  44.     public function courseIndex(string $productIdstring $courseId, ?string $lessonId nullRequest $requestSalesChannelContext $salesChannelContext): Response
  45.     {
  46.         $fromAccount $request->get('fromAccount');
  47.         $this->videoCourseService->setContext($salesChannelContext->getContext());
  48.         $course $this->videoCourseService->getCourse($courseId);
  49.         if (!$course || !$course->getChapters()->first()) {
  50.             throw new PageNotFoundException($courseId);
  51.         }
  52.         if ($lessonId) {
  53.             $currentLesson $this->videoCourseService->getLesson($lessonId);
  54.         } else {
  55.             $currentLesson $this->videoCourseService->getLesson($course->getChapters()->first()->getLessons()->first()->getId());
  56.         }
  57.         $esdSubscription $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId$currentLesson->getCourseId());
  58.         if (!$currentLesson->getIsPreview() && !$esdSubscription) {
  59.             throw new CustomerNotLoggedInException();
  60.         }
  61.         $this->videoCourseService->setProgress($esdSubscription$courseId$currentLesson->getId());
  62.         $page $this->genericLoader->load($request$salesChannelContext);
  63.         if ($page->getMetaInformation()) {
  64.             $page->getMetaInformation()->setRobots('noindex,follow');
  65.         }
  66.         return $this->renderStorefront('@Storefront/studygood/course/index.html.twig', [
  67.             'page' => $page,
  68.             'esdSubscription' => $esdSubscription,
  69.             'course' => $course,
  70.             'currentLesson' => $currentLesson,
  71.             'productId' => $productId,
  72.             'fromAccount' => $fromAccount
  73.         ]);
  74.     }
  75.     /**
  76.      * @Route("/course-content/{productId}/{courseId}/{lessonId}", name="studygood.course.content", methods={"GET"}, defaults={"lessonId"=null, "XmlHttpRequest"=true})
  77.      * @throws PageNotFoundException
  78.      */
  79.     public function courseContent(string $productIdstring $courseId, ?string $lessonId nullSalesChannelContext $salesChannelContext): JsonResponse
  80.     {
  81.         $this->videoCourseService->setContext($salesChannelContext->getContext());
  82.         $course $this->videoCourseService->getCourse($courseId);
  83.         if (!$course || !$course->getChapters()->first()) {
  84.             throw new PageNotFoundException($courseId);
  85.         }
  86.         if ($lessonId) {
  87.             $currentLesson $this->videoCourseService->getLesson($lessonId);
  88.         } else {
  89.             $currentLesson $this->videoCourseService->getLesson($course->getChapters()->first()->getLessons()->first()->getId());
  90.         }
  91.         $esdSubscription $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId$currentLesson->getCourseId());
  92.         if (!$currentLesson->getIsPreview() && !$esdSubscription) {
  93.             throw new CustomerNotLoggedInException();
  94.         }
  95.         $this->videoCourseService->setProgress($esdSubscription$courseId$lessonId);
  96.         $viewData = [
  97.             'course' => $course,
  98.             'currentLesson' => $currentLesson,
  99.             'esdSubscription' => $esdSubscription,
  100.             'productId' => $productId,
  101.             'sgMedia' => $currentLesson->getSgMedia()
  102.         ];
  103.         $return = ['html' => []];
  104.         $return['html']['#videoContainer'] = $this->renderWithSeoUrl('studygood/course/component/main-area.html.twig'$viewData);
  105.         $return['html']['#lessonFilesCount'] = $currentLesson->getFiles()->count() ?: null;
  106.         $return['html']['#lessonBoardCount'] = $currentLesson->getBoard()->count() ?: null;
  107.         $return['html']['#lessonMcTestCount'] = $currentLesson->getQuestions()->count() ?: null;
  108.         $return['html']['#lessonDescription'] = $this->renderWithSeoUrl('studygood/course/tab/description.html.twig'$viewData);
  109.         $return['html']['#check'.$currentLesson->getId()] = '<i class="fa-fw far fa-check-square"></i>';
  110.         $return['html']['#lessonBoard'] = $this->renderWithSeoUrl('studygood/course/tab/board.html.twig'$viewData);
  111.         $return['html']['#lessonFiles'] = $this->renderWithSeoUrl('studygood/course/tab/files.html.twig'$viewData);
  112.         $return['html']['#lessonMcTest'] = $this->renderWithSeoUrl('studygood/course/tab/test.html.twig'$viewData);
  113.         return new JsonResponse($return);
  114.     }
  115.     /**
  116.      * @Route("/course/lesson/{productId}/{lessonId}/board", name="studygood.board.modal.new", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  117.      * @Route("/course/lesson/{productId}/{lessonId}/board/{boardId}/edit", name="studygood.board.modal.edit", methods={"GET"}, defaults={"XmlHttpRequest"=true, "boardId"=null})
  118.      * @Route("/course/lesson/{productId}/{lessonId}/board/{parentId}/reply", name="studygood.board.modal.reply", methods={"GET"}, defaults={"XmlHttpRequest"=true, "parentId"=null})
  119.      * @throws CustomerNotLoggedInException
  120.      */
  121.     public function boardModal(string $productIdstring $lessonId, ?string $boardId, ?string $parentIdSalesChannelContext $salesChannelContext): Response
  122.     {
  123.         $customer $salesChannelContext->getCustomer();
  124.         if (!$customer) {
  125.             return $this->notFoundModal();
  126.         }
  127.         $this->videoCourseService->setContext($salesChannelContext->getContext());
  128.         $board $this->videoCourseService->getBoard($boardId);
  129.         $currentLesson $this->videoCourseService->getLesson($lessonId);
  130.         $esdSubscription $this->videoCourseService->getSubscription($salesChannelContext->getCustomer(), $productId$currentLesson->getCourseId());
  131.         if (!$esdSubscription || ($board && $board->getCustomerId() !== $customer->getId())) {
  132.             return $this->notFoundModal();
  133.         }
  134.         return $this->renderStorefront('studygood/course/component/board-form-modal.html.twig', [
  135.             'modal' => [
  136.                 'size' => 'lg',
  137.             ],
  138.             'board' => $board,
  139.             'currentLesson' => $currentLesson,
  140.             'parentId' => $parentId,
  141.             'productId' => $productId
  142.         ]);
  143.     }
  144.     /**
  145.      * @Route("/studygood/report/{courseId}", name="studygood.report.modal", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  146.      */
  147.     public function reportModal(string $courseIdRequestDataBag $requestDataBag): Response
  148.     {
  149.         return $this->renderStorefront('studygood/course/component/report-form-modal.html.twig', [
  150.             'modal' => [
  151.                 'size' => 'lg',
  152.             ]
  153.         ]);
  154.     }
  155.     /**
  156.      * @Route("/studygood/media/modal/{sgMediaId}", name="studygood.media.modal", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  157.      */
  158.     public function mediaModal(string $sgMediaIdRequestDataBag $requestDataBagSalesChannelContext $salesChannelContext): Response
  159.     {
  160.         $this->videoCourseService->setSalesChannelContext($salesChannelContext);
  161.         $sgMedia $this->videoCourseService->getSgMedia($sgMediaId);
  162.         return $this->renderStorefront('studygood/component/media-modal.html.twig', [
  163.             'modal' => [
  164.                 'size' => 'lg',
  165.                 'centered' => true
  166.             ],
  167.             'sgMedia' => $sgMedia
  168.         ]);
  169.     }
  170.     /**
  171.      * @Route("/studygood/account/my-subscriptions", name="studygood.account.my-subscriptions", methods={"GET"})
  172.      */
  173.     public function profileMySubscriptions(Request $requestSalesChannelContext $salesChannelContext): Response
  174.     {
  175.         $page $this->profilePageLoader->load($request$salesChannelContext);
  176.         return $this->renderStorefront('@Storefront/studygood/page/account/my-subscriptions/index.html.twig', [
  177.             'page' => $page,
  178.         ]);
  179.     }
  180.     private function notFoundModal(): Response
  181.     {
  182.         return $this->renderStorefront('studygood/course/component/404-modal.html.twig');
  183.     }
  184. }