/** * Just to quickly seed the DB */ public function storeLogfileAction(Request $request) { // Truncate tables $em = $this->getDoctrine()->getManager(); $connection = $em->getConnection(); $tables = ['Note', 'Log', 'Card', 'Employee']; $query = 'SET foreign_key_checks = 0;'; foreach ($tables as $table) { $query .= 'TRUNCATE TABLE ' . $table . ';'; } $connection->executeQuery($query); $em->flush(); // Seed Tables $faker = Faker::create(); $csvLog = $request->files->get('log'); $csvLog = file_get_contents($csvLog->getPathName()); $csvLog = explode("\n", $csvLog); $cards = []; $logs = []; foreach ($csvLog as $line) { $entry = str_getcsv($line, ';'); $logs[] = $entry; if (!in_array($entry[1], $cards)) { $cards[] = $entry[1]; } } foreach ($cards as $cardSignature) { $employee = new Employee(); $employee->setFirstName($faker->firstName); $employee->setLastName($faker->lastName); $em->persist($employee); $card = new Card(); $card->setSignature($cardSignature); $card->setEmployee($employee); $card->setIsActive(true); $em->persist($card); foreach ($logs as $log) { if ($log[1] == $cardSignature) { $date = DateTime::createFromFormat('d.m.Y H:i', $log[2]); $newLog = new Log(); $newLog->setCardSignature($cardSignature); $newLog->setTime($date); $newLog->setIsEdited(false); $newLog->setCard($card); $em->persist($newLog); } } } $em->flush(); return $this->redirect($this->generateUrl('employee')); }
/** * @Route("/api/cardreader") * @Method("GET") */ public function storeAction(Request $request) { $signature = $request->query->get('id'); $em = $this->getDoctrine()->getManager(); $card = $em->getRepository('TimetrackerBundle:Card')->findOneBy(['signature' => $signature]); $response = new Response(''); if (!$card) { $response->headers->set('X-Return', '5'); $card = new Card(); $card->setSignature($signature); $em->persist($card); } else { $response->headers->set('X-Return', '2'); } $newLog = new Log(); $newLog->setTime(new \DateTime('now')); $newLog->setCard($card); $newLog->setIsEdited(false); $em->persist($newLog); $em->flush(); return $response; }