<?php
/**
* Created by PhpStorm.
* User: DEVELUP-USER3
* Date: 12/12/2019
* Time: 10:09
*/
namespace App\MDS\WebInOutTravelBundle\Controller;
use App\Entity\NewsLetterControl;
use App\Entity\NewsLetterEmail;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\AbstractFOSRestController;
class ApiSaveEmailNewsletterController extends AbstractFOSRestController
{
/**
* @Rest\Post("/api/savenewsletter/inGet")
* @param Request $request
* @return Response
*/
public function apiNewsWebListGetAction(Request $request){
$em = $this->getDoctrine()->getManager();
$tokenList = $request->request->get('token');
$file = fopen('newsletter.txt', "wb");
fwrite($file, $tokenList. 'carlos' . 'hola');
fclose($file);
if (!empty($tokenList)){
switch ($tokenList) {
case "7013ae93b6de16c9c935dad6ca66f83f": // Web InOut
$controlName = "WEB INOUT";
$type = "WebInOut";
break;
case "7013ae93b6de16c9c935dad6ca66f83fem": // Web InOut
$controlName = "WEB INOUT EMPLEO";
$type = "WebInOutEmpleo";
break;
case "5dc65d4195b9e1ab55b269da93c90455": // Web Develup
$controlName = "WEB DEVELUP";
$type = "Webdevelup";
break;
case "765b9d325b17b6d82c873259355c7cfc": // Web LoLiTa
$controlName = "WEB LOLITA";
$type = "WebLolita";
break;
case "2b9c7dc24f8e68bac2f0c1e9e7360779": // Web AvExpress
$controlName = "WEB AVEXPRESS";
$type = "WebAvexpress";
break;
case "98f06708915a31c53e52a6bed2ed804a": // Web InOutEvents
$controlName = "WEB INOUT EVENTS";
$type = "WebInoutEvents";
break;
default:
$controlName = "ko";
}
if ($controlName != "ko"){
// $email = $request->query->get('email');
$email = $request->request->get('email');
$ip = $request->request->get('ip');
if (!empty($email)){
$NewsLetterControl = $em->getRepository(NewsLetterControl::class)->findOneBy(
array(
'name' => $controlName
)
);
$NewsLetterEmail = $em->getRepository(NewsLetterEmail::class)->findOneBy(
array(
'email' => $email,
'type' => $type,
)
);
if (empty($NewsLetterEmail)){
$newsletter = new NewsLetterEmail();
$newsletter->setNewsletterId($NewsLetterControl->getId());
$newsletter->setEmail($email);
$newsletter->setType($type);
$newsletter->setIp($ip);
$newsletter->setCreatedId(0);
$newsletter->setUpdatedId(0);
$em->persist($newsletter);
$em->flush();
$response = new JsonResponse("ok", 200);
}else{
$response = new JsonResponse("ko, exists", 200);
}
}else{
$response = new JsonResponse("ko", 401);
}
}else{
$response = new JsonResponse("ko", 401);
}
}else{
$response = new JsonResponse("ko", 401);
}
return $response;
}
}