Voici votre URL de partage https://sharemycode.io/c/be2329a (Cliquer pour copier) (Copié)
<?php
namespace App\Controller;
use App\Entity\Users;
use App\Entity\Addresses;
use App\Form\NewsletterType;
use App\Model\SearchData;
use App\Form\AddressesType;
use App\Form\SearchFormType;
use App\Form\ProfileFormType;
use App\Repository\AddresstypesRepository;
use App\Repository\MenusRepository;
use App\Repository\OrdersRepository;
use App\Repository\BlocCmsRepository;
use App\Repository\ArticlesRepository;
use App\Repository\ProductsRepository;
use App\Repository\AddressesRepository;
use App\Repository\MenuTypesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Repository\ProductCategoriesRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/myaccount', name: 'profile_')]
class ProfileController extends AbstractController
{
#[Route('/', name: 'index')]
public function index(
ArticlesRepository $articles,
BlocCmsRepository $cms,
OrdersRepository $orders,
ProductsRepository $products,
ProductCategoriesRepository $productCategories,
MenusRepository $menus,
MenuTypesRepository $menutypes,
Request $request
): Response {
if ($this->denyAccessUnlessGranted('ROLE_USER')) {
return $this->redirectToRoute('app_login');
}
$articles = $articles->findBy([], ['id' => 'asc']);
$cms = $cms->findBy([], ['id' => 'asc']);
$categories = $productCategories->findBy([], ['categoryOrder' => 'asc']);
$menus = $menus->findBy([], ['id' => 'asc']);
$menutypes = $menutypes->findBy([], ['id' => 'asc']);
$usr = new Users();
$profileForm = $this->createForm(ProfileFormType::class, $usr);
$orders = $orders->findByUsers($this->getUser(), ['id' => 'desc']);
$searchData = new SearchData();
$form = $this->createForm(SearchFormType::class, $searchData);
$form->handleRequest($request);
$searchForm = $form->createView();
if ($form->isSubmitted() && $form->isValid()) {
$searchData->page = $request->query->getInt('page', 1);
$products = $products->findBySearch(1, $searchData);
$searchForm = $form->createView();
return $this->render('products/index.html.twig', compact('cms', 'categories', 'menus', 'menutypes', 'articles', 'products', 'searchForm'));
}
return $this->render('profile/index.html.twig', compact('articles', 'categories', 'orders', 'menus', 'menutypes', 'searchForm', 'profileForm'));
}
#[Route('/editprofile/{id}', name: 'edit', methods: ['GET'])]
public function edit(
ArticlesRepository $articles,
AddressesRepository $addressesRepository,
BlocCmsRepository $cms,
ProductsRepository $products,
ProductCategoriesRepository $productCategories,
MenusRepository $menus,
MenuTypesRepository $menutypes,
Request $request,
Users $usr
): Response {
if ($this->denyAccessUnlessGranted('ROLE_USER')) {
return $this->redirectToRoute('app_login');
}
if ($usr !== $this->getUser()) {
return $this->redirectToRoute('profile_index');
}
$articles = $articles->findBy([], ['id' => 'asc']);
$cms = $cms->findBy([], ['id' => 'asc']);
$categories = $productCategories->findBy([], ['categoryOrder' => 'asc']);
$menus = $menus->findBy([], ['id' => 'asc']);
$menutypes = $menutypes->findBy([], ['id' => 'asc']);
$addresses = $addressesRepository->findByUsers($this->getUser());
$addr = new Addresses();
$profileForm = $this->createForm(ProfileFormType::class, $usr);
$frm = $this->createForm(AddressesType::class, $addr);
$frm1 = $this->createForm(NewsletterType::class, $usr);
$searchData = new SearchData();
$form = $this->createForm(SearchFormType::class, $searchData);
$form->handleRequest($request);
$searchForm = $form->createView();
$AddressesForm = $frm->createView();
$newsletterForm = $frm1->createView();
return $this->render('profile/editprofile.html.twig', compact('addresses', 'articles', 'categories', 'menus', 'menutypes', 'searchForm', 'profileForm', 'AddressesForm', 'newsletterForm'));
}
#[Route('/createaddress', name: 'createaddress', methods: ['POST'])]
public function createaddress(
EntityManagerInterface $manager,
AddresstypesRepository $addresstypesRepo,
Request $request,
TranslatorInterface $translator
): Response {
$usr = $this->getUser();
$addressData = new Addresses();
$form = $this->createForm(AddressesType::class, $addressData);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
if (!empty($data->getFirstname()) && !empty($data->getLastname())) {
// User edit personal informations only
$usr->setCivility($data->getCivility())
->setFirstname($data->getFirstname())
->setLastname($data->getLastname())
->setCountry($data->getCountry());
if (isset($_POST["newsletter"])) {
$usr->setIsNewslettersubs($_POST["newsletter"]);
}
$manager->flush();
$this->addFlash('success', $translator->trans('USERINFORMATIONS_UPDATED'));
}
if (
!is_null($data->getAddressname2()) && !is_null($data->getCity()) && !is_null($data->getZipcode())
&& !is_null($data->getPhonetypes()) && !is_null($data->getPhoneprefix()) && !is_null($data->getPhonenumber())
&& !is_null($data->getStreetnumbername())
) {
// User add new address
$Addressname1 = (null !== $data->getAddressname1()) ? $data->getAddressname1() : '';
$addrTypes = $addresstypesRepo->find(['id' => '2']);
$addressData->setCivility($data->getCivility())
->setFirstname($data->getFirstname())
->setLastname($data->getLastname())
->setCountry($data->getCountry())
->setFirstname($data->getFirstname())
->setLastname($data->getLastname())
->setCivility($data->getCivility())
->setCountry($data->getCountry())
->setUsers($usr)
->setAddressname1($Addressname1)
->setAddressname2($data->getAddressname2())
->setZipcode($data->getZipcode())
->setCity($data->getCity())
->setPhoneprefix($data->getPhoneprefix())
->setPhonenumber($data->getPhonenumber())
->setStreetnumbername($data->getStreetnumbername())
->setPhonetypes($data->getPhonetypes())
->setAddresstype($addrTypes)
;
$manager->persist($addressData);
$manager->flush();
$this->addFlash('success', $translator->trans('ADDRESS_ADDED_SUCCESSFULLY'));
}
return $this->redirectToRoute('profile_index');
}
return $this->redirectToRoute('profile_edit');
}
#[Route('/subscribe/{id}', name: 'subscribe', methods: ['POST'])]
public function subscribe(
EntityManagerInterface $manager,
Request $request,
TranslatorInterface $translator,
Users $usr
): Response {
$user = $this->getUser();
if (!$this->getUser()) {
return $this->redirectToRoute('app_login');
}
if ($usr !== $this->getUser()) {
return $this->redirectToRoute('profile_index');
}
$form = $this->createForm(NewsletterType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$manager->persist($user);
$manager->flush();
$this->addFlash('success', $translator->trans('NEWSLETTER_SUCCESSFULLY_REGISTERED'));
}
return $this->redirectToRoute('profile_edit');
}
}
Informations
Cet extrait a été créé le 4 sept. 2024 à 12:21:35
Cet extrait expire le 4 oct. 2024 à 12:21:35
Langage : php
Link
Voici votre URL de partage : https://sharemycode.io/c/be2329a Copié