We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    Running MODX Revolution 2.1.3-pl (traditional).

    I'm using the following codes from this thread to show/hide chunks based on a user's group: http://modxcms.com/forums/index.php?topic=64148.0

    This is working well in most cases, however I am using a tiered user group setup on my website.  For example, when a user first signs up, they are added to a free-member group, then if they decide to subscribe they are added to a paid-member group (in addition to being in the free-member group still), and finally, they can be added to any number of other groups after that based on their selected preferences.

    What I'm looking to do is to have a chunk (or two) show for members who aren't logged in (the (anonymous) user group) and for members in the free-member group, but to be hidden to members in the paid-member group.  The problem I'm having is even once they're added to the paid-member group, they're still in the free-member group (which is intended), so the chunk still shows using the codes from the thread I linked.

    I'm not sure if that makes sense, or if it's even possible, but it would save me a lot of trouble. [ed. note: firebot6 last edited this post 12 years, 7 months ago.]
      • 3749
      • 24,544 Posts
      You could do it with output modifiers, but a custom snippet would be faster. Something like this:

      [[!ShowChunk? &subscriberChunks=`Chunk1,Chunk7` &otherChunks=`Chunk2,Chunk3`]]


      <?php
      /* ShowChunk snippet */
      
      $subChunks = explode(',',  $scriptProperties['subscriberChunks']);
      $otherChunks = explode(',',  $scriptProperties['otherChunks']);
      
      if ($modx->user->isMember('subscribers') ) {
          foreach ($subChunks as $subChunk) {
              $output .= $modx->getChunk('$subChunk);
          }
      } else {
          foreach ($otherChunks as $otherChunk) {
              $output .= $modx->getChunk($otherChunk);
          }
      }
      
      return $output;
        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
        • 36760
        • 136 Posts
        Thanks for your help, I believe this will accomplish what I was looking for. I’m just getting an error from the snippet, and my PHP skills aren’t yet good enough to figure out what it is. I tried a few things, but they didn’t work:

        Parse error: syntax error, unexpected T_VARIABLE in /core/cache/includes/elements/modsnippet/58.include.cache.php on line 9

        The cache file has the snippet in it, so I assume that’s where the issue is.

        **EDIT**
        Line 9 in the cache file ends up being this line:

        $subChunks = explode(',' $scriptProperties['subscriberChunks'];
          • 3749
          • 24,544 Posts
          Add a ) just before the semicolon at the end. wink
            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
            • 36760
            • 136 Posts
            I had tried that, but I still get the same error for the same line. My code looks like this now:

            <?php
            /* ShowChunk snippet */
            
            $subChunks = explode(',' $scriptProperties['subscriberChunks']);
            $otherChunks = explode(',' $scriptProperties['otherChunks']);
            
            if ($modx->user->isMember('paid-members') {
                foreach $subChunks as $subChunk) {
                    $output .= $modx->getChunk($subChunk);
                }
            } else {
                foreach $otherChunks as $otherChunk) {
                    $output .= $modx->getChunk($otherChunk);
                }
            }
            
            return $output;
              • 3749
              • 24,544 Posts
              Three more missing parentheses and two missing commas -- try this:

              <?php
              /* ShowChunk snippet */
              
              $subChunks = explode(',',  $scriptProperties['subscriberChunks']);
              $otherChunks = explode(',',  $scriptProperties['otherChunks']);
              
              if ($modx->user->isMember('paid-members')) {
                  foreach ($subChunks as $subChunk) {
                      $output .= $modx->getChunk($subChunk);
                  }
              } else {
                  foreach ($otherChunks as $otherChunk) {
                      $output .= $modx->getChunk($otherChunk);
                  }
              }
              
              return $output;


              The errors were in my code (fixed above). I was exhausted when I typed it after hiking all day at high altitude in 90-degree heat. Sorry about the confusion.
                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
                • 36760
                • 136 Posts
                Not a problem at all, it works out great!

                Unfortunately, I need to add another "layer" to the snippet. Since I have another set of groups passed the paid-member group, I need to display another chunk for them, but hide &subscriberChunks and the &otherChunks from them also. Or, if that can’t be done, I at least need to hide the &subscriberChunks and &otherChunks from those groups (then I’ll just use another snippet call like the one you gave me to bring up the chunks I need to show).

                Here’s what I tried (I wanted desperately to not have to ask for help!):

                <?php
                /* ShowChunk snippet */
                
                $subChunks = explode(',',  $scriptProperties['subscriberChunks']);
                $otherChunks = explode(',',  $scriptProperties['otherChunks']);
                $stratChunks = explode(',',  $scriptProperties['stratChunks']);
                
                if ($modx->user->isMember('member-paid')) {
                    foreach ($subChunks as $subChunk) {
                        $output .= $modx->getChunk($subChunk);
                    }
                } else if ($modx->user->isMember(array('group_1','group_2'))) {
                    foreach ($stratChunks as $stratChunk) {
                        $output .= $modx->getChunk($stratChunk);
                    }
                } else {
                    foreach ($otherChunks as $otherChunk) {
                        $output .= $modx->getChunk($otherChunk);
                    }
                }
                
                return $output;


                It doesn’t bring back any errors, but it still shows the &subscriberChunks for users in group_1 and group_2, and does not show the chunk that’s in the &stratChunks area.
                  • 4172
                  • 5,888 Posts
                  That’s untested, may be something like that can do it:

                  <?php
                  
                  /* showChunks snippet */
                  
                  /*
                  
                  [[showChunks?
                  &addchunks = `
                  [
                  {"groups":["all"],"chunks":["chunk_D"]},
                  {"groups":["member-paid"],"chunks":["chunk_A","chunk_B"]},
                  {"groups":["group_A","group_B"],"chunks":["chunk_E","chunk_F","chunk_G"]}
                  ]`
                  
                  &removechunks = `
                  [
                  {"groups":["member-paid"],"chunks":[,"chunk_D","chunk_E","chunk_F","chunk_G"]},
                  {"groups":["group_A","group_B"],"chunks":["chunk_A","chunk_B","chunk_D"]}
                  ]`
                  ]]
                  
                  */
                  
                  $addchunks = $modx->fromJson($addchunks);
                  $removechunks = $modx->fromJson($removechunks);
                  $chunks = array();
                  
                  foreach ($addchunks as $value) {
                     if ($modx->user->isMember($value['groups']) || $value['groups'][0] == 'all') {
                          foreach ($value['chunks'] as $chunk){
                             $chunks[$chunk] = $chunk;   
                          }
                      }
                  }
                  
                  foreach ($removechunks as $value) {
                      if ($modx->user->isMember($value['groups'])) {
                          foreach ($value['chunks'] as $chunk){
                             unset ($chunks[$chunk]);   
                          }
                      }
                  }
                  
                  foreach ($chunks as $chunk){
                     $output .= $modx->getChunk($chunk); 
                  }
                  
                  return $output;
                  


                  the idea is first add all chunks for user-groups the user is in, than remove all chunks his user-group should not see.

                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 3749
                    • 24,544 Posts
                    Would reversing the first to if statements do it?

                    Also, if necessary you can make the if tests more complex using and ( &&) and not (!):

                    if ($modx->user->isMember('member-paid') &&  (!$modx->user->isMember('group1,group2') )) {
                      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
                      • 36760
                      • 136 Posts
                      I think what’s causing problems is the fact that the users are still part of the member-free group when they’re also part of the member-paid group. So when I try to hide the chunk for the member-free group, it will still show when I also add the user to the member-paid group.

                      That seems to be what’s happening no matter what techniques I try.