src/Controller/ProfileController.php line 229

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\UtilsConstant;
  4. use App\Entity\Action;
  5. use App\Form\UserType;
  6. use App\Services\StatisticManager;
  7. use DateTime;
  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\AjaxLoadingLimitConstant;
  13. use App\Constant\PeopleActionConstant;
  14. use App\Constant\ActionStatusConstant;
  15. use App\Entity\FileAction;
  16. use App\Entity\PremiumSubscription;
  17. use App\Entity\User;
  18. use App\Entity\PeopleAction;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. const PROFILE_USER 'profileUser';
  21. const USER_NAME 'user_name';
  22. class ProfileController extends WhiplayController
  23. {
  24.     /**
  25.      * Show profile of specified user (or current user if there is no id in the url)
  26.      *
  27.      * @param Request $request
  28.      * @param SeoManager $seoManager
  29.      * @param StatisticManager $statisticManager
  30.      * @param TranslatorInterface $translator
  31.      * @param $id
  32.      * @return Response
  33.      */
  34.     public function show(
  35.         Request $request,
  36.         SeoManager $seoManager,
  37.         StatisticManager $statisticManager,
  38.         TranslatorInterface $translator,
  39.         $id
  40.     ): Response
  41.     {
  42.         /** @var User $user */
  43.         $user $this->getUser();
  44.         if (!$id) {
  45.             if ($user instanceof User) {
  46.                 $profileUser $user;
  47.             } else {
  48.                 return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  49.             }
  50.         } else {
  51.             /** @var User $profileUser*/
  52.             $profileUser $this->getDoctrine()
  53.                 ->getRepository(User::class)
  54.                 ->find($id);
  55.             if (!($profileUser instanceof User)) {
  56.                 return $this->render('@Twig/views/Exception/error404.html.twig', [
  57.                     'customMessage' => $translator->trans('profile.show.not_found', ['id' => $id])
  58.                 ]);
  59.             }
  60.         }
  61.         $seoManager
  62.             ->setTitleAndDescription(
  63.                 'profile.show.seo.title',
  64.                 'profile.show.seo.description',
  65.                 ['user_name' => $profileUser->getUsername()]
  66.             )
  67.             ->useLogo();
  68.         $currentPremiumSubscription false;
  69.         if ($user instanceof User) {
  70.             $currentPremiumSubscription $this
  71.                 ->getDoctrine()
  72.                 ->getRepository(PremiumSubscription::class)
  73.                 ->findOneBy(array('default' => true));
  74.             $currentOrder $user->getCurrentOrder();
  75.             if ($currentOrder) {
  76.                 $currentPremiumSubscription $currentOrder->getPremiumSubscription();
  77.             }
  78.         }
  79.         //STATISTIC VIEWS
  80.         $statisticManager->addCommonVisit($profileUser->getCommonInformation());
  81.         return $this->render('Profile/show.html.twig', array(
  82.             PROFILE_USER => $profileUser,
  83.             'currentPremiumSubscription' => $currentPremiumSubscription,
  84.         ));
  85.     }
  86.     /**
  87.      * Show edition page for current user
  88.      *
  89.      * @param Request $request
  90.      * @param SeoManager $seoManager
  91.      * @param TranslatorInterface $translator
  92.      * @return Response
  93.      */
  94.     public function edit(
  95.         Request $request,
  96.         SeoManager $seoManager,
  97.         TranslatorInterface $translator
  98.     ): Response {
  99.         /** @var User $user */
  100.         $user $this->getUser();
  101.         if (!$user instanceof User) {
  102.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  103.         }
  104.         $form $this->createForm(UserType::class, $user);
  105.         $entityManager $this
  106.             ->getDoctrine()
  107.             ->getManager();
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             if ($request->get('pictureProfileDelete') && $user->getCommonInformation()->getProfilePicture()) {
  111.                 $entityManager->remove($user->getCommonInformation()->getProfilePicture());
  112.                 $entityManager->flush();
  113.                 $user->getCommonInformation()->setProfilePicture();
  114.             }
  115.             if ($request->get('pictureCoverDelete') && $user->getCommonInformation()->getCoverPicture()) {
  116.                 $entityManager->remove($user->getCommonInformation()->getCoverPicture());
  117.                 $entityManager->flush();
  118.                 $user->getCommonInformation()->setCoverPicture();
  119.             }
  120.             $entityManager->persist($user);
  121.             $entityManager->flush();
  122.             $this->addFlash(
  123.                 UtilsConstant::NOTICE,
  124.                 $translator->trans('profile.edit.saved')
  125.             );
  126.         }
  127.         $seoManager
  128.             ->setTitleAndDescription('profile.edit.seo.title''profile.edit.seo.description')
  129.             ->useLogo();
  130.         return $this->render('Profile/edit.html.twig', array(
  131.             'user' => $user,
  132.             'form' => $form->createView(),
  133.             PROFILE_USER => $this->getUser(),
  134.         ));
  135.     }
  136.     /**
  137.      * Method for the current user to follow the specified user (based on user_source GET variable)
  138.      * There is no view, it redirects to referer
  139.      *
  140.      * @param Request $request
  141.      * @param TranslatorInterface $translator
  142.      * @return Response
  143.      */
  144.     public function follow(Request $requestTranslatorInterface $translator): Response
  145.     {
  146.         /** @var User $user */
  147.         $user $this->getUser();
  148.         if (!($user instanceof User)) {
  149.             return $this->ajaxForward($requestUtilsConstant::LOGIN_ROUTE);
  150.         }
  151.         $userId $request->request->get('user_source');
  152.         /* @var User $user */
  153.         $followingUser $this->getDoctrine()
  154.             ->getRepository(User::class)
  155.             ->find($userId);
  156.         if (!$followingUser) {
  157.             return $this->ajaxForward($request'whiplay_home');
  158.         }
  159.         $parameters = array(
  160.             'userPeople' => $followingUser->getId(),
  161.             'action' => PeopleActionConstant::FOLLOW,
  162.             'user' => $user->getId()
  163.         );
  164.         $userFollowAction $this
  165.             ->getDoctrine()
  166.             ->getRepository(PeopleAction::class)
  167.             ->findOneBy($parameters);
  168.         $em $this->getDoctrine()->getManager();
  169.         if ($userFollowAction) {
  170.             $em->remove($userFollowAction);
  171.             $this->addFlash(
  172.                 'notice',
  173.                 $translator->trans('profile.follow.deleted')
  174.             );
  175.         } else {
  176.             $userFollowAction = new PeopleAction();
  177.             $userFollowAction->setCreatedAt(new DateTime());
  178.             $userFollowAction->setUser($user);
  179.             $userFollowAction->setUserPeople($followingUser);
  180.             $userFollowAction->setAction(PeopleActionConstant::FOLLOW);
  181.             $userFollowAction->setStatus(ActionStatusConstant::STATUS_PUBLIC);
  182.             $em->persist($userFollowAction);
  183.             $this->addFlash(
  184.                 'notice',
  185.                 $translator->trans('profile.follow.added')
  186.             );
  187.         }
  188.         $em->flush();
  189.         return $this->ajaxForwardToReferer(
  190.             $request,
  191.             'user_profile_show',
  192.             array('id' => $followingUser->getId())
  193.         );
  194.     }
  195.     /**
  196.      * Show likes of specified user based on $type
  197.      *
  198.      * @param SeoManager $seoManager
  199.      * @param User $profileUser
  200.      * @param $type
  201.      * @return Response
  202.      */
  203.     public function likes(SeoManager $seoManagerUser $profileUser$type): Response
  204.     {
  205.         $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::getConstantFromType($type);
  206.         $likes $this
  207.             ->getDoctrine()
  208.             ->getRepository(Action::class)
  209.             ->getUserPublicLikes($profileUser$type0$ajaxLoadingLimitNumber);
  210.         $totalLikesCount count($this
  211.             ->getDoctrine()
  212.             ->getRepository(Action::class)
  213.             ->getUserPublicLikes($profileUser$type));
  214.         $seoManager
  215.             ->setTitleAndDescription(
  216.                 'profile.likes.seo.title',
  217.                 'profile.likes.seo.description',
  218.                 [USER_NAME => $profileUser->getUsername(), 'type' => $type]
  219.             )
  220.             ->useUserLogo($profileUser);
  221.         return $this->render('Profile/likes.html.twig', array(
  222.             PROFILE_USER => $profileUser,
  223.             'likes' => $likes,
  224.             'selectedType' => $type,
  225.             'totalLikesCount' => $totalLikesCount,
  226.             'ajaxLoadingLimitNumber' => $ajaxLoadingLimitNumber,
  227.         ));
  228.     }
  229.     /**
  230.      * Api method to fetch specified user likes based on $type
  231.      * lastId get parameter define from which id actions will be fetched
  232.      *
  233.      * @param Request $request
  234.      * @param User $profileUser
  235.      * @param string $type
  236.      * @return JsonResponse
  237.      */
  238.     public function likesApi(Request $requestUser $profileUserstring $type): JsonResponse
  239.     {
  240.         if (!$request->isXmlHttpRequest()) {
  241.             return new JsonResponse(array('Forbidden'), Response::HTTP_BAD_REQUEST);
  242.         }
  243.         $lastId $request->get('lastId') ? $request->get('lastId') : 0;
  244.         $ajaxLoadingLimitNumber AjaxLoadingLimitConstant::getConstantFromType($type);
  245.         if ($profileUser === $this->getUser()) {
  246.             $likes $this
  247.                 ->getDoctrine()
  248.                 ->getRepository(Action::class)
  249.                 ->getUserLikes($profileUser$type$lastId$ajaxLoadingLimitNumber);
  250.         } else {
  251.             $likes $this
  252.                 ->getDoctrine()
  253.                 ->getRepository(Action::class)
  254.                 ->getUserPublicLikes($profileUser$type$lastId$ajaxLoadingLimitNumber);
  255.         }
  256.         $response = array();
  257.         if (count($likes) < $ajaxLoadingLimitNumber) {
  258.             $response['noMore'] = true;
  259.         }
  260.         $response['actions'] = array();
  261.         /** @var FileAction $action */
  262.         foreach ($likes as $action) {
  263.             $actionInfos = array();
  264.             $actionInfos['id'] = $action->getId();
  265.             $actionInfos['view'] = $this->render('File/item.html.twig', array(
  266.                 'file' => $action->getFile(),
  267.             ))->getContent();
  268.             $response['actions'][] = $actionInfos;
  269.         }
  270.         return new JsonResponse($responseResponse::HTTP_OK);
  271.     }
  272.     /**
  273.      * Show subscriptions of specified user
  274.      *
  275.      * @param SeoManager $seoManager
  276.      * @param TranslatorInterface $translator
  277.      * @param $id
  278.      * @return Response
  279.      */
  280.     public function subscriptions(SeoManager $seoManagerTranslatorInterface $translator$id): Response
  281.     {
  282.         $profileUser $this->getDoctrine()
  283.             ->getRepository(User::class)
  284.             ->find($id);
  285.         if (!($profileUser instanceof User)) {
  286.             return $this->render('@Twig/views/Exception/error404.html.twig', [
  287.                 'customMessage' => $translator->trans('profile.show.not_found', ['id' => $id])
  288.             ]);
  289.         }
  290.         $seoManager
  291.             ->setTitleAndDescription(
  292.                 'profile.subscriptions.seo.title',
  293.                 'profile.subscriptions.seo.description',
  294.                 [USER_NAME => $profileUser->getUsername()]
  295.             )
  296.             ->useUserLogo($profileUser);
  297.         return $this->render('Profile/subscriptions.html.twig', array(
  298.             PROFILE_USER => $profileUser,
  299.         ));
  300.     }
  301. }