<commit_before><?php

/*
 * This file is part of the Incipio package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mgate\PersonneBundle\Controller;

use Mgate\PersonneBundle\Entity\Mandat;
use Mgate\PersonneBundle\Entity\Membre;
use Mgate\PersonneBundle\Form\Type\MembreType;
use Mgate\PubliBundle\Entity\RelatedDocument;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Request;

class MembreController extends Controller
{
    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MgatePersonneBundle:Membre')->getMembres();

        return $this->render('MgatePersonneBundle:Membre:index.html.twig', array(
                    'membres' => $entities,
                ));
    }

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function listIntervenantsAction()
    {
        $em = $this->getDoctrine()->getManager();
        $intervenants = $em->getRepository('MgatePersonneBundle:Membre')->getByMissionsNonNul();

        return $this->render('MgatePersonneBundle:Membre:indexIntervenants.html.twig', array(
                    'intervenants' => $intervenants,
                ));
    }

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function statistiqueAction($page)
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MgatePersonneBundle:Membre')->findAll();

        $membresActifs = array();
        foreach ($entities as $membre) {
            foreach ($membre->getMandats() as $mandat) {
                if ($mandat->getPoste()->getIntitule() == 'Membre' && $mandat->getDebutMandat() < new \DateTime('now') && $mandat->getFinMandat() > new \DateTime('now')) {
                    $membresActifs[] = $membre;
                }
            }
        }

        return $this->render('MgatePersonneBundle:Membre:index.html.twig', array(
                    'membres' => $membresActifs,
                ));
    }

    /**
     * @Security("has_role('ROLE_ELEVE')")
     */
    public function voirAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MgatePersonneBundle:Membre')->getMembreCompetences($id);

        if (!$entity) {
            throw $this->createNotFoundException('Le membre demandé n\'existe pas !');
        }

        return $this->render('MgatePersonneBundle:Membre:voir.html.twig', array(
                    'membre' => $entity,
        ));
    }

    /*
     * Ajout ET modification des membres (create if membre not existe )
     */

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function modifierAction(Request $request, $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(MembreType::class, $membre);
        $deleteForm = $this->createDeleteForm($id);

        $mandatsToRemove = $membre->getMandats()->toArray();

        $form = $this->createForm(MembreType::class, $membre);

        if ($request->getMethod() == 'POST') {
            $form->handleRequest($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 ($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);<commit_msg>Remove specific EMSE member image handling Fix #41<commit_after><?php

/*
 * This file is part of the Incipio package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mgate\PersonneBundle\Controller;

use Mgate\PersonneBundle\Entity\Mandat;
use Mgate\PersonneBundle\Entity\Membre;
use Mgate\PersonneBundle\Form\Type\MembreType;
use Mgate\PubliBundle\Entity\RelatedDocument;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Request;

class MembreController extends Controller
{
    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MgatePersonneBundle:Membre')->getMembres();

        return $this->render('MgatePersonneBundle:Membre:index.html.twig', array(
                    'membres' => $entities,
                ));
    }

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function listIntervenantsAction()
    {
        $em = $this->getDoctrine()->getManager();
        $intervenants = $em->getRepository('MgatePersonneBundle:Membre')->getByMissionsNonNul();

        return $this->render('MgatePersonneBundle:Membre:indexIntervenants.html.twig', array(
                    'intervenants' => $intervenants,
                ));
    }

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function statistiqueAction($page)
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MgatePersonneBundle:Membre')->findAll();

        $membresActifs = array();
        foreach ($entities as $membre) {
            foreach ($membre->getMandats() as $mandat) {
                if ($mandat->getPoste()->getIntitule() == 'Membre' && $mandat->getDebutMandat() < new \DateTime('now') && $mandat->getFinMandat() > new \DateTime('now')) {
                    $membresActifs[] = $membre;
                }
            }
        }

        return $this->render('MgatePersonneBundle:Membre:index.html.twig', array(
                    'membres' => $membresActifs,
                ));
    }

    /**
     * @Security("has_role('ROLE_ELEVE')")
     */
    public function voirAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MgatePersonneBundle:Membre')->getMembreCompetences($id);

        if (!$entity) {
            throw $this->createNotFoundException('Le membre demandé n\'existe pas !');
        }

        return $this->render('MgatePersonneBundle:Membre:voir.html.twig', array(
                    'membre' => $entity,
        ));
    }

    /*
     * Ajout ET modification des membres (create if membre not existe )
     */

    /**
     * @Security("has_role('ROLE_SUIVEUR')")
     */
    public function modifierAction(Request $request, $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(MembreType::class, $membre);
        $deleteForm = $this->createDeleteForm($id);

        $mandatsToRemove = $membre->getMandats()->toArray();

        $form = $this->createForm(MembreType::class, $membre);

        if ($request->getMethod() == 'POST') {
            $form->handleRequest($request);
            $photoUpload = $form->get('photo')->getData();

            if ($form->isValid()) {

                /*
                 * 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());
                    }
                }

                /*
                 * Traitement des postes
                 */
                if ($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);
{'fraction_non_alphanumeric': 0.15233608166470358, 'fraction_numerical': 0.0015704750687082843, 'mean_word_length': 1.975005839757066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4623994', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3689, 'n_words': 546}