Skip to content

Instantly share code, notes, and snippets.

@mkay
Created January 17, 2014 19:40
Show Gist options
  • Save mkay/8480032 to your computer and use it in GitHub Desktop.
Save mkay/8480032 to your computer and use it in GitHub Desktop.
AndIf snippet for MODX. Extended version of the 'If' snippet.
<?php
/**
* AndIf - for MODx Revolution
*
* Copyright 2011 by Luke Bagshaw (MODx forum user 'lucas')
* This is a modified (extended) version of the 'If' snippet, created by
* Jason Coward <jason@modx.com> and Shaun McCormick <shaun@modx.com>
*
* AndIf is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* AndIf is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* AndIf; if not, see <http://www.gnu.org/licenses/>
*
*
* USAGE:
*
* [[!AndIf?
* &condition=`` <-- `subject relation operand` (space delimited)
* &operator=`` <-- AND or OR (defaults to AND)
* &then=`` <-- result if conditions met (defaults to 'true')
* &else=`` <-- result if not (defaults to 'false')
* ]]
*
* Multiple conditions delimited by a pipe '|'
* eg. &condition=`x < 6|y = 5`
*
*/
if (!empty($debug)) {
print_r($scriptProperties);
if (!empty($die)) die();
}
$modx->parser->processElementTags('',$condition,true,true);
$output = '';
// Set default params
$condition = !isset($condition) ? '' : $condition;
$operator = !isset($operator) ? 'and' : $operator;
$then = !isset($then) ? true : $then;
// Set up function to compare subject to operand
if(!function_exists('compareSubject')) {
function compareSubject($subject, $relation, $operand) {
$relation = !empty($relation) ? $relation : '';
$operand = !isset($operand) ? '' : $operand;
if (isset($subject)) {
if (!empty($relation)) {
$relation = strtolower($relation);
switch ($relation) {
case '!=':
case 'neq':
case 'not':
case 'isnot':
case 'isnt':
case 'unequal':
case 'notequal':
return ($subject != $operand);
break;
case '<':
case 'lt':
case 'less':
case 'lessthan':
return ($subject < $operand);
break;
case '>':
case 'gt':
case 'greater':
case 'greaterthan':
return ($subject > $operand);
break;
case '<=':
case 'lte':
case 'lessthanequals':
case 'lessthanorequalto':
return ($subject <= $operand);
break;
case '>=':
case 'gte':
case 'greaterthanequals':
case 'greaterthanequalto':
return ($subject >= $operand);
break;
case 'isempty':
case 'empty':
return empty($subject);
break;
case '!empty':
case 'notempty':
case 'isnotempty':
return !empty($subject) && $subject != '';
break;
case 'isnull':
case 'null':
return $subject == null || strtolower($subject) == 'null';
break;
case 'inarray':
case 'in_array':
case 'in':
case 'ia':
$operand = explode(',',$operand);
return in_array($subject,$operand);
break;
case '==':
case '=':
case 'eq':
case 'is':
case 'equal':
case 'equals':
case 'equalto':
default:
return ($subject == $operand);
break;
}
}
}
}
}
// if no condition supplied, give error 'No condition supplied!'
if (!empty($condition)) {
// add results of compareSubject to $proposition array to apply AND/OR logic
$proposition = array();
// split conditions by pipe "|"
$conditions = explode('|', $condition);
foreach ($conditions as $condition) {
// statements delimited by space ' ' to avoid conflict with in_array (uses commas)
$statement = explode(' ', $condition);
$subject = $statement[0];
$relation = $statement[1];
$operand = $statement[2];
$proposition[] = compareSubject($subject,$relation,$operand);
}
// Apply AND/OR logic
$operator = !empty($operator) ? $operator : 'and';
$operator = !isset($operator) ? 'and' : $operator;
if (isset($operator)) {
if (!empty($operator)) {
$operator = strtolower($operator);
if ($operator == 'or') {
$output = in_array(true, $proposition) ? $then : (isset($else) ? $else : false);
}
else {
$output = !in_array(false, $proposition) ? $then : (isset($else) ? $else : false);
}
}
}
}
else { $output = 'ERROR: at least one condition required'; }
// return output
if (!empty($debug)) { var_dump($output); }
unset($subject,$statement,$relation,$operand,$condition);
return $output;
//?>
@pawelmil
Copy link

pawelmil commented Oct 8, 2014

I tried to use snippet with pdoresources. When I try: &condition=[[+content]] notempty it doesnt works but &condition=[[+content]] empty works ok. I testet it with snippet "If" - in both cases it works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment