xtrim Posted December 28, 2007 Posted December 28, 2007 Hi, How do I implement audit trail using cwp (PHP)?
Genx Posted December 28, 2007 Posted December 28, 2007 That's a bit of a broad question really where the answer is simple, but maybe not if you're not sure what you're doing... 1) User "Saves" data 2) This data is POSTed to your submission page - not the database 3) You should have posted a record ID with the rest of your data 4) Send a request to fm for the current record details i.e. $record = $fm->getRecordById('layout',$_POST[recid]) 5) Compare your fields in the current record to those the user just submitted: $rec = $fm->getRecordById('layout',$_POST[recid]); if ( $rec->getField('myField') <> $_POST['myField'] ) { $audit[myField][prev] = $rec->getField('myField'); $audit[myField][new] = $_POST['myField']; } ... or better yet, just stick a function at the top of your page: function audit_check($fieldName,$record,&$audit){ if( $record->getField($fieldName) <> $_POST[$fieldName]) $audit[$fieldName] = array("prev"=>$record->getField($fieldName),"new"=>$_POST[$fieldName]); } $record = $fm->getRecordById('layout',$_POST[recid]); audit_check('field_1',$record,$audit_array); audit_check('field_2',$record,$audit_array); audit_check('field_3',$record,$audit_array); audit_check('field_4',$record,$audit_array); if( count($audit) > 0 ) { foreach( $audit_array as $fieldName=>$entry ) { $entry['fieldName'] = $fieldName; $entry['parent_id'] = $record->getField('pk'); /*This should be your PK field*/ $add = $fm->newAddCommand('auditLayout',$entry) /*assumes you have four fields called "prev", "new", "fieldName" and "parent_id" on a layout called "auditLayout", you could also do this to a portal attached to the main record - would probably also be quicker, but for the sake of simplicity....*/ $res = $add->execute(); /*Might want to trap for an error here*/ } } You would then proceed to actually post the changes to your database... But to re-iterate this is the idea: 1) Database has data 2) User sends data to php - not to database 3) You can retrieve the current record values and compare to user submitted values - save in temporary array 4) Save records to database 5) Submit Audit to wherever you are storing it in the db. Feel free to ask if you want clarification
xtrim Posted December 29, 2007 Author Posted December 29, 2007 Thats a good Idea. I will try this 1. $myAudit = contains all changes 2. submit actual data 3. create new record in audit table with $myAudit details. Thank you
Recommended Posts
This topic is 6436 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now