We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46580
    • 167 Posts
    I would like to use an extended field to retrieve specific user profile, instead ID

    The following code works fine with ID (login extra)
    [[!Profile? &user=`12` &prefix=`user.`]]
    Email: [[+user.email]]
    Fullname: [[+user.fullname]]


    Is there a way to write a snippet to use :
    [[!Profile? &myExtendedFieldName=`1214z69adda2-unique-value` &prefix=`user.`]]
    Email: [[+user.email]]
    Fullname: [[+user.fullname]]
    ?
      MODX lover
      -
      Développeur MODX / Webdesign / Solutions web
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I think you would need a wrapper snippet to first get the ID of the user with that extended value, then use runSnippet() to run the Profile snippet with the user ID you get from your snippet code. You would need to search for a user against the whole "extended" field, using a "WHERE 'extended' LIKE '%1214z69adda2-unique-value', then get that user's ID.

        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Here's a rather ugly snippet that returns a user's ID based on an extended field's value. It isn't particularly reliable, as the string could exist in other places in the JSON. But it sort of pretty much works most of the time...
        <?php
        $c = $modx->getOption('value', $scriptProperties);
        if(!$c) return;
        
        $sql = "SELECT * FROM modx_user_attributes WHERE extended LIKE '%$c%'";
        $result = $modx->query($sql);
        
        if (!is_object($result)) {
           return 'No result!';
        }
        else {
           $row = $result->fetch(PDO::FETCH_ASSOC);
        }
        
        return $row['internalKey'];
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You can increase the reliability of the snippet if you are careful of the &value property you use. For example,
          [[byExtended? &value=`"attribute1":"value1"`]]

          would be much better than
          [[byExtended? &value=`value1`]]
            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
            You might also consider using ClassExtender to extend the modUser object. That way, the search field can be indexed, you won't have to use LIKE (which is fairly slow), and you can use the getExtUsers snippet to get them *much* faster.

            http://bobsguides.com/blog.html/2014/05/27/why-extend-moduser/


            Another alternative is to put the data in an unused text field of the User Profile if there is one.


              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
              • 46580
              • 167 Posts
              Thanks ! Finally I decided to use ClassExtender.

              I wrote some mods in ExtraUserFields plugin to generate a random key (extra field "kagi").

              Then with a snippet using paramStash, Profile and SetUserPlaceholders I can retrieve any specific user data by passing an "encrypted" URL param ( i.e. http://mydmodx.net/show.html?kagi=gfg45AdzdzA5dd )

              <?php
              // run paramStash snippet to extract paramm string
              $params['params'] = 'kagi';
              $params['valueOnly'] = '1';
              $kagi = $modx->runSnippet('paramStash', $params);
              
              // retrieve ID matching ukey = string
              $result = $modx->query("SELECT * FROM ext_user_data WHERE ukey LIKE '".$kagi."'");
              if (!is_object($result)) {
                 return 'No result!';
              }
              else {
                 $row = $result->fetch(PDO::FETCH_ASSOC);
                 //return 'Result:' .print_r($row['userdata_id'],true);
                 $uid = print_r($row['userdata_id'],true);
              }
              //return $uid;
              
              // login/profile: use specified user ID, not logged user
              $params3['user'] = $uid;
              $params3['prefix'] = 'user.';
              $modx->runSnippet('Profile', $params3);
              
              // classExtender: use specific user ID and set placeholders
              $params2['userId'] = $uid;
              $params2['prefix'] = 'Data';
              $modx->runSnippet('SetUserPlaceholders', $params2);


              The idea is to serve/deliver scripts or other modX content with specific user data.
                MODX lover
                -
                Développeur MODX / Webdesign / Solutions web
                • 3749
                • 24,544 Posts
                Cool idea! I'm curious about why you're calling the Profile snippet with runSnippet(), but not using the return value.
                  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