Skip to content

Commit

Permalink
Issue #5382 History/Revert changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Feb 6, 2025
1 parent 73d4e28 commit 10ed5f8
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
23 changes: 18 additions & 5 deletions e107_admin/history.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class admin_history_ui extends e_admin_ui
'history_table' => array ( 'title' => 'Table', 'type' => 'text', 'data' => 'safestr', 'width' => 'auto', 'filter' => true, 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'left', 'thclass' => 'left',),
'history_record_id' => array ( 'title' => LAN_ID, 'type' => 'number', 'data' => 'int', 'width' => '5%', 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'left', 'thclass' => 'left',),
'history_action' => array ( 'title' => 'Action', 'type' => 'dropdown', 'data' => 'int', 'width' => 'auto', 'filter' => true, 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'left', 'thclass' => 'left', 'batch' => false,),
'history_data' => array ( 'title' => 'Data', 'type' => 'method', 'data' => 'str', 'width' => 'auto', 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'history-data left', 'thclass' => 'left', 'filter' => false, 'batch' => false,),
'history_data' => array ( 'title' => 'Changed Data', 'type' => 'method', 'data' => 'str', 'width' => 'auto', 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'history-data left', 'thclass' => 'left', 'filter' => false, 'batch' => false,),
'history_user_id' => array ( 'title' => LAN_USER, 'type' => 'user', 'data' => 'int', 'width' => '5%', 'filter' => true, 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'left', 'thclass' => 'left',),
'history_restored' => array ( 'title' => "Restored", 'type' => 'datestamp', 'data' => 'int', 'width' => '5%', 'filter' => true, 'help' => '', 'readParms' => [], 'writeParms' => [], 'class' => 'center', 'thclass' => 'center',),

