367 lines
15 KiB
PHP
367 lines
15 KiB
PHP
<?php
|
|
namespace Module\Agefoddx\Classe\GestionBD;
|
|
use Module\Agefoddx\Classe\Classe\GestionTexte;
|
|
|
|
class tableGestion {
|
|
/**
|
|
* @return array|false return la liste des noms tables présentes dans agefodd ou faux en cas d'erreur sql
|
|
*/
|
|
public function getListeTableAgefodd() { // function de récupération des données dans Agefodd
|
|
global $db;
|
|
$resql = $db->query("SELECT DATABASE()");
|
|
$adresse = $db->fetch_array($resql)[0];
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$sql = "SELECT table_name as 'NomTable' FROM information_schema.tables WHERE table_schema = '$adresse' AND table_name LIKE 'llx_agefodd_%';";
|
|
$resql1=$db->query($sql);
|
|
if ($resql1) {
|
|
// Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql1)) {
|
|
// Accès aux colonnes via leur index ou alias
|
|
$rowid = htmlspecialchars($row['NomTable']); // Par alias (nom de la colonne)
|
|
$list[] = $rowid;
|
|
}
|
|
} else {
|
|
return false;
|
|
} return $list;
|
|
}
|
|
|
|
/**
|
|
* @param $id int identifient de la table à récupérer
|
|
* @return array|false renvoie la liste des infos de la table sous forme de dictionnaire (idTable, idBD, nomTableAgefodd, nomTableExterne)
|
|
*/
|
|
public function getTableByPk(int $id){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$id = $db->escape($id); // pour protéger contre l'injection sql
|
|
$sql = "SELECT tid, bdid, nomTableExterne, nomTableAgfodd FROM llx_desal_external_table WHERE tid=$id;";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
// Parcours des résultats avec fetch_array
|
|
$row = $db->fetch_array($resql);
|
|
$list = ['idTable'=>$row["tid"], "idBD"=>$row["bdid"],"nomTableExterne"=>$row["nomTableExterne"],"nomTableAgefodd"=>$row["nomTableAgfodd"]]; // Par alias (nom de la colonne)
|
|
|
|
} else {
|
|
// Gestion de l'erreur
|
|
return false;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @param int $idBD id de la base de donnée
|
|
* @return array|false renvoie la liste des tables a lié
|
|
*/
|
|
public function getListeTableCorreler(int $idBD) {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$idBD = $db->escape($idBD);
|
|
$sql = "SELECT bd.bdid as bdid, bd.nom as nom, t.tid as tid, t.nomTableExterne as te, t.nomTableAgfodd as ta, COUNT(rid) as nbr
|
|
FROM llx_desal_external_bd bd JOIN llx_desal_external_table t ON bd.bdid = t.bdid
|
|
LEFT JOIN llx_desal_external_row r ON r.tid = t.tid
|
|
WHERE bd.bdid = $idBD
|
|
GROUP BY bd.bdid, bd.nom, t.tid, t.nomTableExterne, t.nomTableAgfodd
|
|
ORDER BY bd.bdid, t.tid, bd.nom";
|
|
$resql = $db->query($sql);
|
|
if ($resql) {// Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
// Accès aux colonnes via leur index ou alias
|
|
$row = ["idBD" => htmlspecialchars($row["bdid"]), "nom"=>htmlspecialchars($row["nomBD"]), "idTable"=>htmlspecialchars($row["tid"]), "nomTableExterne"=>htmlspecialchars($row["te"]), "nomTableAgfodd"=>htmlspecialchars($row["ta"]), "nombreLigne"=>htmlspecialchars($row["nbr"])]; // Par alias (nom de la colonne)
|
|
// Affichage des résultats
|
|
$list[] = $row;
|
|
}} else {// Gestion de l'erreur
|
|
return false;
|
|
}return $list;
|
|
}
|
|
|
|
/**
|
|
* @param int $idBD id de la base de donnée
|
|
* @param string $nomTableExterne nom de la table a lié
|
|
* @param string $nomTableAgefodd nom de la table d'Agefodd
|
|
* @return bool renvoie la valeur d'insertion dans la table
|
|
*/
|
|
public function insertionTable(int $idBD, string $nomTableExterne, string $nomTableAgefodd) : bool{
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$idBD = $db->escape($idBD); // pour protéger contre l'injection sql
|
|
$nomTableExterne = $db->escape($nomTableExterne);
|
|
$nomTableAgefodd = $db->escape($nomTableAgefodd);
|
|
$sql = "INSERT INTO llx_desal_external_table(bdid, nomTableExterne, nomTableAgfodd) VALUES ('$idBD', '$nomTableExterne', '$nomTableAgefodd');";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
return true;
|
|
} return false;
|
|
}
|
|
|
|
/**
|
|
* @param $idT int|string clef primaire de la table
|
|
* @return bool renvoie si la suppression a bien été efféctuer
|
|
*/
|
|
public function suppressionTable($idT) : bool {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$idT = $db->escape($idT); // pour protéger contre l'injection sql
|
|
$sql = "DELETE FROM llx_desal_external_table WHERE tid='$idT';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
return true;
|
|
}return false;
|
|
}
|
|
|
|
/**
|
|
* @param $tableA string nom de la table d'agefodd
|
|
* @param $tableE string nom de la table a lié
|
|
* @return bool|null renvoie si la table est déjà lié ou null en cas d'erreur sql
|
|
*/
|
|
public function tablesDejaEnregistrer(string $tableA, string $tableE) {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$sql = "SELECT COUNT(tid) as nbTable FROM llx_desal_external_table WHERE nomTableAgfodd = '$tableA' AND nomTableExterne = '$tableE'";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
$value = $db->fetch_array($resql)[0];
|
|
if ($value == 0) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param $id string|int identifient de la table
|
|
* @return int return le nombre de lignes present dans llx_desal_external_row
|
|
*/
|
|
public function tablePossedeDesLignes($id) : int {
|
|
global $db;
|
|
$res = 0;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$id = $db->escape($id); // pour protéger contre l'injection sql
|
|
$sql = "SELECT COUNT(tid) as nbrow FROM llx_desal_external_row WHERE tid = $id;";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
$row = $db->fetch_array($resql);
|
|
$res = $row["nbrow"];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @param $tableA string nom de la table d'agefodd
|
|
* @param $type_id string nom du type d'id
|
|
* @return void modifie la table pour y inséré id_source & table_source
|
|
*/
|
|
private function alterTableAjout(string $tableA, string $type_id) {
|
|
global $db;
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$type_id = $db->escape($type_id);
|
|
if ($type_id == 'varchar') {$type_id=$type_id.'(255)';}
|
|
$sql = "ALTER TABLE $tableA ADD COLUMN id_source $type_id, ADD COLUMN table_source VARCHAR(255)";
|
|
$res = $db->query($sql);
|
|
}
|
|
|
|
/**
|
|
* @param $tableA string nom de la table d'agefodd
|
|
* @param $type_id string type de la clef primaire de $tableA (varchar, integer, ...)
|
|
* @return void modifie la table $tableA si cela n'a pas déjà été fait
|
|
*/
|
|
public function alterTableAvantPremiereInsertion(string $tableA, string $type_id) : void {
|
|
global $db;
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$sql = "SELECT id_source FROM $tableA ;";
|
|
$resql=$db->query($sql);
|
|
if (!$resql) $this->alterTableAjout($tableA, $type_id);
|
|
}
|
|
|
|
/**
|
|
* @param string $nomTableA nom de la table d'agefodd
|
|
* @param string $nomTableE nom de la table à lier
|
|
* @return array|false return la liste des parameters obligatoire non rempli ou faux en cas d'erreur sql
|
|
*/
|
|
public function getParametreObligatoireNonRempli(string $nomTableA,string $nomTableE) {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$nomTableA = $db->escape($nomTableA);
|
|
$nomTableE = $db->escape($nomTableE);
|
|
$sql = "SELECT isc.COLUMN_NAME as 'cm', isc.IS_NULLABLE as 'value'
|
|
FROM INFORMATION_SCHEMA.COLUMNS isc
|
|
WHERE TABLE_NAME = '$nomTableA'
|
|
AND NOT EXISTS(
|
|
SELECT nomLigneAgfodd
|
|
FROM llx_desal_external_row as lder
|
|
JOIN llx_desal_external_table as ldet
|
|
ON lder.tid = ldet.tid
|
|
WHERE nomTableAgfodd = '$nomTableA'
|
|
AND nomTableExterne = '$nomTableE'
|
|
AND isc.COLUMN_NAME = nomLigneAgfodd
|
|
OR EXTRA = 'auto_increment'
|
|
);";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
while ($row = $db->fetch_array($resql)) {
|
|
if ($row["value"] == "NO") {
|
|
$list[] = ["nom"=>$row['cm'], "type"=>(new ligneGestion())->getTypeLigne($nomTableA, $row['cm'])];}
|
|
}
|
|
if (sizeof($list) == 0) return [];
|
|
return $list;
|
|
}
|
|
else return false;
|
|
}
|
|
|
|
/**
|
|
* @param $idTable int|string identifiant de la table
|
|
* @return array|false return la liste des paramètres ou faux en cas d'erreur sql
|
|
*/
|
|
public function getParametreExterne($idTable){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$list = [];
|
|
$idTable = $db->escape($idTable); // pour protéger contre l'injection sql
|
|
$sql = "SELECT nomLigneExterne as 'nle', nomLigneAgfodd as 'nla', valeurFixe as 'vf' FROM llx_desal_external_row WHERE tid = $idTable;";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
// Parcours des résultats avec fetch_array
|
|
while ($row = $db->fetch_array($resql)) {
|
|
if ($row['vf']=="") $list[] = [$row["nla"]=>$row['nle'], "fixe"=>"false"];
|
|
else $list[] = [$row["nla"]=>$row['vf'], "fixe"=>"true"];
|
|
}
|
|
} else return false;
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @param $liste array liste des liaisons
|
|
* @param $tableE string nom de la table à lier
|
|
* @param $idTableE int|string identifient de la table à lier
|
|
* @param $tableA string nom de la table d'agefodd
|
|
* @param $type string type de la clef primaire
|
|
* @return string return le nombre d'insertions et le nombre d'erreurs sous format string
|
|
*/
|
|
public function importerTout(array $liste,string $tableE, $idTableE,string $tableA,string $type):string{
|
|
self::alterTableAvantPremiereInsertion($tableA, $type);
|
|
global $db, $langs;
|
|
$liste_clef = "";
|
|
$listeValeur = "";
|
|
$lien = "";
|
|
$enrg = 0;
|
|
$erreur = 0;
|
|
$idSource = "";
|
|
foreach ($liste as $item){
|
|
foreach (array_keys($item) as $clef){
|
|
$clef = $db->escape($clef);
|
|
$valeur = $db->escape($item[$clef]);
|
|
$liste_clef = $liste_clef .", ".$clef;
|
|
$listeValeur = $listeValeur .", '".$valeur."'";
|
|
if ($clef == "id_source") {$idSource = $valeur;}
|
|
}
|
|
$liste_clef = $liste_clef .", table_source";
|
|
$listeValeur = $listeValeur .", '".$tableE."'";
|
|
$liste_clef = substr($liste_clef, 1);
|
|
$listeValeur = substr($listeValeur, 1);
|
|
$value = self::importerUneLigne($tableA, $liste_clef, $listeValeur, $idSource, $tableE);
|
|
$liste_clef = "";
|
|
$listeValeur = "";
|
|
$lien = "";
|
|
if ($value) $enrg ++; else $erreur ++;
|
|
}
|
|
return GestionTexte::tronquerParentheses("$enrg ".$langs->trans("AgefoddxImporterTout"), $erreur);
|
|
}
|
|
|
|
/**
|
|
* @param $tableA string nom de la table en d'insertion
|
|
* @param $clef string|int nom de la colonne
|
|
* @param $valeur string valeur a inséré
|
|
* @param string $idSource
|
|
* @param string $tableE
|
|
* @return bool return si la ligne, c'est inséré
|
|
*/
|
|
private function importerUneLigne(string $tableA, $clef, string $valeur, string $idSource, string $tableE){
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$id = $this->estDansLaTable($tableA, $idSource, $tableE);
|
|
if ($id) {
|
|
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
WHERE TABLE_NAME = '$tableA' AND CONSTRAINT_NAME = 'PRIMARY';";
|
|
$resql=$db->query($sql);
|
|
$nomId = $resql->fetch_array()["COLUMN_NAME"];
|
|
$sqlTemp = ["SET FOREIGN_KEY_CHECKS = 0;",
|
|
"DELETE FROM $tableA WHERE $nomId = '$id'",
|
|
"SET FOREIGN_KEY_CHECKS = 1;"];
|
|
foreach ($sqlTemp as $temp) { $resql=$db->query($temp);}
|
|
$sql = "INSERT INTO $tableA ($nomId, $clef) values ('$id', $valeur) ";
|
|
} else {$sql = "INSERT INTO $tableA ($clef) values ($valeur) ";}
|
|
//WHERE NOT EXISTS (SELECT 1 FROM $tableA WHERE $lien);";
|
|
$resql=$db->query($sql);
|
|
if ($resql) return true;
|
|
else {
|
|
echo "Erreur SQL : " . $db->lasterror().'</br>'; // affichage des erreurs SQL
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $tableA
|
|
* @param string $id_source
|
|
* @param string $table_source
|
|
* @return string renvoie si un lien est déjà présent dans la table
|
|
*/
|
|
public function estDansLaTable(string $tableA, string $id_source, string $table_source) : string {
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
WHERE TABLE_NAME = '$tableA' AND CONSTRAINT_NAME = 'PRIMARY';";
|
|
$resql=$db->query($sql);
|
|
$nomId = $resql->fetch_array()["COLUMN_NAME"];
|
|
$sql = "Select $nomId as id FROM $tableA WHERE id_source = '$id_source' AND table_source = '$table_source'";
|
|
$resql=$db->query($sql);
|
|
$resql = $resql->fetch_array()["id"];
|
|
if ($resql) return $resql;
|
|
else {//echo "Erreur SQL : " . $db->lasterror().'</br>'; // affichage des erreurs SQL
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $nomTableRef string nom de la table
|
|
* @param $fk string nom de la clef étrangère
|
|
* @return array|false return la liste des valeurs étrangères
|
|
*/
|
|
public function getTableConversionFK(string $nomTableRef, string $fk) {
|
|
global $db;
|
|
$list = [];
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$nomTableRef = $db->escape($nomTableRef); // pour protéger contre l'injection sql
|
|
$fk = $db->escape($fk); // pour protéger contre l'injection sql
|
|
$sql = "SELECT $fk as fk, id_source FROM $nomTableRef;";
|
|
$resql=$db->query($sql);
|
|
if ($resql) {
|
|
while ($row = $db->fetch_array($resql)) {
|
|
$list[] = [$row["id_source"]=>$row["fk"]];
|
|
}
|
|
}else return false;
|
|
return $list;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $tableA string nom de la table d'agefodd
|
|
* @param $tableE string nom de la table exterieur
|
|
* @return bool return si la suppression, c'est effectuée
|
|
*/
|
|
public function vider(string $tableA, string $tableE) {
|
|
return true;
|
|
global $db;
|
|
$db->query("SET NAMES 'utf8mb4'");
|
|
$tableA = $db->escape($tableA); // pour protéger contre l'injection sql
|
|
$tableE = $db->escape($tableE); // pour protéger contre l'injection sql
|
|
$sql = "DELETE FROM $tableA WHERE table_source = '$tableE';";
|
|
$resql=$db->query($sql);
|
|
if ($resql) { return true;
|
|
} else { return false; }
|
|
}
|
|
} |