Undo last edit?

Undo last edit?

monkeyboymonkeyboy Posts: 60Questions: 19Answers: 0

Does anyone know how to create an "undo" button?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I don't think it has been attempted yet - at least not to my knowledge.

    You have two options:

    1. Use an append only database and just roll back as required
    2. Store the information that was changed in a set of variables (or an array if you want multi-level undo) which will give you the ability to send a new request to reset it to the old data.

    Where it gets interesting is considering the concurrency of multiple users and sessions. You would presumably only want to undo the last change for that session - or possibly even that browser window.

    Allan

  • monkeyboymonkeyboy Posts: 60Questions: 19Answers: 0

    Allan,

    I am considering how to do this. In the PHP side:

    function backupRow($list, $table, $id, $Uid, $delete=false)
    {
        global $dbc;
        
        if (!$delete)
        {
            $backupQuery = "INSERT INTO `$table`
            ($list,
                `createdBy`, `createTS`, `editedBy`, `editTS`, `deletedBy`, `deleteTS`, `archive`, `deleted`)  
            (SELECT
                $list,
                `createdBy`, `createTS`, `editedBy`, `editTS`, `deletedBy`, `deleteTS`,
                1, `deleted` FROM `$table` 
            WHERE id=$id)";
        }
        else // deleted Row
        {
            $backupQuery = "INSERT INTO `$table`
            ($list,
                `createdBy`, `createTS`, `editedBy`, `editTS`, `deletedBy`, `deleteTS`, `archive`, `deleted`)  
            (SELECT 
                $list,
                `createdBy`, `createTS`, `editedBy`, `editTS`, '".$attUid."', NOW(), `archive`, 1 
            FROM `$table` 
            WHERE id=$id)";     
        }
        
        $backupResult = mysqli_query($dbc, $backupQuery);
        if (!$backupResult)
        {
            errorlib::logmsg ("Error in $backupQuery : ". mysqli_error($dbc));
        }
    

    .
    .
    .

    Editor::inst( $db, 'myTable' )
        ->fields( 
    ........
            ->on( 'preCreate', function ( $editor, $values ) {
                $editor
                    ->field( myTable.createdBy' )
                    ->setValue( $GLOBALS["Uid"] );
                $editor
                    ->field( 'myTable.createTS' )
                    ->setValue( date('Y-m-d H:i:s') );
                })
    
            ->on( 'preEdit', function ( $editor, $id, $values ) {
                backupRow($id, $GLOBALS["Uid"],false);  
                $editor
                    ->field( 'myTable.editedBy' )
                    ->setValue( $GLOBALS["Uid"] );
                $editor
                    ->field( 'myTable.editTS' )
                    ->setValue( date('Y-m-d H:i:s') );
                    
                })
    
            ->on( 'postEdit', function ( $editor, $id, $values ) 
                {
                    // something happens here (updating another related table)
                })
    
    
            ->on( 'preRemove', function ( $editor, $id, $values ) {
                backupRow($id, $GLOBALS["Uid"],true);   
                })
    
            ->process( $_POST )
            ->json();
    

    This retains the current user's previous state (all edits by timestamp)
    An undo function might not even depend on a session. A single inline edit could be undone.

    If anyone wants to write the undo portion - I hope they post it.
    I plan to do this in about 60 days, and will post if no one else has offered a solution

  • monkeyboymonkeyboy Posts: 60Questions: 19Answers: 0

    The code above creates a historical record of all changes, who made the change, and when the change occurred. It establishes an audit-able trail of changes. How to report on this is left as an exercise for the user (meaning I have not figure that out yet :blush: )

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Thanks for sharing this, its an interesting issue. I can't promise to look into it myself, but if I have time I will do so.

    Allan

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    I wish I could add to this work! An undo feature would be an excellent addition to Editor.

  • cudzich09cudzich09 Posts: 12Questions: 2Answers: 1

    Agreed!

    I'm about to tackle this project with the array-based solution, storing row edits as they are triggered. We'll see how this plays out, thanks for the sample of code!

This discussion has been closed.