add_post_js = $add_post_js; $this->setId(uniqid('macro-value-')); } /** * Get content of all Javascript code. * * @return string Javascript code. */ public function getPostJS(): string { return 'jQuery("#'.$this->getId().'").macroValue();'; } /** * Allow to revert macro value. */ public function addRevertButton() { $this->revert_button = (new CSimpleButton()) ->addClass(ZBX_STYLE_BTN_ALT) ->addClass(ZBX_ICON_ARROW_BACK) ->addClass(self::ZBX_STYLE_BTN_UNDO) ->setAttribute('title', _('Revert changes')); return $this; } /** * Set revert macro value button visibility. * * @param bool $visible Button visibility state. */ public function setRevertButtonVisibility(bool $visible) { $this->revert_visible = $visible; return $this; } /** * Render object. * * @param boolean $destroy * * @return string */ public function toString($destroy = true) { $name = $this->getAttribute('name'); $value_type = $this->getAttribute('type'); $value = $this->getAttribute('value'); $readonly = (bool) $this->getAttribute('readonly'); $elements = []; if ($value_type == ZBX_MACRO_TYPE_TEXT) { $wrapper_class = self::ZBX_STYLE_MACRO_INPUT_GROUP.' '.self::ZBX_STYLE_MACRO_VALUE_TEXT; $dropdown_btn_class = ZBX_ICON_TEXT; $elements[] = (new CTextAreaFlexible($name.'[value]', $value, ['add_post_js' => $this->add_post_js])) ->setMaxlength($this->maxlength) ->setAttribute('placeholder', _('value')) ->disableSpellcheck() ->setReadonly($readonly); } elseif ($value_type == ZBX_MACRO_TYPE_VAULT) { $wrapper_class = self::ZBX_STYLE_MACRO_INPUT_GROUP.' '.self::ZBX_STYLE_MACRO_VALUE_VAULT; $dropdown_btn_class = ZBX_ICON_LOCK; $elements[] = (new CTextAreaFlexible($name.'[value]', $value, ['add_post_js' => $this->add_post_js])) ->setMaxlength($this->maxlength) ->setAttribute('placeholder', _('value')) ->disableSpellcheck() ->setReadonly($readonly); } else { $wrapper_class = self::ZBX_STYLE_MACRO_INPUT_GROUP.' '.self::ZBX_STYLE_MACRO_VALUE_SECRET; $dropdown_btn_class = ZBX_ICON_EYE_OFF; $elements[] = (new CInputSecret($name.'[value]', $value, $this->add_post_js)) ->setAttribute('maxlength', $this->maxlength) ->setAttribute('disabled', $readonly ? 'disabled' : null) ->setAttribute('placeholder', _('value')); } if ($this->revert_button !== null) { $elements[] = $this->revert_button->addStyle($this->revert_visible ? 'display: block' : ''); } $elements[] = (new CButtonDropdown($name.'[type]', $value_type, [ ['label' => _('Text'), 'value' => ZBX_MACRO_TYPE_TEXT, 'class' => ZBX_ICON_TEXT], ['label' => _('Secret text'), 'value' => ZBX_MACRO_TYPE_SECRET, 'class' => ZBX_ICON_EYE_OFF], ['label' => _('Vault secret'), 'value' => ZBX_MACRO_TYPE_VAULT, 'class' => ZBX_ICON_LOCK] ])) ->addClass($dropdown_btn_class) ->setAttribute('disabled', $readonly ? 'disabled' : null) ->setAttribute('aria-label', _('Change type')); $node = (new CDiv()) ->addClass($wrapper_class) ->addItem($elements); if ($this->add_post_js) { zbx_add_post_js($this->getPostJS()); } return $node->toString(true); } }