1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * @var CView $this 24 */ 25 26// Create form. 27$expression_form = (new CForm()) 28 ->cleanItems() 29 ->setName('expression') 30 ->addVar('action', 'popup.triggerexpr') 31 ->addVar('dstfrm', $data['dstfrm']) 32 ->addVar('dstfld1', $data['dstfld1']) 33 ->addItem((new CVar('hostid', $data['hostid']))->removeId()) 34 ->addVar('groupid', $data['groupid']) 35 ->addVar('function', $data['function']) 36 ->addItem((new CInput('submit', 'submit')) 37 ->addStyle('display: none;') 38 ->removeId() 39 ); 40 41if ($data['parent_discoveryid'] !== '') { 42 $expression_form->addVar('parent_discoveryid', $data['parent_discoveryid']); 43} 44 45// Create form list. 46$expression_form_list = new CFormList(); 47 48// Append item to form list. 49$popup_options = [ 50 'srctbl' => 'items', 51 'srcfld1' => 'itemid', 52 'srcfld2' => 'name', 53 'dstfrm' => $expression_form->getName(), 54 'dstfld1' => 'itemid', 55 'dstfld2' => 'item_description', 56 'with_webitems' => '1', 57 'writeonly' => '1' 58]; 59 60if ($data['hostid']) { 61 $popup_options['hostid'] = $data['hostid']; 62} 63 64if ($data['parent_discoveryid'] !== '') { 65 $popup_options['normal_only'] = '1'; 66} 67 68if ($data['item_required']) { 69 $expression_form->addVar('itemid', $data['itemid']); 70 71 $item = [ 72 (new CTextBox('item_description', $data['item_description'], true)) 73 ->setAriaRequired() 74 ->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH), 75 (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN), 76 (new CButton('select', _('Select'))) 77 ->addClass(ZBX_STYLE_BTN_GREY) 78 ->onClick('return PopUp("popup.generic",'.json_encode($popup_options).', null, this);') 79 ]; 80 81 if ($data['parent_discoveryid'] !== '') { 82 $item[] = (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN); 83 $item[] = (new CButton('select', _('Select prototype'))) 84 ->addClass(ZBX_STYLE_BTN_GREY) 85 ->onClick('return PopUp("popup.generic",'. 86 json_encode([ 87 'srctbl' => 'item_prototypes', 88 'srcfld1' => 'itemid', 89 'srcfld2' => 'name', 90 'dstfrm' => $expression_form->getName(), 91 'dstfld1' => 'itemid', 92 'dstfld2' => 'item_description', 93 'parent_discoveryid' => $data['parent_discoveryid'] 94 ]).', null, this);' 95 ) 96 ->removeId(); 97 } 98 99 $expression_form_list->addRow((new CLabel(_('Item'), 'item_description'))->setAsteriskMark(), $item); 100} 101 102$function_select = (new CSelect('function_select')) 103 ->setFocusableElementId('label-function') 104 ->setId('function-select') 105 ->setValue($data['function_type'].'_'.$data['function']); 106 107$function_types = [ 108 ZBX_FUNCTION_TYPE_AGGREGATE => _('Aggregate functions'), 109 ZBX_FUNCTION_TYPE_BITWISE => _('Bitwise functions'), 110 ZBX_FUNCTION_TYPE_DATE_TIME => _('Date and time functions'), 111 ZBX_FUNCTION_TYPE_HISTORY => _('History functions'), 112 ZBX_FUNCTION_TYPE_MATH => _('Mathematical functions'), 113 ZBX_FUNCTION_TYPE_OPERATOR => _('Operator functions'), 114 ZBX_FUNCTION_TYPE_PREDICTION => _('Prediction functions'), 115 ZBX_FUNCTION_TYPE_STRING => _('String functions') 116]; 117 118$functions_by_group = []; 119foreach ($data['functions'] as $id => $function) { 120 foreach ($function['types'] as $type) { 121 $functions_by_group[$function_types[$type]][$type.'_'.$id] = $function['description']; 122 } 123} 124ksort($functions_by_group); 125 126foreach ($functions_by_group as $group_name => $functions) { 127 $function_select->addOptionGroup( 128 (new CSelectOptionGroup($group_name))->addOptions(CSelect::createOptionsFromArray($functions)) 129 ); 130} 131 132$expression_form_list->addRow(new CLabel(_('Function'), $function_select->getFocusableElementId()), $function_select); 133 134if (array_key_exists('params', $data['functions'][$data['selectedFunction']])) { 135 $paramid = 0; 136 137 // Functions with optional #num and time shift parameters. 138 $count_functions = [ 139 'acos', 'ascii', 'asin', 'atan', 'atan2', 'between', 'bitand', 'bitlength', 'bitlshift', 'bitnot', 'bitor', 140 'bitrshift', 'bitxor', 'bytelength', 'cbrt', 'ceil', 'char', 'concat', 'cos', 'cosh', 'cot', 'degrees', 'exp', 141 'expm1', 'floor', 'in', 'insert', 'last', 'left', 'log', 'log10', 'ltrim', 'mid', 'mod', 'power', 'radians', 142 'repeat', 'replace', 'right', 'round', 'rtrim', 'signum', 'sin', 'sinh', 'sqrt', 'tan', 'trim', 'truncate' 143 ]; 144 145 foreach ($data['functions'][$data['selectedFunction']]['params'] as $param_name => $param_function) { 146 if (array_key_exists($param_name, $data['params'])) { 147 $param_value = $data['params'][$param_name]; 148 } 149 else { 150 $param_value = array_key_exists($paramid, $data['params']) ? $data['params'][$paramid] : null; 151 } 152 153 $label = $param_function['A'] ? (new CLabel($param_function['C']))->setAsteriskMark() : $param_function['C']; 154 155 if ($param_function['T'] == T_ZBX_INT) { 156 $param_type_element = null; 157 158 if (in_array($param_name, ['last'])) { 159 if (array_key_exists('M', $param_function)) { 160 if (in_array($data['selectedFunction'], $count_functions)) { 161 $param_type_element = $param_function['M'][PARAM_TYPE_COUNTS]; 162 $label = $param_function['C']; 163 $expression_form->addItem((new CVar('paramtype', PARAM_TYPE_COUNTS))->removeId()); 164 } 165 else { 166 $param_type_element = (new CSelect('paramtype')) 167 ->setValue($param_value === '' ? PARAM_TYPE_TIME : $data['paramtype']) 168 ->addOptions(CSelect::createOptionsFromArray($param_function['M'])); 169 } 170 } 171 else { 172 $expression_form->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId()); 173 $param_type_element = _('Time'); 174 } 175 } 176 elseif (in_array($param_name, ['shift'])) { 177 $param_type_element = _('Time'); 178 } 179 elseif (in_array($param_name, ['period_shift'])) { 180 $param_type_element = _('Period'); 181 } 182 183 $param_field = (new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH); 184 185 if ($param_name === 'period_shift') { 186 $param_field->setAttribute('placeholder', 'now/h'); 187 } 188 elseif ($param_name === 'shift') { 189 $param_field->setAttribute('placeholder', 'now-h'); 190 } 191 192 $expression_form_list->addRow($label, [ 193 $param_field, 194 (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN), 195 $param_type_element 196 ]); 197 } 198 else { 199 $expression_form_list->addRow($label, 200 (new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH) 201 ); 202 if ($paramid === 0) { 203 $expression_form->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId()); 204 } 205 } 206 207 $paramid++; 208 } 209} 210else { 211 $expression_form->addVar('paramtype', PARAM_TYPE_TIME); 212} 213 214if (array_key_exists('operators', $data['functions'][$data['selectedFunction']])) { 215 $expression_form_list->addRow( 216 (new CLabel(_('Result'), 'value'))->setAsteriskMark(), [ 217 (new CSelect('operator')) 218 ->setValue($data['operator']) 219 ->setFocusableElementId('value') 220 ->addOptions(CSelect::createOptionsFromArray(array_combine($data['functions'][$data['function']]['operators'], 221 $data['functions'][$data['function']]['operators'] 222 ))), 223 ' ', 224 (new CTextBox('value', $data['value'])) 225 ->setAriaRequired() 226 ->setWidth(ZBX_TEXTAREA_SMALL_WIDTH) 227 ] 228 ); 229} 230 231$expression_form->addItem($expression_form_list); 232 233$output = [ 234 'header' => $data['title'], 235 'body' => (new CDiv([$data['errors'], $expression_form]))->toString(), 236 'buttons' => [ 237 [ 238 'title' => _('Insert'), 239 'class' => '', 240 'keepOpen' => true, 241 'isSubmit' => true, 242 'action' => 'return validate_trigger_expression(overlay);' 243 ] 244 ], 245 'script_inline' => $this->readJsFile('popup.triggerexpr.js.php') 246]; 247 248if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) { 249 CProfiler::getInstance()->stop(); 250 $output['debug'] = CProfiler::getInstance()->make()->toString(); 251} 252 253echo json_encode($output); 254