Email Notification Diff on Update

A place for users and developers of the Xataface to discuss and receive support.

Email Notification Diff on Update

Postby ADobkin » Sun Aug 07, 2011 1:18 am

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
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA

Re: Email Notification Diff on Update

Postby shannah » Mon Aug 08, 2011 9:37 am

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.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Email Notification Diff on Update

Postby ADobkin » Mon Aug 08, 2011 10:45 am

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.
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA

Re: Email Notification Diff on Update

Postby ADobkin » Mon Aug 08, 2011 3:38 pm

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?
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 17 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved