forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_templates.php
511 lines (466 loc) · 17.4 KB
/
device_templates.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
require_once('db.inc.php');
require_once('facilities.inc.php');
if((isset($_POST['getslots']) || isset($_POST['getports'])) && isset($_POST['templateid'])){
$returndata=array();
if(isset($_POST['getports'])){
$tport=new TemplatePorts();
$tport->TemplateID=$_POST['templateid'];
$returndata=$tport->GetPorts();
}else{
$returndata=Slot::GetAll($_POST['templateid']);
}
header('Content-Type: application/json');
echo json_encode($returndata);
exit;
}
// Get list of color codes
if(isset($_GET['cc'])){
header('Content-Type: application/json');
echo json_encode(ColorCoding::GetCodeList());
exit;
}
// Get list of media types
if(isset($_GET['mt'])){
header('Content-Type: application/json');
echo json_encode(MediaTypes::GetMediaTypeList());
exit;
}
if(!$user->WriteAccess){
// No soup for you.
header('Location: '.redirect());
exit;
}
$template=new DeviceTemplate();
$manufacturer=new Manufacturer();
$status='';
if(isset($_FILES['templateFile']) && $_FILES['templateFile']['error']==0 && $_FILES['templateFile']['type']='text/xml'){
$result=$template->ImportTemplate($_FILES['templateFile']['tmp_name']);
$status=($result["status"]=="")?__("Template File Imported"):$result["status"].'<a id="import_err" style="margin-left: 1em;" title="'.__("View errors").'" href="#"><img src="images/info.png"></a>';
}
if(isset($_REQUEST['templateid']) && $_REQUEST['templateid'] >0){
//get template
$template->TemplateID=$_REQUEST['templateid'];
$template->GetTemplateByID();
}
if(isset($_POST['action'])){
$template->ManufacturerID=$_POST['manufacturerid'];
$template->Model=transform($_POST['model']);
$template->Height=$_POST['height'];
$template->Weight=$_POST['weight'];
$template->Wattage=$_POST['wattage'];
$template->DeviceType=$_POST['devicetype'];
$template->PSCount=$_POST['pscount'];
$template->NumPorts=$_POST['numports'];
$template->Notes=trim($_POST['notes']);
$template->Notes=($template->Notes=="<br>")?"":$template->Notes;
$template->FrontPictureFile=$_POST['FrontPictureFile'];
$template->RearPictureFile=$_POST['RearPictureFile'];
$template->ChassisSlots=($template->DeviceType=="Chassis")?$_POST['ChassisSlots']:0;
$template->RearChassisSlots=($template->DeviceType=="Chassis")?$_POST['RearChassisSlots']:0;
function UpdateSlotsPorts($template,$status){
//Update slots
$template->DeleteSlots();
for ($i=1; $i<=$template->ChassisSlots;$i++){
$slot=new Slot();
$slot->TemplateID=$template->TemplateID;
$slot->Position=$i;
$slot->BackSide=False;
$slot->X=isset($_POST["XF".$i])?$_POST["XF".$i]:0;
$slot->Y=isset($_POST["YF".$i])?$_POST["YF".$i]:0;
$slot->W=isset($_POST["WF".$i])?$_POST["WF".$i]:0;
$slot->H=isset($_POST["HF".$i])?$_POST["HF".$i]:0;
$status=($slot->CreateSlot())?$status:__('Error updating front slots');
}
for ($i=1; $i<=$template->RearChassisSlots;$i++){
$slot=new Slot();
$slot->TemplateID=$template->TemplateID;
$slot->Position=$i;
$slot->BackSide=True;
$slot->X=isset($_POST["XR".$i])?$_POST["XR".$i]:0;
$slot->Y=isset($_POST["YR".$i])?$_POST["YR".$i]:0;
$slot->W=isset($_POST["WR".$i])?$_POST["WR".$i]:0;
$slot->H=isset($_POST["HR".$i])?$_POST["HR".$i]:0;
$status=($slot->CreateSlot())?$status:__('Error updating rear slots');
}
//update template ports
$template->DeletePorts();
for ($i=1; $i<=$template->NumPorts;$i++){
$tport=new TemplatePorts();
$tport->TemplateID=$template->TemplateID;
$tport->PortNumber=$i;
$tport->Label=isset($_POST["label".$i])?$_POST["label".$i]:"";
$tport->MediaID=(isset($_POST["mt".$i]) && $_POST["mt".$i]>0)?$_POST["mt".$i]:0;
$tport->ColorID=(isset($_POST["cc".$i]) && $_POST["cc".$i]>0)?$_POST["cc".$i]:0;
$tport->PortNotes=isset($_POST["portnotes".$i])?$_POST["portnotes".$i]:"";
$status=($tport->CreatePort())?$status:__('Error updating template ports');
}
return $status;
}
switch($_POST['action']){
case 'Create':
if($template->CreateTemplate()){
$status=UpdateSlotsPorts($template,$status);
}else{
$status=__('An error has occured, template not created');
}
break;
case 'Update':
$status=($template->UpdateTemplate())?__('Updated'):__('Error updating template');
if ($status==__('Updated')){
$status=UpdateSlotsPorts($template,$status);
}
break;
case 'Device':
// someone will inevitibly try to update the template values and just click
// update devices so make sure the template will update before trying to
// update the device values to match the template
$status=($template->UpdateTemplate())?__('Updated'):__('Error updating template');
if ($status==__('Updated')){
$status=UpdateSlotsPorts($template,$status);
}
if($status==__('Updated')){
$status=($template->UpdateDevices())?__('Updated'):__('Error updating devices');
}
break;
case 'Export':
//update template before export
$status=($template->UpdateTemplate())?__('Updated'):__('Error');
if ($status==__('Updated')){
$status=UpdateSlotsPorts($template,$status);
}
if($status==__('Updated')){
$status=($template->ExportTemplate())?__('Exported'):__('Error');
}
break;
default:
// do nothing if the value isn't one we expect
}
}
$templateList=$template->GetTemplateList();
$ManufacturerList=$manufacturer->GetManufacturerList();
$mtList=MediaTypes::GetMediaTypeList();
$ccList=ColorCoding::GetCodeList();
$imageselect='<div id="preview"></div><div id="filelist">';
$path='./pictures';
$dir=scandir($path);
foreach($dir as $i => $f){
if(is_file($path.DIRECTORY_SEPARATOR.$f)){
// Suppress the getimagesize because it will error out on non-image files
@$imageinfo=getimagesize($path.DIRECTORY_SEPARATOR.$f);
if(preg_match('/^image/i', $imageinfo['mime'])){
$imageselect.="<span>$f</span>\n";
}
}
}
$imageselect.="</div>";
?>
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>openDCIM Device Class Templates</title>
<link rel="stylesheet" href="css/inventory.php" type="text/css">
<link rel="stylesheet" href="css/jquery-ui.css" type="text/css">
<link rel="stylesheet" href="css/imgareaselect-default.css" type="text/css">
<link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css">
<link rel="stylesheet" href="css/jHtmlArea.css" type="text/css">
<!--[if lt IE 9]>
<link rel="stylesheet" href="css/ie.css" type="text/css">
<![endif]-->
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery-ui.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="scripts/jHtmlArea-0.8.min.js"></script>
<script type="text/javascript" src="scripts/jquery.textext.js"></script>
<script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
<script type="text/javascript" src="scripts/common.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var oModel=$('#model').val();
var chgmsg='<?php echo __('This value must be different than'); ?>'+' '+oModel;
$('#deviceform').validationEngine();
$('#model').change(function(){
if($('#model').val()==oModel){
setTimeout(function(){
$('#model').validationEngine('showPrompt',chgmsg,'','',true);
},500);
}
});
$('#clone').click(function(){
$('#templateid').val(0);
$('button[name="action"]').val('Create').text('<?php echo __('Create');?>');
$('#model').trigger('change');
$('#device, #clone').remove();
});
$('#FrontPictureFile,#RearPictureFile').click(function(){
var input=this;
$("#imageselection").dialog({
resizable: false,
height:500,
width: 600,
modal: true,
buttons: {
<?php echo ' ',__("Select"),': function() {'; ?>
if($('#imageselection #preview').attr('image')!=""){
$(input).val($('#imageselection #preview').attr('image')).trigger('change');
}
$(this).dialog("close");
}
}
});
$("#imageselection span").each(function(){
var preview=$('#imageselection #preview');
$(this).click(function(){
preview.css({'border-width': '5px', 'width': '380px', 'height': '380px'});
preview.html('<img src="pictures/'+$(this).text()+'" alt="preview">').attr('image',$(this).text());
preview.children('img').load(function(){
var topmargin=0;
var leftmargin=0;
if($(this).height()<$(this).width()){
$(this).width(preview.innerHeight());
$(this).css({'max-width': preview.innerWidth()+'px'});
topmargin=Math.floor((preview.innerHeight()-$(this).height())/2);
}else{
$(this).height(preview.innerHeight());
$(this).css({'max-height': preview.innerWidth()+'px'});
leftmargin=Math.floor((preview.innerWidth()-$(this).width())/2);
}
$(this).css({'margin-top': topmargin+'px', 'margin-left': leftmargin+'px'});
});
$("#imageselection span").each(function(){
$(this).removeAttr('style');
});
$(this).css('border','1px dotted black')
});
if($(input).val()==$(this).text()){
$(this).click();
}
});
});
$('#devicetype').change(function(){
if($('#devicetype').val()=="Chassis"){
$('#DivChassisSlots').css({'display': 'table-row'});
$('#DivRearChassisSlots').css({'display': 'table-row'});
}else{
$('#DivChassisSlots').css({'display': 'none'});
$('#DivRearChassisSlots').css({'display': 'none'});
}
});
$( "#importButton" ).click(function() {
$("#dlg_importfile").dialog({
resizable: false,
width: 400,
height: 200,
modal: true,
buttons: {
<?php echo __("Import");?>: function() {
// Llamamos al Formulario
$('#frmImport').submit();
},
<?php echo __("Cancel");?>: function() {
$("#dlg_importfile").dialog("close");
}
}
});
});
$( "#import_err" ).click(function() {
$("#dlg_import_err").dialog({
resizable: false,
width: 500,
height: 400,
modal: true,
buttons: {
<?php echo __("Close");?>: function() {
$("#dlg_import_err").dialog("close");
}
}
});
});
});/* END of $(document).ready function() */
$(document).ready(function(){
FetchSlots();
TemplateButtons();
$('.templatemaker input[id*=Chassis] + button').each(function(){
var front=($(this).prev('input').attr('id')=='ChassisSlots')?true:false;
InsertCoordsTable(front,$(this));
$(this).on('click',function(){
FetchSlots();
// Fill in the coords table
InsertCoordsTable(front,$(this));
// Open the dialog
Poopup(front);
});
});
buildportstable();
$('.templatemaker input#numports + button').each(function(){
$(this).on('click',function(){
// Fill in the ports table
buildportstable();
// Open the dialog
PortsPoopup();
});
});
$('#FrontPictureFile,#RearPictureFile,#ChassisSlots,#RearChassisSlots,#numports').on('change keyup keydown', function(){ TemplateButtons(); });
});
</script>
</head>
<body>
<div id="header"></div>
<div class="page">
<?php
include( 'sidebar.inc.php' );
echo '<div class="main">
<div class="templatemaker">
<h2>',$config->ParameterArray["OrgName"],'</h2>
<h3>',__("Data Center Device Templates"),'</h3>
<h3>',$status,'</h3>
<div class="center"><div>
<form id="deviceform" action="',$_SERVER["PHP_SELF"],'" method="POST">
<div class="table">
<div>
<div><label for="templateid">',__("Template"),'</label></div>
<div><input type="hidden"><select name="templateid" id="templateid" onChange="form.submit()">
<option value=0>',__("New Template"),'</option>';
foreach($templateList as $templateRow){
$manufacturer->ManufacturerID=$templateRow->ManufacturerID;
$manufacturer->GetManufacturerByID();
$selected=($template->TemplateID==$templateRow->TemplateID)?" selected":"";
print " <option value=\"$templateRow->TemplateID\"$selected>[$manufacturer->Name] $templateRow->Model</option>\n";
}
echo ' </select></div>
</div>
<div>
<div><label for="manufacturerid">',__("Manufacturer"),'</label></div>
<div><select name="manufacturerid" id="manufacturerid">';
foreach($ManufacturerList as $ManufacturerRow){
if($template->ManufacturerID==$ManufacturerRow->ManufacturerID){$selected=" selected";}else{$selected="";}
print " <option value=\"$ManufacturerRow->ManufacturerID\"$selected>$ManufacturerRow->Name</option>\n";
}
echo ' </select>
</div>
</div>
<div>
<div><label for="model">',__("Model"),'</label></div>
<div><input type="text" name="model" id="model" class="validate[required]" value="',$template->Model,'"></div>
</div>
<div>
<div><label for="height">',__("Height"),'</label></div>
<div><input type="text" name="height" id="height" value="',$template->Height,'"></div>
</div>
<div>
<div><label for="weight">',__("Weight"),'</label></div>
<div><input type="text" name="weight" id="weight" value="',$template->Weight,'"></div>
</div>
<div>
<div><label for="wattage">',__("Wattage"),'</label></div>
<div><input type="text" name="wattage" id="wattage" value="',$template->Wattage,'"></div>
</div>
<div>
<div><label for="devicetype">',__("Device Type"),'</label></div>
<div><select name="devicetype" id="devicetype">';
foreach(array('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure', 'Firewall', 'Tape Library', 'External Storage', 'PC', 'KVM', 'Laptop', 'NTU') as $DevType){
if($DevType==$template->DeviceType){$selected=" selected";}else{$selected="";}
print " <option value=\"$DevType\"$selected>$DevType</option>\n";
}
echo ' </select>
</div>
</div>
<div>
<div><label for="pscount">',__("No. Power Supplies"),'</label></div>
<div><input type="text" name="pscount" id="pscount" value="',$template->PSCount,'"></div>
</div>
<div>
<div><label for="numports">',__("No. Ports"),'</label></div>
<div><input type="text" name="numports" id="numports" value="',$template->NumPorts,'"><button type="button">',__("Edit Ports"),'</button></div>
</div>
<div>
<div><label for="FrontPictureFile">',__("Front Picture File"),'</label></div>
<div><input type="text" name="FrontPictureFile" id="FrontPictureFile" value="',$template->FrontPictureFile,'"></div>
</div>
<div>
<div><label for="RearPictureFile">',__("Rear Picture File"),'</label></div>
<div><input type="text" name="RearPictureFile" id="RearPictureFile" value="',$template->RearPictureFile,'"></div>
</div>
<div id="DivChassisSlots" style="display: ',(($template->DeviceType=="Chassis")?'table-row':'none'),';">
<div><label for="ChassisSlots">',__("Chassis Slots"),'</label></div>
<div><input type="text" name="ChassisSlots" id="ChassisSlots" value="',$template->ChassisSlots,'"><button type="button">',__("Edit Coordinates"),'</button></div>
</div>
<div id="DivRearChassisSlots" style="display: ',(($template->DeviceType=="Chassis")?'table-row':'none'),';">
<div><label for="RearChassisSlots">',__("Rear Chassis Slots"),'</label></div>
<div><input type="text" name="RearChassisSlots" id="RearChassisSlots" value="',$template->RearChassisSlots,'"><button type="button">',__("Edit Coordinates"),'</button></div>
</div>
<div>
<div><label for="notes">',__('Notes'),'</label></div>
<div><textarea name="notes" id="notes" cols="40" rows="8">',$template->Notes,'</textarea></div>
</div>
<div class="caption">';
if($template->TemplateID >0){
echo ' <button type="submit" name="action" value="Update">',__("Update Template"),'</button><button type="button" id="clone">',__("Clone Template"),'</button><button type="submit" name="action" value="Device" id="device">',__("Update Devices"),'</button><button type="submit" name="action" value="Export">',__("Export"),'</button>';
}else{
echo ' <button type="submit" name="action" value="Create">',__("Create"),'</button>';
}
echo '<button type="button" name="importButton" id="importButton" value="Import">',__("Import"),'</button>';
?>
</div>
</div><!-- END div.table -->
<div id="hiddenports">
</div>
<div id="hiddencoords">
<div class="table front">
<div>
<div>
<div id="previewimage">
</div>
</div>
<div id="coordstable">
</div>
</div>
</div>
<div class="table rear">
<div>
<div>
<div id="previewimage">
</div>
</div>
<div id="coordstable">
</div>
</div>
</div>
</div>
</form>
</div></div><!-- END div.center -->
</div> <!-- END div.templatemaker-->
<?php
echo '<a href="index.php">[ ',__("Return to Main Menu"),' ]</a>';
echo '<div id="imageselection" title="',__("Image file selector"),'">
',$imageselect,'
</div>';
?>
</div><!-- END div.main -->
</div><!-- END div.page -->
<!-- dialog: importFile -->
<div id="dlg_importfile" style="display:none;" title="<?php echo __("Import Template From File");?>">
<br>
<form enctype="multipart/form-data" name="frmImport" id="frmImport" method="POST">
<input type="file" size="60" id="templateFile" name="templateFile" />
</form>
</div>
<!-- end dialog: importFile -->
<!-- dialog: import_err -->
<div id="dlg_import_err" style="display:none;" title="<?php echo __("Import log");?>">
<?php
if (isset($result["log"])){
print '<ul style="list-style-type:disc; padding: 5px;">';
foreach($result["log"] as $logline){
print "<li style=\"padding: 5px;\">$logline</li>";
}
print "</ul>";
}
?>
</div>
<!-- end dialog: importFile -->
</body>
</html>