From fcfeb80627ba486e48dc659a445ab9bf6473c1a5 Mon Sep 17 00:00:00 2001
From: Marius Burkard <m.burkard@pixcept.de>
Date: Fri, 14 Dec 2018 16:25:13 +0100
Subject: [PATCH] - added datalog undelete to viewer

---
 interface/lib/classes/db_mysql.inc.php        |  3 ++
 interface/web/monitor/dataloghistory_undo.php | 34 ++++++++++++++-----
 interface/web/monitor/dataloghistory_view.php |  1 +
 .../lib/lang/de_dataloghistory_undo.lng       |  1 +
 .../lib/lang/en_dataloghistory_undo.lng       |  1 +
 .../monitor/templates/dataloghistory_view.htm |  2 +-
 6 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/interface/lib/classes/db_mysql.inc.php b/interface/lib/classes/db_mysql.inc.php
index 8443d106b1..c4764cb555 100644
--- a/interface/lib/classes/db_mysql.inc.php
+++ b/interface/lib/classes/db_mysql.inc.php
@@ -759,6 +759,9 @@ class db
 		
 		$old_rec = array();
 		$index_value = $this->insertID();
+		if(!$index_value && isset($insert_data[$index_field])) {
+			$index_value = $insert_data[$index_field];
+		}
 		$new_rec = $this->queryOneRecord("SELECT * FROM ?? WHERE ?? = ?", $tablename, $index_field, $index_value);
 		$this->datalogSave($tablename, 'INSERT', $index_field, $index_value, $old_rec, $new_rec);
 
diff --git a/interface/web/monitor/dataloghistory_undo.php b/interface/web/monitor/dataloghistory_undo.php
index 5ff08faa0a..455f8c9d0d 100644
--- a/interface/web/monitor/dataloghistory_undo.php
+++ b/interface/web/monitor/dataloghistory_undo.php
@@ -50,18 +50,36 @@ $dbidx = explode(':', $record['dbidx']);
 
 $old_record = $app->db->queryOneRecord('SELECT * FROM ?? WHERE ??=?', $record['dbtable'], $dbidx[0], $dbidx[1]);
 
-if (is_array($old_record)) {
-	if(!$data = unserialize(stripslashes($record['data']))) {
-		$data = unserialize($record['data']);
+if($record['action'] === 'u') {
+	if (is_array($old_record)) {
+		if(!$data = unserialize(stripslashes($record['data']))) {
+			$data = unserialize($record['data']);
+		}
+
+		$new_record = $data['old'];
+
+		$app->db->datalogUpdate($record['dbtable'], $new_record, $dbidx[0], $dbidx[1]);
+
+		$app->tpl->setVar('success', true);
+	} else {
+		$app->tpl->setVar('success', false);
 	}
+} elseif($record['action'] === 'd') {
+	if(is_array($old_record)) {
+		$app->tpl->setVar('success', false);
+		$app->tpl->setVar('error_txt', $wb['error_undelete_txt']);
+	} else {
+		if(!$data = unserialize(stripslashes($record['data']))) {
+			$data = unserialize($record['data']);
+		}
 
-	$new_record = $data['old'];
+		$new_record = $data['old'];
+		/* TODO: maybe check some data, e. g. server_id -> server still there?, sys_groupid -> sys_group/sys_user still there? */
 
-	$app->db->datalogUpdate($record['dbtable'], $new_record, $dbidx[0], $dbidx[1]);
+		$app->db->datalogInsert($record['dbtable'], $new_record, $dbidx[0]);
 
-	$app->tpl->setVar('success', true);
-} else {
-	$app->tpl->setVar('success', false);
+		$app->tpl->setVar('success', true);
+	}
 }
 
 $app->tpl_defaults();
diff --git a/interface/web/monitor/dataloghistory_view.php b/interface/web/monitor/dataloghistory_view.php
index e3b5e7b63c..2b5ea1e032 100644
--- a/interface/web/monitor/dataloghistory_view.php
+++ b/interface/web/monitor/dataloghistory_view.php
@@ -109,6 +109,7 @@ switch ($record['action']) {
 }
 
 $app->tpl->setVar($out);
+$app->tpl->setVar('can_undo', ($out['action_char'] === 'u' || $out['action_char'] === 'd'));
 
 $app->tpl_defaults();
 $app->tpl->pparse();
diff --git a/interface/web/monitor/lib/lang/de_dataloghistory_undo.lng b/interface/web/monitor/lib/lang/de_dataloghistory_undo.lng
index 306f7c5eac..9bae987d50 100644
--- a/interface/web/monitor/lib/lang/de_dataloghistory_undo.lng
+++ b/interface/web/monitor/lib/lang/de_dataloghistory_undo.lng
@@ -2,5 +2,6 @@
 $wb['list_head_txt'] = 'Datalog-History-Eintrag';
 $wb['success_txt'] = 'Erfolgreich zurückgesetzt';
 $wb['error_txt'] = 'Fehler beim Zurücksetzen: Eintrag existiert nicht mehr';
+$wb['error_undelete_txt'] = 'Fehler beim Wiederherstellen: Eintrag mit dieser Id existiert bereits';
 $wb['btn_cancel_txt'] = 'Zurück';
 ?>
diff --git a/interface/web/monitor/lib/lang/en_dataloghistory_undo.lng b/interface/web/monitor/lib/lang/en_dataloghistory_undo.lng
index 258d886696..0e040a3e77 100644
--- a/interface/web/monitor/lib/lang/en_dataloghistory_undo.lng
+++ b/interface/web/monitor/lib/lang/en_dataloghistory_undo.lng
@@ -2,5 +2,6 @@
 $wb['list_head_txt'] = 'Data Log History Entry';
 $wb['success_txt'] = 'Undo successful';
 $wb['error_txt'] = 'Error during undo: Record does not exist anymore';
+$wb['error_undelete_txt'] = 'Error during undelete: Record with primary id already existing.';
 $wb['btn_cancel_txt'] = 'Back';
 ?>
diff --git a/interface/web/monitor/templates/dataloghistory_view.htm b/interface/web/monitor/templates/dataloghistory_view.htm
index 0ce595e265..4ba82bbf05 100644
--- a/interface/web/monitor/templates/dataloghistory_view.htm
+++ b/interface/web/monitor/templates/dataloghistory_view.htm
@@ -116,7 +116,7 @@
 
 <div class="clear">
 	<div class="right">
-		<tmpl_if name='action_char' value='u'>
+		<tmpl_if name='can_undo'>
 			<a class="btn btn-default formbutton-danger formbutton-narrow" href="javascript: ISPConfig.confirm_action('monitor/dataloghistory_undo.php?id={tmpl_var name='id'}','{tmpl_var name='undo_confirmation_txt'}');"> {tmpl_var name='undo_txt'}</a>
 		</tmpl_if>
 		<button class="btn btn-default formbutton-default" type="button" value="{tmpl_var name='btn_cancel_txt'}" data-load-content="monitor/dataloghistory_list.php">{tmpl_var name='btn_cancel_txt'}</button>
-- 
GitLab