<?php

class DOA_Societes_Session
{

    const OBJ_SESSION = 'Societe';

    const id_RAIS_SOC = 'RAIS_SOC';

    const id_CODE_STE = 'CODE_STE';

    const id_GROUPE = 'GROUPE';

    const id_FORM_SOCIETE = 'FORM_SOCIETE';

    private $arrayKeyValue = null;

    public function setValueSociete($key, $value)
    {
        $this->arrayKeyValue[$key] = $value;
        $this->saveSession();
    }

    public function &getAllValue()
    {
        return $this->arrayKeyValue;
    }

    public function getValueOf($key)
    {
        if (is_array($this->arrayKeyValue) && array_key_exists($key, $this->arrayKeyValue))
            return $this->arrayKeyValue[$key];
        return null;
    }

    public function isExisteTrue($key)
    {
        if (is_array($this->arrayKeyValue) && array_key_exists($key, $this->arrayKeyValue)) {
            return ($this->arrayKeyValue[$key] == 'on');
        } else
            return false;
    }

    static function getSession()
    {
        if (array_key_exists(DOA_Societes_Session::OBJ_SESSION, $_SESSION))
            return unserializeSecure($_SESSION[DOA_Societes_Session::OBJ_SESSION]);
        else
            return null;
    }

    public function saveSession()
    {
        $_SESSION[DOA_Societes_Session::OBJ_SESSION] = serializeSecure($this);
    }

    static function copySocieteVersDoa($idDoa)
    {
        // Recuperation du contexte à copier
        $valide = 'ok';
        $clImportDonnee = DOA_Societes_Session::getSession();
        if ($liste = $clImportDonnee->getAllValue()) {
            foreach ($liste as $key => &$data) {
                if ($data === 'on') {
                    // Ajout de la donnée et des data lié
                    $tmp = explode('|', $key);
                    $code = $tmp[1];
                    
                    // $GROUPE = DOA_Societe::getSocieteByCode($code, 'GROUPE');
                    $TYPE_GS = ($tmp[0] == 'CODE_STE') ? 'S' : 'G';
                    // $CODE = (trim($GROUPE) == '')? $code : $GROUPE;
                    /*
                     * $pile = new PileSql(DB_OXASQLDB);
                     * $sql = 'insert into DOA_SOC (ID_DOA, TYPE_GS, CODE) values '.
                     * '('.$idDoa.
                     * ',\''.ReplaceSqlPointeur($TYPE_GS).
                     * '\',\''.ReplaceSqlPointeur($code).
                     * '\')';
                     * $reqMaj = new RequeteSQL($sql, DB_OXASQLDB);
                     * $pile->AddRequeteSql($reqMaj);
                     * return $pile->Commit();
                     */
                    $sqlDoaSoc = SqlDoaSociete::rqGetDoaSoc($idDoa, $TYPE_GS, $tmp[1])->Trouve1();
                    if ($sqlDoaSoc == null) {
                        $res = SqlDoaSociete::rqDOA_AjouteSociete($idDoa, $TYPE_GS, $tmp[1])->ExecuteSql();
                    } else
                        $valide = 'ko';
                }
            }
        }
        return array(
            'valide' => $valide
        );
    }
}