<?php

use Website\Controller\Action;

class TestController extends Action {

    public function designServicesBAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/new-services.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/hover.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function designServicesBNlAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/new-services.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/hover.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function designUsersAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/home-agency.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function designUsersNlAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/home-agency.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function newsFrAction(){}

    public function newsNlAction(){}

    public function menuxAction()
    {
        
    }

    public function getName($contenu){
        $r_name = '/<meta property=\"og:title\" content=\".*\"\\/>/';
        preg_match($r_name, $contenu, $arr_h2);

        // <meta property="og:title" content=" Brussels I Sotheby's International Realty- Shelterr"/>
        $name = substr($arr_h2[0], strlen('<meta property="og:title" content="'));
        $name = trim(substr($name, 0, strpos($name, '- Shelterr')));

        return $name;
    }

    public function getFB($contenu){
        $r_facebook     = "/https:\\/\\/[w\\.]*facebook.com\\/[^\"]*/";

        preg_match($r_facebook,$contenu,$arr_fb);
        $facebook = strpos($arr_fb[0], 'shelterreurope')?'':$arr_fb[0];

        return $facebook;

    }

    public function getTw($contenu){
        $r_twitter      = "/https:\\/\\/[w\\.]*twitter.com\\/[^\"]*/";

        preg_match($r_twitter,$contenu, $arr_tw);
        $twitter = strpos($arr_tw[0], 'shelterreurope')?'':$arr_tw[0];

        return $twitter;
    }

    public function getLi($contenu){
        //tous 5025012 ??

        $r_linkedin     = "/https:\\/\\/[w\\.]*linkedin.com\\/[^\"]*/";

        preg_match($r_linkedin,$contenu, $arr_li);
        $linkedin = strpos($arr_li[0], '5268095')?'':$arr_li[0];

        return $linkedin;
    }

    public function getDescription($contenu){
        $r_name = '/<meta property=\"og:description\" content=\"[^\\/]*/s';
        preg_match($r_name, $contenu, $arr_h2);

        // <meta property="og:title" content=" Brussels I Sotheby's International Realty- Shelterr"/>
        $txt = substr($arr_h2[0], strlen('<meta property="og:description" content="'));
        $txt = trim(substr($txt, 0, strlen($txt)-1));

        return $txt;

    }

    public function getImageUrl($contenu){{
        $r_name = '/<meta property=\"og:image\" content=\"[^\"]*/s';
        preg_match($r_name, $contenu, $arr_h2);

        // <meta property="og:image" content="https://www.shelterr.com/data/images/143067/mediabrowser_Logo.1411314757.jpg"/>
        $txt = substr($arr_h2[0], strlen('<meta property="og:image" content="'));

        return $txt;
    }

    }

    public function getPhone($contenu){
        $r_name = '/<ul class=\"dropdown-menu dropdown-phone\" role=\"menu\">.<li><a href=\"[^\"]+/s';
        preg_match($r_name, $contenu, $arr_h2);
        $txt = substr($arr_h2[0], 67);

        $txt = preg_replace('/[a-z:]/s','', $txt);

        return $txt;
    }

    public function getWebsite($contenu)
    {
        $r_name = '/<ul class=\"dropdown-menu dropdown-website\" role=\"menu\">.<li><a href=\"[^\"]*/s';
        preg_match($r_name, $contenu, $arr_h2);
        $txt = substr($arr_h2[0], 69);

        return $txt;
    }

    public function suceurAction(){
        ini_set('memory_limit', '-1');
        //$content = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/liste-agences-shelter');

        echo 'id;uid;name;website;facebook;twitter;linkedin;short_description;long_description;phone<br>';

        $files = scandir($_SERVER['DOCUMENT_ROOT'] .'/dl/');
        $i = 0;
        foreach($files as $file){
            if ($i == 1){
                if ($file != '.' && $file != ".."){
                    $contenu = file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/dl/' . $file);

                    $uid = str_replace('.', '', uniqid("", true));


                    $dir = $_SERVER['DOCUMENT_ROOT'] . '/logos/' . $uid;
                    $url = $this->getImageUrl($contenu);

                    if (! is_dir($dir))mkdir($dir);
                    try{
                        //file_put_contents($dir.'/logo.png', file_get_contents($url));
                    }catch (\Exception $e){

                    }


                    echo ';' . $uid .' ; '. $this->getName($contenu). ' ; ' . $this->getWebsite($contenu). ' ; ' . $this->getFb($contenu). ' ; ' . $this->getTw($contenu). ' ; ' . $this->getLi($contenu). ' ;; ' . $this->getDescription($contenu). ';' . $this->getPhone($contenu) . '<br>';

                    $i += 1;
                }
            }

        }






//        $r_name         = "/<h2 id=\"pageTitle\" class=\"title col-xs-12 pull-left\" style='text-overflow: ellipsis; white-space: nowrap; overflow: hidden'>[^<]*/";

//        $r_facebook     = "/https:\\/\\/[w\\.]*facebook.com\\/[^\"]*/";
//        $r_twitter      = "/https:\\/\\/[w\\.]*twitter.com\\/[^\"]*/";
//        $r_linkedin     = "/https:\\/\\/[w\\.]*linkedin.com\\/[^\"]*/";
//        $r_website      = "/^<li><a href=\"[^\"]*/m";

//        preg_match($r_facebook,$content,$arr_fb);
//        $facebook = $arr_fb[0];

//        preg_match($r_twitter,$content, $arr_tw);
//        $twitter = $arr_tw[0];

//        preg_match($r_linkedin,$content, $arr_li);
//        $linkedin = $arr_li[0];

//        preg_match($r_website,$content, $arr_ws);
//        preg_match($r_name,$content, $arr_name);

//        $name = substr($arr_name[0], strpos($arr_name[0],'>')-1);


//        echo $arr_fb[0] . '<br>';
//        echo $arr_li[0] . '<br>';


//        echo $name[0] . '<br>';

//        echo $arr_ws[0] . '<br>';

    }

    public function testLanguageAction()
    {
        $this->view->path = '/' . $this->getParam('path');
    }

    public function testPageAction()
    {

    }

    public function addViewAction(){

    }

    public function apiAction()
    {
/*
        $secret = 'F6bSAMzI5rDPssrbj1yB';
        $survey =  '50ZXrHpr3u4BwyB5F371';

        $cNps = "How likely is it that you would recommend our agency to a friend, family or colleague ?
;";
        $cComment = "Leave us a comment, we'll be happy to read your opinion about our agency.
";
        $cCreateTime = "createtime";

        $message = '';
        try{


            $csv = new Csv_Reader($_SERVER['DOCUMENT_ROOT'] . "/tmp/" . $survey, new Csv_Dialect(array('delimiter' => ';')));

            $csv->setHeader($csv->current());

            //skip first line
            $csv->next();

            do{
                //var_dump($csv->current()[$cNps]); continue;
                if (($csv->current()[$cComment]) != ""){
                    echo $csv->current()[$cCreateTime] . ' <strong>' . $csv->current()[$cNps] . '/10</strong><br>';
                    echo $csv->current()[$cComment] . '<hr>';

                }
            }while($csv->next());

        }catch(\Exception $e){
            echo $message . 'Error parsing CSV : [' . $e->getMessage() . ']<br>';
        }*/

        $a = \Website\Model\Agency::getByUid('55edaf2569395295539020');

        $a->updateComments();

/*        echo '<pre>';
        var_dump($a->getComments());
        echo '</pre>';*/




    }

    public function starsAction()
    {
        
    }

    public function sitemapAction()
    {
        $this->enableLayout()->setLayout('searchlight');
        $this->view->zip = \Website\Model\Zipcode::getAll();

    }

    public function designAgenceAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/home-agency.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function designServicesAction(){
        $this->enableLayout()->setLayout('agence-large-header');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/new-services.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick.css');
        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/slick-theme.css');
        $this->view->headScript()->appendFile('/website/views/layouts/js/slick.min.js');
    }

    public function whiseAction(){


    }

    public function rapportAction(){

        $id      = "id";

        $birth   = "birthyear";
        $sex     = "sex";
        $country = "country";
        $zip     = "postcode";
        $district= "district";
        $pro     = "professionalstatus";
        $lang    = "firstlanguage";
        $match   = "matchtarget";


        $services     = "You took advantage of our services for :A real estate rent;A property purchase;Renting a property as lessor;Selling a property ;The rental management implementation of a real property;The rental or venal value estimation of a real property;Other";
        $relation     = "How would you decribe the relationship with the agency (or agent) today ?A first step in the relationship.;The relationship process is in progress (second or third meeting).;I am into a commercial relationship.;The commercial relationship has been concluded.;Other";
        $contact      = "Your contact (real estate) agent is: Harrison Amelinckx;Julian Boccardi;Fanny Paulus;Sarah De Man;Cédric De Man";
        $dispo        =  "How do you evaluate the following performances ?AvailabilityVERY DISSATISFIED;DISSATISFIED;NEITHER SATISFIED, NOR DISSATISFIED;SATISFIED;VERY SATISFIED";
        $pro_skills   = "How do you evaluate the following performances ?Professional skillsVERY DISSATISFIED;DISSATISFIED;NEITHER SATISFIED, NOR DISSATISFIED;SATISFIED;VERY SATISFIED";
        $transparancy = "How do you evaluate the following performances ?The transparancyVERY DISSATISFIED;DISSATISFIED;NEITHER SATISFIED, NOR DISSATISFIED;SATISFIED;VERY SATISFIED";
        $expectation  =  "How do you evaluate the following performances ?Meet of expectationsVERY DISSATISFIED;DISSATISFIED;NEITHER SATISFIED, NOR DISSATISFIED;SATISFIED;VERY SATISFIED";
        $flexibility  = "What is your feeling against the following criteria ?FlexibilityVery dissatisfied;Dissatisfied;Neither satisfied, nor dissatisfied;Satisfied;Very satisfied";
        $reactivity   = "What is your feeling against the following criteria ?ReactivityVery dissatisfied;Dissatisfied;Neither satisfied, nor dissatisfied;Satisfied;Very satisfied";
        $solivng      = "What is your feeling against the following criteria ?The solving of encountered problemsVery dissatisfied;Dissatisfied;Neither satisfied, nor dissatisfied;Satisfied;Very satisfied";
        $follow_up    = "What is your feeling against the following criteria ?Customer follow-upVery dissatisfied;Dissatisfied;Neither satisfied, nor dissatisfied;Satisfied;Very satisfied";
        $informations = "What is your feeling against the following criteria ?Information supplyVery dissatisfied;Dissatisfied;Neither satisfied, nor dissatisfied;Satisfied;Very satisfied";
        $nps          = "How likely is it that you would recommend our agency to a friend, family or colleague ?;";
        $comment      = "Leave us a comment, we'll be happy to read your opinion about our agency.";
        $follow_fb    = "Follow us on Facebook";
        $email        = "What is your e-mail address ?";



        $arrSexe = ['f' => array(), 'm' =>array()];
        $arrLang= array();
        $arrDistrict= array();
        $arrRelation= array();
        $arrNps = ['d' => array(), 'n' => array(), 'p' => array()];

        try{
            $csv = new \Csv_Reader($_SERVER['DOCUMENT_ROOT'] . "/tmp/" . 'XFBb0uoiBIhIfO4lt49V', new \Csv_Dialect(array('delimiter' => ';')));
            $csv->setHeader(\Website\Helper\SeerusHelper::sanitizeHeader($csv->current()));

            //var_dump(\Website\Helper\SeerusHelper::sanitizeHeader($csv->current()));

            $csv->next();



            do{
                if ($match == '1') continue;

                $c = new commentaire();

                $c->comment = $csv->current()[$comment];
                $c->email = $csv->current()[$email];
                $c->quote = $csv->current()[$nps];
                $c->id = $csv->current()[$id];

                if ($csv->current()[$sex] == 'f') array_push($arrSexe['f'], $c); else array_push($arrSexe['m'], $c);

                if (is_null($arrLang[$csv->current()[$lang]]))$arrLang[$csv->current()[$lang]]=array();
                array_push($arrLang[$csv->current()[$lang]], $c);

                if (is_null($arrDistrict[$csv->current()[$district]]))$arrDistrict[$csv->current()[$district]]=array();
                array_push($arrDistrict[$csv->current()[$district]], $c);

                if (is_null($arrRelation[$csv->current()[$relation]]))$arrRelation[$csv->current()[$relation]]=array();
                array_push($arrRelation[$csv->current()[$relation]], $c);

                if ($c->quote < 7) array_push($arrNps['d'], $c);
                elseif ($c->quote < 9) array_push($arrNps['n'], $c);
                else array_push($arrNps['p'], $c);

            }while($csv->next());

            $this->view->arrLang = $arrLang;
            $this->view->arrSexe = $arrSexe;
            $this->view->arrDistrict = $arrDistrict;
            $this->view->arrRelation = $arrRelation;
            $this->view->arrNps = $arrNps;


        }catch(\Exception $e){
            echo $e->getMessage();
        }

    }

