We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hi, I'm trying to create a plugin that sends an email to a user when that user had been unblocked. I've based my plugin on this thread: https://forums.modx.com/thread/?thread=15212&page=1

    It is working great, but not when I unblock a user inline through the user grid (the list of all users) instead of choosing 'edit user'. I would like to know if it's possible to get the ID of the edited user and the posted values when a user is edited through the grid. I did find out that the OnBeforeUserFormSave event does get called when I edit through the grid so I think I can use that event if I can get the values.

    But I use this to get the user ID:
    $uid = $_POST['id'];

    and also to get the new blocked value:
    $isBlocked = $profile->get('blocked');

    and these are not working.

    Thanks for any pointers!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      For that event, the $user variable should be available. I'm pretty sure that's an object with all of the user values
      $user->get('id')

      maybe something like
      $profile = $user->getOne('Profile');
      $profile->get('blocked');
        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
        • 19328
        • 433 Posts
        Thanks sottwell, I'll try that. But for my plugin to work I also need to know if the blocked value has changed. When editing a user from the 'normal' edit screen, the new value for blocked can be retrieved by looking at $_POST['blocked']. Would there also be a way to get that value when editing from the grid?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I think that second bit of code will be the same as the POST, but all it can tell you is whether or not the user is blocked. I'm not sure if the supplied profile object will be the new profile, or the original. I still haven't figured out how to check values returned by this kind of function call - you can't just echo or var_dump to see what you're getting.
            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
            • 3749
            • 24,544 Posts
            Take a look at the ActivationEmail extra. I think it does exactly what you want except for using the 'active' field.
              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
              • 19328
              • 433 Posts
              Thanks BobRay, I looked at the ActivationEmail extra and it got me on the right track!
              For others who are looking for this, I use this code to determine if a user had been unblocked:

              $id = $user->get('id');
              $dbUser = $modx->getObject('modUser',$id);
              $profile = $dbUser->getOne('Profile');
              $email = $profile->get('email');
              $fullname = $profile->get('fullname');
              
              $before = $profile->get('blocked');
              $after = $user->get('blocked');


              It's working now when I edit inline in the user grid, and also when I edit a user from the edit user screen. Great!
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Excellent! Do you mind if I use this in the Cookbook?
                  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
                  • 19328
                  • 433 Posts
                  No problem at all! If you want the whole plugin I can post it here as well.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    That would be great. Always better to have the whole solution.
                      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
                      • 19328
                      • 433 Posts
                      Here it is. All credits should go to Sean Kimball (https://forums.modx.com/thread/15212/send-email-on-blocked-user#dis-post-82261) and BobRay (ActivationEmail extra).

                      <?php
                      	// get currently edited user
                      	$id = $user->get('id');
                      	$dbUser = $modx->getObject('modUser',$id);
                           
                          // get some profile information
                      	$profile = $dbUser->getOne('Profile');    
                      
                          // check if user was blocked and is still blocked now
                      	$before = $profile->get('blocked');
                      	$after = $user->get('blocked');
                      
                          // email settings
                          $mailFrom = '[email protected]';
                      	$replyTo = '[email protected]';
                          
                          // set some defaults
                          $mailFromName = 'XXX';
                          $message = '';
                          $mailChunk = '';
                       
                          // set unblock message chunk & subject only if user is  
                          // currently blocked and after edit unblocked
                          if (($before == 1) && ($after == 0 ) ){
                              $mailChunk = 'userUnBlockedEmailTpl';
                              $subject = 'Access granted';
                          }
                      
                          // send mail if there is a chunk defined
                          if ($mailChunk != ''){
                       		
                              $modx->setPlaceholder('fullname',$profile->get('fullname'));
                              $modx->setPlaceholder('email',$profile->get('email'));
                              $placeHolders = array('fullname' => $fullname, 'email' => $email);
                              $message .= $modx->parseChunk($mailChunk, $placeHolders, '[[+', ']]');
                      
                              $modx->getService('mail', 'mail.modPHPMailer');
                              $modx->mail->set(modMail::MAIL_BODY,$message);
                              $modx->mail->set(modMail::MAIL_FROM,$mailFrom);
                              $modx->mail->set(modMail::MAIL_FROM_NAME,$mailFromName);
                              $modx->mail->set(modMail::MAIL_SENDER,$mailFrom);
                              $modx->mail->set(modMail::MAIL_SUBJECT,$subject);
                              $modx->mail->address('to',$email);
                              $modx->mail->address('reply-to',$replyTo);
                              $modx->mail->setHTML(true);
                               
                              if (!$modx->mail->send()) {
                                  $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
                              }
                               
                              $modx->mail->reset();
                           
                          }