Merge branch 'maint/7.0'
[ninja.git] / modules / lsfilter / libraries / LSFilterQueryBuilderVisitor.php
blob2a1bdb97d20b144a2b7b78d9f637de18594a3618
1 <?php
2 /**
3 * Visitor to convert a livestatus filter to a search query
4 */
5 class LSFilterQueryBuilderVisitor implements LivestatusFilterVisitor {
6 /**
7 * Visit an and node, to build the subquery
8 */
9 public function visit_and( LivestatusFilterAnd $filt, $prio ) {
10 $subfilters = $filt->get_sub_filters();
11 $subqueries = array();
13 if( count( $subfilters ) == 1) {
14 return $subfilters[0]->visit($this, $prio);
15 } else if( count( $subfilters ) == 0 ) {
16 return 'all';
19 foreach( $subfilters as $sub_filt ) {
20 $subqueries[] = $sub_filt->visit($this, 2);
22 $query = implode(' and ', $subqueries);
24 if( $prio > 2 ) $query = "($query)";
25 return $query;
28 /**
29 * Visit an or node, to build the subquery
31 public function visit_or( LivestatusFilterOr $filt, $prio ) {
32 $subfilters = $filt->get_sub_filters();
33 $subqueries = array();
35 if( count( $subfilters ) == 1) {
36 return $subfilters[0]->visit($this, $prio);
37 } else if( count( $subfilters ) == 0 ) {
38 return 'none';
41 foreach( $subfilters as $sub_filt ) {
42 $subqueries[] = $sub_filt->visit($this, 2);
44 $query = implode(' or ', $subqueries);
46 if( $prio > 1 ) $query = "($query)";
47 return $query;
50 /**
51 * Visit an match node, to build the subquery
53 public function visit_match( LivestatusFilterMatch $filt, $prio ) {
54 $op = $filt->get_op();
55 $field = $filt->get_field();
56 $value = $filt->get_value();
58 if( !is_numeric($value) ) {
59 $value = '"'.addslashes($value).'"';
61 if( $op == '>=' ) {
62 /* Special case for groups */
63 $field_parts = explode('.', $field);
64 if( end($field_parts) == 'groups' ) {
65 $op = ' in ';
66 array_pop($field_parts);
67 $field = implode('.', $field_parts);
72 return $field.$op.$value;
76 /**
77 * Visit an not node, to build the subquery
79 public function visit_not( LivestatusFilterNot $filt, $prio ) {
80 $query = 'not '.$filt->get_filter()->visit($this, 3);
81 if( $prio >= 3 ) $query = "($query)";
82 return $query;