/**
  * Imports Utilisateur entities.
  * Same method explained in CoursController.php
  * Parse CSV file
  * The CSV cours structure was defined with Christophe Widmer (please contact him for CSV examples and notices)
  * This one is a bit more complex
  * WARNING : each column has its own role, dont blend it
  *
  */
 public function importAction(Request $request)
 {
     $parser = new Csv();
     $type = 'text/plain';
     $ext = 'csv';
     $path = __DIR__ . '/../../../../web/uploads/csv';
     $em = $this->getDoctrine()->getManager();
     $form = $this->createForm(new CsvType());
     if ($request->getMethod() == 'POST') {
         $form->bind($request);
         if ($form->isValid()) {
             $data = $form->getData();
             $file = $data['csv']->move($path, 'temp.csv');
             $parser->set($path, 'temp.csv');
             $rows = $parser->parseCsv();
             $length = count($rows);
             foreach ($rows as $row) {
                 $statement = array();
                 foreach ($row as $cell) {
                     $cell = trim($cell);
                     $statement[] = empty($cell);
                 }
                 if (in_array(true, $statement)) {
                     $length--;
                 } else {
                     $role = $em->getRepository('AppCoreBundle:Role')->findOneBy(array('name' => $row[4]));
                     if (!empty($role)) {
                         $entity = new Utilisateur();
                         $entity->setLastname($row[0]);
                         $entity->setFirstname($row[1]);
                         $entity->setUsername($row[2]);
                         $entity->setEmail($row[3]);
                         $entity->setRole($role);
                         $entity->setPassword($row[5]);
                         $factory = $this->get('security.encoder_factory');
                         $encoder = $factory->getEncoder($entity);
                         $entity->setSalt(md5(time()));
                         $entity->setPassword($encoder->encodePassword($entity->getPassword(), $entity->getSalt()));
                         $validator = $this->get('validator');
                         if (!count($validator->validate($entity))) {
                             $em->persist($entity);
                         } else {
                             $length--;
                         }
                     } else {
                         $length--;
                     }
                 }
             }
             $em->flush();
             if (file_exists($path . '/temp.csv')) {
                 unlink($path . '/temp.csv');
             }
             $this->get('session')->getFlashBag()->add('success', $length . ' utilisateurs ont été ajoutés !');
             return $this->redirect($this->generateUrl('utilisateur_import'));
         }
     }
     return $this->render('AppCoreBundle:Utilisateur:import.html.twig', array('headline' => 'Utilisateurs', 'title' => 'Importer des utilisateurs', 'form' => $form->createView()));
 }