** 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.
**/
require_once dirname(__FILE__) . '/js/adm.regexprs.edit.js.php';
$widget = (new CWidget())->setTitle(_('Regular expressions'))->setControls((new CForm())->cleanItems()->addItem((new CList())->addItem(makeAdministrationGeneralMenu('adm.regexps.php'))));
$form = (new CForm())->setId('zabbixRegExpForm')->addVar('form', 1)->addVar('regexpid', $data['regexpid']);
/*
 * Expressions tab
 */
$exprTable = (new CTable())->setId('tbl_expr')->setAttribute('style', 'width: 100%;')->setHeader([_('Expression type'), _('Expression'), _('Delimiter'), _('Case sensitive'), _('Action')]);
foreach ($data['expressions'] as $i => $expression) {
    $exp_delimiter = new CComboBox('expressions[' . $i . '][exp_delimiter]', $expression['exp_delimiter'], null, expressionDelimiters());
    if ($expression['expression_type'] != EXPRESSION_TYPE_ANY_INCLUDED) {
        $exp_delimiter->addStyle('display: none;');
    }
    $row = [(new CComboBox('expressions[' . $i . '][expression_type]', $expression['expression_type'], null, expression_type2str()))->onChange('onChangeExpressionType(this, ' . $i . ')'), (new CTextBox('expressions[' . $i . '][expression]', $expression['expression'], false, 255))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH), $exp_delimiter, (new CCheckBox('expressions[' . $i . '][case_sensitive]', '1'))->setChecked($expression['case_sensitive'])];
    $button_cell = [(new CButton('expressions[' . $i . '][remove]', _('Remove')))->addClass(ZBX_STYLE_BTN_LINK)->addClass('element-table-remove')];
    if (array_key_exists('expressionid', $expression)) {
        $button_cell[] = new CVar('expressions[' . $i . '][expressionid]', $expression['expressionid']);
    }
    $row[] = (new CCol($button_cell))->addClass(ZBX_STYLE_NOWRAP);
    $exprTable->addRow((new CRow($row))->addClass('form_row')->setAttribute('data-index', $i));
}
$exprTable->setFooter((new CButton('expression_add', _('Add')))->addClass(ZBX_STYLE_BTN_LINK)->addClass('element-table-add'));
$exprTab = (new CFormList('exprTab'))->addRow(_('Name'), (new CTextBox('name', $data['name'], false, 128))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)->setAttribute('autofocus', 'autofocus'))->addRow(_('Expressions'), (new CDiv($exprTable))->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;'));
/*
 * Test tab
/*
 * Expressions tab
 */
$exprTab = new CFormList('exprTab');
$nameTextBox = new CTextBox('name', $this->get('name'), ZBX_TEXTBOX_STANDARD_SIZE, false, 128);
$nameTextBox->attr('autofocus', 'autofocus');
$exprTab->addRow(_('Name'), $nameTextBox);
$exprTable = new CTable(null, 'formElementTable formWideTable');
$exprTable->attr('id', 'exprTable');
$exprTable->setHeader(array(_('Expression'), new CCol(_('Expression type'), 'nowrap'), new CCol(_('Case sensitive'), 'nowrap'), SPACE));
$exprTable->setFooter(new CButton('add', _('Add'), null, 'link_menu exprAdd'));
$exprTab->addRow(_('Expressions'), new CDiv($exprTable, 'inlineblock border_dotted objectgroup'));
$exprForm = new CTable(null, 'formElementTable');
$exprForm->addRow(array(_('Expression'), new CTextBox('expressionNew', null, ZBX_TEXTBOX_STANDARD_SIZE)));
$exprForm->addRow(array(_('Expression type'), new CComboBox('typeNew', null, null, expression_type2str())));
$exprForm->addRow(array(_('Delimiter'), new CComboBox('delimiterNew', null, null, expressionDelimiters())), null, 'delimiterNewRow');
$exprForm->addRow(array(_('Case sensitive'), new CCheckBox('case_sensitiveNew')));
$exprFormFooter = array(new CButton('saveExpression', _('Add'), null, 'link_menu'), SPACE, new CButton('cancelExpression', _('Cancel'), null, 'link_menu'));
$exprTab->addRow(null, new CDiv(array($exprForm, $exprFormFooter), 'objectgroup inlineblock border_dotted'), true, 'exprForm');
/*
 * Test tab
 */
$testTab = new CFormList('testTab');
$testTab->addRow(_('Test string'), new CTextArea('test_string', $this->get('test_string')));
$preloaderDiv = new CDiv(null, 'preloader', 'testPreloader');
$preloaderDiv->addStyle('display: none');
$testTab->addRow(SPACE, array(new CButton('testExpression', _('Test expressions')), $preloaderDiv));
$tabExp = new CTableInfo(null);
$tabExp->attr('id', 'testResultTable');
$tabExp->setHeader(array(_('Expression'), _('Expression type'), _('Result')));
$testTab->addRow(_('Result'), $tabExp);
<script type="text/x-jquery-tmpl" id="row_expr">
	<?php 
echo (new CRow([(new CComboBox('expressions[#{rowNum}][expression_type]', null, null, expression_type2str()))->onChange('onChangeExpressionType(this, #{rowNum})'), (new CTextBox('expressions[#{rowNum}][expression]', '', false, 255))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH), (new CComboBox('expressions[#{rowNum}][exp_delimiter]', null, null, expressionDelimiters()))->addStyle('display: none;'), new CCheckBox('expressions[#{rowNum}][case_sensitive]'), (new CCol((new CButton('expressions[#{rowNum}][remove]', _('Remove')))->addClass(ZBX_STYLE_BTN_LINK)->addClass('element-table-remove')))->addClass(ZBX_STYLE_NOWRAP)]))->addClass('form_row')->setAttribute('data-index', '#{rowNum}')->toString();
?>
</script>

<script type="text/x-jquery-tmpl" id="testTableRow">
	<?php 
echo (new CRow(['#{type}', '#{expression}', (new CSpan('#{result}'))->addClass('#{resultClass}')]))->addClass('test_row')->toString();
?>
</script>

<script type="text/x-jquery-tmpl" id="testCombinedTableRow">
	<?php 
echo (new CRow([(new CCol(_('Combined result')))->setColspan(2), (new CSpan('#{result}'))->addClass('#{resultClass}')]))->addClass('test_row')->toString();
?>
</script>

<script>
	function onChangeExpressionType(obj, index) {
		if (obj.value === '<?php 
echo EXPRESSION_TYPE_ANY_INCLUDED;
?>
') {
			jQuery('#expressions_' + index + '_exp_delimiter').show();
		}
		else {
			jQuery('#expressions_' + index + '_exp_delimiter').hide();
		}
	}