ordersprinter/webapp/php/utilities/HistFiller.php

248 lines
9.9 KiB
PHP
Raw Normal View History

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) {
2020-11-19 23:03:48 +01:00
$sql = "SELECT * FROM %user% WHERE active='1'";
2020-11-19 22:59:47 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array());
2020-11-19 23:03:48 +01:00
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
2020-11-19 22:59:47 +01:00
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) {
2020-11-19 23:03:48 +01:00
self::updateOrCreateUserEntryInHist($pdo, $userid, $histaction, self::getColNamesForUserHistTable(), 'userid', 'user','histuser',null,null);
2020-11-19 22:59:47 +01:00
}
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);
}
2020-11-19 23:03:48 +01:00
private static function updateOrCreateUserEntryInHist($pdo,$id,$histaction,$colsInSourceTable,$idInHist,$sourcetable, $histtable,$extraCol,$extraVal) {
if (!is_null($extraVal)) {
if (strlen($extraVal) > 299) {
$extraVal = substr($extraVal, 0, 299);
}
}
$sql = "SELECT * from %". $sourcetable . "%,%roles% WHERE %user%.id=? AND %user%.roleid=%roles%.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) {
if ($aHistCol == "id") {
$vals[] = $id;
} else {
$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));
try {
if (strlen($extraVal) > 300) {
$extraVal = substr($extraVal, 0, 300);
}
$stmt->execute(array($extraVal,$newRefIdForHist));
} catch (Exception $ex) {
}
}
self::insertIntoHist($pdo, $histaction, $newRefIdForHist);
}
2020-11-19 22:59:47 +01:00
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));
2020-11-19 23:03:07 +01:00
try {
if (strlen($extraVal) > 300) {
$extraVal = substr($extraVal, 0, 300);
}
$stmt->execute(array($extraVal,$newRefIdForHist));
} catch (Exception $ex) {
}
2020-11-19 22:59:47 +01:00
}
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 23:10:26 +01:00
2020-11-19 22:47:44 +01:00
public function updateConfigInHist($pdo,$theItem, $theValue) {
2020-11-19 23:10:26 +01:00
$sql = "SELECT id FROM %config% WHERE name=?";
$result = CommonUtils::fetchSqlAll($pdo, $sql, array($theItem));
if (count($result) == 0) {
$sql = "INSERT INTO %config% (name,setting) VALUES(?,?)";
CommonUtils::execSql($pdo, $sql, array($theItem,$theValue));
$idInConfig = $pdo->lastInsertId();
$action = 2;
} else {
$sql = "UPDATE %config% SET setting=? WHERE name=?";
CommonUtils::execSql($pdo, $sql, array($theValue,$theItem));
$idInConfig = $result[0]["id"];
$action = 6;
}
$sql = "INSERT INTO %histconfig% (configid,setting) VALUES (?,?)";
CommonUtils::execSql($pdo, $sql, array($idInConfig,$theValue));
2020-11-19 22:44:19 +01:00
$newRefIdForHist = $pdo->lastInsertId();
2020-11-19 23:10:26 +01:00
self::insertIntoHist($pdo, $action, $newRefIdForHist);
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:10:26 +01:00
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');
2020-11-19 23:10:26 +01:00
$sql = "INSERT INTO %hist% (date,action,refid) VALUES (?,?,?)";
CommonUtils::execSql($pdo, $sql, array($currentTime, $action, $refIdForHist));
2020-11-19 22:44:19 +01:00
}
2020-11-19 23:03:35 +01:00
}