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/javascript"> 28 29 jQuery(function($) { 30 var $form = $('form[name="user_group_form"]'), 31 $new_group_right_table = $form.find('table#new-group-right-table'), 32 $group_right_table_container = $form.find('table#group-right-table').parent(), 33 $new_tag_filter_table = $form.find('table#new-tag-filter-table'), 34 $tag_filter_table_container = $form.find('table#tag-filter-table').parent(), 35 $ms_tag_filter_groups = $new_tag_filter_table.find('.multiselect'), 36 $ms_group_right_groups = $new_group_right_table.find('.multiselect'), 37 timeoutid_new_group_right, 38 timeoutid_new_tag_filter, 39 xhr_new_group_right, 40 xhr_new_tag_filter; 41 42 $form.submit(function() { 43 $form.trimValues(['#name']); 44 }); 45 46 /** 47 * Collects data. 48 * 49 * @return {object} 50 */ 51 function collectTagFilterFormData() { 52 var data = { 53 new_tag_filter: {groupids: []}, 54 tag_filters: [] 55 }; 56 57 $ms_tag_filter_groups.multiSelect('getData').forEach(function(ms_item) { 58 data.new_tag_filter.groupids.push(ms_item.id); 59 }); 60 61 data.new_tag_filter.include_subgroups = $new_tag_filter_table 62 .find('[name="new_tag_filter[include_subgroups]"]').prop('checked') ? '1' : '0'; 63 64 data.new_tag_filter.tag = $new_tag_filter_table.find('[name="new_tag_filter[tag]"]').val(); 65 data.new_tag_filter.value = $new_tag_filter_table.find('[name="new_tag_filter[value]"]').val(); 66 67 $tag_filter_table_container.find('[name="tag_filter"]').each(function(i, node) { 68 data.tag_filters.push(JSON.parse(node.value)); 69 }); 70 71 return data; 72 } 73 74 /** 75 * Collects data. 76 * 77 * @return {object} 78 */ 79 function collectGroupRightFormData() { 80 var data = { 81 new_group_right: {groupids: []}, 82 group_rights: {} 83 }; 84 85 $ms_group_right_groups.multiSelect('getData').forEach(function(ms_item) { 86 data.new_group_right.groupids.push(ms_item.id); 87 }); 88 89 data.new_group_right.include_subgroups = $new_group_right_table 90 .find('[name="new_group_right[include_subgroups]"]').prop('checked') ? '1' : '0'; 91 92 data.new_group_right.permission = $new_group_right_table 93 .find('[name="new_group_right[permission]"]').filter(':checked').val(); 94 95 data.group_rights = $.extend.apply({}, 96 $group_right_table_container.find('[name="group_right"]').map(function(i, node) { 97 var obj = JSON.parse(node.value), 98 permission = jQuery(node).parent().find('input[type="radio"]').filter(':checked').val(); 99 100 if (typeof permission !== 'undefined') { 101 obj[Object.keys(obj)[0]].permission = permission; 102 } 103 return obj; 104 }) 105 ); 106 107 return data; 108 } 109 110 /** 111 * During long request, shows indicator and disables form elements. 112 */ 113 function disableNewGroupRightForm() { 114 timeoutid_new_group_right = setTimeout(function() { 115 $ms_group_right_groups.multiSelect('disable'); 116 $new_group_right_table.find('button, [name^="new_group_right"]').prop('disabled', true); 117 $group_right_table_container.find('input[type="radio"]').prop('disabled', true); 118 }, 150); 119 } 120 121 /** 122 * During long request, shows indicator and disables form elements. 123 */ 124 function disableNewTagFilterForm() { 125 timeoutid_new_tag_filter = setTimeout(function() { 126 $ms_tag_filter_groups.multiSelect('disable'); 127 $new_tag_filter_table.find('button, [name^="new_tag_filter"]').prop('disabled', true); 128 $tag_filter_table_container.find('button').prop('disabled', true); 129 }, 150); 130 } 131 132 /** 133 * Removes loading indicator and enables form elements. 134 */ 135 function enableNewGroupRightForm() { 136 clearTimeout(timeoutid_new_group_right); 137 $ms_group_right_groups.multiSelect('enable'); 138 $new_group_right_table.find('button, [name^="new_group_right"]').prop('disabled', false); 139 $group_right_table_container.find('input[type="radio"]').prop('disabled', false); 140 } 141 142 /** 143 * Removes loading indicator and enables form elements. 144 */ 145 function enableNewTagFilterForm() { 146 clearTimeout(timeoutid_new_tag_filter); 147 $ms_tag_filter_groups.multiSelect('enable'); 148 $new_tag_filter_table.find('button, [name^="new_tag_filter"]').prop('disabled', false); 149 $tag_filter_table_container.find('button').prop('disabled', false); 150 } 151 152 /** 153 * Successful response handler. 154 * 155 * @param {string} html 156 */ 157 function respNewGroupRight(html) { 158 $ms_group_right_groups.multiSelect('clean'); 159 $new_group_right_table.find('[name="new_group_right[tag]"]').val(''); 160 $new_group_right_table.find('[name="new_group_right[value]"]').val(''); 161 $group_right_table_container.html(html); 162 163 // Trigger event to update tab indicator. 164 document.dispatchEvent(new Event('tab-indicator-update')); 165 } 166 167 /** 168 * Successful response handler. 169 * 170 * @param {string} html 171 */ 172 function respNewTagFilter(html) { 173 $ms_tag_filter_groups.multiSelect('clean'); 174 $new_tag_filter_table.find('[name="new_tag_filter[tag]"]').val(''); 175 $new_tag_filter_table.find('[name="new_tag_filter[value]"]').val(''); 176 $tag_filter_table_container.html(html); 177 178 // Trigger event to update tab indicator. 179 document.dispatchEvent(new Event('tab-indicator-update')); 180 } 181 182 /** 183 * Response handler factory, dries up logic on how error response is recognized and handled. 184 * 185 * @param {callable} handle_data Successful response handler. 186 * 187 * @return {callable} Response handler. 188 */ 189 function respHandler(handle_data) { 190 return function(resp) { 191 clearMessages(); 192 193 if (resp.messages) { 194 addMessage(resp.messages); 195 } 196 197 if (resp.body) { 198 var html = resp.body; 199 200 if (resp.debug) { 201 html += resp.debug; 202 } 203 204 handle_data(html); 205 } 206 } 207 } 208 209 /** 210 * Collects data, sends to controller for processing. On success, tag filter table is updated and form objects 211 * are removed from DOM. On failure error message is displayed. During request, loader is displayed. 212 * 213 * @param {string} action 214 */ 215 function submitNewTagFilter(action) { 216 var url = new Curl('zabbix.php'), 217 data = collectTagFilterFormData(); 218 219 url.setArgument('action', action); 220 221 disableNewTagFilterForm(); 222 223 xhr_new_tag_filter && xhr_new_tag_filter.abort(); 224 xhr_new_tag_filter = $.post(url.getUrl(), data) 225 .always(enableNewTagFilterForm) 226 .done(respHandler(respNewTagFilter)) 227 .fail(function() {}); 228 } 229 230 /** 231 * Collects data, sends to controller for processing. On success, permissions table is updated and form objects 232 * are removed from DOM. On failure error message is displayed. During request, loader is displayed. 233 * 234 * @param {string} action 235 */ 236 function submitNewGroupRight(action) { 237 var url = new Curl('zabbix.php'), 238 data = collectGroupRightFormData(); 239 240 url.setArgument('action', action); 241 242 disableNewGroupRightForm(); 243 244 xhr_new_group_right && xhr_new_group_right.abort(); 245 xhr_new_group_right = $.post(url.getUrl(), data) 246 .always(enableNewGroupRightForm) 247 .done(respHandler(respNewGroupRight)) 248 .fail(function() {}); 249 } 250 251 function removeTagFilterRow($this) { 252 $this 253 .closest('tr') 254 .remove(); 255 256 // Trigger event to update tab indicator. 257 document.dispatchEvent(new Event('tab-indicator-update')); 258 } 259 260 /** 261 * Public API. 262 */ 263 window.usergroups = { 264 submitNewGroupRight: submitNewGroupRight, 265 submitNewTagFilter: submitNewTagFilter, 266 removeTagFilterRow: removeTagFilterRow 267 }; 268 }); 269</script> 270