Forums

Before and after image log.

NickOg 03 Jun, 2013
Hi

I needed to include before and after images of a db record in an editor for various reasons. I have written the code using JLog and it works fine. I have it in mind to change it to write to a table in order to simplify analysis of changes and so on.

Before I start on that, how difficult would it be to write a plug in that one could drop into the On Load and Submit events to do that automagically? I guess one would need two - one to save the before image on load and a second to write the saved before and new after image to a table.

Regards
GreyHead 04 Jun, 2013
Hi Nick,

The DB Save action would probably do it OK; one in the On Load and one in the On Submit event.

Bob
NickOg 04 Jun, 2013
This one doesn't seem quite right, Bob.

As I understand it, if I put a DB Save in the onLoad event that would save a copy of the data before it was edited - the Before Image. Then, once edited, the DB Save in the submit event would save that edited data - the After Image.
The problem there is one wouldn't want to save the Before Image data until the Submit Event had been triggered. I have managed that by, in the on load event, saving a copy of the Before Image into the form data object so:-

// save the current data on entry
$beforeImage= $form->data;
// as beforeImage
$form->data['beforeImage']=$beforeImage;


Then, in the Submit event, after DB Save, I extract the before image so
// save the before image
$beforeImage=$theDataObect['beforeImage'];
// delete it from the data array
unset($theDataObect['beforeImage']);
// save the after image
$afterImage=$theDataObect;


and then write both the before and after image to the log. That works fine and I can make it write to a database table rather than the log easily enough. But my question is, how would I go about writing an action to do that? It isn't exactly a rhetorical question - more sizing up the task to make that process a custom action or actions that might be more widely usable.

Does that make sense?
Max_admin 06 Jun, 2013
Hi Nick,

Writing action is not something hard, you can check the "Custom code" action and do something similar, you may also check the "email verification" action for the code to trigger/use the "DB Save" action class, that would do what you need easily I think!

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
NickOg 06 Jun, 2013
Thanks Max - I will come back to it and give it a try.

At the moment I am stuck carrying the 'before image' via $form->data['beforeImage'] set in onLoad to then retrieve onSubmit, after the form has been edited. I had it in mind, onSubmit, to extract that beforeImage, save that, unset it from $form->data and then save $form->data as an afterImage. The problem that I have hit is that I cannot unset beforeImage. It seems to cascade through a series of form submissions.

It may be that I have not understood Bob's suggestion of DBSave on load and again in Submit. I need to try things out some more.

Regards

Nick
GreyHead 07 Jun, 2013
Hi Nick,

I'm far from clear what you need to do here?

The only problem that I can see with not having a DB in the ON Load event is that the user may not submit the form so you never get the second version to save.

One way round that would be to grab the data in the On Load event; JSON encode it into a string; save the string into the User session. Then you can recover the data and remove it from the session.

Bob
NickOg 07 Jun, 2013
Hi Bob,

That is my problem precisely. I was doing something similar - just saving that data as an array rather than an encoded string. But for reasons I could not understand and find difficult to explain I could not then get remove the data from the session. I was using unset to remove the before image copy.

I am thinking a better approach may be to save the before image to a database record onLoad and either a second after image onsubmit. If I pass the record number across from onload to onsubmit I can add the after image to the record and update it. I think. I guess the problem with that approach is if someone backs out of the form without submitting it then I will be left with an orphan before image.

The other option I had considered was to start a transaction (I have change the db to InnoDB) in OnLoad and close it Onsubmit. I am not sure if the opened transaction will carry across sessions though. Still pondering. I had better set up a small test form to check the options.
Max_admin 13 Jun, 2013
Hello,

I don't think the DB transactions are working the way you need here, you can simply do it the way Bob has explained, but if the user waits too long before submitting the form then your will lose the data in the session, but you can increase the session life time or check the data after submission and block the user if it doesn't exist..etc

You can do it your way too, and then write a different piece of code to drop any open records using some cron job.

You may also encode the "beforeImage" array using serialize then base64 and place this code into a hidden textarea in your form so its kept when the form is submitted, decode it back and use it, but the user may alter it if they want to.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
NickOg 13 Jun, 2013
Thanks Max & Bob

A fair bit to digest and try out here, A few things to clean up and I need to come back and resolve the matter.

I will reply and explain where I get to.

Regards

Nick
This topic is locked and no more replies can be posted.