easy way to check if a value has changed (nodeapi)
Hi there, I'm working on a (node) profile system that has a checkbox to allow people to be informed by postal mail. If checked the system sends a mail with the details to a certain email address. Of course this email shouldn't be sent every time the profile is updated, but only when the checkbox was previously unchecked (if it was checked allready, nothing should happen). For the moment I'm thinking of creating a second (hidden) value to which I compare on 'update' and sync with the actual value after the check. But I'm thinking there must be an easier way, but can't find it in documentation or by inspecting $node on 'update'. Tnx, HnLn
No real need for a second checkbox. Since you only want the email sent if the check box is checked (and not previously checked) don't store the status of the check box and always display as un-checked. That way if the user checks the check box, you send the email.
2009/8/22 Hans Langouche <hans.langouche@gmail.com>:
Hi there,
I'm working on a (node) profile system that has a checkbox to allow people to be informed by postal mail. If checked the system sends a mail with the details to a certain email address. Of course this email shouldn't be sent every time the profile is updated, but only when the checkbox was previously unchecked (if it was checked allready, nothing should happen).
For the moment I'm thinking of creating a second (hidden) value to which I compare on 'update' and sync with the actual value after the check. But I'm thinking there must be an easier way, but can't find it in documentation or by inspecting $node on 'update'.
Wouldn't it suffice adding a table (uid, sent) so when user checked the checkbox it will check for a record in that table for the current user, if not there, it will send system email and add a record to that table? That way, you know if the checkbox was checked previously and can decide to display it defaulting to checked, or just hide the checkbox. Henrique
Tnx,
HnLn
Perhaps, on hook_api($op = 'presave'), load a copy of the node from the db, and check if the field has changed? <?php function yourmodule_nodeapi($op, &$node, $a3 = NULL, a4 = NULL) { if ($op == 'presave' && $node->type == 'your_type') { $old = node_load($node->nid); if ($old->field_notify_by_email == 0 && $node->field_notify_by_email == 1) { // Send the changes via email. } } } ?> Henrique Recidive wrote:
2009/8/22 Hans Langouche <hans.langouche@gmail.com>:
Hi there,
I'm working on a (node) profile system that has a checkbox to allow people to be informed by postal mail. If checked the system sends a mail with the details to a certain email address. Of course this email shouldn't be sent every time the profile is updated, but only when the checkbox was previously unchecked (if it was checked allready, nothing should happen).
For the moment I'm thinking of creating a second (hidden) value to which I compare on 'update' and sync with the actual value after the check. But I'm thinking there must be an easier way, but can't find it in documentation or by inspecting $node on 'update'.
Wouldn't it suffice adding a table (uid, sent) so when user checked the checkbox it will check for a record in that table for the current user, if not there, it will send system email and add a record to that table?
That way, you know if the checkbox was checked previously and can decide to display it defaulting to checked, or just hide the checkbox.
Henrique
Tnx,
HnLn
tnx all, this approach did the trick and saved me the hassle of creating extra value fields or tables. HnLn Brian Vuyk wrote:
Perhaps, on hook_api($op = 'presave'), load a copy of the node from the db, and check if the field has changed?
<?php
function yourmodule_nodeapi($op, &$node, $a3 = NULL, a4 = NULL) {
if ($op == 'presave' && $node->type == 'your_type') { $old = node_load($node->nid); if ($old->field_notify_by_email == 0 && $node->field_notify_by_email == 1) { // Send the changes via email. } }
}
?>
Henrique Recidive wrote:
2009/8/22 Hans Langouche <hans.langouche@gmail.com>:
Hi there,
I'm working on a (node) profile system that has a checkbox to allow people to be informed by postal mail. If checked the system sends a mail with the details to a certain email address. Of course this email shouldn't be sent every time the profile is updated, but only when the checkbox was previously unchecked (if it was checked allready, nothing should happen).
For the moment I'm thinking of creating a second (hidden) value to which I compare on 'update' and sync with the actual value after the check. But I'm thinking there must be an easier way, but can't find it in documentation or by inspecting $node on 'update'.
Wouldn't it suffice adding a table (uid, sent) so when user checked the checkbox it will check for a record in that table for the current user, if not there, it will send system email and add a record to that table?
That way, you know if the checkbox was checked previously and can decide to display it defaulting to checked, or just hide the checkbox.
Henrique
Tnx,
HnLn
i'm using the user_post_count value from $user array, but in a new site this value don't apeear... why ???
I do exactly that in several modules. But don't use "hidden;" use "value." Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
participants (7)
-
Aldo Martinez Selleras -
Brian Vuyk -
Hans Langouche -
Henrique Recidive -
John VanDyk -
Nancy Wichmann -
Steve Ringwood