src/Controller/Front/CustomerController.php line 113

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Manager\CustomerManager;
  11. use App\Entity\Customer;
  12. use App\Manager\CartManager;
  13. class CustomerController extends FrontController
  14. {
  15. public function block(CustomerManager $customerMgr)
  16. {
  17. $customer = $customerMgr->getCustomer();
  18. return $this->render('front/customer/blocks/header.html.twig', [
  19. 'customer' => $customer
  20. ]);
  21. }
  22. /**
  23. * @Route("/{_locale}/customer/login", name="customer_login")
  24. */
  25. public function login(Request $request, CustomerManager $customerMgr, Session $session, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  26. {
  27. $from = $request->get('from','index');
  28. $customer = $customerMgr->getCustomer();
  29. if(!empty($customer))
  30. return $this->redirectToRoute ('customer_account');
  31. $loginForm = $this->createForm(\App\Form\Customer\LoginType::class,null,[
  32. 'action' => $this->generateUrl('customer_login', ['from'=>$from]),
  33. 'method' => 'POST',
  34. 'translator' => $translator,
  35. 'locale' => $request->getLocale(),
  36. 'attr' => [
  37. 'novalidate'=>'novalidate'
  38. ]
  39. ]);
  40. $registerForm = $this->createForm(\App\Form\Customer\QuickRegisterType::class,null,[
  41. 'action' => $this->generateUrl('customer_login', ['from'=>$from]),
  42. 'method' => 'POST',
  43. 'entity_manager'=>$em,
  44. 'translator'=>$translator,
  45. 'attr' => [
  46. 'novalidate'=>'novalidate'
  47. ]
  48. ]);
  49. if($request->isMethod('POST')){
  50. $loginForm->handleRequest($request);
  51. if($loginForm->isSubmitted()){
  52. if($loginForm->isValid()){
  53. $data = $loginForm->getData();
  54. try{
  55. $result = $customerMgr->login($data['email'], $data['password']);
  56. if($result!==false){
  57. $status = $result->getStatus();
  58. if($status == Customer::STATUS_WAITING){
  59. $session->getFlashBag()->add('popup', $translator->trans("Votre demande de création de compte PRO a bien été prise en compte. Notre délai de traitement est de 72h. Au-delà de ce délai, si vous rencontrez des problèmes de connexion, nous vous invitons à contacter le service client au +33 (0)2 47 73 38 38, email : infos@vivog.fr"));
  60. // $tvaForm = $this->createTvaForm($result, $em, $translator, $from);
  61. // $popup = $this->renderView('front/customer/forms/tva.html.twig', [
  62. // 'tvaForm' => $tvaForm->createView()
  63. // ]);
  64. // $session->getFlashBag()->add('modal', $popup);
  65. }elseif($status == Customer::STATUS_BLOCKED){
  66. $session->getFlashBag()->add('popup', $translator->trans("Votre demande de compte Pro n’a pas pu être validée. Il nous manque des éléments pour valider votre accès PRO. Nous vous invitons à contacter le service client au +33 (0)2 47 73 38 38, email : infos@vivog.fr."));
  67. }
  68. $priceGroups = $result->getPriceGroups();
  69. if(count($priceGroups) > 1) {
  70. $msg = $this->renderView('front/customer/popups/price-groups/login.html.twig', [
  71. 'customer' => $customer,
  72. 'priceGroups' => $priceGroups,
  73. 'from' => $this->generateUrl($from)
  74. ]);
  75. $session->getFlashBag()->add('popup', $msg);
  76. }else if(count($priceGroups) == 1) {
  77. $customerMgr->setPriceContext($priceGroups->first()->getGroup());
  78. }
  79. return $this->redirectToRoute($from);
  80. }else{
  81. $session->getFlashBag()->add('error', $translator->trans("Erreur d'identification."));
  82. }
  83. } catch (\App\Exception\PriceGroupException $ex) {
  84. $session->getFlashBag()->add('error', $translator->trans("Vous n'avez accès à aucun type de tarif. <br>Veuillez contacter notre équipe commerciale."));
  85. } catch (\Exception $ex) {
  86. $session->getFlashBag()->add('error', $translator->trans("Une erreur s'est produite."));
  87. }
  88. }else{
  89. $session->getFlashBag()->add('error', $translator->trans($loginForm->getErrors()));
  90. }
  91. }
  92. $registerForm->handleRequest($request);
  93. if($registerForm->isSubmitted() && $registerForm->isValid()){
  94. $email = strtolower(trim($registerForm->get('email')->getData()));
  95. $customer = $em->getRepository('App:Customer')->findOneBy(array('email'=>$email));
  96. if(!empty($customer)){
  97. $session->getFlashBag()->add('error', $translator->trans("Un compte utilisateur utilise déjà l'adresse email : ").$email);
  98. return $this->redirectToRoute('customer_login');
  99. }
  100. $session->set('email', $email);
  101. return $this->redirectToRoute('customer_register');
  102. }
  103. }
  104. $tpl = 'front/customer/identification.html.twig';
  105. if(strpos($from, 'checkout')!==false)
  106. $tpl = 'front/checkout/identification.html.twig';
  107. return $this->render($tpl, [
  108. 'loginForm' => $loginForm->createView(),
  109. 'registerForm' => $registerForm->createView()
  110. ]);
  111. }
  112. /**
  113. * @Route("/{_locale}/customer/context", name="customer_context")
  114. */
  115. public function changeContext(Request $request, CustomerManager $customerMgr, CartManager $cartManager, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  116. {
  117. $ctx = $request->get('ctx', false);
  118. $from = $request->get('from', false);
  119. $confirm = $request->get('confirm', false);
  120. $customer = $customerMgr->getCustomer();
  121. if(empty($from)) {
  122. $from = $this->generateUrl('index');
  123. }
  124. if($customer && ctype_alpha($ctx)) {
  125. $priceGroup = $em->getRepository(\App\Entity\PriceGroup::class)->findOneByCode($ctx);
  126. if($priceGroup) {
  127. try{
  128. if($confirm == 1) {
  129. $cartManager->empty();
  130. $customerMgr->setPriceContext($priceGroup);
  131. return $this->redirect($from);
  132. }else{
  133. $msg = $this->renderView('front/customer/popups/price-groups/change.html.twig', [
  134. 'customer' => $customer,
  135. 'priceGroup' => $priceGroup,
  136. 'from' => $from
  137. ]);
  138. $request->getSession()->getFlashBag()->add('popup', $msg);
  139. }
  140. } catch (Exception $ex) {
  141. $request->getSession()->getFlashBag()->add('error', $translator->trans("Vous n'avez accès à aucun type de tarif. <br>Veuillez contacter notre équipe commerciale."));
  142. }
  143. }
  144. }
  145. return $this->redirectToRoute('index');
  146. }
  147. /**
  148. * @Route("/{_locale}/customer/logout", name="customer_logout")
  149. */
  150. public function logout(CustomerManager $customerMgr)
  151. {
  152. $customerMgr->logout();
  153. return $this->redirectToRoute('index');
  154. }
  155. protected function createTvaForm(Customer $customer, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator, $from) {
  156. return $this->createForm(\App\Form\Customer\TvaType::class, $customer,[
  157. 'action' => $this->generateUrl('customer_tva_popup', ['from'=>$from]),
  158. 'method' => 'POST',
  159. 'entity_manager'=>$em,
  160. 'translator'=>$translator
  161. ]);
  162. }
  163. /**
  164. * @Route("/{_locale}/customer/popup/tva", name="customer_tva_popup")
  165. */
  166. public function tvaPopup(Request $request, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  167. {
  168. $output = [
  169. 'success' => false,
  170. 'message' => ''
  171. ];
  172. $from = $request->get('from','index');
  173. $customer = $customerMgr->getCustomer();
  174. if(!empty($customer))
  175. return $this->redirectToRoute ('customer_account');
  176. $tvaForm = $this->createTvaForm($customer, $em, $translator, $from);
  177. $customerId = $customer->getId();
  178. if($request->isMethod('POST')){
  179. $tvaForm->handleRequest($request);
  180. if($tvaForm->isSubmitted() && $tvaForm->isValid()){
  181. try {
  182. $customer = $tvaForm->getData();
  183. if($customer->getId() == $customerId){
  184. $em->persist($customer);
  185. $em->flush();
  186. $output['success'] = true;
  187. }else{
  188. $output['message'] = "Identification incorrecte.";
  189. }
  190. } catch (\Exception $ex) {
  191. $output['message'] = "Données saisies non valides.";
  192. }
  193. }else{
  194. $output['message'] = "Données saisies non valides.";
  195. }
  196. }
  197. return new JsonResponse($output);
  198. }
  199. /**
  200. * @Route("/{_locale}/customer/register", name="customer_register")
  201. */
  202. public function register(Request $request, CustomerManager $customerMgr, Session $session, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  203. {
  204. $from = $request->get('from','customer_account');
  205. $email = $session->get('email','');
  206. $registerForm = $this->createForm(\App\Form\Customer\RegisterType::class,['email'=>$email],[
  207. 'action' => $this->generateUrl('customer_register', ['from'=>$from]),
  208. 'method' => 'POST',
  209. 'entity_manager'=>$em,
  210. 'translator'=>$translator,
  211. 'locale' => $request->getLocale(),
  212. 'attr' => [
  213. 'novalidate'=>'novalidate'
  214. ]
  215. ]);
  216. if($request->isMethod('POST')){
  217. $registerForm->handleRequest($request);
  218. if($registerForm->isValid()){
  219. $data = $registerForm->getData();
  220. $result = $customerMgr->register($data, $request->getLocale());
  221. if($result!==false){
  222. $session->getFlashBag()->add('popup', $translator->trans("Votre demande de création de compte a bien été prise en compte. Votre compte sera activé sous 72h. Au-delà de ce délai, si vous rencontrez des problèmes de connexion, nous vous invitons à contacter le service client au +33 (0)2 47 73 38 38, email : infos@vivog.fr."));
  223. return $this->redirectToRoute($from);
  224. }else{
  225. $session->getFlashBag()->add('error', $result);
  226. }
  227. }
  228. }
  229. return $this->render('front/customer/register.html.twig',[
  230. 'registerForm' => $registerForm->createView()
  231. ]);
  232. }
  233. /**
  234. * @Route("/{_locale}/customer/password", name="customer_password")
  235. */
  236. public function password(Request $request, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  237. {
  238. $from = $request->get('from','customer_account');
  239. $passwordForm = $this->createForm(\App\Form\Customer\PasswordRecoveryType::class,null,[
  240. 'action' => $this->generateUrl('customer_password', ['from'=>$from]),
  241. 'method' => 'POST',
  242. 'translator' => $translator,
  243. 'locale' => $request->getLocale()
  244. ]);
  245. if($request->isMethod('POST')){
  246. $passwordForm->handleRequest($request);
  247. if($passwordForm->isValid()){
  248. $data = $passwordForm->getData();
  249. $result = $customerMgr->renewPassword($data['email'], $request->getLocale());
  250. if($result !== true){
  251. $session->getFlashBag()->add('error', $result);
  252. }
  253. $session->getFlashBag()->add('notice', $translator->trans('Un nouveau mot de passe a été envoyé à votre adresse email.'));
  254. return $this->redirectToRoute('customer_login');
  255. }
  256. }
  257. return $this->render('front/customer/password.html.twig', [
  258. 'passwordForm' => $passwordForm->createView()
  259. ]);
  260. }
  261. /**
  262. * @Route("/{_locale}/customer/password/change", name="customer_password_change")
  263. */
  264. public function changePassword(Request $request, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  265. {
  266. $from = $request->get('from','customer_account');
  267. $customer = $customerMgr->getCustomer();
  268. if(empty($customer))
  269. return $this->redirectToRoute ('customer_login',['from'=>'customer_account']);
  270. $passwordForm = $this->createForm(\App\Form\Customer\PasswordChangeType::class,null,[
  271. 'action' => $this->generateUrl('customer_password_change', ['from'=>$from]),
  272. 'method' => 'POST',
  273. ]);
  274. if($request->isMethod('POST')){
  275. $passwordForm->handleRequest($request);
  276. if($passwordForm->isValid()){
  277. $data = $passwordForm->getData();
  278. $result = $customerMgr->changePassword($customer, $data['password'], $data['plainPassword']);
  279. if($result===true){
  280. $session->getFlashBag()->add('notice', $translator->trans('Votre mot de passe a été mis à jour...'));
  281. }else{
  282. $session->getFlashBag()->add('error', $result);
  283. }
  284. return $this->redirectToRoute('customer_password_change');
  285. }
  286. }
  287. return $this->render('front/customer/password-change.html.twig', [
  288. 'customer' => $customer,
  289. 'passwordForm' => $passwordForm->createView()
  290. ]);
  291. }
  292. /**
  293. * @Route("/{_locale}/customer/account", name="customer_account")
  294. */
  295. public function account(CustomerManager $customerMgr)
  296. {
  297. $customer = $customerMgr->getCustomer();
  298. if(!$customer){
  299. return $this->redirectToRoute('customer_login',[
  300. 'from'=>'customer_account'
  301. ]);
  302. }
  303. return $this->render('front/customer/account.html.twig', [
  304. 'customer' => $customer
  305. ]);
  306. }
  307. /**
  308. * @Route("/{_locale}/aj/customer/address", name="customer_address" )
  309. */
  310. public function addressUpdate(Request $request, CustomerManager $customerMgr, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  311. {
  312. $customer = $customerMgr->getCustomer();
  313. if(!$customer){
  314. return $this->redirectToRoute('customer_login',[
  315. 'from'=>'customer_account'
  316. ]);
  317. }
  318. $address = null;
  319. $id = $request->get('id',0);
  320. $from = $request->get('from',false);
  321. if($id)
  322. $address = $em->getRepository('App:Address')->find($id);
  323. if(empty($address)){
  324. $address = new \App\Entity\Address();
  325. $france = $em->getRepository(\App\Entity\Country::class)->find(73);
  326. $address->setCountry($france);
  327. }elseif($address->getCustomer()!=$customer){
  328. throw new NotFoundHttpException();
  329. }
  330. $actionData = ['id'=>$id];
  331. if(!empty($from)){
  332. $actionData['from'] = $from;
  333. }
  334. $formType = \App\Form\Customer\AddressType::class;
  335. if($customerMgr->isExpert()) {
  336. $formType = \App\Form\Customer\AddressExpertType::class;
  337. }
  338. $form = $this->createForm($formType, $address,[
  339. 'action' => $this->generateUrl('customer_address',$actionData),
  340. 'method' => 'POST',
  341. 'entity_manager' => $em,
  342. 'translator' => $translator,
  343. 'attr' => [
  344. 'id'=>'form-address',
  345. 'novalidate'=>'novalidate'
  346. ]
  347. ]);
  348. if($request->isMethod('POST')){
  349. $form->handleRequest($request);
  350. if($form->isValid()){
  351. try{
  352. $address = $form->getData();
  353. $address->setCustomer($customer);
  354. $em->persist($address);
  355. $default = $form->get('default')->getData();
  356. if(!empty($default)){
  357. $customer->setDefaultAddress($address);
  358. $em->persist($customer);
  359. }
  360. $em->flush();
  361. if($from == 'checkout_shipping') {
  362. $cartMgr->setAddress($address, 'shipping');
  363. }
  364. return new JsonResponse([
  365. 'success'=>true,
  366. 'address'=>$address->toArray(),
  367. 'redirect'=>empty($from)?false:$this->redirectToRoute ($from)
  368. ]);
  369. }
  370. catch (\Exception $ex) {
  371. return new JsonResponse([
  372. 'success'=>false,
  373. 'error'=>$ex->getMessage()
  374. ]);
  375. }
  376. }
  377. }
  378. $content = $this->renderView('front/customer/forms/address.html.twig',[
  379. 'addressForm' => $form->createView()
  380. ]);
  381. $response = new JsonResponse();
  382. $response->setContent(json_encode([
  383. 'content' => $content
  384. ]));
  385. return $response;
  386. }
  387. /**
  388. * @Route("/{_locale}/aj/customer/address/selection", name="customer_address_selection" )
  389. */
  390. public function addressSelection(Request $request, CustomerManager $customerMgr, EntityManagerInterface $em)
  391. {
  392. $type = $request->get('type','');
  393. $customer = $customerMgr->getCustomer();
  394. if(!$customer){
  395. throw new NotFoundHttpException();
  396. }
  397. $addresses = $em->getRepository('App:Address')->findByCustomer($customer);
  398. $content = $this->renderView('front/customer/blocks/address-selection.html.twig',[
  399. 'addresses' => $addresses,
  400. 'type' => $type
  401. ]);
  402. $response = new JsonResponse();
  403. $response->setContent(json_encode([
  404. 'content' => $content
  405. ]));
  406. return $response;
  407. }
  408. /**
  409. * @Route("/{_locale}/aj/customer/address/delete", name="customer_address_delete" )
  410. */
  411. public function deleteAddress(Request $request, CustomerManager $customerMgr, EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  412. {
  413. $id = $request->get('id','');
  414. $customer = $customerMgr->getCustomer();
  415. if(!$customer){
  416. throw new NotFoundHttpException();
  417. }
  418. $address = $em->getRepository('App:Address')->findByCustomer($customer);
  419. try{
  420. $address = $em->getRepository('App:Address')->find($id);
  421. if($address){
  422. if($address->getId() == $customer->getDefaultAddressId())
  423. throw new \Exception($translator->trans('Impossible de supprimer votre adresse principale.'));
  424. $em->remove($address);
  425. $em->flush();
  426. }else{
  427. throw new \Exception($translator->trans('Adresse inconnue.'));
  428. }
  429. return new JsonResponse([
  430. 'success'=>true,
  431. 'address'=>$address->toArray()
  432. ]);
  433. }
  434. catch (\Exception $ex) {
  435. return new JsonResponse([
  436. 'success'=>false,
  437. 'error'=>$ex->getMessage()
  438. ]);
  439. }
  440. $response = new JsonResponse();
  441. $response->setContent(json_encode([
  442. 'success'=>false,
  443. 'error'=>''
  444. ]));
  445. return $response;
  446. }
  447. /**
  448. * @Route("/{_locale}/customer/information", name="customer_information")
  449. */
  450. public function information(Request $request, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, CustomerManager $customerMgr, EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  451. {
  452. $customer = $customerMgr->getCustomer();
  453. if(!$customer){
  454. return $this->redirectToRoute('customer_login',[
  455. 'from'=>'customer_history'
  456. ]);
  457. }
  458. $form = $this->createForm(\App\Form\Customer\InformationType::class, $customer, [
  459. 'method' => 'POST',
  460. 'attr' => [
  461. 'novalidate' => 'novalidate',
  462. 'class' => 'customer-info'
  463. ],
  464. 'translator' => $translator
  465. ]);
  466. if($request->isMethod('POST')){
  467. $form->handleRequest($request);
  468. if($form->isValid()){
  469. $customer = $form->getData();
  470. try{
  471. $customer->setAccountUpdate(new \DateTime);
  472. $em->persist($customer);
  473. $em->flush();
  474. $session->getFlashBag()->add('notice',$translator->trans('Vos informations ont été enregistrées.'));
  475. return $this->redirectToRoute('customer_information');
  476. }
  477. catch (\Exception $ex) {
  478. $session->getFlashBag()->add('error',$translator->trans('Une erreur est survenue.'));
  479. }
  480. } else {
  481. $session->getFlashBag()->add('error',$translator->trans('Erreur présente dans le formulaire.'));
  482. }
  483. }
  484. return $this->render('front/customer/informations.html.twig', [
  485. 'customer' => $customer,
  486. 'form' => $form->createView()
  487. ]);
  488. }
  489. /**
  490. * @Route("/{_locale}/customer/history", name="customer_history")
  491. */
  492. public function history(CustomerManager $customerMgr, EntityManagerInterface $em)
  493. {
  494. $customer = $customerMgr->getCustomer();
  495. if(!$customer){
  496. return $this->redirectToRoute('customer_login',[
  497. 'from'=>'customer_history'
  498. ]);
  499. }
  500. $orders = $em->getRepository('App:Order')->history($customer);
  501. return $this->render('front/customer/history.html.twig', [
  502. 'customer' => $customer,
  503. 'orders' => $orders
  504. ]);
  505. }
  506. /**
  507. * @Route("/{_locale}/customer/addresses", name="customer_addresses")
  508. */
  509. public function adresses(CustomerManager $customerMgr, EntityManagerInterface $em)
  510. {
  511. $customer = $customerMgr->getCustomer();
  512. if(!$customer){
  513. return $this->redirectToRoute('customer_login',[
  514. 'from'=>'customer_addresses'
  515. ]);
  516. }
  517. $addresses = $em->getRepository('App:Address')->findByCustomer($customer);
  518. return $this->render('front/customer/addresses.html.twig', [
  519. 'customer' => $customer,
  520. 'addresses' => $addresses
  521. ]);
  522. }
  523. /**
  524. * @Route("/{_locale}/json/customer/invoiceType/{cid}/{token}/{type}", name="json_customer_invoice_type", requirements={"cid"="\d+","type":"0|1","token":".+"})
  525. */
  526. public function invoiceType(Request $request, $cid, $token, $type)
  527. {
  528. $em = $this->getDoctrine()->getManager();
  529. $output = ['success' => false, 'message'=>''];
  530. if($request->isMethod('POST')){
  531. $customer = null;
  532. if(md5('LKLSDF456ERF'.$cid) == $token){
  533. $customer = $em->getRepository('App:Customer')->find($cid);
  534. }
  535. if(empty($customer))
  536. throw new NotFoundHttpException ();
  537. try{
  538. $customer->setInvoiceType($type);
  539. $em->persist($customer);
  540. $em->flush();
  541. $output['success'] = true;
  542. }
  543. catch (\Exception $ex) {
  544. $output['message'] = $ex->getMessage();
  545. }
  546. }
  547. $response = new JsonResponse($output);
  548. if(!empty($_SERVER['HTTP_ORIGIN'])){
  549. $http_origin = $_SERVER['HTTP_ORIGIN'];
  550. if (in_array($http_origin,["https://test.dogcat.com","https://www.dogcat.com","https://v2.dogcat.com","https://v2-test.dogcat.com"]))
  551. {
  552. $response->headers->set("Access-Control-Allow-Origin",$http_origin);
  553. }
  554. }
  555. return $response;
  556. }
  557. }