Agefoddx/classe/Controleur/ControleurTable.php
2025-03-26 10:22:20 +01:00

183 lines
9.8 KiB
PHP

<?php
namespace Module\Agefoddx\Classe\Controleur;
use Module\Agefoddx\Classe\Classe\GestionTexte;
use Module\Agefoddx\Classe\GestionBD\bdGestion;
use Module\Agefoddx\Classe\GestionBD\ligneGestion;
use Module\Agefoddx\Classe\GestionBD\tableGestion;
use Module\Agefoddx\Classe\GestionBD\bdExterneInteraction;
use Module\Agefoddx\Classe\GestionBD\bdExterneTableGestion;
class ControleurTable extends ControleurGenerique {
/**
* @var string $lien represent le chemin relatif vers la vue
*/
protected static string $lien = "../view/gestionTable";
/**
* @param $message string message passable en parameter pour les interactions utilisateur
* @return void affichage de la liste des tables
*/
public static function afficherListe(string $message = ""){
global $langs;
$id = intval($_POST["idBD"]);
$nom = $_POST["nomBD"];
$titre = GestionTexte::tronquerParentheses($langs->trans("AgefoddxAfficherListeTable"), $nom);
if (isset($id)){
$liste = (new tableGestion())->getListeTableCorreler($id);
self::afficherVue(["titre"=>"$titre","cheminCorpsVue" => ControleurTable::$lien."/afficherListe.php", "liste" => $liste, "idBD"=>$id ,"nomBD"=>$nom, "message" => $message,"mdp"=>$_POST["mdp"]]);
}else ControleurBd::afficherListe($langs->trans("AgefoddxAfficherListeTableError"));
}
/**
* @param string $titre titre de la page
* @param string $actionApres action a réalisé
* @param string $titreAction label de l'action
* @param $message string message passable en parameter pour les interactions utilisateur
* @return void permet la connection avec la base de donnée extérieur
*/
public static function connectionTr(string $titre, string $actionApres, string $titreAction, string $message = ""){
$id = intval($_POST["idBD"]);
$nom = $_POST["nomBD"];
$idT = $_POST["idTable"] ?? "";
$nomA = $_POST["nomTableA"] ?? "";
$nomE = $_POST["nomTableE"] ?? "";
if (isset($id) && isset($nom)){
$bd = (bdExterneInteraction::construireDepuisSQL((new bdGestion())->getBdByPk($id)));
self::afficherVue(["titre"=>$titre,"actionApres"=>$actionApres, "nomTableA"=>$nomA, "nomTableE"=>$nomE, "titreAction"=>$titreAction, "cheminCorpsVue" => ControleurTable::$lien."/transition.php", "idBD"=>$id ,"nomBD"=>$nom, "idTable"=>$idT, "message" => $message]);
}
}
/**
* @param $message string message passable en parameter pour les interactions utilisateur
* @return void affichage de la page d'ajout
*/
public static function ajouter(string $message = ""){
global $langs;
$id = intval($_POST["idBD"]);
$nom = $_POST["nomBD"] ?? "";
$mdp = $_POST["mdp"];
if ($mdp != ""){ $bd = bdExterneInteraction::construireDepuisSQL((new bdGestion())->getBdByPk($id), $mdp);
if ($bd->connectionBD()) {
if (isset($id) && isset($nom)){
$listeAgefodd = (new tableGestion())->getListeTableAgefodd();
$listeBdExterne = bdExterneTableGestion::getListeTables($bd);
self::afficherVue(["titre"=>$langs->trans("AgefoddxAjouterLien")." Agefodd ".$langs->trans("AgefoddxEt")." $nom", "cheminCorpsVue" => ControleurTable::$lien."/ajouter.php", "idBD"=>$id ,"nomBD"=>$nom, "message" => $message, "listeAgefodd"=>$listeAgefodd, "listeBdExterne"=>$listeBdExterne ,"bdConnection"=>$bd, "mdp"=>$mdp]);
}
} else self::connectionTr($langs->trans("AgefoddxConnexion")." $nom", "ajouter", $langs->trans("AgefoddxAjouter"), $langs->trans("AgefoddxMDPInc"));
} else self::connectionTr($langs->trans("AgefoddxConnexion")." $nom", "ajouter", $langs->trans("AgefoddxAjouter"));
}
/**
* @return void insert les liens
*/
public static function insertion(){
global $langs;
$id = intval($_POST["idBD"]);
$nom = $_POST["nomBD"];
$mdp = $_POST["mdp"];
$tableA = $_POST["agefoddTable"];
$tableE = $_POST["bdExterneTable"];
if ((isset($tableE) && isset($tableA)) && ($tableA!='' && $tableE!='')){
$value = (new tableGestion())->tablesDejaEnregistrer($tableA, $tableE);
if ($value == true) self::afficherListe("$tableA ".$langs->trans("AgefoddxEt")." $tableE ".$langs->trans("AgefoddxTableDejaLiee"));
elseif ($value == false) {$ins = (new tableGestion)->insertionTable($id, $tableE, $tableA);
if ($ins) self::afficherListe($langs->trans("AgefoddxInsertionTrueTable"));
else self::ajouter($langs->trans("AgefoddxInsertionFalseTable"));
}else self::ajouter($langs->trans("AgefoddxPBConnexion"));
} else self::ajouter($langs->trans("AgefoddxTableNonSelect"));
}
/**
* @param $message string message passable en parameter pour les interactions utilisateur
* @return void affiche la page de suppression de lien entre les tables
*/
public static function supprimer(string $message = ""){
global $langs;
$id = intval($_POST["idBD"]);
$idT = intval($_POST["idTable"]);
$nom = $_POST["nomBD"];
$nomA = $_POST["nomTableA"];
$nomE = $_POST["nomTableE"];
$titre = $langs->trans("AgefoddxSupprimerLienTable")." $nomA ".$langs->trans("AgefoddxEt")." $nomE";
self::afficherVue(["titre"=>"$titre","cheminCorpsVue" => ControleurTable::$lien."/supprimer.php", "idBD"=>$id, "idTable" => $idT ,"nomBD"=>$nom, "nomTableA"=>$nomA, "nomTableE"=>$nomE, "message" => $message, "mdp"=>$_POST["mdp"]]);
}
/**
* @return void supprime les liens entre les tables
*/
public static function suppression() {
global $langs;
$idT = intval($_POST["idTable"]);
$nomBD = $_POST["nomBD"];
$nomA = $_POST["nomTableA"];
$nomE = $_POST["nomTableE"];
$mdp=$_POST["mdp"];
$value = $_POST["ouiNon"];
$remp = $nomA . " " . $langs->trans("AgefoddxEt") . " " . $nomE;
if ($value == "true" && $idT && isset($nomBD) && isset($nomA) && isset($nomE)) {
$connexion_table = new tableGestion();
if ($connexion_table->tablePossedeDesLignes($idT)) self::afficherListe(GestionTexte::tronquerParentheses($langs->trans("AgefoddxSuppressionImpLigneLiee"), $remp));
else {$value = $connexion_table->suppressionTable($idT);
if ($value)self::afficherListe(GestionTexte::tronquerParentheses($langs->trans("AgefoddxSuppressionLienTrue"), $remp));
else self::afficherListe(GestionTexte::tronquerParentheses($langs->trans("AgefoddxSuppressionLienFalse"), $remp));
}
}else self::afficherListe(GestionTexte::tronquerParentheses($langs->trans("AgefoddxSuppressionLienAnnuler"), $remp));
}
/**
* @param $message string message passable en parameter pour les interactions utilisateur
* @return void affiche la table d'importation des données
*/
public static function importer(string $message = "") {
global $langs;
$id = intval($_POST["idBD"]);
$idT = intval($_POST["idTable"]);
$nom = $_POST["nomBD"];
$nomA = $_POST["nomTableA"];
$nomE = $_POST["nomTableE"];
$titre = $langs->trans("AgefoddxImporterDonnee")." $nomA ".$langs->trans("AgefoddxVers")." $nomE";
$value = (new tableGestion())->getParametreObligatoireNonRempli($nomA, $nomE);
$importationPossible = false;
if ($value == []) $importationPossible = true;
self::afficherVue(["titre"=>$titre,"cheminCorpsVue" => ControleurTable::$lien."/importer.php",
"idBD"=>$id, "idTable" => $idT ,"nomBD"=>$nom, "nomTableA"=>$nomA, "nomTableE"=>$nomE, "valueImportation" => $importationPossible, "liste"=>$value,"message" => $message, "mdp"=>$_POST["mdp"]]);
}
/**
* @return void importe les données en fonction des tables liée
*/
public static function importation(){
global $langs;
$id = intval($_POST["idBD"]);
$idT = intval($_POST["idTable"]);
$nom = $_POST["nomBD"];
$nomA = $_POST["nomTableA"];
$nomE = $_POST["nomTableE"];
$mdp = $_POST["mdp"] ?? "";
$bd = bdExterneInteraction::construireDepuisSQL((new bdGestion())->getBdByPk($id), $mdp);
if ($mdp!=""){
if ($bd->connectionBD()) {
$idTableE = bdExterneTableGestion::getPk($bd, $nomE);
$parametre = (new TableGestion())->getParametreExterne($idT);
$parametre[] = ["id_source"=>$idTableE, "fixe"=>"false"];
$liste = bdExterneTableGestion::getValeurAExporter($bd, $parametre, $nomE);
if ((new ligneGestion())->possedeFk($nomA, $idT)){
$fkListe = (new ligneGestion())->getListeFKCorreler($nomA, $idT);
foreach ($fkListe as $ligne) {
$listeConvertion = (new tableGestion())->getTableConversionFK($ligne["TableReference"],(new ligneGestion())->getClefReferente($nomA, $ligne["NomLigne"]));
if ($listeConvertion != false ){
for ($x=0; $x<sizeof($liste); $x++){
$liste[$x][ $ligne["NomLigne"] ] = reset($listeConvertion[ $liste[$x] [$ligne["NomLigne"]] ]);
}
}
}
}
$type = bdExterneTableGestion::getTypePk($bd, $nomE);
$message = (new tableGestion())->importerTout($liste,$nomE, $idTableE, $nomA, $type);
self::afficherListe($message);
} else self::connectionTr($langs->trans("AgefoddxImporter"), "importation", $langs->trans("AgefoddxImporter"), "mot de passe incorrecte");
} else self::connectionTr($langs->trans("AgefoddxImporter"), "importation", $langs->trans("AgefoddxImporter"));
}
}