-
Notifications
You must be signed in to change notification settings - Fork 206
/
device_templates.php
1381 lines (1258 loc) · 49.9 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once('db.inc.php');
require_once('facilities.inc.php');
$subheader=__("Data Center Device Templates");
$timestamp=time();
$salt=md5('unique_salt' . $timestamp);
if((isset($_POST['getslots']) || isset($_POST['getports']) || isset($_POST['getpowerports'])) && isset($_POST['TemplateID'])){
$returndata=array();
if(isset($_POST['getports']) || isset($_POST['getpowerports'])){
$tport=(isset($_POST['getports']))?new TemplatePorts():new TemplatePowerPorts();
$tport->TemplateID=$_POST['TemplateID'];
$returndata=$tport->GetPorts();
}else{
$returndata=Slot::GetAll($_POST['TemplateID']);
}
header('Content-Type: application/json');
echo json_encode($returndata);
exit;
}
if(isset($_GET['cdutemplate']) || isset($_GET['sensortemplate'])){
$t=(isset($_GET['cdutemplate']))?new CDUTemplate():new SensorTemplate();
$t->TemplateID=(isset($_GET['cdutemplate']))?$_GET['cdutemplate']:$_GET['sensortemplate'];
$t->GetTemplate();
header('Content-Type: application/json');
echo json_encode($t);
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(!$person->WriteAccess){
// No soup for you.
header('Location: '.redirect());
exit;
}
$template=new DeviceTemplate();
$manufacturer=new Manufacturer();
if(isset($_POST['deleteme'])){
$template->TemplateID=$_POST['TemplateID'];
if($template->GetTemplateByID()){
// First deal with the case that we are transferring
if($template->TemplateID!=$_POST['transferid'] && $_POST['transferid']!=0){
// We should do this in bulk, this has potential to be a real time sink
foreach(Device::GetDevicesByTemplate($template->TemplateID) as $dev){
$dev->TemplateID=$_POST['transferid'];
$dev->UpdateDevice();
}
}
// Transfers are done, delete this shit
$template->DeleteTemplate();
}
echo '1';
exit;
}
$status='';
if(isset($_FILES['templateFile']) && $_FILES['templateFile']['error']==0 && $_FILES['templateFile']['type']='text/xml'){
$importresult=$template->ImportTemplate($_FILES['templateFile']['tmp_name']);
$status=($importresult["status"]=="")?__("Template File Imported"):$importresult["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();
$deviceList = Device::GetDevicesByTemplate( $template->TemplateID );
}
if(isset($_POST['action'])){
$template->ManufacturerID=$_POST['ManufacturerID'];
$template->Model=transform($_POST['Model']);
$template->Height=$_POST['Height'];
$template->Weight=$_POST['Weight'];
$template->Wattage=(isset($_POST['Wattage']))?$_POST['Wattage']:0;
$template->DeviceType=$_POST['DeviceType'];
$template->PSCount=$_POST['PSCount'];
$template->NumPorts=$_POST['NumPorts'];
$template->ShareToRepo=isset( $_POST['ShareToRepo'] ) ? 1 : 0;
$template->KeepLocal=isset( $_POST['KeepLocal'] ) ? 1 : 0;
$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;
$template->SNMPVersion=$_POST['SNMPVersion'];
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->Notes=isset($_POST["portnotes".$i])?$_POST["portnotes".$i]:"";
$status=($tport->CreatePort())?$status:__("Error updating template ports");
}
$template->DeletePowerPorts();
//update template power connections
for ($i=1; $i<=$template->PSCount;$i++){
$tport=new TemplatePowerPorts();
$tport->TemplateID=$template->TemplateID;
$tport->PortNumber=$i;
$tport->Label=isset($_POST["powerlabel".$i])?$_POST["powerlabel".$i]:"";
$tport->PortNotes=isset($_POST["powerportnotes".$i])?$_POST["powerportnotes".$i]:"";
$status=($tport->CreatePort())?$status:__("Error updating template power connections");
}
return $status;
}
function UpdateCustomValues($template,$status) {
/* Note: if a user changes the value or required status of an attribute that is not "all devices" and does not
enable it on the device_templates screen, it won't update here. it is a weird ui experience, but it
seems like the proper functionality since we aren't saving data for anything that isn't enabled */
$dcaList=DeviceCustomAttribute::GetDeviceCustomAttributeList();
$template->DeleteCustomValues();
if ( isset( $_POST['tdca'] ) && is_array( $_POST['tdca'] ) ) {
foreach($_POST["tdca"] as $dcaid=>$currentdca) {
if((isset($currentdca["enabled"]) && $currentdca["enabled"]==="on")) {
$insertval = '';
$requiredval = 0;
if(isset($currentdca["value"]) && trim($currentdca["value"] != '')) {
$insertval = trim($currentdca["value"]);
}
if(isset($currentdca["required"]) && $currentdca["required"] == "on") {
$requiredval = 1;
}
$status=($template->InsertCustomValue($dcaid, $insertval,$requiredval))?$status:__("Error updating device template custom values");
} elseif(array_key_exists($dcaid, $dcaList) && $dcaList[$dcaid]->AllDevices==1) {
/* since the enabled checkbox for attributes marked as "all devices" is disabled, it doesn't get passed in with the form,
so parse through and if the value or required status are different than the defaults, add a row for them as well. this
helps keep the table clean of a bunch of default values too */
$insertval = $dcaList[$dcaid]->DefaultValue;
$requiredval = $dcaList[$dcaid]->Required;
// don't check if the value is empty - this lets us overwrite a default value from the config page with empty here
if(isset($currentdca["value"])) {
$insertval = trim($currentdca["value"]);
}
if(isset($currentdca["required"]) && $currentdca["required"] == "on") {
$requiredval = 1;
}
if(($insertval != $dcaList[$dcaid]->DefaultValue) || ($requiredval != $dcaList[$dcaid]->Required)) {
$status=($template->InsertCustomValue($dcaid, $insertval, $requiredval))?$status:__("Error updating device template custom values");
}
}
}
}
return $status;
}
function updatesensor($template,$status){
$sensortemplate=new SensorTemplate();
$sensortemplate->TemplateID=$template->TemplateID;
$sensortemplate->ManufacturerID=$template->ManufacturerID;
$sensortemplate->Model=$template->Model;
$sensortemplate->TemperatureOID=$_POST['TemperatureOID'];
$sensortemplate->HumidityOID=$_POST['HumidityOID'];
$sensortemplate->TempMultiplier=$_POST['TempMultiplier'];
$sensortemplate->HumidityMultiplier=$_POST['HumidityMultiplier'];
$sensortemplate->mUnits=$_POST['mUnits'];
$status=($sensortemplate->UpdateTemplate())?$status:__("Error updating cdu attributes");
return $status;
}
function updatecdu($template,$status){
$cdutemplate=new CDUTemplate();
$cdutemplate->TemplateID=$template->TemplateID;
$cdutemplate->ManufacturerID=$template->ManufacturerID;
$cdutemplate->Model=$template->Model;
$cdutemplate->Managed=isset($_POST['Managed'])?1:0;
$cdutemplate->ATS=isset($_POST['ATS'])?1:0;
$cdutemplate->VersionOID=$_POST['VersionOID'];
$cdutemplate->OutletNameOID=$_POST['OutletNameOID'];
$cdutemplate->OutletDescOID=$_POST['OutletDescOID'];
$cdutemplate->OutletCountOID=$_POST['OutletCountOID'];
$cdutemplate->OutletStatusOID=$_POST['OutletStatusOID'];
$cdutemplate->OutletStatusOn=$_POST['OutletStatusOn'];
$cdutemplate->Multiplier=$_POST['Multiplier'];
$cdutemplate->OID1=$_POST['OID1'];
$cdutemplate->OID2=$_POST['OID2'];
$cdutemplate->OID3=$_POST['OID3'];
$cdutemplate->ATSStatusOID=$_POST['ATSStatusOID'];
$cdutemplate->ATSDesiredResult=$_POST['ATSDesiredResult'];
$cdutemplate->ProcessingProfile=$_POST['ProcessingProfile'];
$cdutemplate->Voltage=$_POST["Voltage"];
$cdutemplate->Amperage=$_POST["Amperage"];
$cdutemplate->NumOutlets=$template->PSCount;
$status=($cdutemplate->UpdateTemplate())?$status:__("Error updating cdu attributes");
return $status;
}
switch($_POST['action']){
case 'Create':
if($template->CreateTemplate()){
$oldstatus=$status;
$status=UpdateSlotsPorts($template,$status);
if($oldstatus==$status){
$status=UpdateCustomValues($template,$status);
}
if($oldstatus==$status){
$status=updatecdu($template,$status);
$status=updatesensor($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);
}
if($status==__("Updated")){
$status=UpdateCustomValues($template,$status);
}
if($status==__("Updated")){
$status=updatecdu($template,$status);
$status=updatesensor($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=UpdateCustomValues($template,$status);
}
if($status==__("Updated")){
$status=updatecdu($template,$status);
$status=updatesensor($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=UpdateCustomValues($template,$status);
}
if($status==__("Updated")){
$status=updatecdu($template,$status);
$status=updatesensor($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();
$ManufacturerListByID=$manufacturer->GetManufacturerList(true);
$mtList=MediaTypes::GetMediaTypeList();
$ccList=ColorCoding::GetCodeList();
$dcaList=DeviceCustomAttribute::GetDeviceCustomAttributeList();
$imageselect='<div id="preview"></div><div id="filelist">';
$path=$config->ParameterArray['picturepath'];
$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
$mimeType=mime_content_type($path.DIRECTORY_SEPARATOR.$f);
if(preg_match('/^image/i', $mimeType)){
$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/jquery-te-1.4.0.css" type="text/css">
<link rel="stylesheet" href="css/uploadifive.css" type="text/css">
<!--[if lt IE 9]>
<link rel="stylesheet" href="css/ie.css" type="text/css">
<![endif]-->
<style type="text/css">
#deviceimages .center > div { border-width: 1px; border-style: solid; }
</style>
<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.uploadifive.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/jquery-te-1.4.0.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?v<?php echo filemtime('scripts/common.js');?>"></script>
<script type="text/javascript">
timestamp="<?php echo $timestamp; ?>";
token="<?php echo $salt; ?>";
$(document).ready(function(){
$('#TemplateID').change(function(e){
location.href='device_templates.php?TemplateID='+this.value;
});
function newtab(e){
var poopup=window.open('search.php?key=dev&TemplateID='+$('#TemplateID').val()+'&search','search');
poopup.focus();
}
$('#templatecount').css({'cursor':'pointer','text-decoration':'underline'}).click(newtab);
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);
$('.caption button:not([value="Update"])').remove();
$('button[name="action"][value="Update"]').val('Create').text("<?php echo __("Create");?>");
$('#Model').trigger('change');
// Flag all ports and slots as changed so they will retain their data
$('#deviceform [id^="hidden"] .table > div ~ div').data('change',true);
});
$('#FrontPictureFile,#RearPictureFile').click(function(){
var upload=$('<input>').prop({type: 'file', name: 'dev_file_upload', id: 'dev_file_upload'}).data('dir','images');
var input=this;
var originalvalue=this.value;
$("#imageselection").dialog({
resizable: false,
height:500,
width: 670,
modal: true,
buttons: {
<?php echo ' ',__("Select"),': function() {'; ?>
if($('#imageselection #preview').attr('image')!=""){
$(input).val($('#imageselection #preview').attr('image')).trigger('change');
}
$(this).dialog("destroy");
}
},
close: function(){
// they clicked the x, set the value back if something was uploaded
input.value=originalvalue;
$(this).dialog("destroy");
}
}).data('input',input);
reload();
$("#imageselection").next('div').prepend(upload);
uploadifive();
});
$('#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'});
}
if($('#DeviceType').val()=="CDU"){
$('#Wattage').parent('div').parent('div').addClass('hide');
$('#hiddencdudata').removeClass('hide').show();
buildcdutable();
}else{
$('#Wattage').parent('div').parent('div').removeClass('hide');
$('#hiddencdudata').hide();
}
if($('#DeviceType').val()=="Sensor"){
$('#hiddensensordata').removeClass('hide').show();
buildsensortable();
}else{
$('#hiddensensordata').hide();
}
(function hidedisclaimer(){
if($('#DeviceType').val()=="CDU" || $('#DeviceType').val()=="Sensor"){
$('.cdudisclaimer').removeClass('hide').show();
}else{
$('.cdudisclaimer').hide();
}
})();
}).change();
$( "#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");
}
}
});
});
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));
// Draw the slots on the preview image
drawSlots();
// Open the dialog
Poopup(front);
});
});
buildportstable();
$('.templatemaker input#NumPorts + button').each(function(){
$(this).on('click',function(){
// Fill in the ports table
buildportstable();
var generateportnames=$('<select>').append($('<option>'));
$.get('scripts/ajax_portnames.php').done(function(data){
$.each(data, function(key,spn){
var option=$("<option>",({'value':spn.Pattern})).append(spn.Pattern.replace('(1)','x'));
generateportnames.append(option);
});
generateportnames.append($("<option>",({'value':'custom'})).text("<?php echo __("Custom")?>"));
generateportnames.append($("<option>",({'value':'invert'})).text("<?php echo __("Invert Port Labels")?>"));
});
generateportnames.on('change',function(e){
var inputs=$(e.currentTarget.parentElement.parentElement.parentElement).find('> div > div:nth-child(2) > input');
var portnames=[];
if(e.currentTarget.value=='invert'){
inputs_rev=inputs.get().reverse();
portnames.push(null);
for (i = 0; i < inputs_rev.length; i++) {
portnames.push(inputs_rev[i].value);
}
}else if(e.currentTarget.value=='custom'){
var dialog=$('<div />', {id: 'modal', title: 'Custom port pattern'}).html('<div id="modaltext"></div><br><div id="modalstatus"></div>');
dialog.find('#modalstatus').prepend('<p>Custom pattern: <input></input></p><p><a href="http://opendcim.org/wiki/index.php?title=NetworkConnections#Custom_Port_Name_Generator_Example_Patterns" target=_blank>Pattern Examples</a></p>');
dialog.dialog({
resizable: false,
modal: true,
dialogClass: "no-close",
buttons: {
OK: function(){
// can't use .get because of the async
$.ajax({type:'get',url:'scripts/ajax_portnames.php',async:false,data: {pattern:$('#modalstatus input').val(),count:$('#NumPorts').val()}}).done(function(data){
portnames=data;
applynames(inputs,portnames,e);
});
$(this).dialog("destroy");
},
Cancel: function(){
$(this).dialog("destroy");
}
}
});
}else{
// can't use .get because of the async
$.ajax({type:'get',url:'scripts/ajax_portnames.php',async:false,data: {pattern:e.currentTarget.value,count:$('#NumPorts').val()}}).done(function(data){
portnames=data;
});
}
function applynames(inputs,portnames,e){
// Use the port names we came with above to apply to the screen
if(portnames.length > $('#NumPorts').val()){
for (i = 1; i < portnames.length; i++) {
inputs.trigger('change')[i-1].value=portnames[i];
}
}
e.currentTarget.value='';
}
applynames(inputs,portnames,e);
});
// Add mass edit controls
$('#hiddenports > div.table > div:first-child > div:nth-child(2)').css('position','relative').append(generateportnames.css({'background-color':'transparent','border':'0 none','position':'absolute','width':'auto','top':0,'right':0}));
// Open the dialog
PortsPoopup();
});
});
buildpowerportstable();
$('.templatemaker input#PSCount + button').each(function(){
$(this).on('click',function(){
// Fill in the ports table
buildpowerportstable();
// Open the dialog
PowerPortsPoopup();
});
});
$('#FrontPictureFile,#RearPictureFile').change(function(){
var fp_file=$('#FrontPictureFile');
var fp_img=$('#img_FrontPictureFile');
var rp_file=$('#RearPictureFile');
var rp_img=$('#img_RearPictureFile');
if(fp_file.val() && rp_file.val()){
$('#rightside').css('min-width','260px');
// Resize images
$('#deviceimages img').css('max-width',$('#rightside').innerWidth()/2);
}else if(fp_file.val() || rp_file.val()){
$('#rightside').css('min-width','130px');
// Resize images
$('#deviceimages img').css('max-width',$('#rightside').innerWidth());
}
fp_img.prop('src',"<?php echo $config->ParameterArray['picturepath'];?>"+fp_file.val());
rp_img.prop('src',"<?php echo $config->ParameterArray['picturepath'];?>"+rp_file.val());
if(fp_file.val()){
fp_img.parent('div').show();
}else{
fp_img.parent('div').hide();
}
if(rp_file.val()){
rp_img.parent('div').show();
}else{
rp_img.parent('div').hide();
}
resize();
drawSlots();
}).trigger('change');
$('#TemplateID').combobox();
$('#ManufacturerID').combobox();
// fuckin layout bug
$('span.custom-combobox').width($('span.custom-combobox').width()+2);
$('.templatemaker input[type=checkbox]').css('margin','2px');
$('#FrontPictureFile,#RearPictureFile,#ChassisSlots,#RearChassisSlots,#NumPorts,#PSCount').on('change keyup keydown', function(){ TemplateButtons(); });
$('#delete').on('click',function(){
// setup shit we might reuse
var dlg_content=$('<div>');
var dlg_select=$('<select>').append($('<option>').text('No').val('false')).append($('<option>').text('Yes').val('true'));
var dlg_transfer=$('<div>');
// make the dialog
dlg_content.append($('<p>').append("There is no undo").addClass('warning'));
dlg_content.append($('<label>').html("Would you like to transfer all devices using this template to another template? "));
dlg_content.append(dlg_select);
dlg_content.append(dlg_transfer);
var TemplateID2;
// logic for dealing someone saying yes they want to swap to another template
dlg_select.on('change',function(){
// Clone the existing device template list
TemplateID2=$('#TemplateID').clone().prop('id','TemplateID2').removeAttr('onchange').removeAttr('style');
// Remove the 'New Template' option
TemplateID2.find('option:first-child').remove();
// Remove the currently selected template as a valid option
TemplateID2.find('option[value='+TemplateID2.val()+']').remove();
if(eval(this.value)){
dlg_transfer.append('<br><br>');
dlg_transfer.append($('<label>').prop('for','TemplateID2').html("Transfer all devices using this template to: "));
dlg_transfer.append(TemplateID2);
}else{
dlg_transfer.html('');
}
});
var dialog=$('<div>').attr('title',"Are you sure you want to delete this template?").html(dlg_content);
dialog.dialog({
closeOnEscape: false,
minHeight: 250,
width: 840,
modal: true,
resizable: false,
position: { my: "center", at: "top", of: window },
show: { effect: "blind", duration: 800 },
beforeClose: function(event,ui){
},
buttons: {
Yes: function(){
$.post('',{TemplateID: $('#TemplateID').val(), transferid: ((typeof TemplateID2=='undefined')?0:TemplateID2.val()), deleteme: ''}).done(function(data){
if(data.trim()==1){
dialog.dialog("destroy");
// seems like a good idea to direct them to the new template
// rather than just a new template
$('#TemplateID').val((typeof TemplateID2=='undefined')?0:TemplateID2.val()).trigger('change');
}else{
alert("something is broken");
}
});
},
No: function(){
$(this).dialog("destroy");
}
}
});
});
// Add some logic to the enabled / required checkboxes so users aren't confused
// An attribute cannot be required if it isn't enabled so make sure that
// required is turned off when enabled is
$('input[name*=enabled]').on('click',function(e){
if(!e.currentTarget.checked){
e.currentTarget.nextElementSibling.checked=false;
}
});
// if a user clicks required it has to be enabled for that to be valid
// this will make sure that the enabled box is ticked
$('input[name*=required]').on('click',function(e){
if(e.currentTarget.checked){
e.currentTarget.previousElementSibling.checked=true;
}
});
});/* END of $(document).ready function() */
</script>
</head>
<body>
<?php include( 'header.inc.php' ); ?>
<div class="page">
<?php
include( 'sidebar.inc.php' );
echo '<div class="main">
<div class="templatemaker">
<h3>',$status,'</h3>
<div class="center"><div>
<form id="deviceform" method="POST">
<div class="table">
<div>
<div class="left"><div id="regulartemplateattributes">
<!-- Left side of device template -->
<div class="table">
<div>
<div><label for="TemplateID">',__("Template"),'</label></div>
<div><input type="hidden"><select name="TemplateID" id="TemplateID">
<option value=0>',__("New Template"),'</option>';
foreach($templateList as $templateRow){
$selected=($template->TemplateID==$templateRow->TemplateID)?" selected":"";
print " <option value=\"$templateRow->TemplateID\"$selected>[{$ManufacturerListByID[$templateRow->ManufacturerID]->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,'"> <span class="cdudisclaimer sensordisclaimer hide">',__("0 for a vertical mounting"),'</span></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','CDU','Sensor') 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 Connections"),'</label></div>
<div><input type="text" name="PSCount" id="PSCount" value="',$template->PSCount,'"><button type="button">',__("Edit Ports"),'</button></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="SNMPVersion">',__("SNMP Version"),'</label></div>
<div><select name="SNMPVersion" id="SNMPVersion">';
$snmpv=array("1","2c", "3");
foreach($snmpv as $unit){
if ( $template->SNMPVersion == $unit ) {
$selected = "selected";
} else {
$selected = "";
}
print "\t\t<option value=\"$unit\" $selected>$unit</option>\n";
}
echo '</select>
</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>';
foreach($dcaList as $dca) {
$templatedcaChecked = "";
$templatedcaDisabled = "";
$templatedcaValue = "";
$templatedcaRequired = "";
if($dca->AllDevices) {
$templatedcaChecked=" checked";
$templatedcaDisabled=" disabled";
}
//TODO the ui seems weird if the default value gets set and this row isn't enabled
if($dca->DefaultValue != '') {
$templatedcaValue=$dca->DefaultValue;
}
if($dca->Required == 1) {
$templatedcaRequired = " checked";
}
// this check goes after the all devices check so that the template value overwrites any system-wide default value
if(isset($template->CustomValues) && array_key_exists($dca->AttributeID, $template->CustomValues)){
$templatedcaChecked=" checked";
$templatedcaValue=$template->CustomValues[$dca->AttributeID]["value"];
$templatedcaRequired=($template->CustomValues[$dca->AttributeID]["required"]==1)?" checked":"";
}
echo '<div>
<div>',$dca->Label,'</div>';
echo '<div>';
if($dca->AttributeType=="checkbox") {
$checked="";
if($templatedcaValue=="1" || $templatedcaValue=="on"){
$checked=" checked";
}
echo __("Default:").' <input type="checkbox" name="tdca[',$dca->AttributeID,'][value]" ',$checked,'>';
} else if ($dca->AttributeType=="set") {
echo '<select name="tdca[',$dca->AttributeID,'][value]">';
foreach(explode(',',$dca->DefaultValue) as $dcaValue){
$selected=($templatedcaValue==$dcaValue)?' selected':'';
print "\n\t<option value=\"$dcaValue\"$selected>$dcaValue</option>";
}
echo '</select>';
} else {
$validation="";
if($templatedcaChecked != "" && $dca->AttributeType!="string" && $dca->AttributeType!="checked") {
$validation=' class="validate[custom['.$dca->AttributeType.']]"';
}
echo '<input type="text" name="tdca[',$dca->AttributeID,'][value]"',$validation,' id="tdca[',$dca->AttributeID,'][value]" value="',$templatedcaValue,'">';
echo '<button type="button" name=tdca[',$dca->AttributeID,'][revert]" id="tdca[',$dca->AttributeID,'][revert]" title="'.__("Revert to default value from configuration page").'" data-val="',$dca->DefaultValue,'" data-id="',$dca->AttributeID,'">'.__("Revert").'</button>';
echo '<script>
$("#tdca\\\[',$dca->AttributeID,'\\\]\\\[revert\\\]").click(function(){
var defaultVal = this.getAttribute("data-val");
var id = this.getAttribute("data-id");
var inputid = "tdca\\\["+id+"\\\]\\\[value\\\]";
$("#"+inputid).val(defaultVal);
});
</script>';
}
echo __("Enabled?").'<input type="checkbox" name="tdca[',$dca->AttributeID,'][enabled]" id="tdca[',$dca->AttributeID,'][enabled]" data=',$dca->AttributeID,' title="'.__("Enabled?").'"',$templatedcaChecked,$templatedcaDisabled,'>
'.__("Required?").'<input type="checkbox" name="tdca[',$dca->AttributeID,'][required]" title="'.__("Required?").'"',$templatedcaRequired,'>
<input type="hidden" name="tdca[',$dca->AttributeID,'][attributetype]" id="tdca[',$dca->AttributeID,'][attributetype]" value="',$dca->AttributeType,'">
<script>
$("#tdca\\\[',$dca->AttributeID,'\\\]\\\[enabled\\\]").click(function(){
var id = this.getAttribute("data");
var typeid = "tdca\\\["+id+"\\\]\\\[attributetype\\\]";
var inputid = "tdca\\\["+id+"\\\]\\\[value\\\]";
var type = $("#"+typeid).val();
if(this.checked){
$("#"+inputid).removeClass();
if(type!="checkbox" && type!="string" && type!="set"){
$("#"+inputid).addClass("validate[custom["+type+"]]");
}
} else {
$("#"+inputid).removeClass();
}
});
</script>
</div>
</div>';
}
if ( $template->TemplateID > 0 && isset( $deviceList ) ) {
echo '
<div>
<div> </div>
<div id="templatecount">' . __("Number of Devices Using This Template:") . ' ' . sizeof( $deviceList ) . '</div>
</div>';
}
echo '
<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 id="delete" type="button">',__("Delete 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><!-- end regular template attributes -->
</div><!-- END left side of device template -->
<div id="rightside" class="right"><!-- right side of device template -->
<div id="deviceimages">
<div class="table">
<div>
<div class="center">
<!-- front image goes here -->
<img id="img_FrontPictureFile" src="',$config->ParameterArray['picturepath'],'/',$template->FrontPictureFile,'">
<br><span>',__("Front"),'</span>
</div>
<div class="center">
<!-- rear image goes here -->
<img id="img_RearPictureFile" src="',$config->ParameterArray['picturepath'],'/',$template->RearPictureFile,'">
<br><span>',__("Rear"),'</span>
</div>
</div>
</div>
</div>
<div id="hiddenports">
</div>
<div id="hiddenpowerports">
</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>
<div id="hiddencdudata" class="hide">
<div class="table">
<div>
<div><label for="Managed">',__("Managed"),'</label></div>
<div>
<input type="checkbox" name="Managed" id="Managed">
</div>
</div>
<div>
<div><label for="VersionOID">',__("Firmware Version OID"),'</label></div>
<div><input type="text" name="VersionOID" id="VersionOID" size=40></div>
</div>
<div>
<div><label for="OutletNameOID">',__("Outlet Factory Name")." ".__("OID"),'</label></div>
<div><input type="text" name="OutletNameOID" id="OutletNameOID" size=40></div>
</div>
<div>
<div><label for="OutletDescOID">',__("Outlet User Description")." ".__("OID"),'</label></div>
<div><input type="text" name="OutletDescOID" id="OutletDescOID" size=40></div>
</div>
<div>
<div><label for="OutletCountOID">',__("Outlet Count")." ".__("OID"),'</label></div>
<div><input type="text" name="OutletCountOID" id="OutletCountOID" size=40></div>