We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I could be wrong about this, but it appears that the for() loop in getMemberChunk() is executed for each group listed in the snippet call. You can’t set the default chunk inside that loop because at any given time, it only knows about the particular group it’s checking.

    I haven’t spent that much time looking at it, but I think that getMemberChunk() returns "" if the groups are all valid and the user is not a member of any of them so I think you could just put your test at the bottom:

    $output = getMemberChunk($groups,$chunk);
    
    if ($output != '') {  // user is a member of a group
    
        if (!isset ($ph)) {
            return $output;
        } else {
            $modx->setPlaceholder($ph, $output);
            return '';
    } else {  // user not a member of any group
    
        if (!isset ($ph)) {
            return $default;
        } else {
            $modx->setPlaceholder($ph, $default);
            return '';
        
    }


    If that doesn’t work, I think you could also just put
    return $default;

    after the closing brace of the for loop because it looks like it will fall through if no group is found.

    Actually, you might try the second method first, since it’s easier and, I think, a little more robust.

    Hope this helps.
      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
      • 12684
      • 118 Posts
      BobRay ,

      Thank you very much for your help. I have tried your suggestions but neither are working.

      *Looking back at the MemberCheck snippet, I think that default has to be a part of what the function is looking for. Not a return. Because it seems to me that the return is the result from when a chunk is not specified. Therefore it looks for a Group and if the group exist it returns the corresponding Chunk but if either is not found then it returns the
      $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : '';


      I think the key is in the MemberCheck, and at one time I had very close. It actually returned "Default not found" although I had the call correctly. However I lost what I had done to get to that point.

      This is really driving me crazy.
        • 3749
        • 24,544 Posts
        Quote from: IG88 at Jul 21, 2008, 09:07 AM


        ... It looks for a Group and if the group exist it returns the corresponding Chunk but if either is not found then it returns the
        $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : '';

        Right, but what that line does is set the return string to the "not found" message when in debug mode and  ’’ (an empty string) if not in debug mode. So testing the return from the getMemberChunk function for that empty string will tell you if no appropriate chunk is found (but only when not in debug mode). Then all you have to do is get the default chunk and return it.

        Sorry, I should have warned you that the method wouldn’t work in debug mode.

        If I can find some time, I’ll try to get it working myself.


          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
          • 12684
          • 118 Posts
          That makes perfect sense. I will try a few things more myself, but I am just to new to php to make a good judgment on what to do in this case. I really hope there is a solution, because I am getting desperate.
            • 12684
            • 118 Posts
            OK here is what I have so far, and it is giving me NO errors but it is still not recognizing the default variable. How freakin irritating.

            	      function getMemberChunk(&$groups, $chunk, $default) {
                        global $modx;
                        $o = '';
                        if (!is_array($chunk)) {
                            $o .= "<p>No chunk names were specified!</p>";
                            return $o;
                        }
                        if (!is_array($groups)) {
                            $o .= "<p>No group names were specified!</p>";
                            return $o;
                        }
                        if (count($groups) != count($chunk)) {
                            if (count($chunk) != 1) {
                                $o .= "<p>Number of group names and chunks must be the same!</p>";
                                return $o;
                            }
                        }
            
                        for ($i = 0; $i < count($groups); $i++) {
                            $groups[$i] = trim($groups[$i]);
                            $chnk = trim($chunk[$i]);
                            if ($this->isValidGroup($groups[$i])) {
                                if (count($chunk) != 1) {
                                    $group = array($groups[$i]);
                                    $check = $modx->isMemberOfWebGroup($group);
                                    if ($check) {
                                        $chunkcheck = $modx->getChunk($chnk);
                                        $o = ($chunkcheck) ? $chunkcheck : '';
                                        if (!$chunkcheck)
                                            $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : '';
                                        return $o;
                                    } 
                                } 
            					 
                                 $check = $modx->isMemberOfWebGroup($groups);
                                    $chunkcheck = $modx->getChunk($chnk);
            						$defaultcheck = $modx->getChunk($default);
            						if ( $check ) {
                                         $o .= ($check && $chunkcheck) ? $chunkcheck : '';
                                         if (!$chunkcheck)
                                              $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : '';
                                    }
            						else
            				        {
            					        if ($defaultcheck && (strlen($default) >= 1)) {
            					 	        $o .= ($defaultcheck) ? $defaultcheck : '';
            				        	}
            					if (!$defaultcheck && (strlen($default) >= 1)) {
            						$o .= $this->debug ? "<p>The default chunk <strong>$default</strong> not found...</p>" : '';
            					}
            				}
            				} 
            			
            			 else 
            					   {
                                if ($this->debug) {
                                    return "<p>The group <strong>" . $groups[$i] . "</strong> could not be found...</p>";
                                }
            else {
            				$o .= "<p>No valid group names were specified!</p>";
            			}
            			return $o;
                            }
                        }
                    }
                }
            }
            
            $memberCheck = extMemberCheck :: getInstance($debug);
            
            if (!isset ($ph)) {
                return $memberCheck->getMemberChunk($groups, $chunk, $default);
            } else {
                $modx->setPlaceholder($ph, $memberCheck->getMemberChunk($groups, $chunk, $default));
                return '';
            }
            ?>
              • 3749
              • 24,544 Posts
              The for() loop checks one group on each pass. If the user is a member of the group, it returns the appropriate chunk. If the user is not a member, it goes on to the next group (next iteration of the loop). When it has done all the groups, it doesn’t execute again. As long as all the groups are valid, your code will never execute. As I mentioned before, there’s no way to put your solution INSIDE that loop because the information you need (that ALL groups have been processed without finding a match) is not available there.

              I think I’ve looked at the code long enough that I can come up with a solution. I’ll post it as soon as I’m sure it works.
                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
                • 3749
                • 24,544 Posts
                Quote from: Soshite at Dec 14, 2007, 08:38 AM

                When I tried using this snippet, I got an error saying that the second datatype mismatched, or something to that degree, using this snippet call...

                [!extMemberCheck? &groups=`Registered Users, Administrators` &chunk=`regUser, adminUser`!]


                I know that both groups and chunks exist. The "normal" MemberCheck snippet works, but I was wanting to use this one this one.

                Anyway, here’s the error I get with extMC...

                « MODx Parse Error »MODx encountered the following error while attempting to parse the requested resource:
                « PHP Parse Error »
                 
                PHP error debug
                  Error: 	array_search(): Wrong datatype for second argument	 
                  Error type/ Nr.: 	Warning - 2	 
                  File: 	/hsphere/local/home/symphoni/symphonicheaven.com/manager/includes/document.parser.class.inc.php(769) : eval()'d code	 
                  Line: 	117	 
                 
                Parser timing
                  MySQL: 	0.0095 s	(9 Requests)
                  PHP: 	0.2004 s	 
                  Total: 	0.2099 s


                I’m replying to an old message here, but in case anyone else runs into this problem. A look at the code of extMemberCheck reveals that it doesn’t work unless the &debug parameter is present and set to `true`.

                The only sql statement in the code is in an
                if ($debug)
                conditional.

                I’ll post a fixed version with the addition of a &default parameter that I hope will work.
                  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
                  • 3749
                  • 24,544 Posts
                  This should work although I only tested it with a minimal number of users and groups. It needs testing with more complex setups.

                  <?php
                  #::::::::::::::::::::::::::::::::::::::::
                  # Snippet name: extMemberCheck
                  # Short Desc: checks logged in groups and displays a chunk
                  # Version: 2.0
                  # Created By Richard Pugh from the MemberCheck 1.0 snippet
                  # MemberCheck Created By                    Ryan Thrash (vertexworks.com)
                  #             Sanitized By                  Jason Coward (opengeek.com)
                  #             &default parameter,
                  #             comments, and minor code
                  #             changes By                    Bob Ray (bobsguides.com)
                  #
                  # Date: February 5th, 2007
                  #
                  # Changelog:
                  # Feb 05, 07 -- initial release
                  # July 22, '08 -- 2.0 release
                  #   Added comments,
                  #   Added some minor changes to speed up execution
                  #   Added &default parameter
                  #   Now works without &debug set to true
                  #
                  #::::::::::::::::::::::::::::::::::::::::
                  # Description:
                  #   Checks to see if users belong to a certain group and
                  #   displays the specified chunk if they do. Performs
                  #   several sanity checks and allows multiple uses on a page.
                  #
                  #   Two modes of use are possible, if only one output chunk
                  #   is specified then this snippet functions as the original
                  #   MemberCheck returning the chunk if the user is in ANY
                  #   of the groups specified.
                  #
                  #   If several chunks are specified there must be a one to one
                  #   correspondence between groups and chunks, i.e. there must be
                  #   as many values specified in groups as in chunks. The snippet
                  #   then checks if the user is in the groups one by one and
                  #   outputs the corresponding chunk, i.e. if the user is part of
                  #   group2 then chunk2 will be output. If you have a hierarchy
                  #   of user groups please ensure that they are placed in
                  #   descending order of importance in the group array as the
                  #   first matching value found is always returned.
                  #
                  # Params:
                  #   &groups [array] (REQUIRED)
                  #       array of webuser group-names to check against
                  #
                  #   &chunk [array] (REQUIRED)
                  #       chunk name(s) to use if user passes the check
                  #
                  #   &default  (optional)
                  #       name of the chunk to use if user is not a member of any group on the list
                  #
                  #   &ph [string] (optional)
                  #       name of the placeholder to set instead of directly returning chunk
                  #
                  #   &debug [boolean] (optional | false)
                  #       turn on debug mode for extra troubleshooting
                  #
                  # Example Usage:
                  #
                  #   [[extMemberCheck? &groups=`group1, group2` &chunks=`chunk1, chunk2` &default=`defaultChunk` &ph=`putItHere` &debug=`true`]]
                  #
                  #   This would place the chunk 'chunk1' into a placeholder (called 'putItHere') if the user
                  #   is logged in as a webuser and is a member of the 'group1' group. If he is a member of the
                  #   'group2' group, then 'chunk2' will be returned.
                  #
                  #   If the user is not a member of any group, the chunk named defaultChunk will be returned.
                  #   If the &default parameter is omitted and the user is not a member of any group, an empty string will be returned.
                  #
                  #   If the chunk array contains only one value, then if the user is in either 'group1' OR 'group2'
                  #   then the value of 'chunk1' is returned ( as in the original MemberCheck ).
                  #
                  #   The optional debug parameter can be used to display informative error messages
                  #   when configuring this snippet for your site. For example, if the developer had
                  #   mistakenly typed 'groupX' for the first group, and none existed with debug mode on,
                  #   it would have returned the error message: The group 'groupX' could not be found....
                  #
                  #::::::::::::::::::::::::::::::::::::::::
                  
                  # debug parameter
                  $debug = isset ($debug) ? $debug : false;
                  
                  # check if inside manager
                  if ($m = $modx->insideManager()) {
                  
                      return ''; # don't go any further when inside manager
                   }
                  
                  if (!isset ($groups)) {
                      return $debug ? '<p>Error: No Group(s) Specified</p>' : '';
                  }
                  
                  if (!isset ($chunk)) {
                      return $debug ? '<p>Error: No Chunk(s) Specified</p>' : '';
                  }
                  
                  $default = isset($default) ? $default : '';  // set $default to null string if parameter is not set
                  
                  # turn comma-delimited list of groups into an array
                  $groups = explode(',', $groups);
                  $chunk = explode(',', $chunk);
                  
                  if (!class_exists('extMemberCheck')) {
                      class extMemberCheck {
                          var $allGroups = NULL;
                          var $debug;
                  
                          function getInstance($debug) {
                              static $instance;
                              if (!isset ($instance)) {
                                  $instance = new extMemberCheck($debug);
                              }
                              return $instance;
                          }
                  
                          function extMemberCheck($debug = false) {
                              global $modx;
                  
                              $this->debug = $debug;
                  
                              $this->allGroups = array ();  // Create an array of group names from the MODx db
                              $tableName = $modx->getFullTableName('webgroup_names');
                              $sql = "SELECT name FROM $tableName";
                              if ($rs = $modx->db->query($sql)) {
                                  while ($row = $modx->db->getRow($rs)) {
                                      $this->allGroups[] = stripslashes($row['name']);
                                  }
                              }
                  
                          }
                  
                          function isValidGroup($groupName) {  //  See if the group was found in the DB
                              $isValid = !(array_search($groupName, $this->allGroups) === false);
                              return $isValid;
                          }
                  
                          function getMemberChunk(&$groups, $chunk, $default) {  //Returns the appropriate chunk or the default chunk (if any) if the user is not a member of any groups
                              global $modx;
                              $o = '';
                              if (!is_array($chunk)) {
                                  return "<p>No chunk names were specified!</p>";
                  
                              }
                              if (!is_array($groups)) {
                                  return "<p>No group names were specified!</p>";
                  
                              }
                              if (count($groups) != count($chunk)) {
                                  if (count($chunk) != 1) {
                                      return "<p>Number of group names and chunks must be the same!</p>";
                  
                                  }
                              }
                              if ($default != '') {  // if the &default paramater was set, see if it points to a valid chunk
                  
                                  $defaultChunk = $modx->getChunk($default);
                                  if (!$defaultChunk) {
                                      return $debug ? '<p>Error: Default Chunk Not Found</p>' : '';
                                      }
                              }
                  
                              for ($i = 0; $i < count($groups); $i++) {  // Walk through all the groups and see of the user is there
                  
                                  $groups[$i] = trim($groups[$i]);   // Get rid of extraneous spaces
                                  $chnk = trim($chunk[$i]);          // in group and chunk names
                  
                                  if ($this->isValidGroup($groups[$i])) {   // Is the group in the DB?
                  
                                      if (count($chunk) != 1) {   // Is there more than one chunk in the call?
                                          $group = array($groups[$i]); // make an array of the group we're currently checking (isMemberOfWebGroup() wants an array)
                                          $check = $modx->isMemberOfWebGroup($group); // Is the user there?
                  
                                          if ($check) { //YES
                                              $chunkcheck = $modx->getChunk($chnk);  // Try to get the corresponding chunk
                                              $o = ($chunkcheck) ? $chunkcheck : '';  // Set $o to chunk if there, to null string if not
                                              if (!$chunkcheck) {  // Couldn't get the chunk
                                                  $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : ''; //Report the error
                                              }
                                              return $o; // We're done
                                          }
                                      } else { // There's only one group in the call
                                          $check = $modx->isMemberOfWebGroup($groups);  // Is the user a member of any groups specified?
                                          if($check) { // YES
                                              $chunkcheck = $modx->getChunk($chnk); // Try to get the chunk.
                                              $o = ($chunkcheck) ? $chunkcheck : '';  // Set $o to chunk if there, to null string if not
                                              if (!$chunkcheck) {  // Couldn't get the chunk
                                                  $o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : ''; //Report the error
                                              }
                                              return $o; // We're done
                                          }
                  
                                      }
                                  } else { // Group is not in the DB
                                      if ($this->debug) {
                                          return "<p>The group <strong>" . $groups[$i] . "</strong> could not be found...</p>";
                                      }
                                  }
                              }
                              return $defaultChunk; // If we've made it this far, the groups are all valid but the user doesn't belong to any of them.
                                                    // Note: $defaultChunk is set to '' if the $default parameter is omitted.
                          }
                      }
                  }
                  
                  $memberCheck = extMemberCheck :: getInstance($debug);
                  
                  if (!isset ($ph)) { // $ph parameter not set, return the output directly
                      return $memberCheck->getMemberChunk($groups, $chunk, $default);
                  } else {  // $ph parameter is set, set the placeholder
                      $modx->setPlaceholder($ph, $memberCheck->getMemberChunk($groups, $chunk, $default));
                      return '';
                  }
                  ?>
                  
                    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
                    • 12684
                    • 118 Posts
                    BobRay,

                    I have tested it with 3 groups and 3 chunks and a default. This is working perfectly, and even though I can see the additions you made it seems to be working better than both originals.

                    I am not sure what you mean by more complex setups, except unless you mean just more numerous groups and chunks, but I can not see why it would not work.

                    Even though I was very close and I almost did what you did at one point, I do not think I could have figured it out on my own anytime soon without you. I told you in PM and I will say so in public that you are a very helpful soul. Since the first day I posted here looking for help you offered the best advice and your newbie site was the absolute best and most comfortable site of information I came across. I can not thank you enough.

                    This snippet is very useful. Since Modx uses groups and chunks so much in so many set ups, it seems logical to me that this should be included in future releases of Modx. It should be considered a part of it.
                      • 3749
                      • 24,544 Posts
                      Thank you. I’m glad it worked for you.

                      One test I didn’t try involves using multiple groups but only one chunk. It should show the chunk to anyone in any of the groups and the default chunk (if any) to others.

                      The snippet doesn’t play very nice with the upcoming version of MODx (MODx Revolution). If I have time, I’ll see if I can adapt it.

                      Bob
                        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