Hey everyone. I am very glad I found this forum. I have been having problems with taking 2 different scripts and combining them. These were built for Modx but it is still php. Basically what I have is that both scripts are pretty identical, but I need to take one variable from one script and put it in another.
The first script is called
MemberCheck. It checks to see if a member of a certain group is logged in and if so then it displays a template chunk to them. If they are not logged in then it displays the default template chunk.
The other script is called
extMemberCheck. It checks to see if a member of a certain group is logged in and will allow for checks of multiple groups and multiple corresponding template chunks. However, this one was not coded with the default variable. That is what I am trying to accomplish. To get the default variable into the extMembercheck.
You would think that this would be pretty straight forward.... not for a php noob like me.
MemberCheck.
<?php
# 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 Specified</p>' : '';
}
if (!isset ($chunk)) {
return $debug ? '<p>Error: No Chunk Specified</p>' : '';
}
# check if default is set, if not sets value
$default = (isset($default))? $default : '';
# turn comma-delimited list of groups into an array
$groups = explode(',', $groups);
if (!class_exists('MemberCheck')) {
class MemberCheck {
var $allGroups = NULL;
var $debug;
function getInstance($debug) {
static $instance;
if (!isset ($instance)) {
$instance = new MemberCheck($debug);
}
return $instance;
}
function MemberCheck($debug = false) {
global $modx;
$this->debug = $debug;
if ($debug) {
$this->allGroups = array ();
$tableName = $modx->getFullTableName('webgroup_names');
$sql = "SELECT name FROM $tableName";
if ($rs = $modx->db->query($sql)) {
while ($row = $modx->db->getRow($rs)) {
array_push($this->allGroups, stripslashes($row['name']));
}
}
}
}
function isValidGroup($groupName) {
$isValid = !(array_search($groupName, $this->allGroups) === false);
return $isValid;
}
function getMemberChunk(& $groups, $chunk, $default) {
global $modx;
$o = '';
if (is_array($groups)) {
for ($i = 0; $i < count($groups); $i++) {
$groups[$i] = trim($groups[$i]);
if ($this->debug) {
if (!$this->isValidGroup($groups[$i])) {
return "<p>The group <strong>" . $groups[$i] . "</strong> could not be found...</p>";
}
}
}
$check = $modx->isMemberOfWebGroup($groups);
$chunkcheck = $modx->getChunk($chunk);
$defaultcheck = $modx->getChunk($default);
if ( $check ) {
$o .= ($check && $chunkcheck) ? $chunkcheck : '';
if (!$chunkcheck)
$o .= $this->debug ? "<p>The chunk <strong>$chunk</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 {
$o .= "<p>No valid group names were specified!</p>";
}
return $o;
}
}
}
$memberCheck = MemberCheck :: getInstance($debug);
if (!isset ($ph)) {
return $memberCheck->getMemberChunk($groups, $chunk, $default);
} else {
$modx->setPlaceholder($ph, $memberCheck->getMemberChunk($groups, $chunk, $default));
return '';
}
?>
extMemberCheck
<?php
# 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>' : '';
}
# 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;
if ($debug) {
$this->allGroups = array ();
$tableName = $modx->getFullTableName('webgroup_names');
$sql = "SELECT name FROM $tableName";
if ($rs = $modx->db->query($sql)) {
while ($row = $modx->db->getRow($rs)) {
array_push($this->allGroups, stripslashes($row['name']));
}
}
}
}
function isValidGroup($groupName) {
$isValid = !(array_search($groupName, $this->allGroups) === false);
return $isValid;
}
function getMemberChunk(&$groups, $chunk) {
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;
}
} else {
$check = $modx->isMemberOfWebGroup($groups);
$chunkcheck = $modx->getChunk($chnk);
$o .= ($check && $chunkcheck) ? $chunkcheck : '';
if (!$chunkcheck)
$o .= $this->debug ? "<p>The chunk <strong>$chnk</strong> not found...</p>" : '';
return $o;
}
} else {
if ($this->debug) {
return "<p>The group <strong>" . $groups[$i] . "</strong> could not be found...</p>";
}
}
}
}
}
}
$memberCheck = extMemberCheck :: getInstance($debug);
if (!isset ($ph)) {
return $memberCheck->getMemberChunk($groups, $chunk);
} else {
$modx->setPlaceholder($ph, $memberCheck->getMemberChunk($groups, $chunk));
return '';
}
?>