src/Controller/ActionController.php line 138

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\BandTypeConstant;
  4. use App\Constant\UtilsConstant;
  5. use App\Entity\FileAction;
  6. use DateTime;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Services\SeoManager;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use App\Constant\ActionPointOfViewConstant;
  13. use App\Constant\ActionStatusConstant;
  14. use App\Constant\AjaxLoadingLimitConstant;
  15. use App\Constant\BandMemberRoleConstant;
  16. use App\Constant\EventParticipationConstant;
  17. use App\Constant\InvitationStatusConstant;
  18. use App\Constant\ShareActionConstant;
  19. use App\Entity\EventAction;
  20. use App\Entity\EventParticipation;
  21. use App\Entity\Invitation;
  22. use App\Entity\Share;
  23. use App\Entity\User;
  24. use App\Entity\Band;
  25. use App\Entity\Action;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. const ACTIONS 'actions';
  28. const ACTION_SHOW_VIEW 'Action/show.html.twig';
  29. const POINT_OF_VIEW 'pointOfView';
  30. class ActionController extends WhiplayController
  31. {
  32.     /**
  33.      * Show custom news feed linked to current user subscriptions
  34.      *
  35.      * @param Request $request
  36.      * @param SeoManager $seoManager
  37.      * @return Response
  38.      */
  39.     public function subscriptions(Request $requestSeoManager $seoManager): Response
  40.     {
  41.         /** @var User $currentUser */
  42.         $currentUser $this->getUser();
  43.         if (!($currentUser instanceof User)) {
  44.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  45.         }
  46.         $seoManager
  47.             ->setTitleAndDescription('action.subscriptions.seo.title','action.subscriptions.seo.description')
  48.             ->setOgTitleAndDescription('action.subscriptions.seo.og_title''action.subscriptions.seo.og_description')
  49.             ->useLogo();
  50.         $this->getDoctrine()
  51.             ->getRepository(Action::class)
  52.             ->addViewsOnSubscriptionsPublications($currentUser);
  53.         $actions $this
  54.             ->getDoctrine()
  55.             ->getRepository(Action::class)
  56.             ->getSubscriptionsPublications($currentUser0AjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  57.         $invitations $this
  58.             ->getDoctrine()
  59.             ->getRepository(Invitation::class)
  60.             ->findBy(array('email' => $currentUser->getEmail(), 'status' => InvitationStatusConstant::WAITING));
  61.         return $this->render(ACTION_SHOW_VIEW, array(
  62.             ACTIONS => $actions,
  63.             'invitations' => $invitations,
  64.             'profileUser' => $currentUser,
  65.         ));
  66.     }
  67.     /**
  68.      * Show private news feed for band in studio
  69.      *
  70.      * @param Request $request
  71.      * @param Band $band
  72.      * @param SeoManager $seoManager
  73.      * @return Response
  74.      */
  75.     public function band(Request $requestBand $bandSeoManager $seoManager): Response
  76.     {
  77.         /** @var User $currentUser */
  78.         $currentUser $this->getUser();
  79.         if (!($currentUser instanceof User)) {
  80.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  81.         }
  82.         if ($band->getType() === BandTypeConstant::USER_COLLABORATION) {
  83.             return $this->ajaxForward($request'whiplay_home');
  84.         }
  85.         $seoManager
  86.             ->setTitleAndDescription(
  87.                 'action.band.seo.title',
  88.                 'action.band.seo.description',
  89.                 ['band_name' => htmlspecialchars($band->getName())]
  90.             )
  91.             ->setOgTitleAndDescription(
  92.                 'action.band.seo.title',
  93.                 'action.band.seo.description',
  94.                 ['band_name' => htmlspecialchars($band->getName())]
  95.             )
  96.             ->useLogo();
  97.         $isInBand $currentUser->isInBand($band);
  98.         $pointOfView $isInBand ActionPointOfViewConstant::INTERNAL ActionPointOfViewConstant::EXTERNAL;
  99.         $this
  100.             ->getDoctrine()
  101.             ->getRepository(Action::class)
  102.             ->addViewsOnBandNews($band$currentUser$pointOfView);
  103.         $actions $this
  104.             ->getDoctrine()
  105.             ->getRepository(Action::class)
  106.             ->getAllForBand($band$pointOfView0AjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  107.         return $this->render(ACTION_SHOW_VIEW, array(
  108.             ACTIONS => $actions,
  109.             POINT_OF_VIEW => $pointOfView,
  110.             'band' => $band,
  111.         ));
  112.     }
  113.     /**
  114.      * Show specified user news feed
  115.      *
  116.      * @param Request $request
  117.      * @param User $user
  118.      * @param SeoManager $seoManager
  119.      * @return Response
  120.      */
  121.     public function user(Request $requestUser $userSeoManager $seoManager): Response
  122.     {
  123.         /** @var User $currentUser */
  124.         $currentUser $this->getUser();
  125.         if (!($currentUser instanceof User)) {
  126.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  127.         }
  128.         $seoManager
  129.             ->setTitleAndDescription(
  130.                 'action.user.seo.title',
  131.                 'action.user.seo.description',
  132.                 ['user_name' => htmlspecialchars($user->getUsername())]
  133.             )
  134.             ->setOgTitleAndDescription(
  135.                 'action.user.seo.title',
  136.                 'action.user.seo.description',
  137.                 ['user_name' => htmlspecialchars($user->getUsername())]
  138.             )
  139.             ->useLogo();
  140.         $pointOfView = ($currentUser === $user) ? ActionPointOfViewConstant::INTERNAL ActionPointOfViewConstant::EXTERNAL;
  141.         $this
  142.             ->getDoctrine()
  143.             ->getRepository(Action::class)
  144.             ->addViewsOnUserNews($user$currentUser$pointOfView);
  145.         $actions $this
  146.             ->getDoctrine()
  147.             ->getRepository(Action::class)
  148.             ->getAllForUser($user$pointOfView0AjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  149.         return $this->render(ACTION_SHOW_VIEW, array(
  150.             ACTIONS => $actions,
  151.             POINT_OF_VIEW => $pointOfView,
  152.             'profileUser' => $user,
  153.         ));
  154.     }
  155.     /**
  156.      * Api method to fetch news feed actions from id (for subscriptions, user or band depends on get parameters)
  157.      * lastId get parameter define from which id actions will be fetched
  158.      * bandId get parameter will be fetching actions linked to this band id
  159.      * userId get parameter will be fetching actions linked to this user id
  160.      *
  161.      * @param Request $request
  162.      * @return Response
  163.      */
  164.     public function api(Request $request): Response
  165.     {
  166.         if (!$request->isXmlHttpRequest()) {
  167.             return new Response('Bad request'Response::HTTP_BAD_REQUEST);
  168.         }
  169.         /** @var User $currentUser */
  170.         $currentUser $this->getUser();
  171.         if (!($currentUser instanceof User)) {
  172.             return new JsonResponse(["error" => true"message" => "Forbidden"], Response::HTTP_FORBIDDEN);
  173.         }
  174.         $lastId $request->get('lastId') ? $request->get('lastId') : 999999999;
  175.         $bandId $request->get('bandId');
  176.         $band is_numeric($bandId) ? $this
  177.             ->getDoctrine()
  178.             ->getRepository(Band::class)
  179.             ->find($bandId) : null;
  180.         $userId $request->get('userId');
  181.         $user is_numeric($userId) ? $this
  182.             ->getDoctrine()
  183.             ->getRepository(User::class)
  184.             ->find($userId) : null;
  185.         if ($band instanceof Band) {
  186.             $pointOfView $currentUser->isInBand($band) ? ActionPointOfViewConstant::INTERNAL ActionPointOfViewConstant::EXTERNAL;
  187.             $actions $this
  188.                 ->getDoctrine()
  189.                 ->getRepository(Action::class)
  190.                 ->getAllForBand($band$pointOfView$lastIdAjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  191.         } else if ($user instanceof User) {
  192.             $pointOfView = ($currentUser === $user) ? ActionPointOfViewConstant::INTERNAL ActionPointOfViewConstant::EXTERNAL;
  193.             $actions $this
  194.                 ->getDoctrine()
  195.                 ->getRepository(Action::class)
  196.                 ->getAllForUser($user$pointOfView$lastIdAjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  197.         } else {
  198.             $pointOfView ActionPointOfViewConstant::INTERNAL;
  199.             $actions $this
  200.                 ->getDoctrine()
  201.                 ->getRepository(Action::class)
  202.                 ->getSubscriptionsPublications($currentUser$lastIdAjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER);
  203.         }
  204.         $response = array();
  205.         if (count($actions) < AjaxLoadingLimitConstant::NEWS_FEED_LIMIT_NUMBER) {
  206.             $response['noMore'] = true;
  207.         }
  208.         $response[ACTIONS] = array();
  209.         foreach($actions as $action) {
  210.             $actionInfos = array();
  211.             $actionInfos['id'] = $action->getId();
  212.             $actionInfos['view'] = $this->render('Action/news_part/item.html.twig', array(
  213.                 'action' => $action,
  214.                 POINT_OF_VIEW => $pointOfView,
  215.             ))->getContent();
  216.             $response[ACTIONS][] = $actionInfos;
  217.         }
  218.         return new JsonResponse($responseResponse::HTTP_OK);
  219.     }
  220.     /**
  221.      * Change file action status to public if it's private and private if it's public
  222.      *
  223.      * @param Request $request
  224.      * @param FileAction $action
  225.      * @return Response
  226.      */
  227.     public function changeFileActionStatus(Request $requestFileAction $action): Response
  228.     {
  229.         /** @var User $currentUser */
  230.         $currentUser $this->getUser();
  231.         if (!($currentUser instanceof User)) {
  232.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  233.         }
  234.         if ($currentUser->IsInBand($action->getFile()->getBand(), BandMemberRoleConstant::LEADER)) {
  235.             $em $this->getDoctrine()->getManager();
  236.             if ($action->getStatus() === ActionStatusConstant::STATUS_PUBLIC) {
  237.                 $action->setStatus(ActionStatusConstant::STATUS_PRIVATE);
  238.             } else {
  239.                 $action->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  240.             }
  241.             $em->persist($action);
  242.             $em->flush();
  243.         }
  244.         return $this->ajaxForwardToReferer($request'whiplay_action_subscriptions');
  245.     }
  246.     /**
  247.      * Share an action from news feed
  248.      *
  249.      * @param Request $request
  250.      * @param Action $action
  251.      * @param TranslatorInterface $translator
  252.      * @return Response
  253.      */
  254.     public function share(Request $requestAction $actionTranslatorInterface $translator): Response
  255.     {
  256.         /** @var User $currentUser */
  257.         $currentUser $this->getUser();
  258.         if (!($currentUser instanceof User)) {
  259.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  260.         }
  261.         $action $action instanceof Share $action->getBaseAction() : $action;
  262.         if ($action->getStatus() === ActionStatusConstant::STATUS_PUBLIC) {
  263.             $em $this->getDoctrine()->getManager();
  264.             $newShare = new Share();
  265.             $newShare->setBaseAction($action);
  266.             $newShare->setUser($currentUser);
  267.             $newShare->setCreatedAt(new DateTime());
  268.             $newShare->setAction(ShareActionConstant::SHARE);
  269.             $newShare->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  270.             $em->persist($newShare);
  271.             $em->flush();
  272.             $this->addFlash(
  273.                 'notice',
  274.                 $translator->trans('action.share.completed')
  275.             );
  276.         }
  277.         return $this->ajaxForwardToReferer($request'whiplay_action_user', array('id' => $currentUser->getId()));
  278.     }
  279.     /**
  280.      * Show event detail page
  281.      *
  282.      * @param Request $request
  283.      * @param EventAction $eventAction
  284.      * @return Response
  285.      */
  286.     public function showEvent(Request $requestEventAction $eventAction): Response
  287.     {
  288.         /** @var User $currentUser */
  289.         $currentUser $this->getUser();
  290.         if (!($currentUser instanceof User)) {
  291.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  292.         }
  293.         if ($eventAction->getStatus() == ActionStatusConstant::STATUS_PRIVATE && !$currentUser->isInBand($eventAction->getBand())) {
  294.             return $this->ajaxForward($request'whiplay_action_band', array('id' => $eventAction->getBand()->getId()));
  295.         }
  296.         return $this->render('Action/event_show.html.twig', array(
  297.             'eventAction' => $eventAction
  298.         ));
  299.     }
  300.     /**
  301.      * Action share deletion
  302.      *
  303.      * @param Request $request
  304.      * @param EntityManagerInterface $em
  305.      * @param Share $share
  306.      * @return Response
  307.      */
  308.     public function deleteShare(Request $requestEntityManagerInterface $emShare $share): Response
  309.     {
  310.         /** @var User $currentUser */
  311.         $currentUser $this->getUser();
  312.         if (!($currentUser instanceof User)) {
  313.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  314.         }
  315.         if ($share->getUser()->getId() !== $currentUser->getId()) {
  316.             return $this->ajaxForward($request'whiplay_home');
  317.         }
  318.         $em->remove($share);
  319.         $em->flush();
  320.         return $this->ajaxForwardToReferer($request'whiplay_action_user', array('id' => $currentUser->getId()));
  321.     }
  322.     /**
  323.      * Set participation at $eventAction event to $action value for current user
  324.      *
  325.      * @param Request $request
  326.      * @param EventAction $eventAction
  327.      * @param string $action
  328.      * @param TranslatorInterface $translator
  329.      * @return Response
  330.      */
  331.     public function participate(Request $requestEventAction $eventActionstring $actionTranslatorInterface $translator): Response
  332.     {
  333.         $errorResponse null;
  334.         /** @var User $currentUser */
  335.         $currentUser $this->getUser();
  336.         if (!($currentUser instanceof User)) {
  337.             $errorResponse $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  338.         } else if (!in_array($actionEventParticipationConstant::getValues()) || (
  339.             $eventAction->getStatus() == ActionStatusConstant::STATUS_PRIVATE &&
  340.             !$currentUser->isInBand($eventAction->getBand())
  341.         )) {
  342.             $errorResponse $this->ajaxForward($request'whiplay_action_band', array('id' => $eventAction->getId()));
  343.         } else if ($eventAction->isOver()) {
  344.             $errorResponse $this->ajaxForwardToReferer($request'whiplay_event_show', array('id' => $eventAction->getId()));
  345.         }
  346.         if ($errorResponse instanceof Response) {
  347.             return $errorResponse;
  348.         }
  349.         $em $this->getDoctrine()->getManager();
  350.         $EventParticipation $this
  351.             ->getDoctrine()
  352.             ->getRepository(EventParticipation::class)
  353.             ->findBy([
  354.                 'user' => $currentUser->getId(),
  355.                 'event' => $eventAction,
  356.             ]);
  357.         if ($EventParticipation) {
  358.             $EventParticipation $EventParticipation[0];
  359.             $EventParticipation->setAction($action);
  360.             $EventParticipation->setCreatedAt(new DateTime());
  361.         } else {
  362.             $EventParticipation = new EventParticipation();
  363.             $EventParticipation->setDescription('');
  364.             $EventParticipation->setAction($action);
  365.             $EventParticipation->setCreatedAt(new DateTime());
  366.             $EventParticipation->setUser($currentUser);
  367.             $EventParticipation->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  368.             $EventParticipation->setEvent($eventAction);
  369.         }
  370.         $em->persist($EventParticipation);
  371.         $em->flush();
  372.         $this->addFlash(
  373.             'notice',
  374.             $translator->trans('event.notice.participation_saved')
  375.         );
  376.         return $this->ajaxForwardToReferer($request'whiplay_event_show', array('id' => $eventAction->getId()));
  377.     }
  378. }