snippet.if.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * If
  4. *
  5. * Copyright 2009-2010 by Jason Coward <jason@modx.com> and Shaun McCormick
  6. * <shaun@modx.com>
  7. *
  8. * If is free software; you can redistribute it and/or modify it under the terms
  9. * of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * If is distributed in the hope that it will be useful, but WITHOUT ANY
  14. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  15. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * If; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  19. * Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * @package if
  22. */
  23. /**
  24. * Simple if (conditional) snippet
  25. *
  26. * @package if
  27. */
  28. if (!empty($debug)) {
  29. print_r($scriptProperties);
  30. if (!empty($die)) die();
  31. }
  32. $modx->parser->processElementTags('',$subject,true,true);
  33. $output = '';
  34. $operator = !empty($operator) ? $operator : '';
  35. $operand = !isset($operand) ? '' : $operand;
  36. if (isset($subject)) {
  37. if (!empty($operator)) {
  38. $operator = strtolower($operator);
  39. switch ($operator) {
  40. case '!=':
  41. case 'neq':
  42. case 'not':
  43. case 'isnot':
  44. case 'isnt':
  45. case 'unequal':
  46. case 'notequal':
  47. $output = (($subject != $operand) ? $then : (isset($else) ? $else : ''));
  48. break;
  49. case '<':
  50. case 'lt':
  51. case 'less':
  52. case 'lessthan':
  53. $output = (($subject < $operand) ? $then : (isset($else) ? $else : ''));
  54. break;
  55. case '>':
  56. case 'gt':
  57. case 'greater':
  58. case 'greaterthan':
  59. $output = (($subject > $operand) ? $then : (isset($else) ? $else : ''));
  60. break;
  61. case '<=':
  62. case 'lte':
  63. case 'lessthanequals':
  64. case 'lessthanorequalto':
  65. $output = (($subject <= $operand) ? $then : (isset($else) ? $else : ''));
  66. break;
  67. case '>=':
  68. case 'gte':
  69. case 'greaterthanequals':
  70. case 'greaterthanequalto':
  71. $output = (($subject >= $operand) ? $then : (isset($else) ? $else : ''));
  72. break;
  73. case 'isempty':
  74. case 'empty':
  75. $output = empty($subject) ? $then : (isset($else) ? $else : '');
  76. break;
  77. case '!empty':
  78. case 'notempty':
  79. case 'isnotempty':
  80. $output = !empty($subject) && $subject != '' ? $then : (isset($else) ? $else : '');
  81. break;
  82. case 'isnull':
  83. case 'null':
  84. $output = $subject == null || strtolower($subject) == 'null' ? $then : (isset($else) ? $else : '');
  85. break;
  86. case 'inarray':
  87. case 'in_array':
  88. case 'ia':
  89. $operand = explode(',',$operand);
  90. $output = in_array($subject,$operand) ? $then : (isset($else) ? $else : '');
  91. break;
  92. case '==':
  93. case '=':
  94. case 'eq':
  95. case 'is':
  96. case 'equal':
  97. case 'equals':
  98. case 'equalto':
  99. default:
  100. $output = (($subject == $operand) ? $then : (isset($else) ? $else : ''));
  101. break;
  102. }
  103. }
  104. }
  105. if (!empty($debug)) { var_dump($output); }
  106. unset($subject);
  107. return $output;