Expand All @@ -94,7 +94,8 @@ public function init()
{
$this->fields['history_action']['writeParms']['optArray'] = [
'delete' => "<span class='label label-danger'>". LAN_DELETE."</span>",
'update' => "<span class='label label-success'>". LAN_UPDATE."</span>"
'update' => "<span class='label label-success'>". LAN_UPDATE."</span>",
'restore' => "<span class='label label-warning'>Restore</span>"
];


Expand Down Expand Up @@ -134,16 +135,24 @@ private function processRestoreAction($id, $action)
$pid = $historyRow['history_pid'];
$recordId = $historyRow['history_record_id'];



if (!empty($originalTable) && !empty($originalData) && !empty($pid) && !empty($recordId))
{

if($action === 'insert')
{
$originalData[$pid] = (int) $recordId;
$result = $db->replace($originalTable, $originalData);
}
else
else // update
{
$backup = $db->retrieve($originalTable, '*', $pid. ' = '.(int) $recordId);
if($changes = array_diff_assoc($originalData, $backup))
{
$old_changed_data = array_intersect_key($backup, $changes);
$this->backupToHistory($originalTable, $pid, $recordId, 'restore', $old_changed_data, false);
}

$originalData['WHERE'] = $pid .' = '. (int) $recordId;
$result = $db->update($originalTable, $originalData);
}
Expand All @@ -153,6 +162,10 @@ private function processRestoreAction($id, $action)
$message->addSuccess("The record (ID: $id) has been successfully restored to the $originalTable table.", 'default', true);
$db->update('admin_history', "history_restored = ".time()." WHERE history_id = $id");
}
elseif($result === 0)
{
$message->addInfo("No changes made", 'default', true);
}
else
{
$message->addError("Failed to restore the record (ID: $id) to the $originalTable table.", 'default', true);
Expand Down Expand Up @@ -325,7 +338,7 @@ public function options($parms, $value, $id, $att = [])
$text .= "<button class='btn btn-default' type='submit' name='$name' title='{$restoreTitle}'><i class='admin-ui-option fa fa-undo fa-2x fa-fw'></i></button>";
}

$att['readParms']['editClass'] = 999; // disable it.
$att['readParms']['editClass'] = 999; // disable it.
$text .= $this->renderValue('options', $value, $att, $id);

// End options group
Expand Down
2 changes: 1 addition & 1 deletion e107_core/sql/core_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
history_table varchar(64) NOT NULL default '',
history_pid varchar(64) NOT NULL default '',
history_record_id int(10) unsigned NOT NULL default '0',
history_action enum('delete','update') NOT NULL,
history_action enum('delete','restore','update') NOT NULL,
history_data JSON DEFAULT NULL,
history_user_id int(10) unsigned NOT NULL default '0',
history_datestamp int(10) unsigned NOT NULL default '0',
Expand Down
3 changes: 3 additions & 0 deletions e107_core/templates/admin_icons_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
$ADMIN_ICONS_TEMPLATE['E_16_PLUGMANAGER'] = "<i class='S16 e-plugmanager-16'></i>";
$ADMIN_ICONS_TEMPLATE['E_16_THEMEMANAGER'] = "<i class='S16 e-themes-16'></i>";

$ADMIN_ICONS_TEMPLATE['E_16_UNDO'] = "<img class='icon S16' src='" . e_IMAGE . "admin_images/undo_16.png' alt='' />";
$ADMIN_ICONS_TEMPLATE['E_32_UNDO'] = "<img class='icon S32' src='" . e_IMAGE . "admin_images/undo_32.png' alt='' />";

$ADMIN_ICONS_TEMPLATE['ADMIN_WARNING_ICON'] = "<i class='fa fa-warning text-warning'></i>";
$ADMIN_ICONS_TEMPLATE['ADMIN_GRID_ICON'] = "<i class='fa fa-th'></i>";
$ADMIN_ICONS_TEMPLATE['ADMIN_LIST_ICON'] = "<i class='fas fa-list'></i>";
Expand Down
12 changes: 8 additions & 4 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -4587,15 +4587,19 @@ public function getParentChildQry($orderby=false)
* @param int $id The ID of the specific record being backed up.
* @param string $action The action performed on the record (e.g., 'update' or 'delete').
* @param array $data An associative array of field data to be included in the history record.
* @param bool $posted Whether the data has been posted and requires additional filter based on current $fields values or not.
* @return bool True on successful creation of the backup record, false on failure.
*/
protected function backupToHistory($table, $pid, $id, $action, $data)
protected function backupToHistory($table, $pid, $id, $action, $data, $posted = true)
{
foreach($data as $field=>$var)
if($posted)
{
if(empty($this->fields[$field]['data'])) // exclude data not in the table.
foreach($data as $field=>$var)
{
unset($data[$field]);
if(empty($this->fields[$field]['data'])) // exclude data not in the table.
{
unset($data[$field]);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions e107_handlers/sitelinks_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,8 @@ function adminLinks($mode=false)
// 7 => array(e_ADMIN.'download.php', ADLAN_24, ADLAN_25, 'R', 3, E_16_DOWNL, E_32_DOWNL),
8 => array(e_ADMIN_ABS.'emoticon.php', ADLAN_58, ADLAN_59, 'F', 1, E_16_EMOTE, E_32_EMOTE),
// 9 => array(e_ADMIN.'filemanager.php', ADLAN_30, ADLAN_31, '6', 5, E_16_FILE, E_32_FILE), // replaced by media-manager
9 => array(e_ADMIN.'history.php', LAN_HISTORY, LAN_HISTORY, '7', 4, E_16_UNDO, E_32_UNDO),

10 => array(e_ADMIN_ABS.'frontpage.php', ADLAN_60, ADLAN_61, 'G', 1, E_16_FRONT, E_32_FRONT),
11 => array(e_ADMIN_ABS.'image.php', LAN_MEDIAMANAGER, LAN_MEDIAMANAGER, 'A', 5, E_16_IMAGES, E_32_IMAGES),
12 => array(e_ADMIN_ABS.'links.php', LAN_NAVIGATION, ADLAN_139, 'I', 1, E_16_LINKS, E_32_LINKS),
Expand Down
2 changes: 2 additions & 0 deletions e107_handlers/user_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1964,11 +1964,13 @@ function __construct()

// Tools
"Y" => array(ADLAN_147,E_16_INSPECT, E_32_INSPECT), // File inspector
"7" => array(defset('LAN_HISTORY', 'History'), E_16_UNDO, E_32_UNDO), // History/Undo
"9" => array(ADLAN_40, E_16_MAINTAIN, E_32_MAINTAIN), // Take Down site for Maintenance
"O" => array(ADLAN_149,E_16_NOTIFY, E_32_NOTIFY), // Notify
"U" => array(ADLAN_157,E_16_CRON, E_32_CRON), // Schedule Tasks
"S" => array(ADLAN_155,E_16_ADMINLOG, E_32_ADMINLOG), // System Logging


// Manage
"B" => array(LAN_COMMENTMAN,E_16_COMMENT, E_32_COMMENT), // Moderate Comments
"6" => array(LAN_MEDIAMANAGER,E_16_FILE, E_32_FILE), // File-Manager - Upload /manage files -
Expand Down
4 changes: 3 additions & 1 deletion e107_languages/English/admin/lan_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,6 @@

define("LAN_SYSTEM_NOTIFICATIONS_X", "[x] System Notification(s)");
define("LAN_PHP_OUTDATED", "Your website is currently running an [outdated version of PHP], which may pose a security risk. If your plugins will allow it, we recommend upgrading to [x] to ensure that your website is secure and up-to-date.");
define("LAN_DATABASE_UPDATE", "An update is available for your database. We recommend [running this update] as soon as possible to ensure that your database is secure and up-to-date.");
define("LAN_DATABASE_UPDATE", "An update is available for your database. We recommend [running this update] as soon as possible to ensure that your database is secure and up-to-date.");

define("LAN_HISTORY", "History");

0 comments on commit 10ed5f8

Please sign in to comment.