<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserData;
use App\Service\CampaignRecurrentService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\UserService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\Repository\UserRepository;
use App\Repository\PortfolioRepository;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Twig\Environment;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
#[Route('/user', name: "user_")]
class UserController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManagerInterface,
private CampaignRecurrentService $campaignRecurrentService,
private UserRepository $userRepository,
private PortfolioRepository $portfolioRepository,
private UserService $userService,
private SerializerInterface $serializer,
){}
#[Route('/delete/{id}', name: "delete")]
public function deletedUser(User $user) : RedirectResponse
{
if($this->getUser()->getId() == $user->getId()){
$this->userService->deleteDataWpAndApp($user);
return $this->redirectToRoute('app_logout');
}
else{
return $this->redirectToRoute('mission_index');
}
}
#[Route('/my-flow/directory/page/{page}/{query}', name: "my_flow_directory", methods: ['GET'],defaults: ['query'=>""])]
public function myFlowDirectory(Request $request,int $page = 1, string $query = "") : JsonResponse
{
$data = $this->userRepository->findByUserAcceptPresentation(preg_replace('/\s+/', ' ', $query));
$limit = 12;
$offset = ($page - 1) * $limit;
$totalItems = count($data);
$totalPages = ceil($totalItems / $limit);
$paginatedData = array_slice($data, $offset, $limit);
$jsonData = $this->serializer->serialize([
'users'=>$paginatedData,
'page'=>$page,
'next_page'=> $totalPages <= $page ? 0 : $page+1,
'total_page'=> $totalPages,
'list_user_rendered'=> 1,
'total_item'=> $totalItems,
], 'json', ['groups' => ['user_directory']]);
$response = new JsonResponse( $jsonData,200, [],true) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $response ;
}
#[Route('/my-flow/directory/{id}', name: "my_flow_directory_show", methods: ['GET'])]
public function myFlowDirectoryShow(Request $request, string $id) : JsonResponse
{
$user = $this->userRepository->findOneBy(['acceptPresentation'=>true, 'id'=>$id]);
$jsonData = $this->serializer->serialize($user, 'json', ['groups' => ['user_directory']]);
$response = new JsonResponse( $jsonData,200, [],true) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $response ;
}
#[Route('/my-flow/realisation-subcontractor-{limit}', name: "my_flow_realisation_subcontractor", methods: ['GET'])]
public function realisationShow(Request $request,$limit = null) : JsonResponse
{
$portfolioId = $this->portfolioRepository->somePortfolioId($limit);
shuffle($portfolioId);
$randomIds = array_slice($portfolioId, 0, 12);
$portfolio = $this->portfolioRepository->somePortfolioRand($limit,$randomIds);
$response = new JsonResponse( $portfolio,200, [],false) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $response ;
}
#[Route('/my-flow/realisation-list-subcontractor-{limit}', name: "my_flow_list_realisation_subcontractor", methods: ['GET'])]
public function realisationShowList(Request $request,$limit = null) : JsonResponse
{
$portfolio = $this->portfolioRepository->somePortfolio($limit);
$response = new JsonResponse( $portfolio,200, [],false) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $response ;
}
#[Route('/my-flow/count-realisation-subcontractor', name: "my_flow_count_realisation_subcontractor", methods: ['GET'])]
public function countRealisationShow(Request $request) : JsonResponse
{
$portfolio = $this->portfolioRepository->countsomePortfolio();
$response = new JsonResponse( $portfolio,200, [],false) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $portfolio ;
}
#[Route('/my-flow/realisation-html-list-{limit}-{offset}', name: "my_flow_realisation_subcontractor_html_results", methods: ['GET'])]
public function realisationShowHtmlResults(Request $request,$limit = null,$offset = null)
{
$categorie = $request->query->get('cat');
$keys = $request->query->get('keys');
$job = $request->query->get('job');
$filter = [
'cat' => $categorie,
'keys' => $keys,
'job' => $job,
];
$portfolio = $this->portfolioRepository->somePortfolio($limit,$offset);
$portfolio = $this->portfolioRepository->somePortfolioFilter($limit,$offset,$filter);
$portfolioAll = $this->portfolioRepository->somePortfolioFilter(null,$offset,$filter);
$nb = count($portfolioAll);
return $this->render('sub_contractor/portfolio/portfolio_results_list.html.twig', [
'portfolio'=> $portfolio,
'offset'=> $offset,
'nb'=> $nb,
]);
}
#[Route('/my-flow/{id}/realisation-list-subcontractor-{limit}', name: "my_flow_realisation_user_subcontractor_results", methods: ['GET'])]
public function realisationUserShowResults(string $id, Request $request,$limit = null)
{
$portfolio = $this->portfolioRepository->somePortfolio($limit,null, $id);
$response = new JsonResponse( $portfolio,200, [],false) ;
$response->setEncodingOptions($response->getEncodingOptions() | JSON_UNESCAPED_UNICODE);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
return $response ;
}
#[Route('/my-flow/{id}/realisation-html-list-{limit}-{offset}', name: "my_flow_realisation_user_subcontractor_html_results", methods: ['GET'])]
public function realisationUserShowHtmlResults(string $id, Request $request,$limit = null,$offset = null)
{
$categorie = $request->query->get('cat');
$keys = $request->query->get('keys');
$job = $request->query->get('job');
$filter = [
'cat' => $categorie,
'keys' => $keys,
'job' => $job,
];
$portfolio = $this->portfolioRepository->somePortfolioFilter($limit,$offset, $id);
$portfolioAll = $this->portfolioRepository->somePortfolioFilter(null,$offset,$filter);
$nb = count($portfolioAll);
return $this->render('sub_contractor/portfolio/portfolio_results_list.html.twig', [
'portfolio'=> $portfolio,
'offset'=> $offset,
'nb'=> $nb,
'limit'=> 16,
]);
}
#[Route('/my-flow/realisation-filter-html-list-{limit}-{offset}', name: "my_flow_realisation_user_subcontractor_html_all_in_filter_results", methods: ['GET'])]
public function filterRealisationResults(Request $request,$limit = null,$offset = null)
{
$categorie = $request->query->get('cat');
$keys = $request->query->get('keys');
$job = $request->query->get('job');
$filter = [
'cat' => $categorie,
'keys' => $keys,
'job' => $job,
];
$portfolio = $this->portfolioRepository->somePortfolioFilter($limit,$offset,$filter);
$portfolioAll = $this->portfolioRepository->somePortfolioFilter(null,$offset,$filter);
$nb = count($portfolioAll);
return $this->render('sub_contractor/portfolio/filter_result.html.twig', [
'portfolio'=> $portfolio,
'limit'=> 24,
'offset'=> $offset,
'nb'=> $nb,
]);
}
#[Route('/my-flow/realisation-filter-count-number-html-list-{limit}-{offset}', name: "my_flow_realisation_user_subcontractor_count_number_html_all_in_filter_results", methods: ['GET'])]
public function filterRealisationResultsCountNumber(Request $request,$limit = null,$offset = null)
{
$categorie = $request->query->get('cat');
$keys = $request->query->get('keys');
$job = $request->query->get('job');
$filter = [
'cat' => $categorie,
'keys' => $keys,
'job' => $job,
];
$portfolioAll = $this->portfolioRepository->somePortfolioFilter(null,$offset,$filter);
$nb = count($portfolioAll);
return new JsonResponse([
'status'=>'OK',
'nb'=>$nb,
]);
}
#[Route('/my-flow/user-menu/{id}', name: "", methods: ['GET'])]
public function menuSave(User $user, Request $request) : JsonResponse
{
$active = $request->query->get('active') == "true" ? true : false;
if($user?->getUserData() == null) {
$userData = new UserData();
$userData->setMenuState($active);
}else{
$userData = $user->getUserData();
$userData->setMenuState($active);
}
$this->entityManagerInterface->persist($userData);
$user->setUserData($userData);
$this->entityManagerInterface->persist($user);
$this->entityManagerInterface->flush();
return new JsonResponse('ok');
}
#[Route('/my-flow/detail-realisation/{id}',name:'get_this_realisation', methods:'GET')]
public function getThisRealisation(int $id,Environment $twig){
$detailPortfolio = [];
$portfolio = $this->portfolioRepository->findOneBy(['id'=>$id]);
if(!is_null($portfolio)){
$detailPortfolio['id'] = $portfolio->getID();
$detailPortfolio['title'] = $portfolio->getTitle();
$detailPortfolio['fileCategory'] = $portfolio->getFileCategory();
$detailPortfolio['fileUrl'] = $portfolio->getFileUrl();
$detailPortfolio['thumbnail'] = $portfolio->getThumbnail();
}
$response = new JsonResponse($detailPortfolio) ;
return $response;
}
#[Route('/my-flow/update-user-filter-state/{opened}',name:"update_user_filter_button_state", )]
public function updateFilterButtonManagerState(string $opened,SessionInterface $session ){
$session->set('is_filter_open',$opened);
$isFilterOpened = $session->get('is_filter_open');
return new JsonResponse($isFilterOpened);
}
#[Route('/my-flow/user-job-related-to-realisation',name:'get_user_job_related_to_realisation')]
public function getUserJobRelatedToRealisation(): JsonResponse {
$listJobToSend = [];
$listJobRelatedToRealisation = $this->portfolioRepository->getJobRelatedToRealisation();
$listJobToSend['job'] = $listJobRelatedToRealisation;
$response = new JsonResponse( $listJobToSend,200, [],false) ;
return $response;
}
#[Route('/my-flow/get-this-realisation/{id}',name:'get_this_realisation', methods:'GET')]
public function get_this_realisation(int $id, Request $request){
$portfolio = $this->portfolioRepository->findOneBy(['id'=>$id]);
return new JsonResponse($portfolio);
}
}