2020-11-19 22:44:19 +01:00
< ? php
2020-11-19 22:59:47 +01:00
require_once ( __DIR__ . '/../dbutils.php' );
2020-11-19 22:47:44 +01:00
require_once ( __DIR__ . '/../globals.php' );
2020-11-19 22:44:19 +01:00
2020-11-19 22:59:47 +01:00
class HistFiller {
var $dbutils ;
function __construct () {
$this -> dbutils = new DbUtils ();
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:59:47 +01:00
2020-11-19 22:44:19 +01:00
public function defineHistActions () {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
2020-11-19 22:59:47 +01:00
$sql = " INSERT INTO %histactions% (id,name,description) VALUES (?,?,?) " ;
$stmt = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql ));
$stmt -> execute ( array ( '1' , 'ProdInit' , 'Initiales Befuellen der Produkttabelle' ));
$stmt -> execute ( array ( '2' , 'ConfigInit' , 'Initiales Befuellen der Konfigurationstabelle' ));
$stmt -> execute ( array ( '3' , 'UserInit' , 'Initiales Befuellen der Benutzertabelle' ));
2020-11-19 22:44:19 +01:00
$stmt -> execute ( array ( '4' , 'ProdChange' , 'Modifikation der Produktdaten' ));
$stmt -> execute ( array ( '5' , 'ProdCreation' , 'Neues Produkt' ));
$stmt -> execute ( array ( '6' , 'ConfigChange' , 'Modifikation der Konfiguration' ));
$stmt -> execute ( array ( '7' , 'UserCreation' , 'Neuer Benutzer' ));
$stmt -> execute ( array ( '8' , 'UserChange' , 'Modifikation eines Benutzers' ));
2020-11-19 22:47:44 +01:00
$stmt -> execute ( array ( '9' , 'DbSave' , 'Komplettsicherung der Datenbank' ));
2020-11-19 22:59:47 +01:00
$stmt -> execute ( array ( '10' , 'DbRestore' , 'Wiederherstellung der Datenbank aus einer Sicherungskopie' ));
}
private static function getColNamesForHistTable ( $tableDescr ) {
$cols = array ();
foreach ( $tableDescr as $aCol ) {
if ( $aCol [ " hist " ] == 1 ) {
$cols [] = $aCol [ " col " ];
}
}
return $cols ;
}
private static function getColNamesForUserHistTable () {
return self :: getColNamesForHistTable ( DbUtils :: $userCols );
}
public static function readUserTableAndSendToHist ( $pdo ) {
$sql = " SELECT * FROM %user% " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ());
$result = $stmt -> fetchAll ();
foreach ( $result as $aUser ) {
self :: createUserInHist ( $pdo , $aUser [ " id " ]);
}
}
public static function createUserInHist ( $pdo , $userid ) {
$pdo -> beginTransaction ();
self :: updateOrCreateUserInHist ( $pdo , $userid , '7' );
$pdo -> commit ();
}
public static function updateUserInHist ( $pdo , $userid ) {
$pdo -> beginTransaction ();
self :: updateOrCreateUserInHist ( $pdo , $userid , '8' );
$pdo -> commit ();
}
private static function updateOrCreateUserInHist ( $pdo , $userid , $histaction ) {
self :: updateOrCreateEntryInHist ( $pdo , $userid , $histaction , self :: getColNamesForUserHistTable (), 'userid' , 'user' , 'histuser' , null , null );
}
public static function readAllProdsAndFillHistByDb ( $pdo ) {
$sql = " SELECT id FROM %products% WHERE removed is null " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ());
$result = $stmt -> fetchAll ();
foreach ( $result as $anElement ) {
self :: createProdInHist ( $pdo , $anElement [ " id " ]);
}
}
private static function getColNamesForProdHistTable () {
return self :: getColNamesForHistTable ( DbUtils :: $prodCols );
}
public static function createProdInHist ( $pdo , $prodid ) {
self :: updateOrCreateProdInHist ( $pdo , $prodid , '5' );
}
public static function updateProdInHist ( $pdo , $prodid ) {
self :: updateOrCreateProdInHist ( $pdo , $prodid , '4' );
}
private static function getExtrasList ( $pdo , $prodid ) {
$sql = " SELECT GROUP_CONCAT(%extras%.name) as extraslist FROM %extras%,%extrasprods% WHERE %extrasprods%.prodid=? AND %extrasprods%.extraid=%extras%.id " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $prodid ));
$row = $stmt -> fetchObject ();
return $row -> extraslist ;
}
private static function updateOrCreateProdInHist ( $pdo , $prodid , $histaction ) {
$extras = self :: getExtrasList ( $pdo , $prodid );
$extraCol = ( is_null ( $extras ) ? null : 'extras' );
self :: updateOrCreateEntryInHist ( $pdo , $prodid , $histaction , self :: getColNamesForProdHistTable (), 'prodid' , 'products' , 'histprod' , $extraCol , $extras );
}
private static function updateOrCreateEntryInHist ( $pdo , $id , $histaction , $colsInSourceTable , $idInHist , $sourcetable , $histtable , $extraCol , $extraVal ) {
2020-11-19 23:00:35 +01:00
if ( ! is_null ( $extraVal )) {
if ( strlen ( $extraVal ) > 299 ) {
$extraVal = substr ( $extraVal , 0 , 299 );
}
}
2020-11-19 22:59:47 +01:00
$sql = " SELECT * from % " . $sourcetable . " % WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $id ));
$row = $stmt -> fetchObject ();
$cols = $colsInSourceTable ;
array_splice ( $cols , 0 , 1 , $idInHist );
$valuesStr = implode ( " , " , $cols );
$quests = array ();
$vals = array ();
foreach ( $colsInSourceTable as $aHistCol ) {
$vals [] = $row -> $aHistCol ;
$quests [] = " ? " ;
}
$sql_insert_hist = " INSERT INTO % " . $histtable . " % (id, " . $valuesStr . " ) VALUES(NULL, " . implode ( " , " , $quests ) . " ) " ;
$stmt_insert_hist = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql_insert_hist ));
$stmt_insert_hist -> execute ( $vals );
$newRefIdForHist = $pdo -> lastInsertId ();
if ( ! is_null ( $extraCol )) {
$sql = " UPDATE % " . $histtable . " % SET " . $extraCol . " =? WHERE id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $extraVal , $newRefIdForHist ));
}
self :: insertIntoHist ( $pdo , $histaction , $newRefIdForHist );
}
public static function insertSaveHistEntry ( $pdo ) {
self :: insertIntoHist ( $pdo , 9 , null );
}
public static function insertRestoreHistEntry ( $pdo ) {
self :: insertIntoHist ( $pdo , 10 , null );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
public function updateConfigInHist ( $pdo , $theItem , $theValue ) {
2020-11-19 22:44:19 +01:00
$sql_find_id = " SELECT id FROM %config% WHERE name=' $theItem ' " ;
$sql_insert_histconfig = " INSERT INTO %histconfig% (id,configid,setting) VALUES (NULL,?,?) " ;
2020-11-19 22:59:47 +01:00
2020-11-19 22:44:19 +01:00
$pdo -> beginTransaction ();
2020-11-19 22:47:44 +01:00
$stmt_query = $pdo -> query ( DbUtils :: substTableAlias ( $sql_find_id ));
2020-11-19 22:44:19 +01:00
$row = $stmt_query -> fetchObject ();
$theConfigId = $row -> id ;
2020-11-19 22:47:44 +01:00
$stmt_insert_histconfig = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql_insert_histconfig ));
2020-11-19 22:44:19 +01:00
$stmt_insert_histconfig -> execute ( array ( $theConfigId , " $theValue " ));
$newRefIdForHist = $pdo -> lastInsertId ();
2020-11-19 22:59:47 +01:00
self :: insertIntoHist ( $pdo , '6' , $newRefIdForHist );
2020-11-19 22:44:19 +01:00
$pdo -> commit ();
}
2020-11-19 22:59:47 +01:00
/*
* Read the complete config table and fill in these values to the histtable
*/
public function readConfigTableAndSendToHist () {
$sql_query = " SELECT * FROM %config% " ;
$sql_insert_histconfig = " INSERT INTO %histconfig% (id,configid,setting) VALUES (
NULL , ? , ? ) " ;
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$pdo -> beginTransaction ();
$stmt_query = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_query ));
$stmt_insert_histconfig = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_insert_histconfig ));
$stmt_query -> execute ();
$result = $stmt_query -> fetchAll ();
foreach ( $result as $row ){
$stmt_insert_histconfig -> execute ( array ( $row [ 'id' ], $row [ 'setting' ]));
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , '2' , $newRefIdForHist );
}
$pdo -> commit ();
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:59:47 +01:00
private static function insertIntoHist ( $pdo , $action , $refIdForHist ) {
date_default_timezone_set ( DbUtils :: getTimeZone ());
$currentTime = date ( 'Y-m-d H:i:s' );
$sql_insert_hist = " INSERT INTO %hist% (id,date,action,refid) VALUES (NULL,?,?,?) " ;
$stmt_insert_hist = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql_insert_hist ));
$stmt_insert_hist -> execute ( array ( $currentTime , $action , $refIdForHist ));
2020-11-19 22:44:19 +01:00
}
}
?>