/**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     $array_projects = array(array('nom' => '315GLA', 'description' => 'Realisation site web', 'statut' => 1, 'nbrJEH' => 9, 'duree' => 5, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Gladiator Consulting', 'adresse' => '3 rue du chene noir', 'codePostal' => 33100, 'ville' => 'Toulouse', 'entite' => 2, 'email' => '*****@*****.**')), array('nom' => '316BLA', 'description' => 'Electronique avancee', 'statut' => 2, 'nbrJEH' => 5, 'duree' => 3, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Blackwater', 'adresse' => '1020 5th Avenue', 'codePostal' => 92200, 'ville' => 'Neuilly', 'entite' => 3, 'email' => '*****@*****.**')), array('nom' => '317IMU', 'description' => 'Design Base de donnes', 'statut' => 4, 'nbrJEH' => 8, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Imuka', 'adresse' => 'Kuruma San', 'codePostal' => 91000, 'ville' => 'Evry', 'entite' => 4, 'email' => '*****@*****.**')), array('nom' => '319UNI', 'description' => 'Conception Radar recul', 'statut' => 4, 'nbrJEH' => 12, 'duree' => 8, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Universal rad', 'adresse' => '2 rue Marie Curie', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 5, 'email' => '*****@*****.**')), array('nom' => '320TEK', 'description' => 'Refactorisation code Java', 'statut' => 2, 'nbrJEH' => 10, 'duree' => 8, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Teknik studio', 'adresse' => '10 impasse sunderland', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 6, 'email' => '*****@*****.**')), array('nom' => '321DUV', 'description' => 'Calcul de flux thermique', 'statut' => 2, 'nbrJEH' => 9, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Duvilcolor', 'adresse' => '600 la pyrennene ', 'codePostal' => 33100, 'ville' => 'Labege', 'entite' => 4, 'email' => '*****@*****.**')), array('nom' => '322NIL', 'description' => 'Application Android', 'statut' => 1, 'nbrJEH' => 8, 'duree' => 12, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Nilsen Industries', 'adresse' => '2 rue saint-louis', 'codePostal' => 31000, 'ville' => 'Bordeaux', 'entite' => 7, 'email' => '*****@*****.**')), array('nom' => '323PRR', 'description' => 'Etude de faisabilite', 'statut' => 3, 'nbrJEH' => 4, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'PRR', 'adresse' => 'PRR', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 4, 'email' => '*****@*****.**')));
     $inserted_projects = 0;
     $inserted_prospects = 0;
     foreach ($array_projects as $etude) {
         //create project
         $e = new Etude();
         $inserted_projects++;
         $mandat = rand(2013, 2016);
         $month = rand(1, 10);
         $day = rand(1, 30);
         $e->setMandat($mandat);
         $e->setNom($etude['nom']);
         $e->setDescription($etude['description']);
         $e->setDateCreation(new \DateTime($mandat . '-' . $month . '-' . $day));
         $e->setStateID($etude['statut']);
         $e->setAcompte(true);
         $e->setPourcentageAcompte(0.3);
         $e->setFraisDossier(90);
         $e->setPresentationProjet('Presentation ' . $etude['description']);
         $e->setDescriptionPrestation('Describe what we will do here');
         $e->setSourceDeProspection(rand(1, 10));
         $em->persist($e);
         $c = $em->getRepository('N7consultingRhBundle:Competence')->find(rand(1, 12));
         if ($c !== null) {
             $c->addEtude($e);
         }
         /** Prospect management */
         $p = new Prospect();
         $inserted_prospects++;
         $p->setNom($etude['prospect']['entreprise']);
         $p->setAdresse($etude['prospect']['adresse']);
         $p->setCodePostal($etude['prospect']['codePostal']);
         $p->setVille($etude['prospect']['ville']);
         $p->setEntite($etude['prospect']['entite']);
         $pe = new Personne();
         $pe->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
         //whitespace explode : not perfect but better than nothing
         $pe->setNom(self::NOM[array_rand(self::NOM)]);
         $pe->setEmailEstValide(true);
         $pe->setEstAbonneNewsletter(false);
         $pe->setEmail($etude['prospect']['email']);
         $pe->setAdresse($etude['prospect']['adresse']);
         $pe->setCodePostal($etude['prospect']['codePostal']);
         $pe->setVille($etude['prospect']['ville']);
         $emp = new Employe();
         $emp->setProspect($p);
         $p->addEmploye($emp);
         $emp->setPersonne($pe);
         $em->persist($emp->getPersonne());
         $em->persist($emp);
         $em->persist($p);
         $e->setProspect($p);
         //create phases
         $g = new GroupePhases();
         //default group
         $g->setTitre('Random generated' . rand());
         $g->setNumero(1);
         $g->setDescription('Automatic description');
         $g->setEtude($e);
         $em->persist($g);
         $ph = new Phase();
         $ph->setEtude($e);
         $ph->setGroupe($g);
         $ph->setPosition(0);
         $ph->setNbrJEH($etude['nbrJEH']);
         $ph->setPrixJEH(340);
         $ph->setTitre('Default phase');
         $ph->setDelai($etude['duree'] * 7);
         $ph->setDateDebut(new \DateTime($mandat . '-' . $month . '-' . $day));
         $em->persist($ph);
         //manage project manager
         $pm = new Personne();
         $pm->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
         $pm->setNom(self::NOM[array_rand(self::NOM)]);
         $pm->setEmailEstValide(false);
         $pm->setEstAbonneNewsletter(false);
         $em->persist($pm);
         $m = new Membre();
         $m->setPersonne($pm);
         $m->setPromotion($mandat + 2);
         $em->persist($m);
         if ($c !== null) {
             $c->addMembre($m);
         }
         $e->setSuiveur($pm);
         //manage AP & CC
         if ($etude['dateCC'] !== null && $etude['statut'] > 1) {
             $ap = new Ap();
             $ap->setEtude($e);
             $em->persist($ap);
             $cc = new Cc();
             $cc->setEtude($e);
             $cc->setDateSignature(new \DateTime($mandat . '-' . $month . '-' . $day));
             $cc->setSignataire2($pe);
             $em->persist($cc);
         }
         //manage intervenant
         if ($etude['statut'] > 1 && $etude['statut'] < 5) {
             //manage developper
             $dev = new Personne();
             $dev->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
             $dev->setNom(self::NOM[array_rand(self::NOM)]);
             $dev->setEmailEstValide(false);
             $dev->setEstAbonneNewsletter(false);
             $em->persist($dev);
             $mdev = new Membre();
             $mdev->setPersonne($dev);
             $mdev->setPromotion($mandat + rand(1, 2));
             $em->persist($mdev);
             if ($c !== null) {
                 $c->addMembre($mdev);
             }
             $mi = new Mission();
             $mi->setSignataire2($dev);
             $mi->setEtude($e);
             $mi->setDateSignature(new \DateTime($mandat . '-' . $month . '-' . $day));
             $mi->setDebutOm(new \DateTime($mandat . '-' . $month . '-' . $day));
             $mi->setAvancement(rand(90, 100));
             $mi->setIntervenant($mdev);
             $em->persist($mi);
         }
         //manage PVR
         if ($etude['statut'] > 1 && $etude['statut'] < 5) {
             $pv = new ProcesVerbal();
             $pv->setEtude($e);
             $pv->setDateSignature(new \DateTime($mandat . '-' . ($month + 1) . '-' . $day));
             $pv->setSignataire2($pe);
             $pv->setType('pvr');
             $em->persist($pv);
         }
     }
     $em->flush();
     $output->writeln('Done.');
 }
 /**
  * @Security("has_role('ROLE_SUIVEUR')")
  */
 public function modifierAction($id)
 {
     $em = $this->getDoctrine()->getManager();
     $documentManager = $this->get('Mgate.document_manager');
     if (!($membre = $em->getRepository('Mgate\\PersonneBundle\\Entity\\Membre')->find($id))) {
         $membre = new Membre();
         $now = new \DateTime('now');
         $now->modify('+ 3 year');
         $membre->setPromotion($now->format('Y'));
         $now = new \DateTime('now');
         $now->modify('- 20 year');
         $membre->setDateDeNaissance($now);
     }
     // Mail étudiant
     if (!$membre->getEmailEMSE()) {
         $email_etu_service = $this->container->get('Mgate.email_etu');
         $membre->setEmailEMSE($email_etu_service->getEmailEtu($membre));
     }
     $form = $this->createForm(new MembreType(), $membre);
     $deleteForm = $this->createDeleteForm($id);
     $mandatsToRemove = $membre->getMandats()->toArray();
     $form = $this->createForm(new MembreType(), $membre);
     if ($this->get('request')->getMethod() == 'POST') {
         $form->handleRequest($this->get('request'));
         $photoUpload = $form->get('photo')->getData();
         if ($form->isValid()) {
             if ($membre->getPersonne()) {
                 // Photo de l'étudiant
                 $path = $membre->getPromotion() . '/' . preg_replace('#[^a-zA-Z0-9ÁÀÂÄÉÈÊËÍÌÎÏÓÒÔÖÚÙÛÜáàâäéèêëíìîïóòôöúùûüÇç\\-_]#', '_', mb_strtolower($membre->getPersonne()->getNom(), 'UTF-8')) . '_' . preg_replace('#[^a-zA-Z0-9ÁÀÂÄÉÈÊËÍÌÎÏÓÒÔÖÚÙÛÜáàâäéèêëíìîïóòôöúùûüÇç\\-_]#', '_', mb_strtolower($membre->getPersonne()->getPrenom(), 'UTF-8'));
             } else {
                 $path = '';
             }
             $promo = $membre->getPromotion();
             /*
              * Traitement de l'image de profil
              */
             if ($membre->getPersonne()) {
                 $authorizedMIMEType = array('image/jpeg', 'image/png', 'image/bmp');
                 $photoInformation = new RelatedDocument();
                 $photoInformation->setMembre($membre);
                 $name = 'Photo - ' . $membre->getIdentifiant() . ' - ' . $membre->getPersonne()->getPrenomNom();
                 if ($photoUpload) {
                     $document = $documentManager->uploadDocumentFromFile($photoUpload, $authorizedMIMEType, $name, $photoInformation, true);
                     $membre->setPhotoURI($document->getWebPath());
                 } elseif (!$membre->getPhotoURI() && $promo !== null && $membre->getPersonne()) {
                     // Spécifique EMSE
                     $ressourceURL = 'http://ismin.emse.fr/ismin/Photos/P' . urlencode($path);
                     $headers = get_headers($ressourceURL);
                     if (preg_match('#200#', $headers[0])) {
                         $document = $documentManager->uploadDocumentFromUrl($ressourceURL, $authorizedMIMEType, $name, $photoInformation, true);
                         $membre->setPhotoURI($document->getWebPath());
                     }
                 }
             }
             /*
              * Traitement des postes
              */
             if ($this->get('request')->get('add')) {
                 $mandatNew = new Mandat();
                 $poste = $em->getRepository('Mgate\\PersonneBundle\\Entity\\Poste')->findOneBy(array('intitule' => 'Membre'));
                 $dt = new \DateTime('now');
                 $dtl = clone $dt;
                 $dtl->modify('+1 year');
                 if ($poste) {
                     $mandatNew->setPoste($poste);
                 }
                 $mandatNew->setMembre($membre);
                 $mandatNew->setDebutMandat($dt);
                 $mandatNew->setFinMandat($dtl);
                 $membre->addMandat($mandatNew);
             }
             if (!$membre->getIdentifiant()) {
                 $initial = substr($membre->getPersonne()->getPrenom(), 0, 1) . substr($membre->getPersonne()->getNom(), 0, 1);
                 $ident = count($em->getRepository('Mgate\\PersonneBundle\\Entity\\Membre')->findBy(array('identifiant' => $initial))) + 1;
                 while ($em->getRepository('Mgate\\PersonneBundle\\Entity\\Membre')->findOneBy(array('identifiant' => $initial . $ident))) {
                     ++$ident;
                 }
                 $membre->setIdentifiant(strtoupper($initial . $ident));
             }
             if (isset($now)) {
                 // Si c'est un nouveau membre et qu'on ajoute un poste
                 $em->persist($membre);
                 $em->flush();
                 return $this->redirect($this->generateUrl('MgatePersonne_membre_modifier', array('id' => $membre->getId())));
             }
             // Suppression des mandat à supprimer
             //Recherche des mandats supprimés
             foreach ($membre->getMandats() as $mandat) {
                 $key = array_search($mandat, $mandatsToRemove);
                 if ($key !== false) {
                     array_splice($mandatsToRemove, $key, 1);
                 }
             }
             //Supression de la BDD
             foreach ($mandatsToRemove as $mandat) {
                 $em->remove($mandat);
             }
             $em->persist($membre);
             // persist $etude / $form->getData()
             $em->flush();
             $form = $this->createForm(new MembreType(), $membre);
         }
     }
     return $this->render('MgatePersonneBundle:Membre:modifier.html.twig', array('form' => $form->createView(), 'delete_form' => $deleteForm->createView(), 'photoURI' => $membre->getPhotoURI()));
 }