/** * Generate data for the trigger configuration form. * * @param string $exprAction expression constructor action, see remakeExpression() for a list of supported values * * @return array */ function getTriggerFormData($exprAction) { $data = array('form' => getRequest('form'), 'form_refresh' => getRequest('form_refresh'), 'parent_discoveryid' => getRequest('parent_discoveryid'), 'dependencies' => getRequest('dependencies', array()), 'db_dependencies' => array(), 'triggerid' => getRequest('triggerid'), 'expression' => getRequest('expression', ''), 'expr_temp' => getRequest('expr_temp', ''), 'description' => getRequest('description', ''), 'type' => getRequest('type', 0), 'priority' => getRequest('priority', 0), 'status' => getRequest('status', 0), 'comments' => getRequest('comments', ''), 'url' => getRequest('url', ''), 'input_method' => getRequest('input_method', IM_ESTABLISHED), 'limited' => false, 'templates' => array(), 'hostid' => getRequest('hostid', 0)); if (!empty($data['triggerid'])) { // get trigger $options = array('output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid'), 'triggerids' => $data['triggerid']); $trigger = $data['parent_discoveryid'] ? API::TriggerPrototype()->get($options) : API::Trigger()->get($options); $data['trigger'] = reset($trigger); // get templates $tmp_triggerid = $data['triggerid']; do { $db_triggers = DBfetch(DBselect('SELECT t.triggerid,t.templateid,id.parent_itemid,h.name,h.hostid' . ' FROM triggers t' . ' LEFT JOIN functions f ON t.triggerid=f.triggerid' . ' LEFT JOIN items i ON f.itemid=i.itemid' . ' LEFT JOIN hosts h ON i.hostid=h.hostid' . ' LEFT JOIN item_discovery id ON i.itemid=id.itemid' . ' WHERE t.triggerid=' . zbx_dbstr($tmp_triggerid))); if (bccomp($data['triggerid'], $tmp_triggerid) != 0) { // parent trigger prototype link if ($data['parent_discoveryid']) { $link = 'trigger_prototypes.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&parent_discoveryid=' . $db_triggers['parent_itemid'] . '&hostid=' . $db_triggers['hostid']; } else { $link = 'triggers.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&hostid=' . $db_triggers['hostid']; } $data['templates'][] = new CLink(CHtml::encode($db_triggers['name']), $link, 'highlight underline weight_normal'); $data['templates'][] = SPACE . '⇒' . SPACE; } $tmp_triggerid = $db_triggers['templateid']; } while ($tmp_triggerid != 0); $data['templates'] = array_reverse($data['templates']); array_shift($data['templates']); $data['limited'] = $data['trigger']['templateid'] != 0; // select first host from triggers if gived not match $hosts = $data['trigger']['hosts']; if (count($hosts) > 0 && !in_array(array('hostid' => $data['hostid']), $hosts)) { $host = reset($hosts); $data['hostid'] = $host['hostid']; } } if (!empty($data['triggerid']) && !isset($_REQUEST['form_refresh']) || $data['limited']) { $data['expression'] = explode_exp($data['trigger']['expression']); if (!$data['limited'] || !isset($_REQUEST['form_refresh'])) { $data['description'] = $data['trigger']['description']; $data['type'] = $data['trigger']['type']; $data['priority'] = $data['trigger']['priority']; $data['status'] = $data['trigger']['status']; $data['comments'] = $data['trigger']['comments']; $data['url'] = $data['trigger']['url']; $db_triggers = DBselect('SELECT t.triggerid,t.description' . ' FROM triggers t,trigger_depends d' . ' WHERE t.triggerid=d.triggerid_up' . ' AND d.triggerid_down=' . zbx_dbstr($data['triggerid'])); while ($trigger = DBfetch($db_triggers)) { if (uint_in_array($trigger['triggerid'], $data['dependencies'])) { continue; } array_push($data['dependencies'], $trigger['triggerid']); } } } if ($data['input_method'] == IM_TREE) { $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; if ($exprAction !== null && $data['eHTMLTree'] != null) { $new_expr = remakeExpression($data['expression'], $_REQUEST['expr_target_single'], $exprAction, $data['expr_temp']); if ($new_expr !== false) { $data['expression'] = $new_expr; $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; } else { show_messages(false, '', _('Expression Syntax Error.')); } $data['expr_temp'] = ''; } else { show_messages(false, '', _('Expression Syntax Error.')); } } $data['expression_field_name'] = 'expr_temp'; $data['expression_field_value'] = $data['expr_temp']; $data['expression_field_readonly'] = true; } else { show_messages(false, '', _('Expression Syntax Error.')); $data['input_method'] = IM_ESTABLISHED; } } if ($data['input_method'] != IM_TREE) { $data['expression_field_name'] = 'expression'; $data['expression_field_value'] = $data['expression']; $data['expression_field_readonly'] = $data['limited']; } if (empty($data['parent_discoveryid'])) { $data['db_dependencies'] = API::Trigger()->get(array('triggerids' => $data['dependencies'], 'output' => array('triggerid', 'flags', 'description'), 'preservekeys' => true, 'selectHosts' => array('hostid', 'name'))); foreach ($data['db_dependencies'] as &$dependency) { if (count($dependency['hosts']) > 1) { order_result($dependency['hosts'], 'name', ZBX_SORT_UP); } $dependency['hosts'] = array_values($dependency['hosts']); $dependency['hostid'] = $dependency['hosts'][0]['hostid']; } unset($dependency); order_result($data['db_dependencies'], 'description'); } return $data; }
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ require_once dirname(__FILE__) . '/include/config.inc.php'; require_once dirname(__FILE__) . '/include/triggers.inc.php'; $page['title'] = _('Test'); $page['file'] = 'tr_testexpr.php'; define('ZBX_PAGE_NO_MENU', 1); define('COMBO_PATTERN', 'str_in_array({},array('); define('COMBO_PATTERN_LENGTH', zbx_strlen(COMBO_PATTERN)); $definedErrorPhrases = array(EXPRESSION_VALUE_TYPE_UNKNOWN => _('Unknown variable type, testing not available'), EXPRESSION_HOST_UNKNOWN => _('Unknown host, no such host present in system'), EXPRESSION_HOST_ITEM_UNKNOWN => _('Unknown host item, no such item in selected host'), EXPRESSION_NOT_A_MACRO_ERROR => _('Given expression is not a macro'), EXPRESSION_FUNCTION_UNKNOWN => _('Incorrect function is used')); require_once dirname(__FILE__) . '/include/page_header.php'; //---------------------------------------------------------------------- // expression analyze $expression = get_request('expression', ''); define('NO_LINK_IN_TESTING', true); list($outline, $eHTMLTree) = analyzeExpression($expression); // test data (create table, create check fields) $data_table = new CTable(null, 'tableinfo'); $data_table->setAttribute('id', 'data_list'); $data_table->setHeader(array(_('Expression Variable Elements'), _('Result type'), _('Value'))); $octet = false; $datas = array(); $fields = array(); $rplcts = array(); $allowedTesting = true; $expressionData = new CTriggerExpression(); if ($expressionData->parse($expression)) { $macrosData = array(); $expressions = array_merge($expressionData->expressions, $expressionData->macros, $expressionData->usermacros); foreach ($expressions as $exprPart) { if (isset($macrosData[$exprPart['expression']])) {
function getTriggerFormData() { $data = array('form' => get_request('form'), 'form_refresh' => get_request('form_refresh'), 'parent_discoveryid' => get_request('parent_discoveryid'), 'dependencies' => get_request('dependencies', array()), 'db_dependencies' => array(), 'triggerid' => get_request('triggerid'), 'expression' => get_request('expression', ''), 'expr_temp' => get_request('expr_temp', ''), 'description' => get_request('description', ''), 'type' => get_request('type', 0), 'priority' => get_request('priority', 0), 'status' => get_request('status', 0), 'comments' => get_request('comments', ''), 'url' => get_request('url', ''), 'input_method' => get_request('input_method', IM_ESTABLISHED), 'limited' => null, 'templates' => array(), 'hostid' => get_request('hostid', 0)); if (!empty($data['triggerid'])) { // get trigger $options = array('output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid'), 'triggerids' => $data['triggerid']); $trigger = $data['parent_discoveryid'] ? API::TriggerPrototype()->get($options) : API::Trigger()->get($options); $data['trigger'] = reset($trigger); // get templates $tmp_triggerid = $data['triggerid']; do { $db_triggers = DBfetch(DBselect('SELECT t.triggerid,t.templateid,id.parent_itemid,h.name,h.hostid' . ' FROM triggers t' . ' LEFT JOIN functions f ON t.triggerid=f.triggerid' . ' LEFT JOIN items i ON f.itemid=i.itemid' . ' LEFT JOIN hosts h ON i.hostid=h.hostid' . ' LEFT JOIN item_discovery id ON i.itemid=id.itemid' . ' WHERE t.triggerid=' . zbx_dbstr($tmp_triggerid))); if (bccomp($data['triggerid'], $tmp_triggerid) != 0) { // parent trigger prototype link if ($data['parent_discoveryid']) { $link = 'trigger_prototypes.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&parent_discoveryid=' . $db_triggers['parent_itemid'] . '&hostid=' . $db_triggers['hostid']; } else { $link = 'triggers.php?form=update&triggerid=' . $db_triggers['triggerid'] . '&hostid=' . $db_triggers['hostid']; } $data['templates'][] = new CLink($db_triggers['name'], $link, 'highlight underline weight_normal'); $data['templates'][] = SPACE . RARR . SPACE; } $tmp_triggerid = $db_triggers['templateid']; } while ($tmp_triggerid != 0); $data['templates'] = array_reverse($data['templates']); array_shift($data['templates']); $data['limited'] = $data['trigger']['templateid'] ? 'yes' : null; // if no host has been selected for the navigation panel, use the first trigger host if (!$data['hostid']) { $hosts = reset($data['trigger']['hosts']); $data['hostid'] = $hosts['hostid']; } } if (!empty($data['triggerid']) && !isset($_REQUEST['form_refresh']) || !empty($data['limited'])) { $data['expression'] = explode_exp($data['trigger']['expression']); if (empty($data['limited']) || !isset($_REQUEST['form_refresh'])) { $data['description'] = $data['trigger']['description']; $data['type'] = $data['trigger']['type']; $data['priority'] = $data['trigger']['priority']; $data['status'] = $data['trigger']['status']; $data['comments'] = $data['trigger']['comments']; $data['url'] = $data['trigger']['url']; $db_triggers = DBselect('SELECT t.triggerid,t.description' . ' FROM triggers t,trigger_depends d' . ' WHERE t.triggerid=d.triggerid_up' . ' AND d.triggerid_down=' . zbx_dbstr($data['triggerid'])); while ($trigger = DBfetch($db_triggers)) { if (uint_in_array($trigger['triggerid'], $data['dependencies'])) { continue; } array_push($data['dependencies'], $trigger['triggerid']); } } } if ($data['input_method'] == IM_TREE) { $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; if (isset($_REQUEST['expr_action']) && $data['eHTMLTree'] != null) { $new_expr = remakeExpression($data['expression'], $_REQUEST['expr_target_single'], $_REQUEST['expr_action'], $data['expr_temp']); if ($new_expr !== false) { $data['expression'] = $new_expr; $analyze = analyzeExpression($data['expression']); if ($analyze !== false) { list($data['outline'], $data['eHTMLTree']) = $analyze; } else { show_messages(false, '', _('Expression Syntax Error.')); } $data['expr_temp'] = ''; } else { show_messages(false, '', _('Expression Syntax Error.')); } } $data['expression_field_name'] = 'expr_temp'; $data['expression_field_value'] = $data['expr_temp']; $data['expression_field_readonly'] = 'yes'; $data['expression_field_params'] = 'this.form.elements["' . $data['expression_field_name'] . '"].value'; $data['expression_macro_button'] = new CButton('insert_macro', _('Insert macro'), 'return call_ins_macro_menu(event);', 'formlist'); if ($data['limited'] == 'yes') { $data['expression_macro_button']->setAttribute('disabled', 'disabled'); } } else { show_messages(false, '', _('Expression Syntax Error.')); $data['input_method'] = IM_ESTABLISHED; } } if ($data['input_method'] != IM_TREE) { $data['expression_field_name'] = 'expression'; $data['expression_field_value'] = $data['expression']; $data['expression_field_readonly'] = $data['limited']; } if (empty($data['parent_discoveryid'])) { $data['db_dependencies'] = API::Trigger()->get(array('triggerids' => $data['dependencies'], 'output' => array('triggerid', 'description'), 'preservekeys' => true, 'selectHosts' => array('name'))); foreach ($data['db_dependencies'] as &$dependency) { if (!empty($dependency['hosts'][0]['name'])) { $dependency['host'] = $dependency['hosts'][0]['name']; } unset($dependency['hosts']); } order_result($data['db_dependencies'], 'description'); } return $data; }