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
27<script type="text/x-jquery-tmpl" id="exec_params_row">
28	<tr class="form_row">
29		<td>
30			<input type="text" id="exec_params_#{rowNum}_exec_param" name="exec_params[#{rowNum}][exec_param]" maxlength="255" style="width: <?= ZBX_TEXTAREA_STANDARD_WIDTH ?>px;">
31		</td>
32		<td>
33			<button type="button" id="exec_params_#{rowNum}_remove" name="exec_params[#{rowNum}][remove]" class="<?= ZBX_STYLE_BTN_LINK ?> element-table-remove"><?= _('Remove') ?></button>
34		</td>
35	</tr>
36</script>
37<script type="text/x-jquery-tmpl" id="message-templates-row-tmpl">
38	<?= (new CRow([
39			new CCol('#{message_type_name}'),
40			(new CCol([
41				new CSpan('#{message}'),
42				new CInput('hidden', 'message_templates[#{message_type}][eventsource]', '#{eventsource}'),
43				new CInput('hidden', 'message_templates[#{message_type}][recovery]', '#{recovery}'),
44				new CInput('hidden', 'message_templates[#{message_type}][subject]', '#{subject}'),
45				new CInput('hidden', 'message_templates[#{message_type}][message]', '#{message}')
46			]))
47				->addClass(ZBX_STYLE_OVERFLOW_ELLIPSIS)
48				->addStyle('max-width: '.ZBX_TEXTAREA_MEDIUM_WIDTH.'px;'),
49			(new CHorList([
50				(new CButton(null, _('Edit')))
51					->addClass(ZBX_STYLE_BTN_LINK)
52					->setAttribute('data-action', 'edit'),
53				(new CButton(null, _('Remove')))
54					->addClass(ZBX_STYLE_BTN_LINK)
55					->onClick("removeMessageTemplate('#{message_type}');")
56			]))->addClass(ZBX_STYLE_NOWRAP)
57		]))
58			->setAttribute('data-message-type', '#{message_type}')
59			->toString()
60	?>
61</script>
62<script type="text/javascript">
63	var message_templates = <?= json_encode(CMediatypeHelper::getAllMessageTemplates(), JSON_FORCE_OBJECT) ?>,
64		message_template_list = {};
65
66	/**
67	 * Draws message template table.
68	 *
69	 * @param {array} list  An array of message templates.
70	 */
71	function populateMessageTemplates(list) {
72		var row_template = new Template(jQuery('#message-templates-row-tmpl').html());
73
74		for (var i = 0; i < list.length; i++) {
75			var template = list[i],
76				message_template = getMessageTemplate(template.eventsource, template.recovery);
77
78			template.message_type = message_template.message_type;
79			template.message_type_name = message_template.name;
80
81			if ('old_message_type' in template && template.old_message_type != -1) {
82				jQuery('tr[data-message-type=' + template.old_message_type + ']')
83					.replaceWith(row_template.evaluate(template));
84
85				if (template.message_type != template.old_message_type) {
86					delete message_template_list[template.old_message_type];
87				}
88			}
89			else {
90				jQuery('#message-templates-footer').before(row_template.evaluate(template));
91			}
92
93			message_template_list[template.message_type] = template;
94		}
95
96		toggleAddButton();
97	}
98
99	/**
100	 * Returns message type and name by the specified event source and operation mode.
101	 *
102	 * @param {number} eventsource  Event source.
103	 * @param {number} recovery     Operation mode.
104	 *
105	 * @return {object}
106	 */
107	function getMessageTemplate(eventsource, recovery) {
108		for (var message_type in message_templates) {
109			if (!message_templates.hasOwnProperty(message_type)) {
110				continue;
111			}
112
113			var template = message_templates[message_type];
114
115			if (template.eventsource == eventsource && template.recovery == recovery) {
116				return {
117					message_type: message_type,
118					name: template.name
119				};
120			}
121		}
122	}
123
124	/**
125	 * Toggles the "Add" button state and changes its text depending on message template count.
126	 */
127	function toggleAddButton() {
128		var limit_reached = (Object.keys(message_template_list).length == Object.keys(message_templates).length);
129
130		jQuery('#message-templates-footer .btn-link')
131			.prop('disabled', limit_reached)
132			.text(limit_reached
133				? <?= json_encode(_('Add (message type limit reached)')) ?>
134				: <?= json_encode(_('Add')) ?>
135			);
136	}
137
138	/**
139	 * Removes a template from the list of message templates.
140	 *
141	 * @param {number} message_type  Message type.
142	 */
143	function removeMessageTemplate(message_type) {
144		jQuery('tr[data-message-type=' + message_type + ']').remove();
145		delete message_template_list[message_type];
146		toggleAddButton();
147	}
148
149	jQuery(function($) {
150		populateMessageTemplates(<?= json_encode(array_values($this->data['message_templates'])) ?>);
151
152		$('#message-templates').on('click', '[data-action]', function() {
153			var $btn = $(this),
154				params = {
155					type: $('#type').val(),
156					content_type: $('input[name="content_type"]:checked').val(),
157					message_types: $('tr[data-message-type]').map(function() {
158						return $(this).data('message-type');
159					}).get()
160				};
161
162			switch ($btn.data('action')) {
163				case 'add':
164					PopUp('popup.mediatype.message', params, null, $btn);
165					break;
166
167				case 'edit':
168					var $row = $btn.closest('tr');
169
170					params.message_type = $row.data('message-type');
171					params.old_message_type = params.message_type;
172
173					$row.find('input[type="hidden"]').each(function() {
174						var $input = $(this),
175							name = $input.attr('name').match(/\[([^\]]+)]$/);
176
177						if (name) {
178							params[name[1]] = $input.val();
179						}
180					});
181
182					PopUp('popup.mediatype.message', params, null, $btn);
183					break;
184			}
185		});
186
187		var old_media_type = $('#type').val();
188
189		// type of media
190		$('#type').change(function() {
191			var media_type = $(this).val();
192
193			$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #gsm_modem, #passwd, #smtp_verify_peer, ' +
194					'#smtp_verify_host, #smtp_username, #smtp_security, #smtp_authentication, #exec_path, ' +
195					'#exec_params_table, #content_type')
196				.closest('li')
197				.hide();
198
199			$('li[id^="row_webhook_"]').hide();
200
201			switch (media_type) {
202				case '<?= MEDIA_TYPE_EMAIL ?>':
203					$('#smtp_server, #smtp_port, #smtp_helo, #smtp_email, #smtp_security, #smtp_authentication, #content_type' )
204						.closest('li')
205						.show();
206					// radio button actions
207					toggleSecurityOptions();
208					toggleAuthenticationOptions();
209					setMaxSessionsType(media_type);
210					break;
211
212				case '<?= MEDIA_TYPE_EXEC ?>':
213					$('#exec_path, #exec_params_table').closest('li').show();
214					setMaxSessionsType(media_type);
215					break;
216
217				case '<?= MEDIA_TYPE_SMS ?>':
218					$('#gsm_modem').closest('li').show();
219					setMaxSessionsType(media_type);
220					break;
221
222				case '<?= MEDIA_TYPE_WEBHOOK ?>':
223					$('li[id^="row_webhook_"]').show();
224					setMaxSessionsType(media_type);
225					break;
226			}
227		});
228
229		// clone button
230		$('#clone').click(function() {
231			$('#mediatypeid, #delete, #clone').remove();
232			$('#chPass_btn').hide();
233			$('#passwd').prop('disabled', false).show();
234			$('#update').text(<?= json_encode(_('Add')) ?>);
235			$('#update').val('mediatype.create').attr({id: 'add'});
236			$('#name').focus();
237		});
238
239		// Trim spaces on sumbit. Spaces for script parameters should not be trimmed.
240		$('#media-type-form').submit(function() {
241			var maxattempts = $('#maxattempts'),
242				maxsessions_type = $('#maxsessions_type :radio:checked').val(),
243				maxsessions = $('#maxsessions');
244
245			if ($.trim(maxattempts.val()) === '') {
246				maxattempts.val(0);
247			}
248
249			if (maxsessions_type !== 'custom') {
250				maxsessions.val(maxsessions_type === 'one' ? 1 : 0);
251			}
252			else if (maxsessions_type === 'custom' && $.trim(maxsessions.val()) === '') {
253				maxsessions.val(0);
254			}
255
256			$(this).trimValues([
257				'#name', '#smtp_server', '#smtp_port', '#smtp_helo', '#smtp_email', '#exec_path', '#gsm_modem',
258				'#smtp_username', '#maxsessions', 'input[name^="parameters"]', 'input[name="script"]',
259				'#event_menu_name', '#event_menu_url'
260			]);
261		});
262
263		$('#maxsessions_type :radio').change(function() {
264			toggleMaxSessionsVisibility($(this).val());
265		});
266
267		// Refresh field visibility on document load.
268		$('#type').trigger('change');
269		$('#maxsessions_type :radio:checked').trigger('change');
270
271		$('input[name=smtp_security]').change(function() {
272			toggleSecurityOptions();
273		});
274
275		$('input[name=smtp_authentication]').change(function() {
276			toggleAuthenticationOptions();
277		});
278
279		$('#show_event_menu').change(function() {
280			$('#event_menu_url, #event_menu_name').prop('disabled', !$(this).is(':checked'));
281		});
282
283		$('#parameters_table').dynamicRows({ template: '#parameters_row' });
284
285		/**
286		 * Show or hide "SSL verify peer" and "SSL verify host" fields.
287		 */
288		function toggleSecurityOptions() {
289			if ($('input[name=smtp_security]:checked').val() == <?= SMTP_CONNECTION_SECURITY_NONE ?>) {
290				$('#smtp_verify_peer, #smtp_verify_host').prop('checked', false).closest('li').hide();
291			}
292			else {
293				$('#smtp_verify_peer, #smtp_verify_host').closest('li').show();
294			}
295		}
296
297		/**
298		 * Show or hide "Username" and "Password" fields.
299		 */
300		function toggleAuthenticationOptions() {
301			if ($('input[name=smtp_authentication]:checked').val() == <?= SMTP_AUTHENTICATION_NORMAL ?>) {
302				$('#smtp_username, #passwd').closest('li').show();
303			}
304			else {
305				$('#smtp_username, #passwd').val('').closest('li').hide();
306			}
307		}
308
309		/**
310		 * Show or hide concurrent sessions custom input box.
311		 *
312		 * @param {string} maxsessions_type		Selected concurrent sessions value. One of 'one', 'unlimited', 'custom'.
313		 */
314		function toggleMaxSessionsVisibility(maxsessions_type) {
315			var maxsessions = $('#maxsessions');
316
317			if (maxsessions_type === 'one' || maxsessions_type === 'unlimited') {
318				maxsessions.hide();
319			}
320			else {
321				maxsessions.show().select().focus();
322			}
323		}
324
325		/**
326		 * Set concurrent sessions accessibility.
327		 *
328		 * @param {number} media_type		Selected media type.
329		 */
330		function setMaxSessionsType(media_type) {
331			var maxsessions_type = $('#maxsessions_type :radio');
332
333			if (media_type == <?= MEDIA_TYPE_SMS ?>) {
334				maxsessions_type.prop('disabled', true).filter('[value=one]').prop('disabled', false);
335			}
336			else {
337				maxsessions_type.prop('disabled', false);
338			}
339
340			if (old_media_type != media_type) {
341				old_media_type = media_type;
342				maxsessions_type.filter('[value=one]').click();
343			}
344		}
345
346		$('#exec_params_table').dynamicRows({ template: '#exec_params_row' });
347
348		$('#chPass_btn').on('click', function() {
349			$(this).hide();
350			$('#passwd')
351				.show()
352				.prop('disabled', false)
353				.focus();
354		});
355	});
356</script>
357