Page 1 of 1

Email Notification Diff on Update

PostPosted: Sun Aug 07, 2011 1:18 am
by ADobkin
Some of my tables send out e-mail notifications using the beforeUpdate delegate class method. I use a function that dumps each field into the body of the message to summarize the record content like this:

Code: Select all
        foreach (array_keys($record->_table->fields()) as $fieldname){
                $key = $fieldname;
                $value = $record->display($key);
                $content .= "$key: $value\n";
        }


That part works well and is helpful to see when someone is updating the record, but it is not usually obvious what was changed. Is it possible to use the getDiffs function in HistoryTool.php for this, or is there another function to indicate what has changed?

Also, there a method to send out e-mail notification or write to a log file, etc. when a user logs in, changes their password, etc.? I suppose I could add some mail statements to the authenticate function in AuthenticationTool.php for successful and/or failed logins, as well as the change_password.php action. Is this the best way to do it?

Thanks,
Alan

Re: Email Notification Diff on Update

PostPosted: Mon Aug 08, 2011 9:37 am
by shannah
Yes. You can use the history tool for this.
e.g.
Code: Select all
$ht = new Dataface_HistoryTool();
$log = $ht->getHistoryLog($record, null, 1);
   // Gets the most recent history log entry
import('Text/Diff.php');
import('Text/Diff/Renderer/inline.php');
$renderer = new Text_Diff_Renderer_inline();

$diff_content = '';
foreach (array_keys($record->_table->fields()) as $fieldname){
                $key = $fieldname;
                $value = $record->display($key);
                if ( count($log) > 0 ){
                    $historyValue = $log[0]->display($key);
                    $diff = new Text_Diff(explode("\n", $historyValue), explode("\n", $value));
                    $diff_content .= "$key: ".$renderer->render($diff)."\n";

                }
                $content .= "$key: $value\n";
        }



Or something along those lines.

Re: Email Notification Diff on Update

PostPosted: Mon Aug 08, 2011 10:45 am
by ADobkin
Thanks! That looks perfect, but I'm having a little trouble getting it to work.

First, I got a fatal error "Class 'Dataface_HistoryTool' not found in" ..., so I added this line at the beginning:

Code: Select all
import('Dataface/HistoryTool.php');


Then, I got a fatal error "Call to a member function display() on a non-object" in the following line:

Code: Select all
$historyValue = $log[0]->display($key);


I changed it to remove the display() function like this:

Code: Select all
$historyValue = $log[0]->$key;


Now it works without any errors, but it reports all of the fields and values, not just the changes.

Re: Email Notification Diff on Update

PostPosted: Mon Aug 08, 2011 3:38 pm
by ADobkin
I've been hacking on this option for a while today and think I am close to a solution, but not quite there yet. It appears that the getHistoryData function call only returns metadata about the history record, but not the actual record. So, when the diff runs against historyValue, there is nothing there to compare it to.

Should I use the function getPreviousVersion to retrieve the actual record and then run the diff on that instance?