<commit_before><?php

/**  
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) 2016 <NAME> - <EMAIL>
 * @license    MIT License
 * @Last Modified time: 2017-11-29 13:41:32
 */
 

namespace Biopen\GeoDirectoryBundle\Controller;


use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\UserInteractionReport;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


class ElementInteractionController extends Controller
{
    public function voteAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('vote', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à voter !");

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || $request->get('value') === null) 
                return $this->returnResponse(false,"Les paramètres du vote sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 

            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $resultMessage = $this->get('biopen.element_vote_service')
                             ->voteForElement($element, $request->get('value'), $request->get('comment'), $request->get('userMail'));
         
            return $this->returnResponse(true, $resultMessage, $element->getstatus());     
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function reportErrorAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {              
            if (!$this->container->get('biopen.config_service')->isUserAllowed('report', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à signaler d'erreurs !");
            
            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || $request->get('value') === null || !$request->get('userMail')) 
                return $this->returnResponse(false,"Les paramètres du signalement sont incomplets");
            
            $em = $this->get('doctrine_mongodb')->getManager(); 

            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId')); 

            $report = new UserInteractionReport();    
            $report->setValue($request->get('value'));
            $report->updateUserInformation($this->container->get('security.context'), $request->get('userMail'));

            $comment = $request->get('comment');
            if ($comment) $report->setComment($comment);
            
            $element->addReport($report);
            
            $em->persist($element);
            $em->flush();
         
            return $this->returnResponse(true, "Merci, votre signalement a bien été enregistré !");        
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function deleteAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('delete', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à supprimer un élément !"); 

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId')) 
                return $this->returnResponse(false,"Les paramètres sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $elementActionService = $this->container->get('biopen.element_action_service');
            $elementActionService->delete($element, true, $request->get('message'));

            $em->persist($element);
            $em->flush();
         
            return $this->returnResponse(true, "L'élément a bien été supprimé");        
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function sendMailAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('sendMail', $request))
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à envoyer des mails !"); 

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || !$request->get('subject') || !$request->get('content') || !$request->get('userMail')) 
                return $this->returnResponse(false,"Les paramètres sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $securityContext = $this->container->get('security.context');
            $user = $securityContext->getToken()->getUser(); 

            $senderMail = $request->get('userMail');
            if ($user && $user->isAdmin()) $senderMail = "<EMAIL>"; // TODO replace by gogoconfig contactMail field

            // TODO make it configurable
            $mailSubject = 'Message reçu depuis la plateform "Près de Chez Nous"';
            $mailContent = 
                "Bonjour <i>" . $element->getName() . '</i>,</br></br>
                Vous avez reçu un message de la part de <a href="mailto:' . $senderMail . '">' . $senderMail . "</a></br>
                </br>
                <b>Titre du message</b></br> " . $request->get('subject') . "</br></br>
                <b>Contenu :</b></br> " . $request->get('content');<commit_msg>Fixs send mail<commit_after><?php

/**  
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) 2016 <NAME> - <EMAIL>
 * @license    MIT License
 * @Last Modified time: 2017-11-30 13:32:44
 */
 

namespace Biopen\GeoDirectoryBundle\Controller;


use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\UserInteractionReport;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


class ElementInteractionController extends Controller
{
    public function voteAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('vote', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à voter !");

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || $request->get('value') === null) 
                return $this->returnResponse(false,"Les paramètres du vote sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 

            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $resultMessage = $this->get('biopen.element_vote_service')
                             ->voteForElement($element, $request->get('value'), $request->get('comment'), $request->get('userMail'));
         
            return $this->returnResponse(true, $resultMessage, $element->getstatus());     
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function reportErrorAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {              
            if (!$this->container->get('biopen.config_service')->isUserAllowed('report', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à signaler d'erreurs !");
            
            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || $request->get('value') === null || !$request->get('userMail')) 
                return $this->returnResponse(false,"Les paramètres du signalement sont incomplets");
            
            $em = $this->get('doctrine_mongodb')->getManager(); 

            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId')); 

            $report = new UserInteractionReport();    
            $report->setValue($request->get('value'));
            $report->updateUserInformation($this->container->get('security.context'), $request->get('userMail'));

            $comment = $request->get('comment');
            if ($comment) $report->setComment($comment);
            
            $element->addReport($report);
            
            $em->persist($element);
            $em->flush();
         
            return $this->returnResponse(true, "Merci, votre signalement a bien été enregistré !");        
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function deleteAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('delete', $request)) 
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à supprimer un élément !"); 

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId')) 
                return $this->returnResponse(false,"Les paramètres sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $elementActionService = $this->container->get('biopen.element_action_service');
            $elementActionService->delete($element, true, $request->get('message'));

            $em->persist($element);
            $em->flush();
         
            return $this->returnResponse(true, "L'élément a bien été supprimé");        
        }
        else 
        {
            return new JsonResponse("Not valid ajax request");
        }
    }

    public function sendMailAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            if (!$this->container->get('biopen.config_service')->isUserAllowed('sendMail', $request))
                return $this->returnResponse(false,"Désolé, vous n'êtes pas autorisé à envoyer des mails !"); 

            // CHECK REQUEST IS VALID
            if (!$request->get('elementId') || !$request->get('subject') || !$request->get('content') || !$request->get('userMail')) 
                return $this->returnResponse(false,"Les paramètres sont incomplets");

            $em = $this->get('doctrine_mongodb')->getManager(); 
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')->find($request->get('elementId'));           

            $securityContext = $this->container->get('security.context');
            $user = $securityContext->getToken()->getUser(); 

            $senderMail = $request->get('userMail');
            //if ($user && $user->isAdmin()) $senderMail = "<EMAIL>"; // TODO replace by gogoconfig contactMail field

            // TODO make it configurable
            $mailSubject = 'Message reçu depuis la plateforme "Près de Chez Nous"';
            $mailContent = 
                "Bonjour <i>" . $element->getName() . '</i>,</br></br>
                Vous avez reçu un message de la part de <a href="mailto:' . $senderMail . '">' . $senderMail . "</a></br>
                </br>
                <b>Titre du message</b></br> " . $request->get('subject') . "</br></br>
                <b>Contenu :</b></br> " . $request->get('content');
{'fraction_non_alphanumeric': 0.15008130081300813, 'fraction_numerical': 0.002926829268292683, 'mean_word_length': 2.471916454981654, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 219, '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': '260566', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3243, 'n_words': 645}