src/Controller/AccountController.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Programa;
  4. use App\Entity\Solicitud;
  5. use App\Entity\SolicitudLog;
  6. use App\Entity\Subscription;
  7. use App\Entity\User;
  8. use App\Form\PassresetType;
  9. use App\Form\SolicitudLogType;
  10. use App\Form\UserProfileType;
  11. use App\Libs\Constantes;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Stripe\BillingPortal\Session;
  14. use Stripe\Stripe;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class AccountController extends AbstractController
  22. {
  23. private $em;
  24. private $hasher;
  25. private $promo_code = null;
  26. /**
  27. * @var RequestStack
  28. */
  29. private $stack;
  30. public function __construct(EntityManagerInterface $em, UserPasswordHasherInterface $hasher, RequestStack $stack)
  31. {
  32. $this->hasher = $hasher;
  33. $this->em = $em;
  34. $this->stack = $stack;
  35. }
  36. /**
  37. * @Route("/account", name="app_account")
  38. */
  39. public function index(): Response
  40. {
  41. $u = $this->getUser();
  42. if($this->stack->getSession()->get('passReset')) return $this->redirectToRoute('app_password');
  43. if(Constantes::ENV == 'dev' && !in_array('ROLE_SUPER_ADMIN', $this->getUser()->getRoles())){
  44. return $this->render('security/maint.html.twig', [
  45. 'last_username' => '', 'error' => '',
  46. 'promo_code' => null,
  47. ]);
  48. }
  49. // dd($u);
  50. $rs = []; $rsols = [];
  51. $sols = $this->em->getRepository(Solicitud::class)->findBy(['abierta' => 1, 'user' => $u]);
  52. if($sols){
  53. foreach ($sols as $sol){
  54. $rsols[] = $sol;
  55. }
  56. }
  57. $subs = $this->em->getRepository(Subscription::class)->findUserVisible($u);
  58. if($subs){
  59. // dd($subs);
  60. foreach ($subs as $sub){
  61. // $sub = new Subscription();
  62. if($sub->getProducto()->getPrograma()->getIsPublico()){
  63. $expiresAt = $sub->getExpiresAt()->format('d M Y');
  64. $rs[$sub->getSubscriptionStatus()->getContent()][] = [
  65. 'user' => $sub->getUser()->getNombre(),
  66. 'expira' => $expiresAt,
  67. 'programa' => $sub->getProducto()->getPrograma(),
  68. 'img' => $sub->getProducto()->getPrograma()->getImageName(),
  69. ];
  70. }
  71. }
  72. }
  73. return $this->render('account/index.html.twig', [
  74. 'controller_name' => 'AccountController',
  75. 'rs' => $rs,
  76. 'rsols' => $rsols,
  77. 'isnew' => $u->getResetPin(),
  78. 'promo_code' => $this->promo_code,
  79. 'user' => $u
  80. ]);
  81. }
  82. /**
  83. * @Route("/profile", name="app_profile")
  84. */
  85. public function profile(Request $request): Response
  86. {
  87. $u = $this->getUser();
  88. $rs = [];
  89. // dump($u);exit();
  90. // $user = $this->em->getRepository(User::class)->findOneBy(['tel1' => $u->getUserIdentifier()]);
  91. $f = $this->createForm(UserProfileType::class, $u);
  92. $f->handleRequest($request);
  93. if($f->isSubmitted() && $f->isValid()){
  94. $d = $f->getData();
  95. $this->em->persist($d);
  96. $this->em->flush();
  97. }
  98. //dump($f->createView());exit();
  99. return $this->render('account/profile.html.twig', [
  100. 'controller_name' => 'AccountController',
  101. 'rs' => $rs,
  102. 'form' => $f->createView(),
  103. 'promo_code' => $this->promo_code
  104. ]);
  105. }
  106. /**
  107. * @Route("/solicitud/{id}", name="app_solicitud")
  108. */
  109. public function solicitud($id, Request $request): Response
  110. {
  111. $u = $this->getUser();
  112. $sol = $this->em->getRepository(Solicitud::class)->find($id);
  113. if($sol){
  114. $f = $this->createForm(SolicitudLogType::class);
  115. $f->handleRequest($request);
  116. if($f->isSubmitted() && $f->isValid()){
  117. $d = $f->getData();
  118. $l = new SolicitudLog();
  119. $l->setSolicitud($sol)->setCreatedAt(new \DateTimeImmutable())->setUser($u)->setContent($d->getContent())
  120. ->setAutorizarRespuesta(false);
  121. // dd($l);
  122. $this->em->persist($l);
  123. $this->em->flush();
  124. $this->addFlash('success', 'Mensaje enviado.');
  125. }
  126. }else{
  127. $this->addFlash('danger', 'Solicitud no Existe');
  128. return $this->redirectToRoute('app_account');
  129. }
  130. //dump($f->createView());exit();
  131. return $this->render('account/solicitud.html.twig', [
  132. 'controller_name' => 'AccountController',
  133. 'sol' => $sol,
  134. 'form' => $f->createView(),
  135. 'promo_code' => $this->promo_code
  136. ]);
  137. }
  138. /**
  139. * @Route("/password", name="app_password")
  140. */
  141. public function password(Request $request): Response
  142. {
  143. $u = $this->getUser();
  144. // dump($u);exit();
  145. $user = $this->em->getRepository(User::class)->findOneBy(['tel1'=>$u->getUserIdentifier()]);
  146. $rs = [];
  147. $flash = '';
  148. $f = $this->createForm(PassresetType::class, $u);
  149. $f->handleRequest($request);
  150. if ($f->isSubmitted() && $f->isValid()) {
  151. $this->stack->getSession()->remove('passReset');
  152. $p = $f->getData();
  153. $hash = $this->hasher->hashPassword($user, $p->getPlainPassword());
  154. $p->setPassword($hash)
  155. ->setPlainPassword('')->setResetPin(0);
  156. try {
  157. $this->em->persist($p);
  158. $this->em->flush();
  159. $this->addFlash('success', 'Contraseña/Pin cambiado con éxito.');
  160. return $this->redirectToRoute('app_account');
  161. } catch (\Exception $e) {
  162. $this->addFlash('danger', $e->getMessage());
  163. }
  164. }
  165. return $this->render('account/password.html.twig', [
  166. 'controller_name' => 'AccountController',
  167. 'rs' => $rs,
  168. 'flash' => $flash,
  169. 'form' => $f->createView(),
  170. 'promo_code' => $this->promo_code
  171. ]);
  172. }
  173. /**
  174. * @Route("/historial", name="app_historial")
  175. */
  176. public function historial(Request $request): Response
  177. {
  178. $u = $this->em->getRepository(User::class)->find($this->getUser());
  179. if($u->getStripeId()){
  180. Stripe::setApiKey(Constantes::API_KEY);
  181. try{
  182. $session = Session::create([
  183. 'customer' => $u->getStripeId(),
  184. 'return_url' => 'https://computechapps.com/account'
  185. ]);
  186. return $this->redirect($session->url);
  187. }catch (\Exception $e){
  188. // dump($e->getMessage());exit();
  189. return $this->redirectToRoute('app_account');
  190. }
  191. }
  192. return $this->redirectToRoute('app_account');
  193. }
  194. }