$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 '';
}return $default;
$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.
... 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>" : '';
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 '';
}
?>
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
if ($debug)
<?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 '';
}
?>