/** * @Security("has_role('ROLE_ADMIN')") */ public function importAction() { /* Handle form. */ $import = new BlacklistImport(); $form = $this->createForm(new BlacklistImportType(), $import); $form->handleRequest($this->get('request')); /* Insert and redirect or show the form. */ if ($form->isValid()) { $fileContent = file_get_contents($import->getFile()->getPathName()); $rules = json_decode($fileContent, true); if ($rules) { $em = $this->getDoctrine()->getManager(); foreach ($rules as $rule) { $ruleObj = new BlacklistRule(); $ruleObj->setProfile($import->getProfile()); $ruleObj->setPath($rule['path']); $ruleObj->setCaller(str_replace('{BASE}', $import->getBase(), $rule['caller'])); $ruleObj->setThreshold($rule['threshold']); $ruleObj->setStatus(3); $em->persist($ruleObj); } $em->flush(); $this->get('session')->getFlashBag()->add('info', 'The rules were imported.'); } else { $this->get('session')->getFlashBag()->add('alert', 'Invalid file.'); } return $this->redirect($this->generateUrl('swd_analyzer_blacklist_rules')); } else { /* Render template. */ return $this->render('SwdAnalyzerBundle:Blacklist:import.html.twig', array('form' => $form->createView())); } }