forked from Bonsaif/new-nconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_service.php
316 lines (249 loc) · 10.5 KB
/
clone_service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
require_once 'include/head.php';
?>
<!-- jQuery part -->
<script type="text/javascript">
$.fn.nconf_ajax_debug = function() {
return this.each(function(){
// Handle ERROR information
// Handle DEBUG information
$(this).filter("#ajax_error").appendTo( $("#jquery_error") );
$("#ajax_error").parent("#jquery_error").show();
// Handle DEBUG information
$(this).filter("#ajax_debug").prependTo( $("#jquery_console") );
$("#jquery_console_parent").show();
});
};
function clone_service(host_id, services, new_service_name){
$("#toBox option").each( function() {
$(this).attr("selected","selected");
});
var destination_host_ids = $("#toBox").val();
$.post("call_file.php?ajax_file=service_clone.php&debug=yes", {
'action': "clone2hosts",
'item': "service",
'source_host_id': host_id,
'destination_host_ids[]': destination_host_ids,
'service_id': services,
'new_service_name': new_service_name,
'username': "<?php echo $_SESSION['userinfos']['username'];?>"
}, function(data){
// this will move the debug information to current page
$(data).nconf_ajax_debug();
var service_id = $(data).filter("#clone_success").html();
$(data).filter("#clone_success").appendTo( $("#clone_feedback") );
$(data).filter("#clone_error").appendTo( $("#clone_error_content") );
if ( $("#clone_error_content > #clone_error").length > 0){
$("#clone_error").show("slow");
}
});
}
$(document).ready(function(){
// hide continue button
$( "#continue" ).hide();
// display progressbar
$( "#progressbar" ).progressbar({ value: 0 });
// define some events for the new service name
$('#services_fromBox, #services_toBox, #services_toBox, span > img').mouseleave(function() {
$('#services_fromBox').change();
});
$('#new_service_name').mouseenter(function() {
$('#services_fromBox').change();
});
// define change event for new service name
$('#services_fromBox').change(function () {
// disable new service name, if more than 1 service is selected
if ( $('#services_toBox option').length > "1" ){
$('#new_service_name').attr("disabled", "disabled");
$('#new_service_name').parent().parent().fadeTo('slow', 0.5, function() {
// Animation complete.
});
}else{
$('#new_service_name').removeAttr("disabled");
$('#new_service_name').parent().parent().fadeTo('slow', 1, function() {
// Animation complete.
});
}
});
// continue button
$('#continue').button({
icons: {
primary: "ui-icon-circle-arrow-e"
}
}).addClass("ui-button-text-icon-primary");
// clone action
$('#clone').button({
icons: {
primary: "ui-icon-circle-plus"
}
}).addClass("ui-button-text-icon-primary")
.click( function() {
var host_id = $("#host_ID").val();
var service_id = $("#services_toBox").val();
var service_count = $("#services_toBox option").length;
var bar_step = 100/(service_count);
var bar = 0;
if ( service_count > "0" && $("#toBox option").length > "0" ){
$('#clone').unbind("click");
$("#clone_error").hide("slow");
$("#feedback").show("slow");
// handle new service name
var new_service_name = "";
if (service_count == "1"){
new_service_name = $("#new_service_name").val()
}
$("#services_toBox option").each( function(i, val) {
// iterate over all services (right select box)
var service_id = $(this).val();
clone_service( host_id, service_id, new_service_name);
bar = bar+bar_step;
if (bar >= "99"){
// reached 100% disable clone button and show continue button
$( "#progressbar > .ui-progressbar-value" ).addClass("ui-corner-right");
$('#clone').fadeTo('slow', 0.5)
$( "#progressbar > .ui-progressbar-value" ).animate({"width": bar+"%"}, "normal", function() {
$( "#continue, #clone_feedback" ).fadeTo('slow', 1);
});
}else{
$( "#progressbar > .ui-progressbar-value" ).animate({"width": bar+"%"}, "normal");
}
});
}else{
$("#clone_error_content").html("Please choose at least one service and one destination host!");
$("#clone_error").show("slow");
}
// prevent click action
return false;
});
});
</script>
<!-- END of jQuery part -->
<?php
//delete cache if not resent from clone
if( !empty($_SESSION["go_back_page"]) AND !preg_match('/^clone/', $_SESSION["go_back_page"]) ){
message ($debug, 'Cleared clone cache' );
unset($_SESSION["cache"]["clone_service"]);
}
//set_page();
// Check chache
if ( isset($_SESSION["cache"]["clone_service"]) ){
$cache = $_SESSION["cache"]["clone_service"];
}elseif( !empty($_GET["service_id"]) ){
$cache["service_id"] = $_GET["service_id"];
}
# Fetch all hosts
$query = 'SELECT fk_id_item,attr_value FROM ConfigValues,ConfigAttrs,ConfigClasses
WHERE id_attr=fk_id_attr
AND naming_attr="yes"
AND id_class=fk_id_class
AND config_class="host"
ORDER BY attr_value';
$hosts = db_handler($query, "array_2fieldsTOassoc", "get all hosts");
$host_id = $_GET["id"];
$item_name = db_templates("naming_attr", $host_id);
$title = 'Clone Service from host '.$item_name;
echo NConf_HTML::page_title("service", $title);
echo '
<br>
<table>
';
echo define_colgroup();
?>
<tr><td class="middle"><br>services to clone
</td>
<td colspan=3>
<div class="select-container">
<?php
echo '<input id="host_ID" type="hidden" name="source_host_id" value="'.$host_id.'">';
?>
<select multiple name="all_services[]" id="services_fromBox">
<?php
$services = db_templates("get_services_from_host_id", $host_id);
foreach ($services as $service_id => $service_name){
echo '<option value="'.$service_id.'">'.$service_name.'</option>';
}
?>
</select>
<select multiple name="destination_service_ids[]" id="services_toBox">
</select>
<script type="text/javascript">
createMovableOptions("services_fromBox","services_toBox",500,145,'Available services','Selected services',"livesearch");
</script>
</div>
</td>
<td valign="top" class="middle attention">*</td>
<td class="desc">You move elements by clicking on the buttons or by double clicking on select box items</td>
</tr>
<tr class="assign_many">
<td class="middle">name of cloned service</td>
<td>
<input id ="new_service_name" name="new_service_name" type=text maxlength=250
value="<?php if (!empty($cache["new_service_name"])) echo $cache["new_service_name"];?>">
</td>
<td colspan="4" class="desc">
set a new service name (only when cloning 1 service)
</td>
</tr>
<tr>
</tr>
<tr><td class="middle"><br>clone service to
</td>
<td colspan=3>
<div class="select-container">
<select multiple name="all_hosts[]" id="fromBox">
<?php
foreach ($hosts as $host_id => $host_name){
echo '<option value="'.$host_id.'">'.$host_name.'</option>';
}
?>
</select>
<select multiple name="destination_host_ids[]" id="toBox">
</select>
<!--</form>-->
<script type="text/javascript">
createMovableOptions("fromBox","toBox",500,145,'Available hosts','Selected hosts',"livesearch");
</script>
</div>
</td>
<td valign="top" class="middle attention">*</td>
<td class="desc">You move elements by clicking on the buttons or by double clicking on select box items</td>
</tr>
</table>
<?php
# Tell the Session, send db query is ok (we are coming from formular)
$_SESSION["submited"] = "yes";
echo '<br>';
echo '<div>';
echo '<button id="clone" class="ui-buttonset">clone services</button>';
/*
echo '<a id="clone" href="#">
clone services
</a>';*/
if (!empty($_SESSION["go_back_page"]) ){
$link = $_SESSION["go_back_page"];
}else{
$link = "index.php";
}
echo '<a id="continue" href="'.$link.'">continue</a>';
echo '<br><br>';
echo '<div id="clone_error" style="display: none;">';
echo NConf_HTML::show_error('Error:', '<span id="clone_error_content"></span>');
echo '</div>';
echo '<div id="feedback" style="display: none;">';
echo '<div style="width: 300px">';
echo '<h2>Progress:</h2>';
echo '<div>
<div id="progressbar"></div>
</div>';
echo '<div style="float: left">0%</div>';
echo '<div style="float: right">100%</div>';
echo '</div>';
echo '<div style="clear: both;"></div>';
echo '<br><h2 class="ui-nconf-header ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">Feedback:</h2>';
echo '<div id="clone_feedback" class="ui-nconf-content ui-widget-content ui-corner-bottom" style="display: none;"></div>';
echo '</div>';
echo '
</div>';
mysqli_close($dbh);
require_once 'include/foot.php';
?>