Created
March 21, 2017 08:40
-
-
Save MikSDigital/6dd4985ff8c2906a596392d09a98afb7 to your computer and use it in GitHub Desktop.
ApiSubscriptionController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Email\EmailBundle\Controller; | |
| use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use FOS\RestBundle\View\View; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Education\Base\EducationBaseBundle\Entity\User; | |
| use Doctrine\ORM\EntityRepository; | |
| class ApiSubscriptionController extends Controller | |
| { | |
| /** | |
| * @Route("/api/subscribe", name="subscribe", defaults={"_format":"json"}) | |
| * @Method({"POST"}) | |
| */ | |
| public function subscribeUserAction(Request $oRequest) | |
| { | |
| // accepts data from /about-us/press-center/press-releases form | |
| // fields: name, surname, company, email, country | |
| // at MODX side Snippet "subscribe-news-form" responsible for form submitting | |
| $oUserRepo = $this->getDoctrine()->getRepository('EducationBaseBundle:User'); | |
| $userMail = $oRequest->request->get('mail'); | |
| return $this->get('fos_rest.view_handler') | |
| ->handle( | |
| View::create() | |
| ->setStatusCode(200) | |
| ->setData($oUserRepo->getIdByEmail($userMail)) | |
| ); | |
| if ($oRequest->getMethod() == 'POST' && $oRequest->request->has('mail')) // email field is a must | |
| { | |
| $oUser = new \Education\Base\EducationBaseBundle\Entity\User(); | |
| $oSubscription = new \Education\Base\EducationBaseBundle\Entity\Subscription(); | |
| $oUserData = new \Education\Base\EducationBaseBundle\Entity\UserData(); | |
| $conn = $this->get('database_connection'); | |
| $users = $conn->fetchAll('SELECT * FROM users'); | |
| echo count($users); | |
| exit(); | |
| $userMail = $oRequest->request->get('mail'); | |
| $oManager = $this->getDoctrine()->getManager(); | |
| $oUserRepo = $this->getDoctrine()->getRepository(\Education\Base\EducationBaseBundle\Entity\User)->find(2544); | |
| // $oUser = $oUserRepo->findByMail($userMail); | |
| var_dump($oUserRepo); | |
| //var_dump($userMail ); | |
| //var_dump($oUser); | |
| exit(); | |
| $oUser | |
| ->setName($oRequest->request->get('name')) | |
| ->setMail($oRequest->request->get('mail')) | |
| ->setPhone($oRequest->request->get('phone')) | |
| ->setSurname($oRequest->request->get('surname')) | |
| ->setCountry($oRequest->request->get('country')); | |
| $oValidator = $this->get('validator'); | |
| $oValidatorErrors = $oValidator->validate($oUser); | |
| $success='false'; | |
| if(count($oValidatorErrors) === 0) { | |
| $em = $this->getDoctrine()->getManager(); | |
| $em->persist($oUser); | |
| $em->flush(); | |
| $success='true'; | |
| } | |
| return $this->get('fos_rest.view_handler') | |
| ->handle( | |
| View::create() | |
| ->setStatusCode(200) | |
| ->setData(array( | |
| 'form_fields' => $oRequest->request->all(), | |
| 'success' => $success, | |
| 'userID' => $oUser->getId() | |
| )) | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment