<?php
namespace Modules\API\APIExterne\Controller;

use Entity\Model\ParamDico;
use Helpers\Session\ParseBase1;
use Skeleton\Controller\Controller;
use Helpers\Outil\Sql;
use SimpleXMLElement;

class IndexController extends Controller
{

    public function getUrlParamAction() : string
    {
        $id_token_param = $this->request->getAttribute('id_token');
        $id_user_param = $this->request->getAttribute('id_user');

        $repIndex = new \Modules\API\APIExterne\Repository\Index($this->pdo);

        /*$start_url = \Helpers\Outil\Roles::droit($this->pdo, 'URLACCESOXALINK', 'PF_OXALINK', true);
        $start_url = explode('/', $start_url);
        $start_url = $start_url[0] . '//'. $start_url[2] . '/' . $start_url[3];*/
        
        $token = Sql::guidGenerate();
        
        $repIndex->insertTokenTmp($id_user_param, $token);

        $sec = (new ParseBase1())->getHttps()? 'https' : 'http';
        $broker = (new ParseBase1())->getBroker()=='1'? '':(new ParseBase1())->getBroker();

        //dossier n° 36584 : si prise en compte de l'URL renseignée par le client si elle existe, sinon URL serveur

        $alias = (explode('/', sql::paravrac($this->pdo, 'PF_OXALINK', 'URLACCESOXALINK')))[2];

        $urlOxalink = $sec.'://'.($alias ?: (new ParseBase1())->getServer()).'/oxalink'.$broker.'/index/changementPassword/'. $token;
        //$repIndex->updateParamDico($urlOxalink, $id_token_param, $id_user_param);
        
        $paramDicoEntity = new ParamDico($this->pdo);
        $paramDicoEntity->insert([
            'TYPEDICO' => $id_token_param,
            'ID1' => $id_user_param,
            'COMMENTAIRE' => $urlOxalink,
        ]);

        header('Content-Type: application/xml; charset=utf-8');
        return "<?xml version='1.0' encoding='iso-8859-1' standalone='no' ?><OXALINK_Tok_Out><Statut>O</Statut></OXALINK_Tok_Out>";
    }
}
