We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16430
    • 217 Posts
    Hi,
    I have a snippet that should save current resource id to current logged user extended value - this part does not work!
    Apart of this it also save IP for non logged user - this part works!
    Please help with non working part (does not save any value, extended value Quip exists)

    <?php
    
    $id = $modx->resource->get('id'); //current id
    
    if ($modx->user->hasSessionContext($modx->context->get('key'))) { 
        // for logged users
        // Get the user's extended fields
        $profile = $modx->user->getOne('Profile');
        $fields = $profile->get('extended');
        $fields['Quip'] = $fields['Quip'] . $id . ',';
        $profile->set('extended',$fields);
        $profile->save();
    }
    else  {
      //not logged in, add ip to tv
      $ip=$_SERVER['REMOTE_ADDR'];
      $tv = $modx->getObject('modResource', $id);
      $tvip = $tv->getTVValue('quip_ip') . $ip . ',';
      $tv->setTVValue('quip_ip', $tvip);
         }
    $modx->cacheManager->refresh();	 
    return true;
      • 3749
      • 24,544 Posts
      I don't see anything wrong with the first part of your code. Are you sure the extended field is 'Quip' and not 'quip'?

      You should also check the $profile object to make sure you're getting the User Profile:

      if ($profile) {
      
      } else {
          /* error - no profile */
      }


      Thought the second part works, I'd recommend against using the variable $tv to reference a resource object. wink It's going to confuse you when you look at this code later. $doc would make more sense.

        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
        • 16430
        • 217 Posts
        Yes exetended filed is indeed Quip. I am also using Lexrating snippet which works only for nonlogged user, like this snippet. Is it possible that I have some premission issue?
          • 3749
          • 24,544 Posts
          Are you previewing from the Manager? If so, you're not logged in to the front end unless you go through a login form there.

          What happens if you just do this:


          $fields['Quip'] = 'xxx';
            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
            • 16430
            • 217 Posts
            Found a strange bug.
            I have this in QuipReply call:
            [[+modx.user.id:isnotloggedin:eq=`1`:then=` &preHooks=`antispam` `]]

            to show abtispam only for non-loggeed user...
            This cause that all posthooks are not working, including one above
              • 3749
              • 24,544 Posts
              Try adding :else at the end. Also, the nested back-ticks may make that impossible.
                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