2020-11-19 22:44:19 +01:00
< ? php
2020-11-19 22:47:44 +01:00
require_once ( __DIR__ . '/../dbutils.php' );
require_once ( __DIR__ . '/../globals.php' );
2020-11-19 22:44:19 +01:00
class HistFiller {
var $dbutils ;
function __construct () {
$this -> dbutils = new DbUtils ();
}
public function defineHistActions () {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
$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' ));
$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' ));
$stmt -> execute ( array ( '10' , 'DbRestore' , 'Wiederherstellung der Datenbank aus einer Sicherungskopie' ));
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:54:51 +01:00
public function readUserTableAndSendToHist ( $pdo ) {
2020-11-19 22:44:19 +01:00
$sql = " SELECT * FROM %user% " ;
2020-11-19 22:54:51 +01:00
$this -> readSqlUserTableAndSendToHist ( $pdo , $sql , '3' );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:54:51 +01:00
public function updateOneUser ( $pdo , $userid ) {
2020-11-19 22:44:19 +01:00
$sql = " SELECT * FROM %user% WHERE id=' $userid ' " ;
2020-11-19 22:54:51 +01:00
$this -> readSqlUserTableAndSendToHist ( $pdo , $sql , '8' );
2020-11-19 22:44:19 +01:00
}
/*
* Read the complete user table and fill in these values to the histtable
*/
2020-11-19 22:54:51 +01:00
private function readSqlUserTableAndSendToHist ( $pdo , $sql_query , $histaction ) {
2020-11-19 22:44:19 +01:00
$sql_insert_histuser = " INSERT INTO %histuser% (id,userid,username,
is_admin , right_waiter , right_kitchen , right_bar , right_supply , right_paydesk , right_statistics ,
2020-11-19 22:50:09 +01:00
right_bill , right_products , right_reservation , right_rating , right_changeprice , right_manager , active ) VALUES (
NULL , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) " ;
2020-11-19 22:54:51 +01:00
2020-11-19 22:44:19 +01:00
$pdo -> beginTransaction ();
$stmt_query = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_query ));
$stmt_insert_histuser = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_insert_histuser ));
$stmt_query -> execute ();
$result = $stmt_query -> fetchAll ();
foreach ( $result as $row ){
$stmt_insert_histuser -> execute ( array ( $row [ 'id' ], $row [ 'username' ],
$row [ 'is_admin' ], $row [ 'right_waiter' ], $row [ 'right_kitchen' ], $row [ 'right_bar' ],
$row [ 'right_supply' ], $row [ 'right_paydesk' ], $row [ 'right_statistics' ], $row [ 'right_bill' ],
2020-11-19 22:50:09 +01:00
$row [ 'right_products' ], $row [ 'right_reservation' ], $row [ 'right_rating' ], $row [ 'right_changeprice' ], $row [ 'right_manager' ], $row [ 'active' ]));
2020-11-19 22:44:19 +01:00
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , $histaction , $newRefIdForHist );
}
$pdo -> commit ();
}
2020-11-19 22:54:51 +01:00
public function updateUserInHist ( $pdo , $userid , $username ,
2020-11-19 22:47:44 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat , $rBill , $rProducts , $rReservation , $rRat , $rChangeprice , $rManager , $active )
2020-11-19 22:44:19 +01:00
{
2020-11-19 22:54:51 +01:00
$this -> updateOrCreateUserInHist ( $pdo , $userid , $username ,
2020-11-19 22:44:19 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat ,
2020-11-19 22:47:44 +01:00
$rBill , $rProducts , $rReservation , $rRat , $rChangeprice , $rManager , $active , '8' );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:54:51 +01:00
public function createUserInHist ( $pdo , $lastId , $username ,
2020-11-19 22:47:44 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat , $rBill , $rProducts , $rRes , $rRat , $rChangeprice , $rManager )
2020-11-19 22:44:19 +01:00
{
2020-11-19 22:54:51 +01:00
$this -> updateOrCreateUserInHist ( $pdo , $lastId , $username ,
2020-11-19 22:44:19 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat ,
2020-11-19 22:47:44 +01:00
$rBill , $rProducts , $rRes , $rRat , $rChangeprice , $rManager , '1' , '7' );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:54:51 +01:00
public function updateOrCreateUserInHist ( $pdo , $id , $username ,
2020-11-19 22:47:44 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat , $rBill , $rProducts , $rRes , $rRat , $rChangeprice , $rManager ,
2020-11-19 22:44:19 +01:00
$active , $histaction ) {
$sql_insert_histuser = " INSERT INTO %histuser% (`id` , `userid`, `username` ,
`is_admin` , `right_waiter` , `right_kitchen` , `right_bar` , `right_supply` , `right_paydesk` ,
2020-11-19 22:47:44 +01:00
`right_statistics` , `right_bill` , `right_products` , `right_reservation` , `right_rating` , `right_changeprice` , `right_manager` , `active` ) VALUES (
NULL , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) " ;
2020-11-19 22:44:19 +01:00
$pdo -> beginTransaction ();
$stmt_insert_histuser = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_insert_histuser ));
$stmt_insert_histuser -> execute ( array ( $id , $username ,
2020-11-19 22:47:44 +01:00
$isAdmin , $rWaiter , $rKitchen , $rBar , $rSupply , $rPayDesk , $rStat , $rBill , $rProducts , $rRes , $rRat , $rChangeprice , $rManager ,
2020-11-19 22:44:19 +01:00
$active ));
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , $histaction , $newRefIdForHist );
$pdo -> commit ();
}
2020-11-19 22:47:44 +01:00
public function insertSaveHistEntry ( $pdo ) {
$this -> insertIntoHist ( $pdo , 9 , null );
}
public function insertRestoreHistEntry ( $pdo ) {
$this -> insertIntoHist ( $pdo , 10 , null );
}
public 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 $aProd ) {
$prodid = $aProd [ " id " ];
$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 ();
$extrasList = $row -> extraslist ;
$sql = " INSERT INTO %histprod% (id,prodid,shortname,longname,priceA,priceB,priceC,tax,sorting,available,extras) " ;
$sql .= " SELECT null,id as prodid,shortname,longname,priceA,priceB,priceC,tax,sorting,available,' $extrasList ' as extras FROM %products% " ;
$sql .= " WHERE %products%.id=? " ;
$stmt = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql ));
$stmt -> execute ( array ( $prodid ));
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , '1' , $newRefIdForHist );
}
}
2020-11-19 22:44:19 +01:00
/*
* Read the complete products table and fill in these values to the histtable
*/
public function readProdTableAndSendToHist ( $pdo ) {
$sql_query = " SELECT * FROM %products% WHERE removed is null " ;
$sql_insert_histprod = " INSERT INTO %histprod% (id,prodid,shortname,longname,
2020-11-19 22:47:44 +01:00
priceA , priceB , priceC , tax , sorting , available , favorite ) VALUES (
NULL , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) " ;
2020-11-19 22:44:19 +01:00
$stmt_query = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_query ));
$stmt_insert_histprod = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_insert_histprod ));
$stmt_query -> execute ();
$result = $stmt_query -> fetchAll ();
foreach ( $result as $row ){
$stmt_insert_histprod -> execute ( array ( $row [ 'id' ], $row [ 'shortname' ],
$row [ 'longname' ], $row [ 'priceA' ], $row [ 'priceB' ], $row [ 'priceC' ],
2020-11-19 22:47:44 +01:00
$row [ 'tax' ], $row [ 'sorting' ], $row [ 'available' ], '0' ));
2020-11-19 22:44:19 +01:00
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , '1' , $newRefIdForHist );
}
}
2020-11-19 22:47:44 +01:00
public function updateProdInHist ( $pdo , $prodid , $shortname , $longname ,
$priceA , $priceB , $priceC , $tax , $sorting , $available , $audioFile , $favorite , $histextra ) {
$this -> updateOrCreateProdInHist ( $pdo , $prodid , $shortname , $longname ,
$priceA , $priceB , $priceC , $tax , $sorting , $available , '4' , $audioFile , $favorite , $histextra );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
public function createProdInHist ( $pdo , $prodid , $shortname , $longname ,
$priceA , $priceB , $priceC , $tax , $sorting , $available , $audioFile , $favorite ) {
$this -> updateOrCreateProdInHist ( $pdo , $prodid , $shortname , $longname ,
$priceA , $priceB , $priceC , $tax , $sorting , $available , '5' , $audioFile , $favorite , null );
2020-11-19 22:44:19 +01:00
}
2020-11-19 22:47:44 +01:00
public function updateOrCreateProdInHist ( $pdo , $prodid , $shortname , $longname ,
$priceA , $priceB , $priceC , $tax , $sorting , $available , $histaction , $audioFile , $favorite , $histextra ) {
2020-11-19 22:44:19 +01:00
$sql_insert_histprod = " INSERT INTO %histprod% (id,prodid,shortname,longname,
2020-11-19 22:47:44 +01:00
priceA , priceB , priceC , tax , sorting , available , audio , favorite , extras ) VALUES (
NULL , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) " ;
2020-11-19 22:44:19 +01:00
2020-11-19 22:47:44 +01:00
if ( is_null ( $pdo )) {
$pdo = $this -> dbutils -> openDbAndReturnPdo ();
}
2020-11-19 22:44:19 +01:00
$stmt_insert_histprod = $pdo -> prepare ( $this -> dbutils -> resolveTablenamesInSqlString ( $sql_insert_histprod ));
$stmt_insert_histprod -> execute ( array ( $prodid , $shortname , $longname ,
2020-11-19 22:47:44 +01:00
$priceA , $priceB , $priceC , $tax , $sorting , $available , $audioFile , $favorite , $histextra ));
2020-11-19 22:44:19 +01:00
$newRefIdForHist = $pdo -> lastInsertId ();
$this -> insertIntoHist ( $pdo , $histaction , $newRefIdForHist );
}
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:47:44 +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 ();
$this -> insertIntoHist ( $pdo , '6' , $newRefIdForHist );
$pdo -> commit ();
}
/*
* 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 ();
}
private function insertIntoHist ( $pdo , $action , $refIdForHist ) {
2020-11-19 22:47:44 +01:00
date_default_timezone_set ( DbUtils :: getTimeZone ());
2020-11-19 22:44:19 +01:00
$currentTime = date ( 'Y-m-d H:i:s' );
2020-11-19 22:47:44 +01:00
$sql_insert_hist = " INSERT INTO %hist% (id,date,action,refid) VALUES (NULL,?,?,?) " ;
$stmt_insert_hist = $pdo -> prepare ( DbUtils :: substTableAlias ( $sql_insert_hist ));
2020-11-19 22:44:19 +01:00
$stmt_insert_hist -> execute ( array ( $currentTime , $action , $refIdForHist ));
}
}
?>