Source
<?php
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program 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.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* A class for creating conditions.
*/
class CConditionHelper {
/**
* Generate a numeric formula from conditions $conditions with respect to evaluation type $evalType.
* Each condition must have a condition type, that will be used for grouping.
*
* Supported $evalType values:
* - CONDITION_EVAL_TYPE_AND_OR
* - CONDITION_EVAL_TYPE_AND
* - CONDITION_EVAL_TYPE_OR
*
* Example:
* echo CFormulaHelper::getFormula(array(
* 1 => 'condition1',
* 2 => 'condition1',
* 5 => 'condition2'
* ), CONDITION_EVAL_TYPE_AND_OR);
*
* // ({1} or {2}) and {5}
*
* Keep in sync with JS getConditionFormula().
*
* @param array $conditions conditions with IDs as keys and condition type with values
* @param int $evalType
*
* @return string
*/
public static function getFormula(array $conditions, $evalType) {
$groupedConditions = [];
foreach ($conditions as $id => $condition) {
$groupedConditions[$condition][] = '{'.$id.'}';
}
// operators
switch ($evalType) {
case CONDITION_EVAL_TYPE_AND:
$conditionOperator = 'and';
$groupOperator = $conditionOperator;
break;