PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/snippets/if/snippet.if.php

https://github.com/good-web-master/modx.evo.custom
PHP | 184 lines | 104 code | 8 blank | 72 comment | 34 complexity | 8fe4180721fc666d21e0a83199a0970a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');}
  3. /**
  4. * if
  5. *
  6. * if проверка по условию
  7. *
  8. * @category snippet
  9. * @version 1.2
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License (GPL)
  11. * @internal @properties
  12. * @internal @modx_category add
  13. */
  14. /**
  15. ** if snippet
  16. ** [[if&is=`[*id*]:=:4:or:[*parent*]:in:5,6,5,7,8,9` &then=`[[if&is=`0||=||0` &then=`true` &else=`false` &separator=`||`]]` &else=`@TPL:else`]]
  17. ** [[if?is=`[*id*]:is:1:or:[*id*]:is:2:and:[*parent*]:is:5:or:[*parent*]:in:2,3,4` &then=`true` &else=`false`]]
  18. **
  19. ** Все выражения обрабатываются по логике (....:or:is:.... ) :and: (...:!empty:.....)
  20. ** выражение and делит условие на 2 части, которые в конце в конце сравниваются к true
  21. **
  22. ** Пример №1
  23. ** Выводить акцию нужно только в каталоге с ID = 5
  24. ** [[if?is=`[*parent*]:=:5` &then=`@TPL:akcia`]]
  25. **
  26. ** Пример №2
  27. ** Выводить акцию нужно только в каталоге с ID = 5 или в каталогас с шаблоном №7,8,9
  28. ** [[if?is=`[*parent*]:=:5:or:[*template*]:in:7,8,9` &then=`@TPL:akcia`]]
  29. **
  30. ** Пример №3
  31. ** Выводить акцию нужно только в каталоге с ID = 5 и только в ресурсе с шаблоном №2
  32. ** [[if?is=`[*parent*]:=:5:and:[*template*]:=:7` &then=`@TPL:akcia`]]
  33. **
  34. ** Пример №4
  35. ** Выводить акцию нужно только в каталоге с ID = 5 и ( только в ресурсе с шаблоном №2 или в других шаблонах но с ТВ `show_akcia`=1
  36. ** [[if?is=`[*parent*]:=:5:and:[*template*]:=:7:or:[*show_akcia*]:=1` &then=`@TPL:akcia`]]
  37. **
  38. ** Пример №5
  39. ** Выводить акцию только для товаров с ценой в диапазоне >300$ <=700$
  40. ** [[if?is=`[*price*]:>:300:and:[*price*]:<=:700` &then=`@TPL:akcia`]]
  41. **
  42. **
  43. ** Пример №6
  44. ** Выводить при кратности записи дитто 3
  45. ** [[if?is=`[+ditto_itteration+]:%:3` &then=`true` &else=`false`]]
  46. **
  47. ** Пример №7
  48. ** Выводить при кратности записи дитто 3 но с умножением значения
  49. ** [[if?is=`[+ditto_itteration+]*2:%:3` &then=`true` &else=`false` &math=`on`]]
  50. **
  51. ** Пример №8
  52. ** Выводить значение математического выражения
  53. ** [[if?is=`[+ditto_itteration+]*2` &math=`on`]]
  54. **
  55. ** только с пропатченым парсером MODx:
  56. ** [[if?is=`[*id*]:>:2` &then=`<a href="[~[*id*]~]">[*pagetitle*]</a>`]]
  57. **
  58. ** Операторы:
  59. ** (is,=) , (not,!=) , (>,gt) , (<,lt) , (>=,gte) , (lte,<=) , (isempty,empty) , (not_empty,!empty)
  60. ** (null, is_null) , (in_array, inarray, in) , (not_in,!in)
  61. **
  62. **
  63. **
  64. ** ===============================================================================================
  65. ** Вкусности
  66. ** [[if?is=`eval('global $iteration;$iteration++;echo $iteration;')` &math=`on`]] // итерация в Ditto,Wayfinder и других каталожниках
  67. ** [[if?is=`:is:` &then=`@eval: echo str_replace('<br/>','','[*pagetitle*]');`]] // 'главное<br/> меню' -> 'главное меню'
  68. ** [[if?is=`:is:` &then=`@eval: echo number_format('[*price*]', 2, ',', ' ');`]] // '1000000,89' -> '1 000 000,89'
  69. **
  70. **
  71. ** @Author: Bumkaka
  72. ** RussAndRussky.org.ua
  73. **/
  74. //echo $is;
  75. $s=empty($separator)?':':$separator;
  76. $opers=explode($s,$is);
  77. $subject=$opers[0];
  78. $eq=true;
  79. $and=false;
  80. for ($i=1;$i<count($opers);$i++){
  81. if ($opers[$i]=='or') {$or=true;$part_eq=$eq;$eq=true;continue;}
  82. if ($or) {$subject=$opers[$i];$or=false;continue;}
  83. if ($opers[$i]=='and') {
  84. $and=true;
  85. if (!empty($part_eq)){if ($part_eq||$eq){$left_part=true;}} else {$left_part=$eq?true:false;}
  86. $eq=true;unset($part_eq);
  87. continue;
  88. }
  89. if ($and) {$subject=$opers[$i];$and=false;continue;}
  90. $operator = $opers[$i];
  91. $operand = $opers[$i+1];
  92. if (isset($subject)) {
  93. if (!empty($operator)) {
  94. if ($math=='on' && !empty($subject)) {eval('$subject='.$subject.';');}
  95. $operator = strtolower($operator);
  96. switch ($operator) {
  97. case '%':
  98. $output = ($subject %$operand==0) ? true: false;$i++;
  99. break;
  100. case '!=':
  101. case 'not':$output = ($subject != $operand) ? true: false;$i++;
  102. break;
  103. case '<':
  104. case 'lt':$output = ($subject < $operand) ? true : false;$i++;
  105. break;
  106. case '>':
  107. case 'gt':$output = ($subject > $operand) ? true : false;$i++;
  108. break;
  109. case '<=':
  110. case 'lte':$output = ($subject <= $operand) ? true : false;$i++;
  111. break;
  112. case '>=':
  113. case 'gte':$output = ($subject >= $operand) ? true : false;$i++;
  114. break;
  115. case 'isempty':
  116. case 'empty':$output = empty($subject) ? true : false;
  117. break;
  118. case '!empty':
  119. case 'notempty':
  120. case 'isnotempty':$output = !empty($subject) && $subject != '' ? true : false;
  121. break;
  122. case 'isnull':
  123. case 'null':$output = $subject == null || strtolower($subject) == 'null' ? true : false;
  124. break;
  125. case 'inarray':
  126. case 'in_array':
  127. case 'in':
  128. $operand = explode(',',$operand);
  129. $output = in_array($subject,$operand) ? true : false;
  130. $i++;
  131. break;
  132. case 'not_in':
  133. case '!in':
  134. case '!inarray':
  135. $operand = explode(',',$operand);
  136. $output = in_array($subject,$operand) ? false : true;
  137. $i++;
  138. break;
  139. case '==':
  140. case '=':
  141. case 'eq':
  142. case 'is':
  143. default:$output = ($subject == $operand) ? true : false;$i++;
  144. break;
  145. }
  146. $eq=$output?$eq:false;
  147. }
  148. }
  149. }
  150. if (!empty($left_part)){
  151. if ($left_part) {
  152. if (!empty($part_eq)){if ($part_eq||$eq){$output=$then;}} else {$output=$eq?$then:$else;}
  153. }
  154. else
  155. {$output=$else;}
  156. } else {
  157. if (!empty($part_eq)){
  158. if ($part_eq||$eq){
  159. $output=$then;
  160. }
  161. } else {$output=$eq?$then:$else;}
  162. }
  163. if (strpos($output,'@TPL:')!==FALSE){$output='{{'.(str_replace('@TPL:','',$output)).'}}';}
  164. if (substr($output,0,6) == "@eval:") {
  165. ob_start();
  166. eval(substr($output,6));
  167. $output = ob_get_contents();
  168. ob_end_clean();
  169. }
  170. if (empty($then)&&empty($else)) {
  171. if ($math=='on') {eval('$subject='.$subject.';');}
  172. return $subject;
  173. }
  174. unset($is,$then,$else,$opers,$subject,$eq,$operand,$chunk,$part_eq);
  175. return $output;
  176. ?>