public function newAlertAction() { $result = array('success' => false); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $data = get_object_vars(json_decode(file_get_contents("php://input"))); // Validate received datas if ($this->validateNewAlert($data)) { $alerteToAdd = new Alert(); /// timestamps $dtStr = date("c", $data['timestamp_current']); $date = new \DateTime($dtStr); $alerteToAdd->setDatetimeSent($date); $dtStr = date("c", $data['timestamp_position']); $date = new \DateTime($dtStr); $alerteToAdd->setDatetimeReceived($date); // location $alerteToAdd->setLongPos($data['longitude']); $alerteToAdd->setLatPos($data['latitude']); // user $newUserToAdd = new PublicUser(); $newUserToAdd->setFirstName($data['firstname']); $newUserToAdd->setLastName($data['lastname']); $newUserToAdd->setPhone($data['phone_number']); $newUserToAdd->setGenre($data['gender']); if (is_integer(intval($data['age']))) { $newUserToAdd->setAge(intval($data['age'])); } else { $newUserToAdd->setAge(null); } $em = $this->getDoctrine()->getManager(); $em->persist($newUserToAdd); $em->flush(); // Default alert level $alertType = $this->getDoctrine()->getRepository('UrgenceBundle:AlertType')->find(1); $alerteToAdd->setAlertType($alertType); $severity = $this->getDoctrine()->getRepository('UrgenceBundle:Severity')->find(3); $alerteToAdd->setSeverity($severity); $alerteToAdd->setInfo($data['drive_link']); $alerteToAdd->setPublicUser($newUserToAdd); // add new datas $em = $this->getDoctrine()->getManager(); $em->persist($alerteToAdd); $em->flush(); $result['success'] = true; $result['message'] = "Alert saved"; } else { $result['message'] = "Wrond datas"; } } else { $result['message'] = "Wrong request"; } return new JsonResponse($result); }
/** * Basic route for testing adding information into the database * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function insertAction(Request $request) { $user = new PublicUser(); $user->setFirstName(); $alert = new Alert(); $severity = new Severity(); $severity->setName("name1"); $alerttype = new \UrgenceBundle\Entity\AlertType(); $alerttype->setName('name2'); $alert->setInfo("Info"); $alert->setDatetimeReceived(new \DateTime()); $alert->setDatetimeSent(new \DateTime()); $alert->setIdParse('idParse'); $alert->setLatPos("46.891296"); $alert->setLongPos("-0.930691"); $alert->setSeverity($severity); $alert->setAlertType($alerttype); $em = $this->getDoctrine()->getManager(); $em->persist($alert); $em->flush(); return $this->render('UrgenceBundle:Form:add.html.twig', array()); }