src/Controller/WhiplayController.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\BandMemberRoleConstant;
  4. use App\Constant\FileTypeConstant;
  5. use App\Constant\UtilsConstant;
  6. use App\Entity\Band;
  7. use App\Entity\Folder;
  8. use App\Entity\User;
  9. use App\Services\WebhookManager;
  10. use DateTime;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use App\Constant\ActionStatusConstant;
  14. use App\Constant\FileActionConstant;
  15. use App\Constant\FileStatusConstant;
  16. use App\Entity\File;
  17. use App\Entity\FileAction;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class WhiplayController extends AbstractController
  21. {
  22.     const ADMIN_ERROR_TRANS 'band.error.admin';
  23.     protected WebhookManager $webhookManager;
  24.     public function __construct(WebhookManager $webhookManager)
  25.     {
  26.         $this->webhookManager $webhookManager;
  27.     }
  28.     /**
  29.      * @param Request $request
  30.      * @param string $route
  31.      * @param array $data
  32.      * @return Response
  33.      */
  34.     public function ajaxForward(Request $requeststring $route, array $data = array()): Response
  35.     {
  36.         $len strlen("media/cache");
  37.         $isMediaRoute = (substr($route0$len) === "media/cache");
  38.         $isApiRoute strpos($route'api')
  39.             || strpos($route'music_data')
  40.             || strpos($route'music_informations');
  41.         if (!$isMediaRoute && !$isApiRoute && $route !== "_wdt") {
  42.             $request->getSession()->set(UtilsConstant::REAL_REDIRECT_URL$this->generateUrl($route$data));
  43.         }
  44.         return parent::forward($this->routeToControllerName($route), $data);
  45.     }
  46.     /**
  47.      * @param Request $request
  48.      * @param string $routeIfNoReferer
  49.      * @param array $dataIfNoReferer
  50.      * @return Response
  51.      */
  52.     public function ajaxForwardToReferer(Request $requeststring $routeIfNoReferer 'whiplay_home', array $dataIfNoReferer = array()): Response
  53.     {
  54.         $previousRoute $request->getSession()->get('previousRoute');
  55.         $previousData $request->getSession()->get('previousData');
  56.         if ($previousRoute && is_array($previousData)) {
  57.             return $this->ajaxForward($request$previousRoute$previousData);
  58.         }
  59.         return $this->ajaxForward($request$routeIfNoReferer$dataIfNoReferer);
  60.     }
  61.     /**
  62.      * @param $routeName
  63.      * @return string
  64.      */
  65.     private function routeToControllerName($routeName): string {
  66.         $routes $this->get('router')->getRouteCollection();
  67.         return $routes->get($routeName)->getDefaults()['_controller'];
  68.     }
  69.     /**
  70.      * @param File $file
  71.      * @param string $action
  72.      * @param bool $withUser
  73.      * @return bool
  74.      */
  75.     protected function toggleFileAction(File $filestring $actionbool $withUser true): bool
  76.     {
  77.         /** @var User $user */
  78.         $user $this->getUser();
  79.         if (!($user instanceof User)) {
  80.             return false;
  81.         }
  82.         $parameters = array(
  83.             'file' => $file->getId(),
  84.             'action' => $action
  85.         );
  86.         if ($withUser) {
  87.             $parameters['user'] = $user->getId();
  88.         }
  89.         $fileAction $this
  90.             ->getDoctrine()
  91.             ->getRepository(FileAction::class)
  92.             ->findOneBy($parameters);
  93.         $create true;
  94.         $em $this->getDoctrine()->getManager();
  95.         if ($fileAction && ($action !== FileActionConstant::PUBLISH || $file->getStatus() === FileStatusConstant::STATUS_PRIVATE)) {
  96.             $em->remove($fileAction);
  97.             $create false;
  98.         } else if ($action !== FileActionConstant::PUBLISH || $file->getStatus() === FileStatusConstant::STATUS_PUBLIC) {
  99.             $fileAction = new FileAction();
  100.             $fileAction->setCreatedAt(new DateTime());
  101.             $fileAction->setUser($user);
  102.             $fileAction->setAction($action);
  103.             $fileAction->setFile($file);
  104.             if (in_array($action, array(FileActionConstant::PUBLISHFileActionConstant::FAVORITE))) {
  105.                 $fileAction->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  106.             }
  107.             $file->addFileAction($fileAction);
  108.             $em->persist($fileAction);
  109.             if ($action === FileActionConstant::PUBLISH) {
  110.                 $this->webhookManager->sendNewMusicOnDiscord($file);
  111.             }
  112.         }
  113.         $em->flush();
  114.         return $create;
  115.     }
  116.     /**
  117.      * Know if logged user if member of specified band
  118.      *
  119.      * @param Band $band
  120.      * @param TranslatorInterface $translator
  121.      * @return bool
  122.      */
  123.     protected function currentUserIsAdminOfBand(Band $bandTranslatorInterface $translator): bool
  124.     {
  125.         /** @var User $user */
  126.         $user $this->getUser();
  127.         if ($user instanceof User && $user->isInBand($bandBandMemberRoleConstant::LEADER)) {
  128.             return true;
  129.         }
  130.         $this->addFlash(
  131.             UtilsConstant::NOTICE,
  132.             $translator->trans(self::ADMIN_ERROR_TRANS)
  133.         );
  134.         return false;
  135.     }
  136.     /**
  137.      * Private method to create default folders after band creation
  138.      *
  139.      * @param Band $band
  140.      * @param TranslatorInterface $translator
  141.      */
  142.     protected function createDefaultFolders(Band $bandTranslatorInterface $translator)
  143.     {
  144.         /** @var User $user */
  145.         $user $this->getUser();
  146.         $em $this->getDoctrine()->getManager();
  147.         foreach (FileTypeConstant::getValues() as $label => $type) {
  148.             $rootFolder = new Folder();
  149.             $rootFolder->setName($label);
  150.             $rootFolder->setParent();
  151.             $rootFolder->setOwner($band);
  152.             $rootFolder->setCreator($user);
  153.             $rootFolder->setCreatedAt(new DateTime());
  154.             $em->persist($rootFolder);
  155.             if ($type == FileTypeConstant::MUSIC) {
  156.                 $defaultFoldersNames = [
  157.                     $translator->trans('band.default_folders.production'),
  158.                     $translator->trans('band.default_folders.works_in_progress'),
  159.                     $translator->trans('band.default_folders.ideas')
  160.                 ];
  161.                 foreach ($defaultFoldersNames as $folderName) {
  162.                     $currentFolder = new Folder();
  163.                     $currentFolder->setName($folderName);
  164.                     $currentFolder->setParent($rootFolder);
  165.                     $currentFolder->setOwner($band);
  166.                     $currentFolder->setCreator($user);
  167.                     $currentFolder->setCreatedAt(new DateTime());
  168.                     $em->persist($currentFolder);
  169.                 }
  170.             }
  171.         }
  172.         $em->persist($band);
  173.         $em->flush();
  174.     }
  175. }