We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38309
    • 40 Posts
    I am trying to update a users extended field with data from a CURL call.
    the snippet is below, and its line 73 to 75, I realise this isn't correct but its what I am trying to do.
    the extended field is 602Time and I am trying to update this with $_GET['time'], it would have to be the user that has the same mas as the $_GET['mac']
    I don't know if this is understandable or not.

    //print_r($_GET);
    if(!isset($_GET['mac']))
    {
    $modx->log(modX::LOG_LEVEL_ERROR," no mac found");
    return;
    
    }
    //$modx->log(modX::LOG_LEVEL_ERROR,implode(",",$_GET));
    $c = $modx->newQuery('modUser');
           // $c->select($this->xpdo->getSelectColumns('modUser','modUser'));
    $c->select('id');
    
    $collection=$modx->getCollection('modUser',$c);
    $arr2=array();
    foreach($collection as $item)
     {
    $arr2=$item->toArray();
    $id=$arr2['id'];
    
    //print_r($arr);
     
    
    $c1=$modx->newQuery('modUserProfile');
    $c1->where(array('id'=>$id));
    
    $collection2=$modx->getCollection('modUserProfile',$c1);
    foreach($collection2 as $item2)
     {
    $arr=$item2->toArray();
    
    //print_r($arr);
    //$id=$arr['id'];
    
      $rem="1";
      $rem2=$arr['extended']['ReportEmail'];
        if($rem!=$rem2)
            continue;
    //endif;
    
    if(isset($arr['extended']) && isset($arr['extended']['mac'])):
     
       $tmac=$arr['extended']['mac'];
        if($tmac!=$_GET['mac'])
    	continue;
    
    $replace=array('address'=>$arr['address'],
    'company'=>$arr['extended']['company'],
    'zone_id'=>$_GET['zone_id'],
    'zone_desc'=>$_GET['zone_desc'],
    'account_code'=>$_GET['account_code'],
    'date'=>date('d M y - g:i A', strtotime($_GET['time'])),
    'event_code'=>$_GET['event_code'],
    'event_group'=>$_GET['event_group'],
    'mac'=>$arr['extended']['mac'],
    'raw_data'=>$_GET['raw_data'],
    'id2'=>$_GET['id2'],
    'alarm_reason'=>$_GET['alarm_reason'],
    'event_desc'=>$_GET['event_desc'],
    'event_alt'=>$_GET['event_alt'],
    'event_state'=>$_GET['event_state'],
    'user_desc'=>$_GET['user_desc'],
    'name'=>$arr['fullname'],
    'id'=>$arr['id'],
    'idM'=>$arr['extended']['userID'],
    'sACC'=>$arr['extended']['tick']['sACC'],
    'mCom'=>$arr['extended']['MONcompany'],
    'mPh'=>$arr['extended']['MONphone'],
    'mEx'=>$arr['extended']['tick']['MONexternal'],
    'reportE'=>$arr['extended']['ReportEmail'],
    
    );
    
    [[!UpdateProfile? &useExtended=`1`]]
    <form class="form" action="[[~[[*id]]]]" method="post"><input type="hidden" name="nospam:blank" value="" />
    <input type="text" name="602Time" id="602Time" value="$_GET['time']" />
    
    $chunk=$modx->getChunk('mailchunk',$replace);
    
    //sending mail
    $message = $modx->getChunk('myEmailTemplate');
     
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$chunk);
    
    $from=(isset($from))?$from:'[email protected]';
    $fname=isset($fname)?$fname:'Alarm Server';
    if(isset($_GET['alarm_reason']) )
    $subject='Alarm Trigger: '.$_GET['alarm_reason'];
    else
    $subject='Alarm Trigger';
    $modx->mail->set(modMail::MAIL_FROM,$from);
    $modx->mail->set(modMail::MAIL_FROM_NAME,$fname);
    $modx->mail->set(modMail::MAIL_SUBJECT,$subject);
    $modx->mail->address('to',$arr['email']);
    $modx->mail->setHTML(true);
    
    if (!$modx->mail->send()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
    }
    
    ///
    
    endif;
    
    }//loop2
    
    }//loop1
    [ed. note: bigben83 last edited this post 11 years, 1 month ago.]
      • 3749
      • 24,544 Posts
      You've got some confusing stuff going on there. It looks like you are calling getCollection() when you only want one object. You're calling it again to get the user's profile. Using getObject() would make more sense, unless I'm misunderstanding you.

      You say you want to update the extended fields for "a user" but your code is retrieving all users on the site, which will be very slow. You also need to get the extended fields array with get().

      I'd recommend that you put the mac value in some unused user profile field (e.g., fax) rather than in an extended field. Extended fields are very slow and cumbersome for searching.

      That way you can do something like as fast and simple as this (untested):

      if(!isset($_GET['mac'])) {
          $modx->log(modX::LOG_LEVEL_ERROR," no mac found");
          return;
      } else {
          $mac = $_GET['mac'];
      }
       
      
      $c = $modx->newQuery('modUserProfile');
      $c->where(array('fax' => $mac));
      
      $profile = $modx->getObject('modUserProfile', $c);
      
      if ($profile) {
          $modx->log(modX::LOG_LEVEL_ERROR," User Profile not found");
          return;
      } 
      
      
      $extended = $profile->get('extended');
      
      /* You only need to set the ones that are being updatad here */
      
      $extended['zone_id'] = $_GET['zone_id'];
      $extended['zone_desc']= $_GET['zone_desc'];
      $extended['account_code']= $_GET['account_code'];
      $extended['date']= date('d M y - g:i A', strtotime($_GET['time']));
      
      $extended['602time'] = $_GET['time'];
      
      // etc.
      
      $profile->set('extended', $extended);
      $profile->save();
      
      /* email code here */
      
      


      TBH, using the extended field of the User Profile is really awkward and slow for this kind of stuff. Take a look at extending the modUser object with ClassExtender. That will create a custom DB table with a row for each user containing all the fields you're currently putting in the extended field. It will be infinitely faster. http://bobsguides.com/blog.html/2014/05/27/why-extend-moduser/







        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
        • 38309
        • 40 Posts
        Thanks for that Bob,
        I will look at your guide and see about changing over to modUser instead of the extended field. It will mean i have to change a few forms but that shouldn't be to hard by the looks.

        The above script was written by someone else which makes it harder to see why they did things. not all of the fields come from extended, most come from another server altogether via curl, they are then matched to the user and the user email address is used in the email.
          • 3749
          • 24,544 Posts
          Are you processing multiple users in one pass? That will make a big difference in the code. It seems unlikely that there's data for more than one user in the $_GET array.
            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
            • 38309
            • 40 Posts
            Yes you are correct it is only one user, it check the curl input for the mac finds the user with that mac then extracts the user data
              • 3749
              • 24,544 Posts
              In that case, ClassExtender may be perfect for you. I would put all the fields in the custom table (even if they duplicate fields in the User Profile).

              Then, all you really need is something like this:

              $userDataObject = $modx->getObject('userData', array('mac' => $_GET['mac']));
               
              if ($userDataObject) {
                  $userDataObject->set('zone_id', $_GET['zone_id']);
                  $userDataObject->set('zone_desc',$_GET['zone_desc']);
                  $userDataObject->set('account_code',$_GET['account_code']);
                  $userDataObject->set('date', date('d M y - g:i A', strtotime($_GET['time'])));
                   
                  $userDataObject->set('602time',$_GET['time']);
              
                  $userDataObject->save();
              
              }
              
              /* Email code here */
              
              


              Doing it this way, you don't need the user ID, the user object, or the User Profile object, so it will be very fast.
                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
                • 38309
                • 40 Posts
                yes ok, the only thing that I update for the user is the 602time with the $_GET['time'],
                all the other data is processed and sent to user in email.
                  • 38309
                  • 40 Posts
                  Bob I have updated my script as below but there must be something incorrect. Would you mind having a look at it

                  <?php
                  /* SCRIPT UPDATE
                  As suggested by BOB RAY to use classExtender instead of the Extended Fields to speed up script
                  https://forums.modx.com/thread/97912/updated-user-extended-fields-in-snippet-from-curl-call#dis-post-529455
                  DATE: 05-08-2015
                  VERSION: 1.0
                  */
                  
                  
                  $userDataObject = $modx->getObject('userData', array('mac' => $_GET['mac']));
                    
                  if ($userDataObject) {
                  	$userDataObject->set('fullname',$_GET['fullname']);
                  	$userDataObject->set('company',$_GET['company']);
                  	$userDataObject->set('id',$_GET['id']);
                      $userDataObject->set('account_code',$_GET['account_code']);
                      $userDataObject->set('602time',$_GET['time']);
                      $userDataObject->save();
                  }
                  
                  $rem="1";
                  $rem2 = $modx->getObject('userData', array('ReportEmail'));
                  	if($rem!=$rem2)
                  continue;
                  
                  /* information received from CURL
                  SAMPLE:
                  http://URL/trigger.html?s=1&account_code=9876&event_code=602&mac=000&raw_data=9876&event_desc=Ne&event_alt=Op&event_state=Di&zone_id=00&zone_desc=&partition=00&alarm_reason=Pe&event_group=6&user_desc=&time=2015-08-04+15%3A38%3A19&id2=20881
                  */ 
                  
                  $replace=array(
                  	'address'=>$arr['address'],
                  	'company'=>$arr['extended']['company'],
                  	'zone_id'=>$_GET['zone_id'],
                  	'zone_desc'=>$_GET['zone_desc'],
                  	'account_code'=>$_GET['account_code'],
                  	'date'=>date('d M y - g:i A', strtotime($_GET['time'])),
                  	'event_code'=>$_GET['event_code'],
                  	'event_group'=>$_GET['event_group'],
                  	'mac'=>$arr['extended']['mac'],
                  	'raw_data'=>$_GET['raw_data'],
                  	'id2'=>$_GET['id2'],
                  	'alarm_reason'=>$_GET['alarm_reason'],
                  	'event_desc'=>$_GET['event_desc'],
                  	'event_alt'=>$_GET['event_alt'],
                  	'event_state'=>$_GET['event_state'],
                  	'user_desc'=>$_GET['user_desc'],
                  	'name'=>$arr['fullname'],
                  	'id'=>$arr['id'],
                  	'idM'=>$modx->getObject('userData', array('userID'));			//$arr['extended']['userID'],
                  	'sACC'=>$modx->getObject('userData', array('sACC'));			//$arr['extended']['tick']['sACC'],
                  	'mCom'=>$modx->getObject('userData', array('MONcompany'));		//$arr['extended']['MONcompany'],
                  	'mPh'=>$modx->getObject('userData', array('MONphone'));			//$arr['extended']['MONphone'],
                  	'mEx'=>$modx->getObject('userData', array('MONexternal'));		//$arr['extended']['tick']['MONexternal'],
                  	'reportE'=>$modx->getObject('userData', array('ReportEmail'));	//$arr['extended']['ReportEmail'],
                  );
                  
                  $chunk=$modx->getChunk('mailchunk',$replace);
                  
                  /* Email code here */
                  //sending mail
                  $message = $modx->getChunk('myEmailTemplate');
                   
                  $modx->getService('mail', 'mail.modPHPMailer');
                  $modx->mail->set(modMail::MAIL_BODY,$chunk);
                  
                  $from=(isset($from))?$from:'[email protected]';
                  $fname=isset($fname)?$fname:'Alarm Server';
                  if(isset($_GET['alarm_reason']) )
                  $subject='Alarm Trigger: '.$_GET['alarm_reason'];
                  else
                  $subject='Alarm Trigger';
                  $modx->mail->set(modMail::MAIL_FROM,$from);
                  $modx->mail->set(modMail::MAIL_FROM_NAME,$fname);
                  $modx->mail->set(modMail::MAIL_SUBJECT,$subject);
                  $modx->mail->address('to',$arr['email']);
                  $modx->mail->setHTML(true);
                  
                  if (!$modx->mail->send()) {
                      $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
                  }
                  
                  
                  
                  ///
                  
                  endif;
                    • 3749
                    • 24,544 Posts
                    First of all, you don't ever want an endif in a MODX snippet.

                    Second, this is all messed up in a variety of ways:

                    $rem="1";
                    $rem2 = $modx->getObject('userData', array('ReportEmail'));
                        if($rem!=$rem2)
                    continue;


                    I can't tell what it's supposed to do but you have a continue statement that's not in a loop, a criterion array with no key as the second argument to getObject(), and a test that doesn't make sense (getObject() returns an object which will never be equal to a string or an integer).
                      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
                      • 38309
                      • 40 Posts
                      Oh, it doesn't pay to copy and paste late at night,
                      its actually meant to check to see if user has enabled Email Reports. if not it doesn't continue with script.