src/Controller/ChatController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ZAPP\HistorialMensajesWhatsapp;
  4. use App\Service\Utils;
  5. use App\Service\UtilsGestion;
  6. use App\Service\UtilsTools;
  7. use App\Service\UtilsZAPP;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use stdClass;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ChatController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/chat/{afiliadoId}/{whabaId}", name="chat")
  18.      */
  19.     public function chat(
  20.         Request $request,
  21.         ManagerRegistry $managerRegistry,
  22.         Utils $utils,
  23.         $afiliadoId '',
  24.         $whabaId ''
  25.     ) {
  26.         $oEm $managerRegistry->getManager();
  27.         $oCentro $oEm->getRepository('App\Entity\ZAPP\Centros')
  28.             ->findOneBy(['idAfiliado' => $afiliadoId]);
  29.         if ($afiliadoId == 620) {
  30.             dd();
  31.         }
  32.         $oConfiguracion $oEm->getRepository('App\Entity\ZAPP\Configuracion')
  33.             ->findOneBy([
  34.                 'keyIdentificador' => 'whaba_id''centroId' => $oCentro->getIdCentro(),
  35.                 'valor' => $whabaId
  36.             ]);
  37.         $valorWhabaId '';
  38.         if (!is_null($oConfiguracion)) {
  39.             $valorWhabaId $oConfiguracion->getValor();
  40.         } else {
  41.             $valorWhabaId $whabaId;
  42.         }
  43.         $oConversaciones $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  44.             ->getConversationsByPhoneOrigen($valorWhabaId$oCentro->getIdCentro(), 20);
  45.         $aConversaciones = [];
  46.         foreach ($oConversaciones as $conversacion) {
  47.             $numTelefono = ($conversacion['whabaDestinoId'] === $valorWhabaId) ? $conversacion['whabaOrigenId'] : $conversacion['whabaDestinoId'];
  48.             if ((strpos($numTelefono"34") === 0)) {
  49.                 $numTelefono substr($numTelefono2);
  50.             }
  51.             if (strpos($numTelefono"+34") === 0) {
  52.                 $numTelefono substr($numTelefono3);
  53.             }
  54.             if (strpos($numTelefono"4") === 0) {
  55.                 $numTelefono substr($numTelefono1);
  56.             }
  57.             $aTotalNotificaciones $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  58.                 ->getTotalNotificaciones($numTelefono$conversacion['centroId'], $valorWhabaId);
  59.             $oPacientesVC = [];
  60.             $pacienteVc '';
  61.             $oTelefonosPacienteVC $oEm->getRepository('App\Entity\ZAPP\TelefonosPacienteVc')->findBy(array('numeroTelefono' => $numTelefono));
  62.             foreach ($oTelefonosPacienteVC as $numTelefonoPacienteVc) {
  63.                 $pacienteVc $numTelefonoPacienteVc->getPacienteVc();
  64.                 if ($pacienteVc->getCentroId() == $oCentro->getIdCentro()) {
  65.                     array_push($oPacientesVC$pacienteVc);
  66.                 }
  67.             }
  68.             $aPacientesPorConversacion = [];
  69.             $imagen '';
  70.             //Quitamos duplicados...
  71.             $oPacientesVC array_map('serialize'$oPacientesVC);
  72.             $oPacientesVC array_unique($oPacientesVCSORT_REGULAR);
  73.             $oPacientesVC array_map('unserialize'$oPacientesVC);
  74.             foreach ($oPacientesVC as $pacienteVc) {
  75.                 if (!is_null($pacienteVc)) {
  76.                     $imagen $pacienteVc->getFoto();
  77.                     if ((strpos($imagen'phpPerfil') === false) && (strpos($imagen'phppaciente') === false)) {
  78.                         $imagen '';
  79.                     }
  80.                 }
  81.                 array_push(
  82.                     $aPacientesPorConversacion,
  83.                     [
  84.                         'vcId' => $pacienteVc->getIdVc(), //Array de VCIDs
  85.                         'nombre_familia' => ucwords(strtolower((!is_null($pacienteVc)) ? $pacienteVc->getApellidos() : $pacienteVc->getIdVc())),
  86.                         'nombre_paciente' => ucwords(strtolower((!is_null($pacienteVc)) ? $pacienteVc->getNombre() : '')),
  87.                         'imagen' => $imagen,
  88.                     ]
  89.                 );
  90.             }
  91.             if (!isset($aConversaciones[$numTelefono]) || $conversacion['maxFechaCreacion'] > $aConversaciones[$numTelefono]['fecha_creacion']) {
  92.                 $aConversaciones[$numTelefono] = [
  93.                     'centroId' => $oCentro->getIdCentro(),
  94.                     'pacientes' => $aPacientesPorConversacion,
  95.                     'num_telefono' => $numTelefono,
  96.                     'total_notificaciones' => $aTotalNotificaciones[0]['totalLeido'],
  97.                     'fecha_creacion' => $conversacion['maxFechaCreacion']
  98.                 ];
  99.             }
  100.         }
  101.         //sort($aConversaciones);
  102.         $column array_column($aConversaciones'total_notificaciones');
  103.         $columnFechaCreacion array_column($aConversaciones'fecha_creacion');
  104.         array_multisort($columnSORT_DESC$columnFechaCreacionSORT_DESC$aConversaciones);
  105.         //array_multisort($column, SORT_DESC, $aConversaciones);
  106.         $aConversaciones array_slice($aConversaciones020);
  107.         $data = [
  108.             'aConversaciones' => $aConversaciones,
  109.             'whabaOrigen' => $valorWhabaId,
  110.             'afiliadoId' => $afiliadoId,
  111.             'centroId' => $oCentro->getIdCentro()
  112.         ];
  113.         return $this->render('chat/base_chat.html.twig'$data);
  114.         /*         foreach ($aConversaciones as $keyNumTelefono => $valueNumTelfono) {
  115.             foreach ($valueNumTelfono as $keyVcId => $valueConversacion) {
  116.                 $aConversacionesTemp[] = [
  117.                     'centroId' => $valueConversacion['centroId'],
  118.                     'vcId' => $valueConversacion['vcId'],
  119.                     'num_telefono' => $valueConversacion['num_telefono'],
  120.                     'nombre_familia' => $valueConversacion['nombre_familia'],
  121.                     'nombre_paciente' => $valueConversacion['nombre_paciente'],
  122.                     'imagen' => $valueConversacion['imagen'],
  123.                     'total_notificaciones' => $valueConversacion['total_notificaciones'],
  124.                     'fecha_creacion' => $valueConversacion['fecha_creacion']
  125.                 ];
  126.             }
  127.         } */
  128.         /* $oPacienteVC = $oEm->getRepository('App\Entity\ZAPP\PacienteVc')
  129.                 ->findOneBy(['idVc' => $conversacion['vcId'], 'centroId' => $conversacion['centroId']]); */
  130.     }
  131.     /**
  132.      * @Route("/consulta-consumos/{afiliadoId}/", name="consulta_consumos")
  133.      */
  134.     public function consultaConsumos(
  135.         Request $request,
  136.         ManagerRegistry $managerRegistry,
  137.         UtilsTools $utilsTools,
  138.         Utils $utils,
  139.         $afiliadoId ''
  140.     ) {
  141.         $oEm $managerRegistry->getManager();
  142.         $oCentro $oEm->getRepository('App\Entity\ZAPP\Centros')
  143.             ->findOneBy(['idAfiliado' => $afiliadoId]);
  144.         $dbItem $utilsTools->getWhabaCentroId($oCentro->getIdCentro());
  145.         $whaba $dbItem["whaba_id"];
  146.         $messageCounts = [];
  147.         // Get the current date
  148.         $currentDate = new \DateTime();
  149.         // Iterate over the last three months
  150.         for ($i 0$i 3$i++) {
  151.             // Clone the current date and subtract months
  152.             $date = clone $currentDate;
  153.             $date->modify("-$i months");
  154.             // Extract year and month
  155.             $year $date->format('Y');
  156.             $month $date->format('m');
  157.             // Call the function and store the result in the array
  158.             $messageCounts["$year-$month"] = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  159.                 ->countMessagesByWhabaIdAndMonth($whaba$month$year);
  160.         }
  161.         $messageCounts["centro"] = $oCentro->getNombreCentro();
  162.         dd($messageCounts);
  163.     }
  164.     /**
  165.      * @Route("/get-listado-conversaciones/{whabaOrigen}/{numTelefonoSelected}", name="get_listado_conversaciones")
  166.      */
  167.     public function getListadoConversaciones(
  168.         Request $request,
  169.         ManagerRegistry $managerRegistry,
  170.         UtilsTools $utilsTools,
  171.         Utils $utils,
  172.         $whabaOrigen '',
  173.         $numTelefonoSelected ''
  174.     ) {
  175.         $oEm $managerRegistry->getManager();
  176.         $oConfiguracion $oEm->getRepository('App\Entity\ZAPP\Configuracion')
  177.             ->findOneBy(['valor' => $whabaOrigen]);
  178.         $centroId '';
  179.         if (!is_null($oConfiguracion)) {
  180.             $centroId $oConfiguracion->getCentroId();
  181.         } else {
  182.             $objConfUserWhaba $utilsTools->getConfiguracionUserTelefonoByWhabaId($whabaOrigen);
  183.             $centroId $objConfUserWhaba->centro_id;
  184.         }
  185.         $oCentro $oEm->getRepository('App\Entity\ZAPP\Centros')
  186.             ->findOneBy(['idCentro' => $centroId]);
  187.         $oConversaciones $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  188.             ->getConversationsByPhoneOrigen($whabaOrigen$centroId100);
  189.         $aConversaciones = [];
  190.         foreach ($oConversaciones as $conversacion) {
  191.             $numTelefono = ($conversacion['whabaDestinoId'] === $whabaOrigen) ? $conversacion['whabaOrigenId'] : $conversacion['whabaDestinoId'];
  192.             if ($numTelefono == "" || $numTelefono === null) {
  193.                 continue;
  194.             }
  195.             if ((strpos($numTelefono"34") === 0)) {
  196.                 $numTelefono substr($numTelefono2);
  197.             }
  198.             if (strpos($numTelefono"+34") === 0) {
  199.                 $numTelefono substr($numTelefono3);
  200.             }
  201.             if (strpos($numTelefono"4") === 0) {
  202.                 $numTelefono substr($numTelefono1);
  203.             }
  204.             $aTotalNotificaciones $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  205.                 ->getTotalNotificaciones($numTelefono$conversacion['centroId'], $whabaOrigen);
  206.             $oPacientesVC = [];
  207.             $pacienteVc '';
  208.             $oTelefonosPacienteVC $oEm->getRepository('App\Entity\ZAPP\TelefonosPacienteVc')->findBy(array('numeroTelefono' => $numTelefono));
  209.             foreach ($oTelefonosPacienteVC as $numTelefonoPacienteVc) {
  210.                 $pacienteVc $numTelefonoPacienteVc->getPacienteVc();
  211.                 if ($pacienteVc->getCentroId() == $oCentro->getIdCentro()) {
  212.                     array_push($oPacientesVC$pacienteVc);
  213.                 }
  214.             }
  215.             $aPacientesPorConversacion = [];
  216.             $imagen '';
  217.             //Quitamos duplicados...
  218.             $oPacientesVC array_map('serialize'$oPacientesVC);
  219.             $oPacientesVC array_unique($oPacientesVCSORT_REGULAR);
  220.             $oPacientesVC array_map('unserialize'$oPacientesVC);
  221.             foreach ($oPacientesVC as $pacienteVc) {
  222.                 if (!is_null($pacienteVc)) {
  223.                     $imagen $pacienteVc->getFoto();
  224.                     if ((strpos($imagen'phpPerfil') === false) && (strpos($imagen'phppaciente') === false)) {
  225.                         $imagen '';
  226.                     }
  227.                 }
  228.                 array_push(
  229.                     $aPacientesPorConversacion,
  230.                     [
  231.                         'vcId' => $pacienteVc->getIdVc(), //Array de VCIDs
  232.                         'nombre_familia' => ucwords(strtolower((!is_null($pacienteVc)) ? $pacienteVc->getApellidos() : $pacienteVc->getIdVc())),
  233.                         'nombre_paciente' => ucwords(strtolower((!is_null($pacienteVc)) ? $pacienteVc->getNombre() : '')),
  234.                         'imagen' => $imagen,
  235.                     ]
  236.                 );
  237.             }
  238.             if (!isset($aConversaciones[$numTelefono]) || $conversacion['maxFechaCreacion'] > $aConversaciones[$numTelefono]['fecha_creacion']) {
  239.                 $aConversaciones[$numTelefono] = [
  240.                     'centroId' => $oCentro->getIdCentro(),
  241.                     'pacientes' => $aPacientesPorConversacion,
  242.                     'num_telefono' => $numTelefono,
  243.                     'total_notificaciones' => $aTotalNotificaciones[0]['totalLeido'],
  244.                     'fecha_creacion' => $conversacion['maxFechaCreacion']
  245.                 ];
  246.             }
  247.         }
  248.         $column array_column($aConversaciones'total_notificaciones');
  249.         $columnFechaCreacion array_column($aConversaciones'fecha_creacion');
  250.         array_multisort($columnSORT_DESC$columnFechaCreacionSORT_DESC$aConversaciones);
  251.         $datos = [
  252.             'aConversaciones' => $aConversaciones,
  253.             'numTelefonoSelected' => $numTelefonoSelected
  254.         ];
  255.         return $this->render('chat/listado-chat.html.twig'$datos);
  256.         /*         foreach ($oConversaciones as $conversacion) {
  257.             $oPacienteVC = $oEm->getRepository('App\Entity\ZAPP\PacienteVc')
  258.                 ->findOneBy(['idVc' => $conversacion['vcId'], 'centroId' => $conversacion['centroId']]);
  259.             $aTotalNotificaciones = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  260.                 ->getTotalNotificaciones($conversacion['vcId'], $conversacion['centroId']);
  261.             $numTelefono = ($conversacion['whabaDestinoId'] === $whabaOrigen) ? $conversacion['whabaOrigenId'] : $conversacion['whabaDestinoId'];
  262.             $imagen = '';
  263.             if (!is_null($oPacienteVC)) {
  264.                 $imagen = $oPacienteVC->getFoto();
  265.                 if ((strpos($imagen, 'phpPerfil') === false) && (strpos($imagen, 'phppaciente') === false)) {
  266.                     $imagen = '';
  267.                 }
  268.             }
  269.             if ((strpos($numTelefono, "34") !== false) || (strpos($numTelefono, "+34") !== false)) {
  270.                 $numTelefono = substr($numTelefono, "2");
  271.             }
  272.             $aConversaciones[$numTelefono][$conversacion['vcId']] = [
  273.                 'centroId' => $conversacion['centroId'],
  274.                 'vcId' => $conversacion['vcId'],
  275.                 'num_telefono' => $numTelefono,
  276.                 'nombre_familia' => (!is_null($oPacienteVC)) ? $oPacienteVC->getApellidos() : $conversacion['vcId'],
  277.                 'nombre_paciente' => (!is_null($oPacienteVC)) ? $oPacienteVC->getNombre() : '',
  278.                 'imagen' => $imagen,
  279.                 'total_notificaciones' => $aTotalNotificaciones[0]['totalLeido'],
  280.                 'fecha_creacion' => $conversacion['fechaCreacion']
  281.             ];
  282.         }
  283.         $aConversacionesTmp = [];
  284.         foreach ($aConversaciones as $keyNumTelefono => $valueNumTelfono) {
  285.             $aVCIDsNumTlf = [];
  286.             foreach ($valueNumTelfono as $keyVcId => $valueConversacion) {
  287.                 array_push($aVCIDsNumTlf, [
  288.                     'vcId_paciente' => $valueConversacion['vcId'],
  289.                     'nombre_paciente' => ucwords(strtolower($valueConversacion['nombre_paciente'])),
  290.                     'nombre_familia_paciente' => ucwords(strtolower($valueConversacion['nombre_familia']))
  291.                 ]);
  292.             }
  293.             $aConversacionesTmp[] = [
  294.                 'centroId' => $valueConversacion['centroId'],
  295.                 'pacientes' => $aVCIDsNumTlf,
  296.                 'num_telefono' => $valueConversacion['num_telefono'],
  297.                 'imagen' => $valueConversacion['imagen'],
  298.                 'total_notificaciones' => $valueConversacion['total_notificaciones'],
  299.                 'fecha_creacion' => $valueConversacion['fecha_creacion']
  300.             ];
  301.         } */
  302.         /*        foreach ($aConversaciones as $keyNumTelefono => $valueNumTelefono) {
  303.             foreach ($valueNumTelefono as $keyVcId => $value) {
  304.                 $aConversacionesTmp[] = [
  305.                     'centroId' => $value['centroId'],
  306.                     'vcId' => $value['vcId'],
  307.                     'num_telefono' => $value['num_telefono'],
  308.                     'nombre_familia' => $value['nombre_familia'],
  309.                     'nombre_paciente' => $value['nombre_paciente'],
  310.                     'imagen' => $value['imagen'],
  311.                     'total_notificaciones' => $value['total_notificaciones'],
  312.                     'fecha_creacion' => $value['fecha_creacion']
  313.                 ];
  314.             }
  315.         } */
  316.     }
  317.     /**
  318.      * @Route("/get-conversacion/{whabaOrigenId}/{numTelefono}/{centroId}", name="get_conversacion")
  319.      */
  320.     public function getConversacion(
  321.         Request $request,
  322.         ManagerRegistry $managerRegistry,
  323.         Utils $utils,
  324.         UtilsZAPP $utilsZAPP,
  325.         $whabaOrigenId '',
  326.         $vcId '',
  327.         $numTelefono '',
  328.         $centroId ''
  329.     ) {
  330.         $oEm $managerRegistry->getManager();
  331.         $aTelefonoPaciente = [];
  332.         $oHistorialMensajes $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  333.             ->getConversacionByPhone($numTelefono$centroId$whabaOrigenId);
  334.         $oTelefonosPacientes $oEm->getRepository('App\Entity\ZAPP\TelefonosPacienteVc')
  335.             ->findBy(['numeroTelefono' => $numTelefono]);
  336.         $oPacientesVC = [];
  337.         $aPacientesVCIDs = [];
  338.         $aTelefonoPaciente = [];
  339.         foreach ($oTelefonosPacientes as $telefonoPaciente) {
  340.             $pacienteVc $telefonoPaciente->getPacienteVc();
  341.             if ($pacienteVc->getCentroId() == $centroId) {
  342.                 array_push($oPacientesVC$pacienteVc);
  343.                 array_push($aPacientesVCIDs$pacienteVc->getIdVc());
  344.                 array_push($aTelefonoPaciente$telefonoPaciente->getTipoTelefono());
  345.             }
  346.         }
  347.         //Quitamos duplicados...
  348.         $oPacientesVC array_map('serialize'$oPacientesVC);
  349.         $oPacientesVC array_unique($oPacientesVCSORT_REGULAR);
  350.         $oPacientesVC array_map('unserialize'$oPacientesVC);
  351.         if ($oPacientesVC === []) {
  352.             $array = array("numerotelefonodesconocido" => $numTelefono);
  353.             $oPacientesVC = array(json_decode(json_encode($array)));
  354.         }
  355.         //Pretendía dar la señal para crear el nuevo pacienteVC de filemaker pero no tengo su id...
  356.         /* if (is_null($aPacientesVCID)) {
  357.             foreach ($aPacientesVCID as $pacienteVC) {
  358.                 $utilsZAPP->createUpdatePacienteMySQL($vcId, $centroId);
  359.             }
  360.         } */
  361.         //Aqui scaa el numero de telefono si no lo conoce. Pero doy por hecho que todo lo que salga en la lista de chats está OK
  362.         /* if (empty($oTelefonosPacientes)) {
  363.             $utilsZAPP->createTelefonoPacienteMySQL($oPacienteVc, $vcId, $centroId);
  364.             $oPacienteTelefonos = $oEm->getRepository('App\Entity\ZAPP\TelefonosPacienteVc')
  365.                 ->findBy(['pacienteVc' => $oPacienteVc]);
  366.         } */
  367.         /*         dd($o)
  368.         foreach ($oTelefonosPacientes as $pacienteTelefono) {
  369.             
  370.         } */
  371.         //Marcamos como leidas las notificaciones TODO- Ver si esto ya puede funcionar sin ID!!!
  372.         $oHistorialMensajesNotificaciones $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  373.             ->getMessageIsOrNotRead($numTelefono$centroId);
  374.         foreach ($oHistorialMensajesNotificaciones as $historialMensajesNotificacione) {
  375.             $historialMensajesNotificacione->setIsRead(true);
  376.             $historialMensajesNotificacione->setFechaRead(new \DateTime('now'));
  377.             $oEm->persist($historialMensajesNotificacione);
  378.             $oEm->flush();
  379.         }
  380.         //Comparamos si ha pasado mas de 48 horas, para cargar la plantilla generica
  381.         $oLastMessage $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  382.             ->getLastMessage($numTelefono$centroId$whabaOrigenId);
  383.         $isTemplateSend false;
  384.         if (!is_null($oLastMessage)) {
  385.             $fechaActual = new \DateTime('now');
  386.             $horaDiferencia $fechaActual->diff($oLastMessage->getFechaCreacion());
  387.             if ($horaDiferencia->days >= 1) {
  388.                 $isTemplateSend true;
  389.             }
  390.         } else {
  391.             $isTemplateSend true;
  392.         }
  393.         $datos = [
  394.             'oHistorialMensajes' => $oHistorialMensajes,
  395.             'oPacienteVc' => $oPacientesVC,
  396.             'whabaOrigenId' => $whabaOrigenId,
  397.             'vcId' => $aPacientesVCIDs,
  398.             'centroId' => $centroId,
  399.             'aTelefonosPaciente' => $aTelefonoPaciente,
  400.             'numTelefono' => $numTelefono
  401.         ];
  402.         $viewCabecera $this->renderView('chat/conversacion-header.html.twig'$datos);
  403.         $viewConversacion $this->renderView('chat/conversacion.html.twig'$datos);
  404.         return new JsonResponse([
  405.             'viewCabecera' => $viewCabecera,
  406.             'viewConversacion' => $viewConversacion'isTemplateSend' => $isTemplateSend
  407.         ]);
  408.     }
  409.     /**
  410.      * @Route("/force-database-update/{numTelefono}/{centroId}", name="force_database_update")
  411.      */
  412.     public function forceDatabaseUpdate(
  413.         Request $request,
  414.         ManagerRegistry $managerRegistry,
  415.         Utils $utils,
  416.         UtilsZAPP $utilsZAPP,
  417.         $numTelefono '',
  418.         $centroId ''
  419.     ) {
  420.         //Llamada a la api para que haga lo suyo
  421.         $EstadoActualizacion = new JsonResponse(json_decode($utils->connectToAPI('force-database-update/' $numTelefono'GET', ['idCentroConnection' => $centroId])));
  422.         return $EstadoActualizacion;
  423.     }
  424.     /**
  425.      * @Route("/send-mensaje", name="send_mensaje")
  426.      */
  427.     public function sendMessage(Request $requestManagerRegistry $managerRegistryUtils $utils)
  428.     {
  429.         $oEm $managerRegistry->getManager();
  430.         $viewMessage '';
  431.         try {
  432.             $whabaOrigen $request->request->get('whabaOrigen');
  433.             /* $vcId = $request->request->get('vcId'); */
  434.             $centroId $request->request->get('centroid');
  435.             $type $request->request->get('typeSend');
  436.             $telefonoDestino '';
  437.             if ($request->request->get('telefonoDestino')) {
  438.                 $telefonoDestino $request->request->get('telefonoDestino');
  439.             }
  440.             /* $oHistorialMensajesPreview = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  441.                 ->findOneBy(['centroId' => $centroId, 'vcId' => $vcId]); */
  442.             $aContenido = [];
  443.             $oHistorialMensaje = new HistorialMensajesWhatsapp();
  444.             $oHistorialMensaje->setIsRead(1);
  445.             $oHistorialMensaje->setFechaRead(new \DateTime('now'));
  446.             $oHistorialMensaje->setFechaActualizacion(new \DateTime('now'));
  447.             $oHistorialMensaje->setFechaCreacion(new \DateTime('now'));
  448.             $oHistorialMensaje->setCentroId($centroId);
  449.             $oHistorialMensaje->setVcId(null);
  450.             $oHistorialMensaje->setWhabaOrigenId($whabaOrigen);
  451.             /*$oHistorialMensaje->setWhabaDestinoId(($telefonoDestino !== '') ? $telefonoDestino
  452.                 : $oHistorialMensajesPreview->getWhabaDestinoId());*/
  453.             $oHistorialMensaje->setWhabaDestinoId($telefonoDestino);
  454.             $oHistorialMensaje->setTemplate('');
  455.             /* $aDatos['vcId'] = $vcId; */
  456.             $aDatos['centroId'] = $centroId;
  457.             if ($telefonoDestino !== '') {
  458.                 $aDatos['telefonoDestino'] = $telefonoDestino;
  459.             }
  460.             $messsage $request->request->get('texto');
  461.             $aDatos['message'] = $messsage;
  462.             if ($type === 'text') {
  463.                 //$aContenido = $this->buildMessage($aDatos, $oEm);
  464.                 $aContenido $this->buildTemplateEstandard($aDatos$oEm);
  465.                 $oHistorialMensaje->setTemplate('plantilla_mensaje');
  466.             } else if ($type === 'template') {
  467.                 $aContenido $this->buildTemplateGenerico($aDatos$oEm);
  468.                 $oHistorialMensaje->setTemplate('mensaje_estandard');
  469.             } else {
  470.                 $aDatos['message'] = $messsage;
  471.                 if ($request->request->get('linkFile')) {
  472.                     $aDatos['linkFile'] = $request->request->get('linkFile');
  473.                     $aTipoArchivoImagen = ['jpg' => 'image/jpg''png' => 'image/png'];
  474.                     $aTipoArchivoDoc = ['pdf' => 'application/pdf'];
  475.                     $aTipoArchivoVideo = ['mp4' => 'video/mp4'];
  476.                     $aTipoArchivoAudio = ['ogg' => 'audio/ogg; codecs=opus'];
  477.                     $extension pathinfo($request->request->get('linkFile'), PATHINFO_EXTENSION);
  478.                     $oHistorialMensaje->setFileMedia($request->request->get('linkFile'));
  479.                     if (isset($aTipoArchivoImagen[$extension]) && !empty($aTipoArchivoImagen[$extension])) {
  480.                         $oHistorialMensaje->setFileType($aTipoArchivoImagen[$extension]);
  481.                     }
  482.                     if (isset($aTipoArchivoDoc[$extension]) && !empty($aTipoArchivoDoc[$extension])) {
  483.                         $oHistorialMensaje->setFileType($aTipoArchivoDoc[$extension]);
  484.                     }
  485.                     if (isset($aTipoArchivoVideo[$extension]) && !empty($aTipoArchivoVideo[$extension])) {
  486.                         $oHistorialMensaje->setFileType($aTipoArchivoVideo[$extension]);
  487.                     }
  488.                     if (isset($aTipoArchivoAudio[$extension]) && !empty($aTipoArchivoAudio[$extension])) {
  489.                         $oHistorialMensaje->setFileType($aTipoArchivoAudio[$extension]);
  490.                     }
  491.                     $oHistorialMensaje->setDownloadFile($request->request->get('linkFile'));
  492.                     $oHistorialMensaje->setDownloadDate(new \DateTime('now'));
  493.                 }
  494.                 $aContenido $this->{$type}($aDatos$oEm);
  495.                 //si es una plantilla definida, entonces guardamos el contenido
  496.                 $oHistorialMensaje->setTemplate($type);
  497.                 $mensajeFinal $this->getTemplatesDefinidos($type$messsage);
  498.                 if ($mensajeFinal !== '') {
  499.                     $messsage $mensajeFinal;
  500.                 }
  501.             }
  502.             $oHistorialMensaje->setMensaje($messsage);
  503.             $aResultado $utils->sendMessageWhatsapp($aContenido$whabaOrigen);
  504.             //Guardamos en la db lo enviado
  505.             $oEm->persist($oHistorialMensaje);
  506.             $oEm->flush();
  507.             $viewMessage $this->buildLastMessage($oEm$oHistorialMensaje$centroId$whabaOrigen);
  508.         } catch (\Exception $exception) {
  509.             dump($exception->getMessage() . "-" $exception->getLine());
  510.             exit;
  511.         }
  512.         return new JsonResponse(['view' => $viewMessage]);
  513.     }
  514.     /**
  515.      * @Route("/get-last-message-chat/{numTlf}/{centroId}", name="get_last_message_chat")
  516.      */
  517.     public function getLastMessageChat(Request $requestManagerRegistry $managerRegistry$numTlf ''$centroId '')
  518.     {
  519.         $oEm $managerRegistry->getManager();
  520.         $qb $oEm->createQueryBuilder();
  521.         $whabaId $request->request->get('whabaid');
  522.         /* $oHistorialMensaje = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  523.             ->findOneBy(['centroId' => $centroId, 'vcId' => $vcId, 'isRead' => 0], ['fechaCreacion' => 'ASC']); */
  524.         $oHistorialMensaje $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  525.             ->createQueryBuilder('hmw')
  526.             ->where('hmw.centroId = :centroId')
  527.             ->andWhere('hmw.isRead = :isRead')
  528.             ->andWhere(
  529.                 $qb->expr()->orX(
  530.                     'hmw.whabaDestinoId = :numTlf',
  531.                     'hmw.whabaOrigenId = :numTlf'
  532.                 )
  533.             )
  534.             ->orderBy('hmw.fechaCreacion''ASC')
  535.             ->setParameters([
  536.                 'centroId' => $centroId,
  537.                 'numTlf' => $numTlf,
  538.                 'isRead' => 0
  539.             ])
  540.             ->getQuery()
  541.             ->getOneOrNullResult();
  542.         /* $oPacienteVc = $oEm->getRepository('App\Entity\ZAPP\PacienteVc')
  543.             ->findOneBy(['idVc' => $numTlf, 'centroId' => $centroId]); */
  544.         /* $oTotalHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  545.             ->findBy(['centroId' => $centroId, 'vcId' => $numTlf, 'isRead' => 0]); */
  546.         $oTotalHistorialMensajes $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  547.             ->createQueryBuilder('hmw')
  548.             ->where('hmw.centroId = :centroId')
  549.             ->andWhere('hmw.isRead = :isRead')
  550.             ->andWhere(
  551.                 $qb->expr()->orX(
  552.                     'hmw.whabaDestinoId = :numTlf',
  553.                     'hmw.whabaOrigenId = :numTlf'
  554.                 )
  555.             )
  556.             ->setParameters([
  557.                 'centroId' => $centroId,
  558.                 'numTlf' => $numTlf,
  559.                 'isRead' => 0
  560.             ])
  561.             ->getQuery()
  562.             ->getResult();
  563.         $datos = [
  564.             'oMensaje' => $oHistorialMensaje,
  565.             /* 'oPacienteVc' => $oPacienteVc */
  566.             'whabaOrigenId' => $whabaId
  567.         ];
  568.         $view $this->renderView('chat/componentes/detalle-linea.html.twig'$datos);
  569.         //Marcamos el historial del mensaje a leido
  570.         if (!is_null($oHistorialMensaje)) {
  571.             $oHistorialMensaje->setIsRead(true);
  572.             $oHistorialMensaje->setFechaRead(new \DateTime('now'));
  573.             $oEm->persist($oHistorialMensaje);
  574.             $oEm->flush();
  575.         }
  576.         return new JsonResponse(['view' => $view'totalItems' => count($oTotalHistorialMensajes)]);
  577.     }
  578.     /**
  579.      * @Route("/get-datos-paciente/{centroId}/{idVc}", name="get_datos_paciente")
  580.      */
  581.     public function getDatosPaciente(
  582.         Request $request,
  583.         ManagerRegistry $managerRegistry,
  584.         Utils $utils,
  585.         $centroId '',
  586.         $idVc ''
  587.     ) {
  588.         $oEm $managerRegistry->getManager();
  589.         $oPaciente $oEm->getRepository('App\Entity\ZAPP\PacienteVc')
  590.             ->findOneBy(['idVc' => $idVc'centroId' => $centroId]);
  591.         $oPacienteApiTel json_decode($utils->connectToAPI('get-paciente-telefonos/' $idVc'GET', ['idCentroConnection' => $centroId]));
  592.         $aTelefonos = [];
  593.         if (isset($oPacienteApiTel->telefonos) && !empty($oPacienteApiTel->telefonos)) {
  594.             $aTelefonos $oPacienteApiTel->telefonos;
  595.         }
  596.         $datos = [
  597.             'oPaciente' => $oPaciente,
  598.             'oPacienteTelefono' => $aTelefonos
  599.         ];
  600.         return $this->render('chat/componentes/detalle-paciente.html.twig'$datos);
  601.     }
  602.     /**
  603.      * @Route("/get-content-media/{conversacionId}", name="get_content_media")
  604.      */
  605.     public function getContentMedia(
  606.         Request $request,
  607.         ManagerRegistry $managerRegistry,
  608.         Utils $utils,
  609.         $conversacionId ''
  610.     ) {
  611.         $oEm $managerRegistry->getManager();
  612.         $urlMedia '';
  613.         $oConversacion $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  614.             ->findOneBy(['id' => $conversacionId]);
  615.         if (!is_null($oConversacion->getFileMedia())) {
  616.             $urlMedia $utils->getDownloadImage($utils->getImageWhatsapp($oConversacion->getFileMedia()), $conversacionId);
  617.         }
  618.         return new JsonResponse(['urlMedia' => $urlMedia]);
  619.     }
  620.     /**
  621.      * @Route("/get-search-pacientes", name="get_search_pacientes")
  622.      */
  623.     public function getSearchPacientes(Request $requestManagerRegistry $managerRegistryUtils $utils)
  624.     {
  625.         $pacienteSearchNombre $request->request->get('pacienteNombre');
  626.         $pacienteSearchApellido $request->request->get('pacienteApellido');
  627.         $centroId $request->request->get('centroId');
  628.         $aPacientes = [];
  629.         $objPacientes json_decode($utils->connectToAPI(
  630.             'get-filtro-paciente/1',
  631.             'POST',
  632.             ['name' => $pacienteSearchNombre'apellido' => $pacienteSearchApellido'idCentroConnection' => $centroId]
  633.         ));
  634.         //dump($objPacientes->aPacientes);exit;
  635.         for ($i 0$i count($objPacientes->aPacientes); $i++) {
  636.             $oPaciente $objPacientes->aPacientes[$i];
  637.             for ($j 0$j count($oPaciente); $j++) {
  638.                 if (isset($oPaciente[$j]) && !empty($oPaciente[$j])) {
  639.                     $aPacientes[] = [
  640.                         'id' => $oPaciente[$j]->id,
  641.                         'nombre' => $oPaciente[$j]->nombre,
  642.                         'apellidos' => $oPaciente[$j]->apellidos,
  643.                         'telefono' => $oPaciente[$j]->telefonos,
  644.                         'categoryTelefono' => $oPaciente[$j]->categoryTelefono
  645.                     ];
  646.                 }
  647.             }
  648.         }
  649.         // $aPacientes = $objPacientes['aPacientes'];
  650.         return new JsonResponse(['aPacientes' => $aPacientes]);
  651.     }
  652.     /**
  653.      * @Route("/upload-file-and-send", name="upload_file_and_send")
  654.      */
  655.     public function uploadFileAndSend(
  656.         Request $request,
  657.         ManagerRegistry $managerRegistry,
  658.         Utils $utils
  659.     ) {
  660.         $oEm $managerRegistry->getManager();
  661.         $status false;
  662.         $message '';
  663.         $viewMessage '';
  664.         try {
  665.             if (isset($_FILES['file']) && !empty($_FILES['file'])) {
  666.                 $typeFile $request->request->get('typeFile');
  667.                 $fileExtensionType $request->request->get('fileExtensionType');
  668.                 $whabaDestino $request->request->get('whabadestino');
  669.                 /* $vcId = $request->request->get('vcId'); */
  670.                 $centroId $request->request->get('centroId');
  671.                 $numTelefono $request->request->get('numtelefono');
  672.                 $result $utils->uploadImageAWS($_FILES['file'], true);
  673.                 if ($result) {
  674.                     $directory getcwd() . '/assets/chat/' $_FILES['file']['name'];
  675.                     move_uploaded_file($_FILES['file']['tmp_name'], $directory);
  676.                     //Guardamos el media descargado en la bd
  677.                     $full_url $result->get('ObjectURL');
  678.                     $last_slash strrpos($full_url'/') + 1;
  679.                     $url substr($full_url$last_slash);
  680.                     $oHistorialMensaje = new HistorialMensajesWhatsapp();
  681.                     $oHistorialMensaje->setWhabaOrigenId($whabaDestino);
  682.                     $oHistorialMensaje->setWhabaDestinoId($numTelefono);
  683.                     $oHistorialMensaje->setFechaCreacion(new \DateTime('now'));
  684.                     $oHistorialMensaje->setFechaActualizacion(new \DateTime('now'));
  685.                     $oHistorialMensaje->setMensaje('');
  686.                     $oHistorialMensaje->setCentroId($centroId);
  687.                     $oHistorialMensaje->setVcId(null);
  688.                     $oHistorialMensaje->setIsRead(true);
  689.                     $oHistorialMensaje->setTemplate('');
  690.                     $oHistorialMensaje->setFechaRead(new \DateTime('now'));
  691.                     $oHistorialMensaje->setFileMedia($url);
  692.                     $oHistorialMensaje->setFileType($fileExtensionType);
  693.                     $oHistorialMensaje->setDownloadFile($url);
  694.                     $oHistorialMensaje->setDownloadDate(new \DateTime('now'));
  695.                     $oEm->persist($oHistorialMensaje);
  696.                     $oEm->flush();
  697.                     /*$message = $utils->buildContentSendImageDocAndSend($aParams);*/
  698.                     $aDatos = [
  699.                         'centroId' => $centroId,
  700.                         'nameFile' => $url,
  701.                         'type' => $typeFile,
  702.                         'linkFile' => 'https://comunicacionprueba.ziving.com/assets/chat/' $_FILES['file']['name'],
  703.                         'telefonoDestino' => $numTelefono,
  704.                         'whabaOrigen' => $whabaDestino
  705.                     ];
  706.                     $aContenido = [];
  707.                     if ($typeFile === 'image') {
  708.                         $aContenido $this->estandard_imagen($aDatos$oEm);
  709.                     } else if ($typeFile === 'video') {
  710.                         $aContenido $this->estandard_video($aDatos$oEm);
  711.                     } else if ($typeFile === 'document') {
  712.                         $aContenido $this->estandard_documentos($aDatos$oEm);
  713.                     }
  714.                     $aResultado $utils->sendMessageWhatsapp($aContenido$whabaDestino);
  715.                     //unlink($directory);
  716.                     $status true;
  717.                     $viewMessage $this->buildLastMessage($oEm$oHistorialMensaje$centroId$whabaDestino);
  718.                 }
  719.             }
  720.         } catch (\Exception $exception) {
  721.             $status false;
  722.             $message $exception->getMessage();
  723.         }
  724.         return new JsonResponse(['status' => $status'message' => $message'view' => $viewMessage]);
  725.     }
  726.     /**
  727.      * @Route("/validar-waba-webhook", name="validar_waba_webhook")
  728.      */
  729.     public function validarWabaWebhook(Utils $utils)
  730.     {
  731.         //Esta funcion sirve para suscribir al campo mensajes un id de waba concreto
  732.         $response $utils->registerPhoneNumber();
  733.         dd($response);
  734.     }
  735.     /**
  736.      * @Route("/cambiar-nombre-waba", name="cambiar_nombre_waba")
  737.      */
  738.     public function cambiarNombreWaba(Utils $utils)
  739.     {
  740.         //Esta funcion sirve para cambiar el nombre de visualización de un telefono concreto
  741.         //Previa aprobación en la web de FB
  742.         $response $utils->changeWabaName();
  743.         dd($response);
  744.     }
  745.     private function buildLastMessage($oEm$oHistorialMensaje$centroId$whabaOrigenId)
  746.     {
  747.         /* $oPacienteVc = $oEm->getRepository('App\Entity\ZAPP\PacienteVc')
  748.             ->findOneBy(['idVc' => $vcId, 'centroId' => $centroId]); */
  749.         $datos = [
  750.             'oMensaje' => $oHistorialMensaje,
  751.             'whabaOrigenId' => $whabaOrigenId
  752.         ];
  753.         return $this->renderView('chat/componentes/detalle-linea.html.twig'$datos);
  754.     }
  755.     private function buildMessage($aDatos$oEm)
  756.     {
  757.         $oHistorialMensajes $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  758.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]);
  759.         $whabaDestino '';
  760.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  761.             $whabaDestino $aDatos['telefonoDestino'];
  762.         } else {
  763.             $whabaDestino $oHistorialMensajes->getWhabaDestinoId();
  764.         }
  765.         $textoFinal trim(preg_replace('/\t+/'' '$aDatos['message']));
  766.         $textoFinal trim(preg_replace('/\n+/'' '$textoFinal));
  767.         $aMessage = [
  768.             'messaging_product' => 'whatsapp',
  769.             'preview_url' => false,
  770.             'recipient_type' => 'individual',
  771.             'to' => $whabaDestino,
  772.             //'to' => '+34670493258',
  773.             'type' => 'text',
  774.             'text' => [
  775.                 'body' => $textoFinal
  776.             ]
  777.         ];
  778.         return $aMessage;
  779.     }
  780.     private function buildTemplateEstandard($aDatos$oEm)
  781.     {
  782.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  783.             ->findOneBy(['centroId' => $aDatos['centroId'], 'whaba_origen_id' => $aDatos['vcId']]); */
  784.         $whabaDestino '';
  785.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  786.             $whabaDestino $aDatos['telefonoDestino'];
  787.         } else {
  788.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  789.         }
  790.         $textoFinal $aDatos['message'];
  791.         //$textoFinal = str_replace("Hola", "", $aDatos['message']);
  792.         $textoFinal trim(preg_replace('/\t+/'' '$textoFinal));
  793.         $textoFinal trim(preg_replace('/\n+/'' '$textoFinal));
  794.         $aParametersBody = [
  795.             'type' => 'text',
  796.             'text' => $textoFinal
  797.         ];
  798.         $aContenido[] = [
  799.             'type' => 'body',
  800.             'parameters' => [
  801.                 $aParametersBody
  802.             ]
  803.         ];
  804.         //$whabaDestino = 670493258;
  805.         $aWhatsAppContent = [
  806.             'messaging_product' => 'whatsapp',
  807.             'recipient_type' => 'individual',
  808.             'to' => $whabaDestino,
  809.             'type' => 'template',
  810.             'template' => [
  811.                 'name' => 'plantilla_mensaje',
  812.                 'language' => [
  813.                     'code' => 'es'
  814.                 ],
  815.                 'components' => $aContenido
  816.             ]
  817.         ];
  818.         return $aWhatsAppContent;
  819.     }
  820.     private function buildTemplateGenerico($aDatos$oEm)
  821.     {
  822.         $oHistorialMensajes $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  823.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]);
  824.         $whabaDestino '';
  825.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  826.             $whabaDestino $aDatos['telefonoDestino'];
  827.         } else {
  828.             $whabaDestino $oHistorialMensajes->getWhabaDestinoId();
  829.         }
  830.         $textoFinal str_replace("Hola"""$aDatos['message']);
  831.         $textoFinal trim(preg_replace('/\t+/'' '$textoFinal));
  832.         $textoFinal trim(preg_replace('/\n+/'' '$textoFinal));
  833.         $aParametersBody = [
  834.             'type' => 'text',
  835.             'text' => $textoFinal
  836.         ];
  837.         $aContenido[] = [
  838.             'type' => 'body',
  839.             'parameters' => [
  840.                 $aParametersBody
  841.             ]
  842.         ];
  843.         //$whabaDestino = 670493258;
  844.         $aWhatsAppContent = [
  845.             'messaging_product' => 'whatsapp',
  846.             'recipient_type' => 'individual',
  847.             'to' => $whabaDestino,
  848.             'type' => 'template',
  849.             'template' => [
  850.                 'name' => 'mensaje_estandard',
  851.                 'language' => [
  852.                     'code' => 'es'
  853.                 ],
  854.                 'components' => $aContenido
  855.             ]
  856.         ];
  857.         return $aWhatsAppContent;
  858.     }
  859.     private function estandard_imagen($aDatos$oEm)
  860.     {
  861.         $aContenidoFinal = [];
  862.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  863.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]); */
  864.         $whabaDestino '';
  865.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  866.             $whabaDestino $aDatos['telefonoDestino'];
  867.         } else {
  868.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  869.         }
  870.         $aParametersHeader = [
  871.             'type' => 'image',
  872.             'image' => [
  873.                 'link' => $aDatos['linkFile']
  874.             ]
  875.         ];
  876.         $aContenidoHeader = [
  877.             'type' => 'header',
  878.             'parameters' => [
  879.                 $aParametersHeader
  880.             ]
  881.         ];
  882.         $aContenidoFinal = [
  883.             $aContenidoHeader
  884.         ];
  885.         // $whabaDestino = 670493258;
  886.         $aWhatsAppContent = [
  887.             'messaging_product' => 'whatsapp',
  888.             'recipient_type' => 'individual',
  889.             'to' => $whabaDestino,
  890.             'type' => 'template',
  891.             'template' => [
  892.                 'name' => 'plantilla_imagen',
  893.                 'language' => [
  894.                     'code' => 'es'
  895.                 ],
  896.                 'components' => $aContenidoFinal
  897.             ]
  898.         ];
  899.         return $aWhatsAppContent;
  900.     }
  901.     private function estandard_video($aDatos$oEm)
  902.     {
  903.         $aContenidoFinal = [];
  904.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  905.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]); */
  906.         $whabaDestino '';
  907.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  908.             $whabaDestino $aDatos['telefonoDestino'];
  909.         } else {
  910.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  911.         }
  912.         $aParametersHeader = [
  913.             'type' => 'video',
  914.             'video' => [
  915.                 'link' => $aDatos['linkFile']
  916.             ]
  917.         ];
  918.         $aContenidoHeader = [
  919.             'type' => 'header',
  920.             'parameters' => [
  921.                 $aParametersHeader
  922.             ]
  923.         ];
  924.         $aContenidoFinal = [
  925.             $aContenidoHeader
  926.         ];
  927.         // $whabaDestino = 670493258;
  928.         $aWhatsAppContent = [
  929.             'messaging_product' => 'whatsapp',
  930.             'recipient_type' => 'individual',
  931.             'to' => $whabaDestino,
  932.             'type' => 'template',
  933.             'template' => [
  934.                 'name' => 'plantilla_video',
  935.                 'language' => [
  936.                     'code' => 'es'
  937.                 ],
  938.                 'components' => $aContenidoFinal
  939.             ]
  940.         ];
  941.         return $aWhatsAppContent;
  942.     }
  943.     private function estandard_documentos($aDatos$oEm)
  944.     {
  945.         $aContenidoFinal = [];
  946.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  947.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]); */
  948.         $whabaDestino '';
  949.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  950.             $whabaDestino $aDatos['telefonoDestino'];
  951.         } else {
  952.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  953.         }
  954.         $aParametersHeader = [
  955.             'type' => 'document',
  956.             'document' => [
  957.                 'link' => $aDatos['linkFile'],
  958.                 'filename' => $aDatos['nameFile']
  959.             ]
  960.         ];
  961.         $aContenidoHeader = [
  962.             'type' => 'header',
  963.             'parameters' => [
  964.                 $aParametersHeader
  965.             ]
  966.         ];
  967.         $aContenidoFinal = [
  968.             $aContenidoHeader
  969.         ];
  970.         // $whabaDestino = 670493258;
  971.         $aWhatsAppContent = [
  972.             'messaging_product' => 'whatsapp',
  973.             'recipient_type' => 'individual',
  974.             'to' => $whabaDestino,
  975.             'type' => 'template',
  976.             'template' => [
  977.                 'name' => 'plantilla_documento',
  978.                 'language' => [
  979.                     'code' => 'es'
  980.                 ],
  981.                 'components' => $aContenidoFinal
  982.             ]
  983.         ];
  984.         return $aWhatsAppContent;
  985.     }
  986.     private function estandard_archivos($aDatos$oEm)
  987.     {
  988.         $aContenidoFinal = [];
  989.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  990.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]); */
  991.         $whabaDestino '';
  992.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  993.             $whabaDestino $aDatos['telefonoDestino'];
  994.         } else {
  995.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  996.         }
  997.         $aParametersHeader = [
  998.             'type' => 'image',
  999.             'image' => [
  1000.                 'link' => $aDatos['linkFile']
  1001.             ]
  1002.         ];
  1003.         $aContenidoHeader = [
  1004.             'type' => 'header',
  1005.             'parameters' => [
  1006.                 $aParametersHeader
  1007.             ]
  1008.         ];
  1009.         $textoFinal trim(preg_replace('/\t+/'''$aDatos['message']));
  1010.         $textoFinal trim(preg_replace('/\n+/'' '$textoFinal));
  1011.         $aParametersBody = [
  1012.             'type' => 'text',
  1013.             'text' => $textoFinal
  1014.         ];
  1015.         $aContenido = [
  1016.             'type' => 'body',
  1017.             'parameters' => [
  1018.                 $aParametersBody
  1019.             ]
  1020.         ];
  1021.         $aContenidoFinal = [
  1022.             $aContenidoHeader,
  1023.             $aContenido
  1024.         ];
  1025.         // $whabaDestino = 670493258;
  1026.         $aWhatsAppContent = [
  1027.             'messaging_product' => 'whatsapp',
  1028.             'recipient_type' => 'individual',
  1029.             'to' => $whabaDestino,
  1030.             'type' => 'template',
  1031.             'template' => [
  1032.                 'name' => 'archivos_texto',
  1033.                 'language' => [
  1034.                     'code' => 'es'
  1035.                 ],
  1036.                 'components' => $aContenidoFinal
  1037.             ]
  1038.         ];
  1039.         return $aWhatsAppContent;
  1040.     }
  1041.     private function plantilla_invitaamigo($aDatos$oEm)
  1042.     {
  1043.         /* $oHistorialMensajes = $oEm->getRepository('App\Entity\ZAPP\HistorialMensajesWhatsapp')
  1044.             ->findOneBy(['centroId' => $aDatos['centroId'], 'vcId' => $aDatos['vcId']]); */
  1045.         $whabaDestino '';
  1046.         if (isset($aDatos['telefonoDestino']) && !empty($aDatos['telefonoDestino'])) {
  1047.             $whabaDestino $aDatos['telefonoDestino'];
  1048.         } else {
  1049.             /* $whabaDestino = $oHistorialMensajes->getWhabaDestinoId(); */
  1050.         }
  1051.         $textoFinal $aDatos['message'];
  1052.         //$textoFinal = str_replace("Hola", "", $aDatos['message']);
  1053.         $textoFinal trim(preg_replace('/\t+/'' '$textoFinal));
  1054.         $textoFinal trim(preg_replace('/\n+/'' '$textoFinal));
  1055.         $aParametersBody = [
  1056.             'type' => 'text',
  1057.             'text' => $textoFinal
  1058.         ];
  1059.         $aContenido[] = [
  1060.             'type' => 'body',
  1061.             'parameters' => [
  1062.                 $aParametersBody
  1063.             ]
  1064.         ];
  1065.         //$whabaDestino = 670493258;
  1066.         $aWhatsAppContent = [
  1067.             'messaging_product' => 'whatsapp',
  1068.             'recipient_type' => 'individual',
  1069.             'to' => $whabaDestino,
  1070.             'type' => 'template',
  1071.             'template' => [
  1072.                 'name' => 'plantilla_invitaamigo',
  1073.                 'language' => [
  1074.                     'code' => 'es'
  1075.                 ],
  1076.                 'components' => $aContenido
  1077.             ]
  1078.         ];
  1079.         return $aWhatsAppContent;
  1080.     }
  1081.     private function getTemplatesDefinidos($template$contenido)
  1082.     {
  1083.         $mensaje '';
  1084.         switch ($template) {
  1085.             case 'plantilla_invitaamigo':
  1086.                 $mensaje 'Esta es vuestra Tarjeta Invita amigos para que podáis invitar a familiares y amigos a conocernos 💚💚💚
  1087.                 Después también el link de google reviews para que me recomendéis.
  1088.                 Es muy importante para mí que estéis a gusto con nosotros y nos ayudéis a crecer 😍' PHP_EOL $contenido;
  1089.                 break;
  1090.         }
  1091.         return $mensaje;
  1092.     }
  1093. }