<?php
namespace App\Controller;
use App\Entity\Product;
use App\Entity\Sizes;
use App\Entity\User;
use App\Form\AddProductType;
use App\Form\RegistrationFormBuyerType;
use App\Form\RegistrationStepTwoType;
use App\Repository\IndividualProductsRepository;
use App\Services\IndividualProductService;
use App\Services\MasterService;
use App\Repository\OrderRepository;
use App\Services\OrderService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Entity;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
class ProfileController extends AbstractController
{
#[Route('/profile/info', name: 'profile_info')]
public function info(Request $request, EntityManagerInterface $entityManager): Response
{
/** @var User $user */
$user = $this->getUser();
if ($user->getStatus() === User::STATUS_USER_STEP_REGISTRATION_2) {
$formSellerStepTwo = $this->createForm(RegistrationStepTwoType::class, $user);
$formSellerStepTwo->handleRequest($request);
if ($formSellerStepTwo->isSubmitted() && $formSellerStepTwo->isValid()) {
// encode the plain password
$user->setAboutMe($formSellerStepTwo->get('aboutMe')->getData());
$user->setWebsite($formSellerStepTwo->get('website')->getData());
$user->setStatus(User::STATUS_USER_STEP_REGISTRATION_3);
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('profile_info');
}
return $this->render('profile/registration_step_two.html.twig', [
'registrationStepTwo' => $formSellerStepTwo->createView(),
]);
} else if ($user->getStatus() === User::STATUS_USER_STEP_REGISTRATION_3) {
return $this->render('profile/info.html.twig', [
'registrationStep' => User::STATUS_USER_STEP_REGISTRATION_3,
'user' => $user
]);
} else if ($user->getStatus() === User::STATUS_USER_STEP_REGISTRATION_3_MODERATOR_VIEW) {
return $this->render('profile/info.html.twig', [
'registrationStep' => User::STATUS_USER_STEP_REGISTRATION_3_MODERATOR_VIEW,
'user' => $user
]);
} else if ($user->getStatus() === User::STATUS_USER_STEP_REGISTRATION_3_MODERATOR_DECLINE) {
return $this->render('profile/info.html.twig', [
'registrationStep' => User::STATUS_USER_STEP_REGISTRATION_3_MODERATOR_DECLINE,
'user' => $user
]);
} else {
return $this->render('profile/info.html.twig', [
'registrationStep' => User::STATUS_USER_READY,
'user' => $user
]);
}
}
#[Route('/profile/info/changeAvatar', name: 'profile_info_change_avatar')]
public function changeAvatar(EntityManagerInterface $entityManager, Request $request)
{
$user = $this->getUser();
$masterService = new MasterService();
$masterService->changeUserAvatar($entityManager, $request, $user);
return new Response('', Response::HTTP_OK);
}
#[Route('/profile/products', name: 'profile_products')]
public function products(EntityManagerInterface $entityManager): Response
{
$userId = $this->getUser()->getId();
$products = $entityManager->getRepository(Product::class)->findBy(['seller' => $userId]);
return $this->render('profile/products.html.twig', [
'controller_name' => 'ProfileController',
'products' => $products
]);
}
#[Route('/profile/products/stop_sale', name: 'stop_sale', methods: 'GET')]
public function stopSale(Request $request, EntityManagerInterface $entityManager): Response
{
$productId = $request->query->get('product');
/** @var Product $product */
$product = $entityManager->getRepository(Product::class)->find($productId);
$product->getStatus() === Product::STATUS_PRODUCTS_ACTUAL ? $product->setStatus(Product::STATUS_PRODUCTS_NOT_ACTUAL) : $product->setStatus(Product::STATUS_PRODUCTS_ACTUAL);
$entityManager->flush();
return $this->redirectToRoute('profile_products');
}
#[Route('/profile/myOrders', name: 'seller_orders')]
public function orders(OrderRepository $orderRepository)
{
$userId = $this->getUser()->getId();
$data = $orderRepository->findSellerOrders($userId);
$orders = (new OrderService())->prepareOrderDataForSellerTemplate($data);
return $this->render('profile/orders.html.twig', [
'orders' => $orders
]);
}
#[Route('/profile/products/addProduct', name:'seller_add_product')]
public function addProduct(EntityManagerInterface $entityManager, Request $request)
{
$user = $this->getUser();
$sizes = $entityManager->getRepository(Sizes::class)->findAll();
$masterService = new MasterService();
$form = $this->createForm(AddProductType::class);
$result = $masterService->addNewProduct($entityManager, $request, $form, $user);
if ($result instanceof JsonResponse)
return $result;
return $this->render('profile/addProduct.html.twig', [
"form" => $form->createView(),
"sizes" => $sizes,
"newProduct" => true
]
);
}
#[Route('/profile/products/addCustomSize', name: 'seller_add_customSize')]
public function addCustomSize(EntityManagerInterface $entityManager,Request $request)
{
$masterService = new MasterService();
$result = $masterService->addNewSize($entityManager, $request);
if ($result) {
return $this->redirectToRoute('addProduct');
}
return $this->render('profile/addCustomSize.html.twig');
}
#[Route('/profile/changeProduct/{productId}', name: 'seller_product_change')]
public function changeProduct(EntityManagerInterface $entityManager, string $productId, Request $request): JsonResponse|Response
{
$user = $this->getUser();
$sizes = $entityManager->getRepository(Sizes::class)->findAll();
$masterService = new MasterService();
$product = $entityManager->getRepository(Product::class)->find($productId);
$form = $this->createForm(AddProductType::class, $product);
if ($masterService->changeProduct($entityManager, $request, $form, $user, $productId))
return new JsonResponse(['success' => 'Товар успешно изменен']);
return $this->render('profile/addProduct.html.twig', [
"form" => $form->createView(),
"sizes" => $sizes,
"newProduct" => false
]
);
}
#[Route('/profile/individualProducts', name: 'seller_individual_products')]
public function showIndividualProducts(IndividualProductsRepository $individualProductsRepository): Response
{
$userId = $this->getUser()->getId();
$iProducts = $individualProductsRepository->getSellerIndividualProducts($userId);
return $this->render('profile/individual_products.html.twig', [
'iProducts' => $iProducts
]);
}
#[Route('/profile/acceptIndividualProduct/{iProductId}', name: 'seller_individual_products_accept')]
public function acceptIndividualProduct(EntityManagerInterface $entityManager, string $iProductId): Response
{
$user = $this->getUser();
$iProductService = new IndividualProductService();
$iProductService->acceptIndividualProduct($entityManager, $user, $iProductId);
return $this->redirectToRoute('seller_individual_products');
}
#[Route('/profile/completeIndividualProduct/{iProductId}', name: 'seller_individual_products_complete')]
public function completeIndividualProduct(EntityManagerInterface $entityManager, string $iProductId): Response
{
$user = $this->getUser();
$iProductService = new IndividualProductService();
$iProductService->completeIndividualProduct($entityManager, $user, $iProductId);
return $this->redirectToRoute('seller_individual_products');
}
}