src/Controller/AuthorController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BeatAuthor;
  4. use App\Entity\ClientNotification;
  5. use App\Entity\NotificationType;
  6. use App\Form\Type\BeatAuthorType;
  7. use App\Repository\BeatAuthorRepository;
  8. use App\Repository\BeatRepository;
  9. use App\Repository\ClientNotificationRepository;
  10. use App\Repository\ClientRepository;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\UX\Cropperjs\Factory\CropperInterface;
  17. class AuthorController extends AbstractController
  18. {
  19.     private BeatAuthorRepository $beatAuthorRepository;
  20.     private ClientRepository $clientRepository;
  21.     private BeatRepository $beatRepository;
  22.     private UserDataController $userDataController;
  23.     private ClientNotificationRepository $clientNotificationRepository;
  24.     public function __construct(
  25.         BeatAuthorRepository $beatAuthorRepository,
  26.         ClientRepository $clientRepository,
  27.         UserDataController $userDataController,
  28.         BeatRepository $beatRepository,
  29.         ClientNotificationRepository $clientNotificationRepository
  30.     )
  31.     {
  32.         $this->beatAuthorRepository $beatAuthorRepository;
  33.         $this->clientRepository $clientRepository;
  34.         $this->userDataController $userDataController;
  35.         $this->beatRepository $beatRepository;
  36.         $this->clientNotificationRepository $clientNotificationRepository;
  37.     }
  38.     #[Route(path'/author/create'name'authorCreate'methods: ['POST'])]
  39.     public function frontendAuthorCreateAction(CropperInterface $cropperRequest $request): Response
  40.     {
  41.         $data $request->request->all();
  42.         $user $this->getUser();
  43.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  44.         $newAuthor = new BeatAuthor();
  45.         $newAuthor
  46.             ->setPseudoname($data['beat_author']['pseudoname'])
  47.             ->setClient($client)
  48.             ->setFirstname($client->getName())
  49.             ->setLastname($client->getSurname())
  50.             ->setCreatedAt(new \DateTime('now'));
  51.         if($this->beatAuthorRepository->save($newAuthor,true)){
  52.             return $this->redirectToRoute('profile');
  53.         }else{
  54.             return new Response("Error"Response::HTTP_BAD_REQUEST);
  55.         }
  56.     }
  57.     #[Route(path'/author/edit'name'authorEdit'methods: ['POST'])]
  58.     public function frontendAuthorEditAction(CropperInterface $cropperRequest $request): Response
  59.     {
  60.         $data $request->request->all();
  61.         $user $this->getUser();
  62.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  63.         $editAuthor $client->getAuthor();
  64.         $editAuthor
  65.             ->setPseudoname($data['beat_author']['pseudoname'])
  66.             ->setClient($client)
  67.             ->setFirstname($client->getName())
  68.             ->setLastname($client->getSurname())
  69.             ->setUpdatedAt(new \DateTime('now'));
  70.         if( $this->beatAuthorRepository->save($editAuthor,true)){
  71.             return $this->redirectToRoute('profile');
  72.         }else{
  73.             return new Response("Error"Response::HTTP_BAD_REQUEST);
  74.         }
  75.     }
  76.     #[Route(path:  [
  77.         'cs' => '/autor/{id}',
  78.         'en' => '/author/{id}'
  79.     ], name'authorDetail')]
  80.     public function frontendRapperDetailAction($id): Response
  81.     {
  82.         $user $this->getUser();
  83.         $author $this->beatAuthorRepository->findOneBy(['id'=>$id]);
  84.         $mostFavoriteBeats $this->beatRepository->findAllFavoritedBeatsByAuthor($author,6);
  85.         if ($user==null){
  86.             return $this->render('pages/author-detail.html.twig',[
  87.                 "author"=>$author,
  88.                 "mostFavoriteBeats"=>$mostFavoriteBeats
  89.             ]);
  90.         }else{
  91.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  92.             if($client!=null && $client->isIsActive()){
  93.                 return $this->render('pages/author-detail.html.twig',[
  94.                     'userData'=>$this->userDataController->getUserData(),
  95.                     "author"=>$author,
  96.                     "mostFavoriteBeats"=>$mostFavoriteBeats
  97.                 ]);
  98.             }else{
  99.                 return $this->render('pages/non-active-content.html.twig',[
  100.                     'userData'=>$this->userDataController->getUserData(),
  101.                     "author"=>$author,
  102.                     "mostFavoriteBeats"=>$mostFavoriteBeats
  103.                 ]);
  104.             }
  105.         }
  106.     }
  107.     #[Route(path'/author-follow'name'authorFollow'methods: ['POST'])]
  108.     public function frontendAuthorFollowAction(Request $request): JsonResponse
  109.     {
  110.         $data $request->request->all();
  111.         $user $this->getUser();
  112.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  113.         $editAuthor $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
  114.         $editAuthor->addFollower($client);
  115.         $notification = new ClientNotification();
  116.         $notification
  117.             ->setCreatedAt(new \DateTime('now'))
  118.             ->setTitle('New beat maker follower '.$client->getName())
  119.             ->setNotificationText('You now have ('.count($editAuthor->getFollowers()).') followers on your beat maker profile.')
  120.             ->setType(NotificationType::INTERACTION_TYPE_FOLLOWED)
  121.             ->setClient($editAuthor->getClient())
  122.             ->setNotificationRead(false);
  123.         $this->clientNotificationRepository->save($notification,true);
  124.         if($this->beatAuthorRepository->save($editAuthor,true)){
  125.             return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
  126.         }else{
  127.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  128.         }
  129.     }
  130.     #[Route(path'/author-unfollow'name'authorUnfollow'methods: ['POST'])]
  131.     public function frontendAuthorUnfollowAction(Request $request): JsonResponse
  132.     {
  133.         $data $request->request->all();
  134.         $user $this->getUser();
  135.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  136.         $editAuthor $this->beatAuthorRepository->findOneBy(['id'=>$data['author_id']]);
  137.         $editAuthor->removeFollower($client);
  138.         if($this->beatAuthorRepository->save($editAuthor,true)){
  139.             return new JsonResponse($editAuthor->getPseudoname(), Response::HTTP_CREATED);
  140.         }else{
  141.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  142.         }
  143.     }
  144.     #[Route(path'/author-avatar-upload'name'authorUploadAvatar'methods: ['POST'])]
  145.     public function frontendAuthorUploadAvatarAction(Request $request): JsonResponse
  146.     {
  147.         $user $this->getUser();
  148.         $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  149.         $avatarNew $request->files->get('imageFile');
  150.         $author $client->getAuthor();
  151.         $form $this->createForm(BeatAuthorType::class, $client);
  152.         $form->handleRequest($request);
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             $author
  155.                 ->setUpdatedAt(new \DateTime('now'));
  156.         }
  157.         if($this->beatAuthorRepository->save($author,true)){
  158.             return new JsonResponse("Uploaded"Response::HTTP_OK);
  159.         }else{
  160.             return new JsonResponse("Error"Response::HTTP_BAD_REQUEST);
  161.         }
  162.     }
  163. }