<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-06-27 20:23:57
 */
 

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\Form\ElementType;
use Biopen\GeoDirectoryBundle\Document\ElementProduct;
use Biopen\GeoDirectoryBundle\Document\Product;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Wantlet\ORM\Point;
use Biopen\GeoDirectoryBundle\Classes\ContactAmap;
use joshtronic\LoremIpsum;

use MongoClient;

class APIController extends Controller
{
    public function getElementsInBoundsAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $em = $this->get('doctrine_mongodb')->getManager();

            $boxes = [];
            $bounds = explode( ';' , $request->get('bounds'));
            foreach ($bounds as $key => $bound) 
            {
              $boxes[] = explode( ',' , $bound);
            }
            $elementRepo = $em->getRepository('BiopenGeoDirectoryBundle:Element');
            //dump('fullRepresentation' . $request->get('fullRepresentation'));
            $elementsFromDB = $elementRepo->findWhithinBoxes($boxes, $request->get('mainOptionId'), $request->get('fullRepresentation')); 
    
            $responseJson = $this->encoreArrayToJsonArray($elementsFromDB, $request->get('fullRepresentation'));  
            //dump($responseJson);
            $result = new Response($responseJson);   

            $result->headers->set('Content-Type', 'application/json');
            return $result;
        }
        else 
        {
            return new Response("Not valid ajax request");
        }
    }

    private function encoreArrayToJsonArray($array, $fullRepresentation)
    {
        $elementsJson = '['; 

        foreach ($array as $key => $value) 
        { 
           if ($fullRepresentation == 'true') $elementsJson .= $value['fullJson'] . ',';  
           else $elementsJson .= $value['compactJson'] .  ','; 
        }   

        $elementsJson = rtrim($elementsJson,",") . ']';    
        $responseJson = '{ "data":'. $elementsJson . ', "fullRepresentation" : '. $fullRepresentation .'}';

        return $responseJson;
    }

    public function getElementByIdAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $elementId = $request->get('elementId');

            $em = $this->get('doctrine_mongodb')->getManager();
            
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')
            ->findOneBy(array('id' => $elementId));

            $elementJson = $element->getFullJson();
            
            $response = new Response($elementJson);    
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
        else 
        {
            return new Response("Not valid ajax request");
        }
    }

    public function getTaxonomyAction(Request $request)
    {
        $em = $this->get('doctrine_mongodb')->getManager();
        
        $taxonomy = $em->getRepository('BiopenGeoDirectoryBundle:Taxonomy')->findMainCategoryJson();

        $response = new Response($taxonomy);    
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }

    public function getElementsFromTextAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $em = $this->get('doctrine_mongodb')->getManager();
            
            $elements = $em->getRepository('BiopenGeoDirectoryBundle:Element')
            ->findElementsWithText($request->get('text'));

            $responseJson = $this->encoreArrayToJsonArray($elements, true);
            
            $response = new Response($responseJson);    
            $response->headers->set('Content-Type', 'application/json');
            return $response;<commit_msg>Prevent taxonomy to be downladed via browser<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-07-13 08:58:08
 */
 

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\Form\ElementType;
use Biopen\GeoDirectoryBundle\Document\ElementProduct;
use Biopen\GeoDirectoryBundle\Document\Product;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Wantlet\ORM\Point;
use Biopen\GeoDirectoryBundle\Classes\ContactAmap;
use joshtronic\LoremIpsum;

use MongoClient;

class APIController extends Controller
{
    public function getElementsInBoundsAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $em = $this->get('doctrine_mongodb')->getManager();

            $boxes = [];
            $bounds = explode( ';' , $request->get('bounds'));
            foreach ($bounds as $key => $bound) 
            {
              $boxes[] = explode( ',' , $bound);
            }
            $elementRepo = $em->getRepository('BiopenGeoDirectoryBundle:Element');
            //dump('fullRepresentation' . $request->get('fullRepresentation'));
            $elementsFromDB = $elementRepo->findWhithinBoxes($boxes, $request->get('mainOptionId'), $request->get('fullRepresentation')); 
    
            $responseJson = $this->encoreArrayToJsonArray($elementsFromDB, $request->get('fullRepresentation'));  
            //dump($responseJson);
            $result = new Response($responseJson);   

            $result->headers->set('Content-Type', 'application/json');
            return $result;
        }
        else 
        {
            return new Response("Not valid ajax request");
        }
    }

    private function encoreArrayToJsonArray($array, $fullRepresentation)
    {
        $elementsJson = '['; 

        foreach ($array as $key => $value) 
        { 
           if ($fullRepresentation == 'true') $elementsJson .= $value['fullJson'] . ',';  
           else $elementsJson .= $value['compactJson'] .  ','; 
        }   

        $elementsJson = rtrim($elementsJson,",") . ']';    
        $responseJson = '{ "data":'. $elementsJson . ', "fullRepresentation" : '. $fullRepresentation .'}';

        return $responseJson;
    }

    public function getElementByIdAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $elementId = $request->get('elementId');

            $em = $this->get('doctrine_mongodb')->getManager();
            
            $element = $em->getRepository('BiopenGeoDirectoryBundle:Element')
            ->findOneBy(array('id' => $elementId));

            $elementJson = $element->getFullJson();
            
            $response = new Response($elementJson);    
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
        else 
        {
            return new Response("Not valid ajax request");
        }
    }

    public function getTaxonomyAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $em = $this->get('doctrine_mongodb')->getManager();
            
            $taxonomy = $em->getRepository('BiopenGeoDirectoryBundle:Taxonomy')->findMainCategoryJson();

            $response = new Response($taxonomy);    
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
        else 
        {
            return new Response("Not valid ajax request");
        }
    }

    public function getElementsFromTextAction(Request $request)
    {
        if($request->isXmlHttpRequest())
        {
            $em = $this->get('doctrine_mongodb')->getManager();
            
            $elements = $em->getRepository('BiopenGeoDirectoryBundle:Element')
            ->findElementsWithText($request->get('text'));

            $responseJson = $this->encoreArrayToJsonArray($elements, true);
            
            $response = new Response($responseJson);    
            $response->headers->set('Content-Type', 'application/json');
            return $response;
{'fraction_non_alphanumeric': 0.13581799474105408, 'fraction_numerical': 0.004115696810334972, 'mean_word_length': 2.5316915623738394, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 88, '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': '3137159', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2306, 'n_words': 413}