src/Controller/RdvController.php line 520

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AppelSujet;
  4. use App\Entity\Apprenant;
  5. use App\Entity\Rdv;
  6. use App\Entity\Utilisateur;
  7. use App\Form\RdvType;
  8. use App\Service\GoogleCalendre;
  9. use Google_Client;
  10. use Google_Service_Calendar;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16. * Reporting controller.
  17. *
  18. * @Route("rdv")
  19. *
  20. */
  21. class RdvController extends AbstractController
  22. {
  23. /**
  24. * @Route("/OP", name="rdv_index")
  25. */
  26. public function rdv_index()
  27. {
  28. $user = $this->getUser();
  29. $em = $this->getDoctrine()->getManager();
  30. $calendre = new GoogleCalendre($this->getDoctrine());
  31. $calendre->itemsEvents();
  32. $client = new Google_Client();
  33. $client->setAuthConfig('ideodashcalendar.json');
  34. $client->setApplicationName('IdeoCalendar');
  35. $client->addScope(Google_Service_Calendar::CALENDAR_EVENTS);
  36. $service = new Google_Service_Calendar($client);
  37. $calendarId = $user->getCalendarId();
  38. if (!empty($calendarId)) {
  39. $optParams = array(
  40. 'maxResults' => 100,
  41. 'orderBy' => 'startTime',
  42. 'singleEvents' => true,
  43. 'timeMin' => date('c', strtotime(" -100 days")),
  44. );
  45. $results = $service->events->listEvents($calendarId, $optParams);
  46. $items = $results->getItems();
  47. // Mettre à jour la liste des Rdv Calendly
  48. $this->updateCalendyEvents($em, $user, $items);
  49. }
  50. // $rdvs = $em->getRepository(Rdv::class)->findBy(array('agentRecepteur' => $user));
  51. $rdvs = $em->getRepository(Rdv::class)->findBy([], ["id" => "DESC"], 301);
  52. $agents = $em->getRepository(Utilisateur::class)->findBy(['id' => [100, 97, 84, 52]]);
  53. return $this->render('rdv/index.html.twig', [
  54. 'rdvs' => $rdvs,
  55. 'agents' => $agents,
  56. ]);
  57. }
  58. function extractZoomLink($text)
  59. {
  60. // Pattern to match Zoom links with meeting ID and password
  61. $pattern = '/https:\/\/us\d+web\.zoom\.us\/j\/\d+\?pwd=[a-zA-Z0-9]+\.[0-9]/';
  62. // Perform the match
  63. if (preg_match($pattern, $text, $matches)) {
  64. return $matches[0];
  65. }
  66. return null;
  67. }
  68. /**
  69. * @Route("/mine/OP", name="rdv_mine")
  70. */
  71. public function rdv_mine()
  72. {
  73. $user = $this->getUser();
  74. $em = $this->getDoctrine()->getManager();
  75. $calendre = new GoogleCalendre($this->getDoctrine());
  76. $calendre->itemsEvents();
  77. $client = new Google_Client();
  78. $client->setAuthConfig('ideodashcalendar.json');
  79. $client->setApplicationName('IdeoCalendar');
  80. $client->addScope(Google_Service_Calendar::CALENDAR_EVENTS);
  81. $service = new Google_Service_Calendar($client);
  82. $calendarId = $user->getCalendarId();
  83. if (!empty($calendarId)) {
  84. $optParams = array(
  85. 'maxResults' => 300,
  86. 'orderBy' => 'startTime',
  87. 'singleEvents' => true,
  88. 'timeMin' => date('c', strtotime(" -30 days")),
  89. 'timeMax' => date('c', strtotime(" +30 days")),
  90. );
  91. $results = $service->events->listEvents($calendarId, $optParams);
  92. $items = $results->getItems();
  93. // Mettre à jour la liste des Rdv Calendly
  94. $this->updateCalendyEvents($em, $user, $items);
  95. }
  96. $rdvs = $this->getDoctrine()->getRepository(Rdv::class)->findBy(array('agentRecepteur' => $user));
  97. if ($user->getRoles()[0] == 'ROLE_OP') {
  98. return $this->render('rdv/coach/todayRdv.html.twig', [
  99. 'rdvs' => $rdvs,
  100. ]);
  101. }
  102. $agents = $em->getRepository(Utilisateur::class)->findBy(['id' => [100, 97, 84, 52]]);
  103. return $this->render('rdv/index.html.twig', [
  104. 'rdvs' => $rdvs,
  105. 'agents' => $agents,
  106. ]);
  107. }
  108. /**
  109. * @Route("/byUser/{user}/OP", name="rdv_user_id")
  110. */
  111. public function rdv_user_id(Utilisateur $user)
  112. {
  113. $em = $this->getDoctrine()->getManager();
  114. $calendre = new GoogleCalendre($this->getDoctrine());
  115. $calendre->itemsEvents();
  116. $client = new Google_Client();
  117. $client->setAuthConfig('ideodashcalendar.json');
  118. $client->setApplicationName('IdeoCalendar');
  119. $client->addScope(Google_Service_Calendar::CALENDAR_EVENTS);
  120. $service = new Google_Service_Calendar($client);
  121. $calendarId = $user->getCalendarId();
  122. if (!empty($calendarId)) {
  123. $optParams = array(
  124. 'maxResults' => 300,
  125. 'orderBy' => 'startTime',
  126. 'singleEvents' => true,
  127. 'timeMin' => date('c', strtotime(" -30 days")),
  128. 'timeMax' => date('c', strtotime(" +30 days")),
  129. );
  130. $results = $service->events->listEvents($calendarId, $optParams);
  131. $items = $results->getItems();
  132. // Mettre à jour la liste des Rdv Calendly
  133. $this->updateCalendyEvents($em, $user, $items);
  134. }
  135. $rdvs = $this->getDoctrine()->getRepository(Rdv::class)->findBy(array('agentRecepteur' => $user));
  136. if ($user->getRoles()[0] == 'ROLE_OP') {
  137. return $this->render('rdv/coach/todayRdv.html.twig', [
  138. 'rdvs' => $rdvs,
  139. ]);
  140. }
  141. $agents = $em->getRepository(Utilisateur::class)->findBy(['id' => [100, 97, 84, 52]]);
  142. return $this->render('rdv/index.html.twig', [
  143. 'rdvs' => $rdvs,
  144. 'agents' => $agents,
  145. 'currentAgentId' => $user->getId(),
  146. ]);
  147. }
  148. /**
  149. * @Route("/annule/OP", name="rdv_annule")
  150. */
  151. public function rdv_annule()
  152. {
  153. $rdvs = $this->getDoctrine()->getRepository(Rdv::class)->findBy(["statut" => 2], ['id' => "DESC"], 100);
  154. return $this->render('rdv/rdv_annule.html.twig', ['rdvs' => $rdvs]);
  155. }
  156. /**
  157. * @Route("/injoignable/OP", name="rdv_injoignable")
  158. */
  159. public function rdv_injoignable()
  160. {
  161. $rdvs = $this->getDoctrine()->getRepository(Rdv::class)->findBy(["statut" => 3], ['id' => "DESC"], 100);
  162. return $this->render('rdv/rdv_injoignable.html.twig', ['rdvs' => $rdvs]);
  163. }
  164. /**
  165. * @Route("/{id}/edit/OP", name="rdv_edit")
  166. */
  167. public function editAction(Request $request, Rdv $rdv)
  168. {
  169. $em = $this->getDoctrine()->getManager();
  170. $utilisateurs = $em->getRepository(Utilisateur::class)->findCPSPOP();
  171. $editForm = $this->createForm(RdvType::class, $rdv);
  172. $editForm->handleRequest($request);
  173. if ($editForm->isSubmitted() && $editForm->isValid()) {
  174. $this->getDoctrine()->getManager()->flush();
  175. $this->addFlash('success', 'Rdv a été modifié');
  176. return $this->redirectToRoute('rdv_index');
  177. }
  178. return $this->render('rdv/edit.html.twig', array(
  179. 'utilisateurs' => $utilisateurs,
  180. 'rdv' => $rdv,
  181. 'form' => $editForm->createView(),
  182. ));
  183. }
  184. /**
  185. * @Route("/{id}/validate/OP", name="rdv_validate")
  186. */
  187. public function validateAction(rdv $rdv)
  188. {
  189. $em = $this->getDoctrine()->getManager();
  190. $rdv->setStatut(1);
  191. $em->persist($rdv);
  192. $em->flush();
  193. return $this->redirectToRoute('rdv_index');
  194. }
  195. /**
  196. * Lists all rdv entities today.
  197. * @Route("/mine-today/OP", name="rdv_mine_today")
  198. */
  199. public function mineTodayAction()
  200. {
  201. $user = $this->getUser();
  202. $em = $this->getDoctrine()->getManager();
  203. $client = new Google_Client();
  204. $client->setAuthConfig('ideodashcalendar.json');
  205. $client->setApplicationName('IdeoCalendar');
  206. $client->addScope(Google_Service_Calendar::CALENDAR_EVENTS);
  207. $service = new Google_Service_Calendar($client);
  208. $calendarId = $user->getCalendarId();
  209. if (!empty($calendarId)) {
  210. $optParams = array(
  211. 'maxResults' => 100,
  212. 'orderBy' => 'startTime',
  213. 'singleEvents' => true,
  214. 'timeMin' => date('c', strtotime(" -10 days")),
  215. );
  216. $results = $service->events->listEvents($calendarId, $optParams);
  217. $items = $results->getItems();
  218. // Mettre à jour la liste des Rdv Calendly
  219. $this->updateCalendyEvents($em, $user, $items);
  220. }
  221. $today = date('Y-m-d', strtotime('today'));
  222. $todayStart = $today . "T00:00";
  223. $todayEnd = $today . "T23:59";
  224. $rdvs = $em->getRepository(Rdv::class)->findAllMyRdvByDate($user, $todayStart, $todayEnd);
  225. return $this->render('rdv/coach/todayRdv.html.twig', [
  226. 'rdvs' => $rdvs,
  227. ]);
  228. }
  229. // recupperer l'ID de l'evenement de Callendly
  230. public function getCalendlyRdvId($text)
  231. {
  232. if (str_contains($text, "EventId")) {
  233. $ArrayExplode = explode(" ", $text);
  234. foreach ($ArrayExplode as $result) {
  235. $ArrayExplode2 = explode("\n", $result);
  236. foreach ($ArrayExplode2 as $result2) {
  237. if (str_contains($result2, "EventId")) {
  238. $calendlyRdvId = explode('EventId:', str_replace(" ", "", $result2));
  239. $calendlyRdvId = $calendlyRdvId[1];
  240. return $calendlyRdvId;
  241. }
  242. }
  243. }
  244. } elseif (str_contains($text, "reschedulings")) {
  245. $ArrayExplode = explode(" ", $text);
  246. foreach ($ArrayExplode as $result) {
  247. $ArrayExplode2 = explode("\n", $result);
  248. foreach ($ArrayExplode2 as $result2) {
  249. if (str_contains($result2, "reschedulings")) {
  250. $calendlyRdvId = explode("/reschedulings/", str_replace(" ", "", $result2));
  251. $calendlyRdvId = $calendlyRdvId[1];
  252. return $calendlyRdvId;
  253. }
  254. }
  255. }
  256. } elseif (str_contains($text, "uuid=")) {
  257. $ArrayExplode = explode(" ", $text);
  258. foreach ($ArrayExplode as $result) {
  259. $ArrayExplode2 = explode("\n", $result);
  260. foreach ($ArrayExplode2 as $result2) {
  261. if (str_contains($result2, "uuid=")) {
  262. $calendlyRdvId = explode("uuid=", str_replace(" ", "", $result2));
  263. $calendlyRdvId = $calendlyRdvId[1];
  264. return $calendlyRdvId;
  265. }
  266. }
  267. }
  268. } else {
  269. return "Ko!";
  270. }
  271. }
  272. // Metre à jour les evenements Calendly par Agent/Coach
  273. public function updateCalendyEvents($em, $user, $items)
  274. {
  275. $today = date("Y-m-d") . 'T';
  276. $rdvsToDelete = $em->createQuery(
  277. 'SELECT r FROM App\Entity\Rdv r WHERE r.appel is null AND r.agentEmetteur = 12 AND r.agentRecepteur IN (100,97,84,52) AND r.date LIKE :today'
  278. )->setParameter('today', $today . '%')
  279. ->getResult();
  280. foreach ($rdvsToDelete as $rdvToDelete) {
  281. $em->remove($rdvToDelete);
  282. }
  283. if (!empty($rdvsToDelete)) {
  284. $em->flush();
  285. }
  286. foreach ($items as $item) {
  287. $calendlyRdvId = "";
  288. $descrption = $item->description;
  289. $summary = $item->summary;
  290. if (str_contains($summary, 'Canceled:')) {
  291. $start = ($item->start->dateTime) ? explode(':', $item->start->dateTime)[0] . ':' . explode(':', $item->start->dateTime)[1] : "";
  292. $rdvBd = $em->getRepository(Rdv::class)->findBy(array('date' => $start, 'agentRecepteur' => $user));
  293. if (!empty($rdvBd)) {
  294. $em->remove($rdvBd[0]);
  295. $em->flush();
  296. }
  297. continue;
  298. }
  299. if ($this->getCalendlyRdvId($descrption) != "Ko!") {
  300. $calendlyRdvId = $this->getCalendlyRdvId($descrption);
  301. }
  302. $etat = explode(' :', $item->getSummary());
  303. $start = ($item->start->dateTime) ? explode(':', $item->start->dateTime)[0] . ':' . explode(':', $item->start->dateTime)[1] : "";
  304. $rdvBd = $em->getRepository(Rdv::class)->findBy(array('date' => $start, 'agentRecepteur' => $user));
  305. if (!$rdvBd) {
  306. if ($etat[0] != 'Annulé') {
  307. if (isset($item->attendees[0]) and !empty($item->attendees[0]['email']) and $item->attendees[0]['email'] != "elearning@ideolearning.com") {
  308. // dump($item);
  309. $email = $item->attendees[0]['email'];
  310. $apprenant = $em->getRepository(Apprenant::class)->findOneByUsername($email);
  311. if (empty($apprenant) and isset($item->attendees[0]) and !empty($item->attendees[0]['email']) and $item->attendees[0]['email'] != "elearning@ideolearning.com") {
  312. $email = $item->attendees[0]['email'];
  313. $apprenant = $em->getRepository(Apprenant::class)->findOneByEmail($email);
  314. }
  315. if (empty($apprenant) and isset($item->attendees[1]) and !empty($item->attendees[1]['email']) and $item->attendees[1]['email'] != "elearning@ideolearning.com") {
  316. $email = $item->attendees[1]['email'];
  317. $apprenant = $em->getRepository(Apprenant::class)->findOneByEmail($email);
  318. }
  319. }
  320. if (empty($apprenant)) {
  321. if (array_key_exists(1, explode('(Ex: a.alaoui@YourCompany.com): ', $descrption))) {
  322. $username2 = explode('(Ex: a.alaoui@YourCompany.com): ', $descrption)[1];
  323. $username2 = explode(chr(10), $username2)[0];
  324. $apprenant = $em->getRepository(Apprenant::class)->findOneByUsername($username2);
  325. if (empty($apprenant))
  326. $apprenant = $em->getRepository(Apprenant::class)->findOneByEmail($username2);
  327. }
  328. }
  329. if (!empty($apprenant)) {
  330. $rdvBd = new rdv();
  331. $prof = $em->getRepository(Utilisateur::class)->findById($user);
  332. $rdvBd->setAgentRecepteur($prof[0]);
  333. $maj = $em->getRepository(Utilisateur::class)->findById(12);
  334. $rdvBd->setAgentEmetteur($maj[0]);
  335. $appelSujet = $em->getRepository(AppelSujet::class)->findOneById(3);
  336. $rdvBd->setSujet($appelSujet);
  337. $rdvBd->setStatut(0);
  338. $rdvBd->setCalendlyRdvId($calendlyRdvId);
  339. $rdvBd->setDate($start);
  340. $rdvBd->setApprenant($apprenant);
  341. $rdvBd->setCommentaire($item->getSummary());
  342. $zoomLink = $this->extractZoomLink(isset($item->description) ? $item->description : "");
  343. if ($zoomLink) {
  344. $rdvBd->setZoomLink($zoomLink);
  345. }
  346. $rdvBd->setDateInsert(date("Y-m-d\TH:i:s", strtotime("+1 hour")));
  347. $rdvBd->setEtat(2);
  348. $em->persist($rdvBd);
  349. $em->flush();
  350. }
  351. }
  352. } elseif ($etat[0] == 'Annulé') {
  353. $rdvBd[0]->setStatut(2);
  354. $rdvBd[0]->setCalendlyRdvId($calendlyRdvId);
  355. $em->persist($rdvBd[0]);
  356. $em->flush();
  357. } else {
  358. $rdvBd[0]->setCalendlyRdvId($calendlyRdvId);
  359. $em->persist($rdvBd[0]);
  360. $em->flush();
  361. }
  362. }
  363. }
  364. /**
  365. * @Route("/rdv-detail/{id}", name="rdv_detail")
  366. * @return void
  367. **/
  368. public function rdv_detail(Rdv $rdv)
  369. {
  370. $response["topic"] = $rdv->getSujet()->getId();
  371. $response["date"] = $rdv->getDate();
  372. $response["status"] = $rdv->getStatut();
  373. $response["agent"] = $rdv->getAgentRecepteur()->getId();
  374. $resp = new Response(json_encode($response, JSON_UNESCAPED_UNICODE));
  375. $resp->headers->set('Content-Type', 'application/json');
  376. return $resp;
  377. }
  378. /**
  379. * @Route("/rdv-edit/{id}/OP", name="edit-rdv-ajax")
  380. */
  381. public function edit_rdvAction(Rdv $rdv)
  382. {
  383. try {
  384. $em = $this->getDoctrine()->getManager();
  385. $appelSujet = $em->getRepository(AppelSujet::class)->findOneById($_POST["rdv-topic"]);
  386. $user = $em->getRepository(Utilisateur::class)->findOneById($_POST["rdv-agent"]);
  387. $rdv->setSujet($appelSujet);
  388. $rdv->setDate($_POST["rdv-date"]);
  389. $rdv->setStatut($_POST["rdv-status"]);
  390. $rdv->setAgentRecepteur($user);
  391. $em->persist($rdv);
  392. $em->flush();
  393. $response["date"] = date_format(date_create($_POST["rdv-date"]), 'd/m/Y H:i:s');
  394. } catch (\Throwable $th) {
  395. $response["error"] = "an error has occurred";
  396. }
  397. $response["success"] = "Appointment has been edited successfully";
  398. $resp = new Response(json_encode($response, JSON_UNESCAPED_UNICODE));
  399. $resp->headers->set('Content-Type', 'application/json');
  400. return $resp;
  401. }
  402. /**
  403. * @Route("/rdvsrecepteur/OP", name="rdvs-recepteur-ajax")
  404. */
  405. public function rdvsrecepteur()
  406. {
  407. $em = $this->getDoctrine()->getManager();
  408. $user = $this->getUser()->getId();
  409. $rdvs = $em->getRepository(Rdv::class)->findRdvByAgentRecepteur($user);
  410. $rendvs = array();
  411. foreach ($rdvs as $k => $rdv) {
  412. $rendvs[$k]['id'] = $rdv->getId();
  413. $rendvs[$k]['apprenant'] = $rdv->getapprenant()->getId();
  414. $rendvs[$k]['date'] = $rdv->getDate();
  415. $rendvs[$k]['username'] = $rdv->getapprenant()->getUsername();
  416. }
  417. $resp = new Response(json_encode($rendvs, JSON_UNESCAPED_UNICODE));
  418. $resp->headers->set('Content-Type', 'application/json');
  419. return $resp;
  420. }
  421. }