<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\Game;
use App\Helper\UploadedBase64File;
use App\Services\Api\CustomerManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class SeasonController extends ApiController
{
/** @var CustomerManager $customerManager */
protected $customerManager;
/**
* @param CustomerManager $customerManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, CustomerManager $customerManager)
{
parent::__construct($em, $parameterBag);
$this->customerManager = $customerManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/list", name="api_season_list", methods={"POST"})
*/
public function listSeason(Request $request)
{
$request = $this->transformJsonBody($request);
$seasons = [];
$active = 0;
/** @var Customer $customer */
$customer = $this->getUser();
$i = 0;
foreach ($customer->getSeason() as $season) {
if ($season->getIsActive()) {
$imageTeam = $season->getTeam()->getImage();
$prefixFile = $this->getParameter("app.path.customer_images");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$imagePath = !empty($imageTeam) ? $baseurl . $prefixFile . "/" . $imageTeam : "";
$seasons[] = [
"id" => $season->getId(),
"year" => $season->getYear(),
"text" => $season->getYear() . " / " . ($season->getYear() + 1),
"teamName" => $season->getTeam()->getName(),
"teamLogo" => $imagePath,
];
if (!empty($customer->getSeasonActive()) && $season->getId() == $customer->getSeasonActive()->getId())
$active = $i;
$i++;
}
}
return $this->response(["seasons" => $seasons, "active" => $active]);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/add", name="api_season_add", methods={"POST"})
*/
public function addSeason(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->customerManager->addSeason($data, $request);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/secondary/add", name="api_season_secondary_add", methods={"POST"})
*/
public function addSecondarySeason(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->customerManager->addSecondarySeason($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/change", name="api_season_change", methods={"POST"})
*/
public function changeSeason(Request $request)
{
$request = $this->transformJsonBody($request);
$season = $request->get('season');
$response = $this->customerManager->changeSeason($season, $request);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/profile/password", name="api_profile_password", methods={"POST"})
*/
public function profilePassword(Request $request)
{
$data = [
"plainPassword" => $request->get('password'),
];
$response = $this->customerManager->setPassword($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/update_config", name="api_season_update_config", methods={"POST"})
*/
public function configSeason(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->customerManager->configSeason($data, $request);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/season/delete/{seasonId}", name="api_season_delete", methods={"POST"})
*/
public function deleteSeason($seasonId, Request $request)
{
$request = $this->transformJsonBody($request);
$response = $this->customerManager->deleteSeason($seasonId);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"name" => $request->get('name'),
"surname" => $request->get('surname'),
"teamName" => $request->get('teamName'),
"teamCategory" => $request->get('teamCategory', ''),
"season" => $request->get('season'),
"monthlyCost" => $request->get('monthlyCost'),
"kitPaymentCost" => $request->get('kitPaymentCost'),
"otherPaymentCost" => $request->get('otherPaymentCost'),
"plainPassword" => $request->get('password'),
"countryId" => $request->get('countryId'),
"typeSoccer" => $request->get('typeSoccer'),
"alignmentDefaultU7" => $request->get("alignmentDefaultU7"),
"alignmentDefaultU8" => $request->get("alignmentDefaultU8"),
"alignmentDefaultU11" => $request->get("alignmentDefaultU11"),
"dateOfBirth" => $request->get("dateOfBirth"),
"phone" => $request->get("phone"),
"stadiumName" => $request->get("stadiumName"),
"numberClubesLeague" => $request->get("numberClubesLeague"),
];
$image = $request->get('teamLogo');
if (!empty($image))
$data["teamLogo"] = new UploadedBase64File($image, $request->get('teamLogoFilename'));
$image = $request->get('trainerLogo');
if (!empty($image))
$data["trainerLogo"] = new UploadedBase64File($image, $request->get('trainerLogoFilename'));
return $data;
}
}