We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8168
    • 1,118 Posts
    Hi, I am looking to use a POSTHook on the UpdateProfile function within the Login addon to send a notification email to the website admin user informing them of the changes the member has made to their profile. I have a basic notification email working fine - what I would like is to understand how to include within the email information on the changes the member has made to their profile - is this possible? e.g. So if the member changed their country from UK to Spain, then hit update changes in the Update Profile page in the website, the notification email would state that user XXX had updated their profile and changed their country to Spain. Is this possible? e.g. to echo the changes made within the email notification in the posthook?

    This question has been answered by BobRay. See the first response.

    • The field values should be available as placeholders (using the field name) for the post-hook.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 8168
        • 1,118 Posts
        Thanks Susan. I can get the fields into the snippet OK yep - but what I am after is for some clever way to work out which fields have just been updated and just include them within the email notification. E.g. so if a user changes their country from Spain to France and hits save, then the email notification should display the fact that the user has changed their country - e.g. I don't want to list all the fields - just list those that have been updated. Is that possible?
        • It would be complicated. You'd need to use a pre-hook to get the values before the form is loaded, store them probably in the SESSION, then in the post-hook compare them. But yes, it could be done.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 8168
            • 1,118 Posts
            OK - thanks - sounds very involved - most likely not worth the effort to create it for the pay off... but appreciate your suggestion however. I was hoping their might of been something out of the box to do this. No worries.
              • 3749
              • 24,544 Posts
              This might do it (untested):

              In the preHook:

              $profile = $modx->user->getOne('Profile');
              
              if ($profile) {
                  $fields = $profile->toArray();
                  $_SESSION['user_profile'] = $profile;
              }
              return true;
              


              In the postHook:


              $oldProfile = isset($_SESSION['user_profile'])
                  ? $_SESSION['user_profile']
                  : array();
              $profile = $modx->user->getOne('Profile');
              if ($profile) {
                  $newProfile = $profile->toArray();
              }
              $output = '';
              
              if (!empty($newProfile) && (!empty($oldProfile))) {
              
                  foreach ($newProfile as $key => $value) {
                      if ($oldProfile['key'] != $newProfile['key']) {
                          $output .= '<br />' . $key . ' changed from ' . $oldProfile['key'] . ' to ' . $newProfile['key'];
                      }
                  }
              }
              if (!empty($output)) {
                  $output = '<br />Changes:<br />' . $output;
              } else {
                  $output = '<br />Changes: none<br />';
              }
              
              /* Change report will be in the $output variable here */
              
              
              return true;
              





              [ed. note: BobRay last edited this post 9 years, 6 months ago.]
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 8168
                • 1,118 Posts
                Thanks Bob - will give it a go!

                UPDATE - Gave it a go... unfortunately - didn't work - ended up breaking the update profile core functionality - thanks for the idea Bob - afraid the code logic is above my comprehension to try and fix it though smiley [ed. note: dubbs last edited this post 9 years, 6 months ago.]
                  • 3749
                  • 24,544 Posts
                  Sorry -- a bit of missing punctuation in the code of the postHook snippet. Fixed above.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 8168
                    • 1,118 Posts
                    Bob - you are a true MODX leg-end - I will give it a spin tomorrow and let you know. Thanks again for all your input and effort here.
                      • 3749
                      • 24,544 Posts
                      Don't thank me until it works. wink
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting