src/Controller/Front/CustomerController.php line 20

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