/assets/snippets/ditto/classes/filter.class.inc.php

https://github.com/modxcms-jp/evolution-jp · PHP · 140 lines · 118 code · 18 blank · 4 comment · 31 complexity · 75ff403ed1c90294330a5bc6552e3cfe MD5 · raw file

  1. <?php
  2. /*
  3. * The Filter class contains all functions relating to filtering,
  4. * the removing of documents from the result set
  5. */
  6. class filter {
  7. public function __construct() {
  8. }
  9. public function execute($docs, $filter) {
  10. foreach ($filter['basic'] as $current_filter) {
  11. if (!is_array($current_filter) || !$current_filter) {
  12. continue;
  13. }
  14. $docs = $this->_basicFilter($docs, $this->_getParams($current_filter));
  15. }
  16. foreach ($filter['custom'] as $current_filter) {
  17. $docs = array_filter($docs, $current_filter);
  18. }
  19. return $docs;
  20. }
  21. private function _basicFilter($docs, $param) {
  22. foreach($docs as $i=>$doc) {
  23. $unset = $this->isTrue($doc, $param);
  24. if($param['flip_mode']) {
  25. $unset = !$unset;
  26. }
  27. if($unset) {
  28. unset($docs[$i]);
  29. }
  30. }
  31. return $docs;
  32. }
  33. private function _getParams($param) {
  34. global $modx;
  35. $rs = array();
  36. $op = $this->_get_operator_name($param['mode']);
  37. if(!$op) {
  38. $op = $this->_get_operator_name($param['value']);
  39. if($op) {
  40. $param['value'] = $param['mode'];
  41. } else {
  42. $op = '9';
  43. }
  44. }
  45. $rs['op'] = $op;
  46. if(stripos($param['value'], '@EVAL') === 0) {
  47. $eval_code = trim(
  48. substr($param['value'],6)
  49. , ';'
  50. ) . ';';
  51. if(strpos($eval_code,'return')===false) {
  52. $eval_code = 'return ' . $eval_code;
  53. }
  54. $rs['creteria'] = eval($eval_code);
  55. } else {
  56. $rs['creteria'] = $param['value'];
  57. }
  58. if(strpos($rs['creteria'],'[+') !== false) {
  59. $rs['creteria'] = $modx->mergePlaceholderContent($rs['creteria']);
  60. }
  61. $rs['creteria'] = trim($rs['creteria']);
  62. if ($this->is_datetime_field($param['source'])) {
  63. if (!preg_match('@^[0-9]+$@',$rs['creteria'])) {
  64. $rs['creteria'] = strtotime($rs['creteria']);
  65. }
  66. }
  67. $rs['field_name'] = $param['source'];
  68. if (strpos($rs['op'], '!') !== 0 || strpos($rs['op'], '!!') === 0) {
  69. $rs['flip_mode'] = 0;
  70. return $rs;
  71. }
  72. $rs['flip_mode'] = 1;
  73. $rs['op'] = substr($rs['op'], 1);
  74. if ($rs['op'] === '=') {
  75. $rs['op'] = '==';
  76. }
  77. return $rs;
  78. }
  79. private function isTrue($doc, $param) {
  80. $field_name = $param['field_name'];
  81. $doc_value = isset($doc[$field_name]) ? $doc[$field_name] : false;
  82. $creteria = $param['creteria'];
  83. switch ($param['op']) {
  84. case '!=' : return ($doc_value != $creteria || $doc_value===false);
  85. case '==' : return ($doc_value == $creteria);
  86. case '<' : return ($doc_value < $creteria);
  87. case '>' : return ($doc_value > $creteria);
  88. case '>=' : return ($doc_value >= $creteria);
  89. case '<=' : return ($doc_value <= $creteria);
  90. case '=~' : return (strpos($doc_value, $creteria)===false);
  91. case '!~' : return (strpos($doc_value, $creteria)!==false);
  92. case 9 : return (stripos($doc_value, $creteria)===false);
  93. case 10 : return (stripos($doc_value, $creteria)!==false);
  94. case 'regex': return (preg_match($doc_value, $creteria)===false);
  95. case 11 :
  96. $firstChr = strtoupper(substr($doc_value, 0, 1));
  97. return ($firstChr!=$creteria);
  98. }
  99. return false;
  100. }
  101. private function _get_operator_name($op) {
  102. if (in_array($op, array('!=','1','<>','ne'),true)) return '!=';
  103. if (in_array($op, array('==',2,'eq'))) return '==';
  104. if (in_array($op, array('<',3,'lt'))) return '<';
  105. if (in_array($op, array('<=',6,'lte','le'))) return '<=';
  106. if (in_array($op, array('>',4,'gt'))) return '>';
  107. if (in_array($op, array('>=',5,'gte','ge'))) return '>=';
  108. if (in_array($op, array('regex','preg'))) return 'regex';
  109. if (in_array($op, array('=~',7,'find','search','strpos'))) return '=~';
  110. if (in_array($op, array('!=~',8,'!~','!find','!search','!strpos'))) return '!=~';
  111. return false;
  112. }
  113. private function is_datetime_field($field_name='') {
  114. if(in_array($field_name, explode(',','pub_date,unpub_date'))) {
  115. return true;
  116. };
  117. if(in_array($field_name, explode(',','published,createdon,editedon,publishedon,deletedon'))) {
  118. return true;
  119. };
  120. return false;
  121. }
  122. }