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
    Quote from: BobRay at Oct 23, 2014, 09:26 PM
    Nothing wrong with the code that I can see.

    Try this:

    <!--?php
    $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 .= '
    ' . $key . ' changed from ' . $oldProfile['key'] . ' to ' . $newProfile['key'];
            }
        }
    }
    if (!empty($output)) {
        $output = '
    Changes:
    ' . $output;
    } else {
        $output = '
    Changes: none
    ';
    }
    
    if (! empty($output)) {
        die($output);
    } else {
        die('Output is empty');
    }
    /* Change report will be in the $output variable here */
    
    
    $modx->log(modX::LOG_LEVEL_ERROR, 'Profile update: $output' . $err);
    
    /* tell our snippet we're good and can continue */
    return true;

    Thanks Bob - that no longer gives a blank screen - it attempts to render the output - but it fails to note the changes in this output mechanism. The changes are made and saved, but the output of the changes is not working still. Nothing being outputted in the error log either...
      • 3749
      • 24,544 Posts
      Can you paste what it's showing?
        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
        It just returns the below:

        <body>
        <br>
        "Changes: none"
        </br>
        </body>
          • 3749
          • 24,544 Posts
          Duh, this is a mistake I've made before (more than once).

          This:

          foreach ($newProfile as $key => $value) {
                  if ($oldProfile['key'] != $newProfile['key']) {
                      $output .= '
          ' . $key . ' changed from ' . $oldProfile['key'] . ' to ' . $newProfile['key'];
                  }
          

          should be:

           foreach ($newProfile as $key => $value) {
                  if ($oldProfile[$key] != $newProfile[$key]) {
                      $output .= '
          ' . $key . ' changed from ' . $oldProfile[$key] . ' to ' . $newProfile[$key];
                  }


          IOW, the four instances of 'key' should be $key, with no quotes.
            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
            Sorry Bob... still not working. Thought worthwhile to paste in the exact code I have just in case their is an error somewhere???

            It still displays a page with the words: "Output:" - not catching what is being changed or reporting it... Sorry sad

            <?php
            $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 .= '
            ' . $key . ' changed from ' . $oldProfile[$key] . ' to ' . $newProfile[$key];
                    }
            }
            }
            if (!empty($output)) {
                $output = '<br />Changes:<br />' . $output;
            } else {
                $output = '<br />Changes: none<br />';
            }
             
            if (! empty($output)) {
                die($output);
            } else {
                die('Output is empty');
            }
            /* Change report will be in the $output variable here */
             
             
            $modx->log(modX::LOG_LEVEL_ERROR, 'Profile update: $output' . $err);
             
            /* tell our snippet we're good and can continue */
            return true;
            
              • 3749
              • 24,544 Posts
              Here's one more thing to try, if you're not sick of the whole process. wink

              Change this:

              $profile = $modx->user->getOne('Profile');


              to this:

              $profile = $modx->user->getOne('Profile', null, false);


              That will make sure the Profile is "fresh" from the DB rather than the cache.

              Normally, I would have done some testing by now, but this case would require a lot of setup and I'm on the road so I can't spare the time.
                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 - I really appreciate your time and continued effort here - but alas it is still not working - just outputs "Changes: none" even when I update a field... Sorry! Maybe when you are back and have some time you could have a test your end and see if it works?
                  • 3749
                  • 24,544 Posts
                  OK, this is tested and works. This tag goes above the update profile form, but below the LogMeIn tag (see below):

                  [[!UpdateProfile? &preHooks=`SaveProfile` &postHooks=`CheckProfile`]]


                  SaveProfile snippet:

                  /* SaveProfile snippet */
                  $profile = $modx->user->getOne('Profile');
                  $_SESSION['user_profile'] = empty($profile)? array() : $profile->toArray();
                  
                  return true;


                  CheckProfile snippet:

                  $oldProfile = isset($_SESSION['user_profile'])? $_SESSION['user_profile'] : 'No Profile';
                  $newProfile = $modx->user->getOne('Profile')->toArray();
                  
                  $msg = '';
                  foreach($oldProfile as $key => $oldValue) {
                      $newValue = empty($newProfile[$key])? '(empty)' : $newProfile[$key];
                      $oldValue = empty($oldValue)? '(empty)' : $oldValue;
                      if ($newValue != $oldValue) {
                          $msg .= "\n" . $key . ' changed from ' . $oldValue . ' to ' . $newValue;
                      }
                  }
                  
                  /* Email $msg here after debugging */
                  
                  
                  /* When through debugging, comment out
                     these three lines */
                  
                   $chunk = $modx->getObject('modChunk', array('name' => 'Debug'));
                   $chunk->setContent($msg);
                   $chunk->save();
                  
                  return true;


                  Before doing anything else, create a chunk called Debug. The message created in CheckProfile will be saved there for debugging purposes.

                  Since you need to be logged in for UpdateProfile to work and you're not logged in in the front end when previewing from the Manager, I also created this utility snippet to log you in (be sure to remove it before putting it on a production server).

                  This goes at the top of the page.

                  [[!LogMeIn]]


                  /* LogMeIn snippet */
                  $modx->user->addSessionContext($modx->context->get('key'));
                  return '';

                    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 - tried all that and updated a field that is a extended member form field and Debug contents became

                    extended changed from Array to Array


                    I guess this is where you inform me this is never gonna work with extended member form fields... smiley I have lots of none standard member form fields in use within the member profile...

                    Works for standard MODx user form fields but I am only using a few of those... can it work with extended form fields too?
                      • 3749
                      • 24,544 Posts
                      It should work fine with extended fields, but the proper way to do that is with a recursive function to generate the output. I'll try to get to it later today or tomorrow.
                        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