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')); $stmt->execute(array('9', 'DbSave', 'Komplettsicherung der Datenbank')); $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) { if (!is_null($extraVal)) { if (strlen($extraVal) > 299) { $extraVal = substr($extraVal, 0, 299); } } $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)); try { if (strlen($extraVal) > 300) { $extraVal = substr($extraVal, 0, 300); } $stmt->execute(array($extraVal,$newRefIdForHist)); } catch (Exception $ex) { } } 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); } public function updateConfigInHist($pdo,$theItem, $theValue) { $sql_find_id = "SELECT id FROM %config% WHERE name='$theItem'"; $sql_insert_histconfig = "INSERT INTO %histconfig% (id,configid,setting) VALUES (NULL,?,?)"; $pdo->beginTransaction(); $stmt_query = $pdo->query(DbUtils::substTableAlias($sql_find_id)); $row =$stmt_query->fetchObject(); $theConfigId = $row->id; $stmt_insert_histconfig = $pdo->prepare(DbUtils::substTableAlias($sql_insert_histconfig)); $stmt_insert_histconfig->execute(array($theConfigId,"$theValue")); $newRefIdForHist = $pdo->lastInsertId(); self::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 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)); } }