src/Controller/BandController.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\BandTypeConstant;
  4. use App\Entity\CommonInformation;
  5. use App\Entity\User;
  6. use App\Services\StatisticManager;
  7. use App\Services\SeoManager;
  8. use App\Services\WebhookManager;
  9. use DateTime;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Constant\AjaxLoadingLimitConstant;
  12. use App\Constant\BandMemberRoleConstant;
  13. use App\Constant\PeopleActionConstant;
  14. use App\Constant\ActionStatusConstant;
  15. use App\Constant\FileTypeConstant;
  16. use App\Entity\Band;
  17. use App\Entity\BandMember;
  18. use App\Entity\EventAction;
  19. use App\Entity\File;
  20. use App\Entity\Invitation;
  21. use App\Entity\Folder;
  22. use App\Entity\PeopleAction;
  23. use App\Form\BandType;
  24. use App\Form\CreateEventType;
  25. use App\Form\InvitationType;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. const BAND_NAME_TRANS_PARAM 'band_name';
  30. class BandController extends WhiplayController
  31. {
  32.     /**
  33.      * Show information about specified band
  34.      *
  35.      * @param Band $band
  36.      * @param Request $request
  37.      * @param SeoManager $seoManager
  38.      * @return Response
  39.      */
  40.     public function show(Band $bandRequest $requestSeoManager $seoManager): Response
  41.     {
  42.         if ($band->getType() === BandTypeConstant::USER_COLLABORATION) {
  43.             return $this->ajaxForward($request'whiplay_home');
  44.         }
  45.         $seoManager
  46.             ->setTitleAndDescription(
  47.                 'band.show.seo.title',
  48.                 'band.show.seo.description',
  49.                 [BAND_NAME_TRANS_PARAM => htmlspecialchars($band->getName())]
  50.             )
  51.             ->useBandLogo($band);
  52.         $followBandSubscribers $band->getSubscribers();
  53.         $newInvitation = new Invitation();
  54.         $invitationForm $this->createForm(InvitationType::class, $newInvitation);
  55.         return $this->render('Band/show.html.twig', array(
  56.             'band' => $band,
  57.             'followBandSubscribers' => $followBandSubscribers,
  58.             'invitationForm' => $invitationForm->createView(),
  59.         ));
  60.     }
  61.     /**
  62.      * Show files of specified band depends on specified type (FileTypeConstant)
  63.      *
  64.      * @param Band $band
  65.      * @param Request $request
  66.      * @param SeoManager $seoManager
  67.      * @param StatisticManager $statisticManager
  68.      * @param string $type
  69.      * @return Response
  70.      */
  71.     public function files(Band $bandRequest $requestSeoManager $seoManagerStatisticManager $statisticManagerstring $type ''): Response
  72.     {
  73.         if ($band->getType() === BandTypeConstant::USER_COLLABORATION) {
  74.             return $this->ajaxForward($request'whiplay_home');
  75.         }
  76.         if (!in_array($typeFileTypeConstant::getValues())) {
  77.             $type FileTypeConstant::MUSIC;
  78.         }
  79.         $seoManager
  80.             ->setTitleAndDescription(
  81.                 'band.files.seo.title',
  82.                 'band.files.seo.description',
  83.                 [BAND_NAME_TRANS_PARAM => htmlspecialchars($band->getName()), 'type' => $type]
  84.             )
  85.             ->useBandLogo($band);
  86.         if ($type == FileTypeConstant::MUSIC) {
  87.             $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::FILE_MUSIC_LIMIT_NUMBER;
  88.         } elseif ($type == FileTypeConstant::VIDEO) {
  89.             $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::FILE_VIDEO_LIMIT_NUMBER;
  90.         } else {
  91.             $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::FILE_PICTURE_LIMIT_NUMBER;
  92.         }
  93.         $favoriteFiles $this
  94.             ->getDoctrine()
  95.             ->getRepository(File::class)
  96.             ->findFavoritesFromBandAndType($band$type);
  97.         $publicMusics $this
  98.             ->getDoctrine()
  99.             ->getRepository(File::class)
  100.             ->findPublicFromBandAndType($band$type$ajaxLoadingLimitNumber);
  101.         $totalPublicMusicsCount count($this
  102.             ->getDoctrine()
  103.             ->getRepository(File::class)
  104.             ->findPublicFromBandAndType($band$type));
  105.         //STATISTIC VIEWS
  106.         $statisticManager->addCommonVisit($band->getCommonInformation());
  107.         return $this->render('Band/files.html.twig', array(
  108.             'band' => $band,
  109.             'favoriteFiles' => $favoriteFiles,
  110.             'publicFiles' => $publicMusics,
  111.             'type' => $type,
  112.             'totalPublicMusicsCount' => $totalPublicMusicsCount,
  113.             'ajaxLoadingLimitNumber' => $ajaxLoadingLimitNumber,
  114.             'baseFolders' => $band->getBaseFolders(),
  115.             'selectedType' => $type,
  116.         ));
  117.     }
  118.     /**
  119.      * Show events list of specified band
  120.      *
  121.      * @param Band $band
  122.      * @param Request $request
  123.      * @param SeoManager $seoManager
  124.      * @return Response
  125.      */
  126.     public function events(Band $bandRequest $requestSeoManager $seoManager): Response
  127.     {
  128.         if ($band->getType() === BandTypeConstant::USER_COLLABORATION) {
  129.             return $this->ajaxForward($request'whiplay_home');
  130.         }
  131.         $seoManager
  132.             ->setTitleAndDescription(
  133.                 'band.events.seo.title',
  134.                 'band.events.seo.description',
  135.                 [BAND_NAME_TRANS_PARAM => $band->getName()]
  136.             )
  137.             ->useBandLogo($band);
  138.         /** @var User $user */
  139.         $user $this->getUser();
  140.         if (!($user instanceof User)) {
  141.             $formCreateEvent '';
  142.         } else {
  143.             $formCreateEvents $this->createForm(CreateEventType::class, new EventAction(), array('userId' => $user->getId(), 'band' => $band));
  144.             $formCreateEvent $formCreateEvents->createView();
  145.         }
  146.         return $this->render('Band/events.html.twig', array(
  147.             'band' => $band,
  148.             'formCreateEvent' => $formCreateEvent,
  149.         ));
  150.     }
  151.     /**
  152.      * Show band member information
  153.      *
  154.      * @param BandMember $bandMember
  155.      * @param Request $request
  156.      * @param SeoManager $seoManager
  157.      * @return Response
  158.      */
  159.     public function memberShow(BandMember $bandMemberRequest $requestSeoManager $seoManager): Response
  160.     {
  161.         if ($bandMember->getBand()->getType() === BandTypeConstant::USER_COLLABORATION) {
  162.             return $this->ajaxForward($request'whiplay_home');
  163.         }
  164.         $seoManager
  165.             ->setTitleAndDescription(
  166.                 'band.member.seo.title',
  167.                 'band.member.seo.description',
  168.                 [
  169.                     BAND_NAME_TRANS_PARAM => $bandMember->getBand()->getName(),
  170.                     'user_name' => $bandMember->getUser()->getUsername()
  171.                 ]
  172.             )
  173.             ->useUserLogo($bandMember->getUser());
  174.         return $this->render('Band/memberShow.html.twig', array(
  175.             'bandMember' => $bandMember
  176.         ));
  177.     }
  178.     /**
  179.      * Show offers to search musicians link to specified band
  180.      *
  181.      * @param Band $band
  182.      * @param Request $request
  183.      * @param SeoManager $seoManager
  184.      * @return Response
  185.      */
  186.     public function offers(Band $bandRequest $requestSeoManager $seoManager): Response
  187.     {
  188.         if ($band->getType() === BandTypeConstant::USER_COLLABORATION) {
  189.             return $this->ajaxForward($request'whiplay_home');
  190.         }
  191.         $seoManager
  192.             ->setTitleAndDescription(
  193.                 'band.offers.seo.title',
  194.                 'band.offers.seo.description',
  195.                 [BAND_NAME_TRANS_PARAM => htmlspecialchars($band->getName())]
  196.             )
  197.             ->setOgTitleAndDescription(
  198.                 'band.offers.seo.title',
  199.                 'band.offers.seo.description',
  200.                 [BAND_NAME_TRANS_PARAM => htmlspecialchars($band->getName())]
  201.             )
  202.             ->useBandLogo($band);
  203.         return $this->render('Band/offers.html.twig', array(
  204.             'band' => $band,
  205.         ));
  206.     }
  207.     /**
  208.      * Show band registration page in order to create a new band
  209.      *
  210.      * @param Request $request
  211.      * @param SeoManager $seoManager
  212.      * @param WebhookManager $webhookManager
  213.      * @param TranslatorInterface $translator
  214.      * @return Response
  215.      */
  216.     public function register(
  217.         Request $request,
  218.         SeoManager $seoManager,
  219.         WebhookManager $webhookManager,
  220.         TranslatorInterface $translator
  221.     ): Response {
  222.         /** @var User $user */
  223.         $user $this->getUser();
  224.         if (!($user instanceof User)) {
  225.             return $this->ajaxForward($request'app_login');
  226.         }
  227.         $seoManager
  228.             ->setTitleAndDescription('band.register.seo.title','band.register.seo.description')
  229.             ->setNoIndex()
  230.             ->useLogo();
  231.         $band = new Band();
  232.         $form $this->createForm(BandType::class, $band, array('userId' => $user->getId()));
  233.         $form->handleRequest($request);
  234.         $em $this->getDoctrine()->getManager();
  235.         if ($form->isSubmitted() && $form->isValid()) {
  236.             if (!$band->getCommonInformation()){
  237.                 $band->setCommonInformation(new CommonInformation());
  238.             }
  239.             $band->getCommonInformation()->setInscriptionDate(new DateTime());
  240.             $band->setCreator($user);
  241.             $member = new BandMember();
  242.             $member->setUser($user);
  243.             $member->setRole(BandMemberRoleConstant::LEADER);
  244.             $band->addMember($member);
  245.             $em->persist($band);
  246.             foreach ($band->getMembers() as $bandMember) {
  247.                 $bandMember->setBand($band);
  248.             }
  249.             $em->flush();
  250.             $this->createDefaultFolders($band$translator);
  251.             $webhookManager->sendNewBandOnDiscord($band);
  252.             return $this->render('Band/registerConfirmed.html.twig', array(
  253.                 'id' => $band->getId(),
  254.                 'band' => $band,
  255.             ));
  256.         }
  257.         return $this->render('Band/register.html.twig', array(
  258.             'form' => $form->createView(),
  259.         ));
  260.     }
  261.     /**
  262.      * Api method to fetch files from band
  263.      * lastId get parameter define from which id actions will be fetched
  264.      *
  265.      * @param Request $request
  266.      * @param Band $band
  267.      * @param $type
  268.      * @return JsonResponse|Response
  269.      */
  270.     public function filesApi(Request $requestBand $band$type)
  271.     {
  272.         if (!$request->isXmlHttpRequest()) {
  273.             return new Response('Forbidden'Response::HTTP_BAD_REQUEST);
  274.         }
  275.         $lastId $request->get('lastId') ? $request->get('lastId') : 0;
  276.         $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::getConstantFromType($type);
  277.         $publicMusics $this
  278.             ->getDoctrine()
  279.             ->getRepository(File::class)
  280.             ->findPublicFromBandAndType($band$type$ajaxLoadingLimitNumber$lastId);
  281.         $response = array();
  282.         if (count($publicMusics) < $ajaxLoadingLimitNumber) {
  283.             $response['noMore'] = true;
  284.         }
  285.         $response['actions'] = array();
  286.         /** @var File $file */
  287.         foreach ($publicMusics as $file) {
  288.             $actionInfos = array();
  289.             $actionInfos['id'] = $file->getPublishAction()->getId();
  290.             $actionInfos['view'] = $this->render('File/item.html.twig', array(
  291.                 'file' => $file,
  292.             ))->getContent();
  293.             $response['actions'][] = $actionInfos;
  294.         }
  295.         return new JsonResponse($responseResponse::HTTP_OK);
  296.     }
  297.     /**
  298.      * Follow band based on band_id get parameter
  299.      *
  300.      * @param Request $request
  301.      * @param TranslatorInterface $translator
  302.      * @return Response
  303.      */
  304.     public function follow(Request $requestTranslatorInterface $translator): Response
  305.     {
  306.         /** @var User $user */
  307.         $user $this->getUser();
  308.         if (!($user instanceof User)) {
  309.             return $this->ajaxForward($request'app_login');
  310.         }
  311.         $bandId $request->request->get('band_id');
  312.         /* @var Band $band */
  313.         $band $this->getDoctrine()
  314.             ->getRepository(Band::class)
  315.             ->find($bandId);
  316.         if (!$band) {
  317.             return $this->ajaxForward($request'whiplay_home');
  318.         }
  319.         $parameters = array(
  320.             'bandPeople' => $band->getId(),
  321.             'action' => PeopleActionConstant::FOLLOW,
  322.             'user' => $user->getId()
  323.         );
  324.         $userFollowAction $this
  325.             ->getDoctrine()
  326.             ->getRepository(PeopleAction::class)
  327.             ->findOneBy($parameters);
  328.         $em $this->getDoctrine()->getManager();
  329.         if ($userFollowAction) {
  330.             $em->remove($userFollowAction);
  331.             $this->addFlash(
  332.                 'notice',
  333.                 $translator->trans('band.subscription.removed')
  334.             );
  335.         } else {
  336.             $userFollowAction = new PeopleAction();
  337.             $userFollowAction->setCreatedAt(new DateTime());
  338.             $userFollowAction->setUser($user);
  339.             $userFollowAction->setBandPeople($band);
  340.             $userFollowAction->setAction(PeopleActionConstant::FOLLOW);
  341.             $userFollowAction->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  342.             $em->persist($userFollowAction);
  343.             $this->addFlash(
  344.                 'notice',
  345.                 $translator->trans('band.subscription.added')
  346.             );
  347.         }
  348.         $em->flush();
  349.         return $this->ajaxForwardToReferer($request'whiplay_band_show', array('id' => $band->getId()));
  350.     }
  351.     /**
  352.      * Edit band information convert collaboration
  353.      *
  354.      * @param Request $request
  355.      * @param Band $band
  356.      * @return Response
  357.      */
  358.     public function convertCollaboration(
  359.         Request             $request,
  360.         Band                $band
  361.     ): Response {
  362.         $em $this->getDoctrine()->getManager();
  363.         $band->setType(BandTypeConstant::BAND);
  364.         $em->persist($band);
  365.         $em->flush();
  366.         return $this->ajaxForward($request'whiplay_studio_edit', [
  367.             'id' => $band->getId()
  368.         ]);
  369.     }
  370. }