    public function subAction(){

        $key = $this->getParam('key');

        $html  = file_get_contents('http://www.seerus.com/fr/restitution/' . $key);

        $regex_logo = "/\\/[a-z]{2}\\/reviews\\/logo\\/[^\"]+/s";
        $regex_name = "/<h1>[^<]+/s";
        $regex_phone = "/<p class=\"phone\">[^<]+/s";
        $regex_website = "/<p class=\"web\"><a href=\"[^\"]+/s";
        $regex_nps_a = "/<div class=\"seerus-nps-quote\">[^\\/]+/";
        $regex_nps_b = "/[0-9,]+/";

        preg_match($regex_logo, $html, $matches);
        $this->view->logo = $matches[0];

        preg_match($regex_name, $html, $matches);
        $this->view->name = trim(substr($matches[0],4 ));

        $adresse = substr($html,strpos($html, '<p class="address">')+19);
        $adresse = trim(substr($adresse,0, strpos($adresse,'</p>')));

        $this->view->rue = trim(explode('<br />', $adresse)[0]);
        $ville = trim(explode('<br />', $adresse)[1]);

        $this->view->cp = trim(substr($ville, 0, strpos($ville, ' ')));
        $this->view->ville = trim(substr($ville, strpos($ville, ' ')));

        preg_match($regex_phone, $html, $matches);
        $this->view->phone = trim(substr($matches[0],17 ));

        preg_match($regex_website, $html, $matches);
        $this->view->website = trim(substr($matches[0],24 ));

        preg_match($regex_nps_a, $html, $matches);
        $nps_a = $matches[0];

        preg_match($regex_nps_b, $nps_a, $matches);
        $this->view->nps = $matches[0];

        

    }

    public function menuAction()
    {
        // get root node if there is no document defined (for pages which are routed directly through static route)
        if(!$this->document instanceof Document_Page) {
            $this->document = Document::getById(1);
        }

        // get the document which should be used to start in navigation | default home
        $navStartNode = $this->document->getProperty("navigationRoot");
        if(!$navStartNode instanceof Document_Page) {
            $navStartNode = Document::getById(1);
        }

        //this is used as id prefix for the html menu element
        $htmlIdPrefix = "mainNav_";

        $navigation = $this->view->pimcoreNavigation()->getNavigation($this->document, $navStartNode, $htmlIdPrefix);
        $this->view->navigation()->menu()->setUseTranslator(false); // to deactivate the translator provided by the view helper
        $this->view->navigation($navigation);
        $this->view->nav = $navigation;
    }

    public function updateStepAction()
    {
        foreach(\Website\Model\Freemium::getAll() as $f){
            if ($f instanceof \Website\Model\Freemium){
                $f->nextStep();
            }
        }
    }

    public function testNewApiAction()
    {
        $a = Website\Model\Agency::getByUid('55edaf2569395295539020');
        $a->updateComments();
    }

    public function dateAction()
    {
        $date = '2016/02/25 - 10:27:33';


        $foo = DateTime::createFromFormat("Y/m/d - H:i:s", $date);

        echo $foo->format('Y-m-d H:i:s');


    }

    public function initFreemiumAction()
    {
        foreach(\Website\Model\Freemium::getAll() as $f){
            if ($f instanceof Website\Model\Freemium){
                $a = $f->getAgency();
                if ($a instanceof Website\Model\Agency){
                    $c = $a->firstComment();
                    echo '<br>' . $a->getName();

                    var_dump($c);
                    if ($c instanceof Website\Model\Comment){
                        echo ' ' . $c->getDate();

                        $f->setStep(2);
                        $f->setStep_2_time($c->getDate());
                        $f->save();
                    }
                }
            }
        }
    }


}

class commentaire{
    public $id;
    public $quote;
    public $comment;
    public $email;
}
