<?php

namespace Cadoles\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Session\Session;

class SondeController extends Controller
{
    public function sondeAction()
    {
        // Debug sonde
        $fgdebug = true;

        // Calcul du login
        $user=$this->getUser();
        if($user) $loginsonde=crypt($user->getUsername(),"dkywqZPYNHtWDnSxACjXVcIsFuaiQT");
        else $loginsonde="";

        // Calcul du profil
        $profilsonde="visiteur";
        if($user) {
            $profilsonde="utilisateur";
            $attributes=$this->get('session')->get('attributes');
            if(isset($attributes["ENTPersonProfils"])) $profilsonde=$attributes["ENTPersonProfils"];
        }

        // Calcul de la provenance
        $e = explode('.', $_SERVER["REMOTE_ADDR"]);
        $s = sizeof($e);
        if(($e[0]=="172"&&$e[1]=="30")||($e[0]=="172"&&$e[1]=="16")||($e[0]=="10"&&($e[1]=="77"||$e[1]=="93"||$e[1]=="94")))
            $provenancesonde='interne';
        else
            $provenancesonde='externe';      
            
        // Calcul du service associé à la page
        $request = $this->get('request_stack')->getMasterRequest();
        $usage=$request->query->get('usage');
        $route = $request->attributes->get('_route');

        if($route=="cadoles_core_user_file_list"||$route=="cadoles_core_home")
            $usagesonde="nosonde";
        elseif($route=="cadoles_portal_user_page_view") {
            if($usage=="group") $usagesonde="collaboratif";
            elseif($usage=="user")  $usagesonde="collaboratif";
            elseif($usage=="accueil")  $usagesonde="accueil";
            else $usagesonde="portail";
        }
        elseif(stripos($route,"cadoles_portal_user_calendar")!==false)
            $usagesonde="calendrier";
        elseif(stripos($route,"cadoles_portal_user_project")!==false)
            $usagesonde="calendrier";
        elseif(stripos($route,"cadoles_portal_user_blog")!==false)
            $usagesonde="collaboratif";
        elseif(stripos($route,"cadoles_core_user_file")!==false)
            $usagesonde="fichier";
        else
            $usagesonde="portail";

        
        $urlsonde="";
        if($usagesonde!="nosonde") {
            $url=$this->getParameter("widsonde_url");
            $app=$this->getParameter("alias");
            $urlsonde = $url."?appli=$app&profil=$profilsonde&login=$loginsonde&provenance=$provenancesonde&usage=$usagesonde&route=$route&fgdebug=$fgdebug";
        }

        return $this->render('CadolesCoreBundle:Sonde:sonde.html.twig',['urlsonde' => $urlsonde]);
    }


}
