Source
* Add a formula ID to each of the given conditions according to the condition ID contained in the given formula.
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* A class to perform operations with conditions.
*/
class CConditionHelper {
/**
* Get formula with condition IDs of the given conditions using the given evaluation method and field to group
* conditions by.
*
* Supported $evalType values:
* - CONDITION_EVAL_TYPE_AND_OR
* - CONDITION_EVAL_TYPE_AND
* - CONDITION_EVAL_TYPE_OR
*
* Example:
* echo CConditionHelper::getFormula(array(
* 1 => ['type' => '1'],
* 2 => ['type' => '1'],
* 5 => ['type' => '2']
* ), CONDITION_EVAL_TYPE_AND_OR);
*
* // ({1} or {2}) and {5}
*
* Keep in sync with JS getConditionFormula().
*
* @param array $conditions
* @param string $group_field_name
* @param int $evalType
*
* @return string
*/
public static function getEvalFormula(array $conditions, string $group_field_name, int $evalType): string {
$groupedConditions = [];
foreach ($conditions as $conditionid => $condition) {
$groupedConditions[$condition[$group_field_name]][] = '{'.$conditionid.'}';
}
// operators
switch ($evalType) {
case CONDITION_EVAL_TYPE_AND:
$conditionOperator = 'and';
$groupOperator = $conditionOperator;
break;
case CONDITION_EVAL_TYPE_OR:
$conditionOperator = 'or';
$groupOperator = $conditionOperator;
break;