Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to delete whole paths when deleting a device #1234

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions classes/DevicePorts.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,5 +641,30 @@ function LooseSearch($indexedbyid=false){
return $this->Search($indexedbyid,true);
}

// Returns the device at the end of the path
function GetEndPathDevice($deviceID, $portNumber){
global $dbh;

$path = DevicePorts::followPathToEndPoint($deviceID,$portNumber);

$dev= new Device;

foreach($path as $p){
$tempP = new DevicePorts;
$tempP->DeviceID = $p->ConnectedDeviceID;
$tempP->PortNumber = $p->ConnectedPort;
$tempP->getPort();
if(in_array($tempP,$path)){
$dev->DeviceID = $p->DeviceID;
$dev->GetDevice();
}
}
if($dev->DeviceID == null){
$dev = null;
}

return $dev;
}

}
?>
194 changes: 179 additions & 15 deletions devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,92 @@ function updatedevice($devid){
echo json_encode($wattage);
exit;
}

//GetPotentialPathsToDelete
if(isset($_POST['getpptd']))
{
function GetPotentialPathsToDelete($DeviceID)
{
// Data type being made [DeviceID(int),portNumber(int),Label(String),Status(enum-int)]
//Status Enum
// Check=0
// Unchecked = 1
// ForceCheck=2
// ForceUnChecked=3

$pathsToRemove = array();
$dev = new Device;
$dev->DeviceID=$DeviceID;
$dev->GetDevice();
$numPorts = $dev->Ports;
$startPortNum = 1;
if($dev->DeviceType == "Chassis"){
$childDevices = $dev->GetDeviceChildren();
foreach($childDevices as $device){
$childPaths = json_decode(GetPotentialPathsToDelete($device->DeviceID));
foreach($childPaths as $cp){
array_push($pathsToRemove,$cp);
}
}
}
else if ($dev->DeviceType == "Patch Panel"){
$startPortNum = $numPorts *-1;
}

if($dev->DeviceType != "Chasis")
{
for($portNumber = $startPortNum; $portNumber <= $numPorts; $portNumber++){
if($dev->DeviceType == "Patch Panel" && $portNumber == 0){$portNumber++;}
$enddev = DevicePorts::GetEndPathDevice($dev->DeviceID,$portNumber);
$paths = DevicePorts::followPathToEndPoint($dev->DeviceID,$portNumber);
$pp = new DevicePorts;//previous path
foreach($paths as $path){
$tp = new DevicePorts;
$tp->DeviceID=$path->ConnectedDeviceID;
$tp->PortNumber=$path->ConnectedPort;
$tp->getPort();
$conDev=new Device;
$conDev->DeviceID=$path->ConnectedDeviceID;
$conDev->GetDevice();
$startDev = new Device;
$startDev->DeviceID=$path->DeviceID;
$startDev->getDevice();

if( $path->ConnectedPort != null && $tp->DeviceID != null && $tp != $pp){
// By default all front connections are checked and uncheckable
// Change to 1 if it is wanted to be unchecked but checkable
$enumval = 0;
//Check if connection is coming from the device
// Make it required
if($dev->DeviceID == $startDev->DeviceID )
{$enumval=2;}
// Is a connection you will never remove by accident (ex a permanant connection)
// This example will never allow you to choose a back link to remove only front connections
// But it will still show rear connections but the checkboxs will be disabled
// Add conditions to not be able to remove a path here
else if($path->PortNumber<0)
{$enumval=3;}

array_push($pathsToRemove,array(
$startDev->DeviceID,
intval($path->PortNumber),
$startDev->Label." | ".$path->Label.($path->PortNumber<0?"(Rear)":"")." -> ".$conDev->Label." | ".$tp->Label.($path->PortNumber<0?"(Rear)":"")."",
$enumval
));

$pp=$path;
}
}
}
}

return json_encode($pathsToRemove);
}
header('Content-Type: application/json');
echo GetPotentialPathsToDelete($_POST['getpptd']);
exit;
}

// END AJAX

// Not really AJAX calls since there's no return, but special actions
Expand Down Expand Up @@ -580,6 +666,22 @@ function updatedevice($devid){
$copyerr=__("This device is a copy of an existing device. Remember to set the new location before saving.");
$childList=array();

// Delete selected paths when deleting a device
// pci is a list of all the selected paths the user wanted to remove
// pic[0] = DeviceID pci[1] = PortNumber
if(isset($_REQUEST['pci'])&& $_REQUEST['pci'] != null)
{
foreach($_REQUEST['pci'] as $port)
{
$path = new DevicePorts();
$path->DeviceID=$port[0];
$path->PortNumber=$port[1];
$path->getPort();
$path->removeConnection();
}
exit;
}

// This page was called from somewhere so let's do stuff.
// If this page wasn't called then present a blank record for device creation.
if(isset($_REQUEST['action'])||isset($_REQUEST['DeviceID'])){
Expand Down Expand Up @@ -1668,26 +1770,85 @@ function parseusage(u){
$('#InstallDate').datepicker("setDate",d);
}
});
// Delete device confirmation dialog

//Delete device confirmation dialog
$('button[value="Delete"]').click(function(e){
var form=$(this).parents('form');
var form=$(this).parents('form');
var btn=$(this);
<?php echo ' dialog.find(\'span + span\').text("',__("This device will be deleted and there is no undo. Are you sure?"),'");'; ?>
dialog.dialog({
resizable: false,
modal: true,
buttons: {
<?php echo ' ',__("Yes"),': function(){'; ?>
$(this).dialog("destroy");
form.append('<input type="hidden" name="'+btn.attr("name")+'" value="'+btn.val()+'">');
form.validationEngine("detach");
form.submit();
var pwc=[];//paths with connection
// Will be up to date if path is added or removed
var answer = $.ajax({
url: "",
method: "POST",
data: {getpptd:$('#DeviceID').val()}
}).done(function (data){
pwc = data;
var pathList=document.getElementById("deleteselectedpaths");
for (i = 0; i < pwc.length; i++) {
pathList.insertAdjacentHTML("beforeend", "<input type=\"checkbox\" id=\""+pwc[i][0]+","+pwc[i][1]+"\""+(pwc[i][3]<2?"checked":"")+">"+pwc[i][2]+"</input><br>");
//add checked
document.getElementById(pwc[i][0]+","+pwc[i][1]).disabled = pwc[i][3]>0?true:false;
}
$('#deleteselectedpaths').removeClass('hide').dialog({
title: "Select paths to delete",
modal: true,
width: 'auto',
buttons: {
'Delete device and selected paths': function(){
var checked = $("#deleteselectedpaths :checkbox:checked");
var pci = [];
var index =0;
$.each(checked,function(index,value){
// Port connection Info
pci[index] = value.id.split(",");
index++;
});

// Create excel sheet
if(pci.length != 0)
{
// Info for excel sheet here because it might be deleted before it can read
var exceldata=[];
var jndex=0;
$.each(pwc,function (indexc, valuec){
$.each(pci,function(indexi,valuei){
if(valuei[0] == valuec[0] && valuei[1] == valuec[1] )
{
exceldata[jndex]=valuec[2];
jndex++;
}
});
});

$('<form>', {"id": "pciform",
"name": "pciform",
"method": "post",
"target": "_blank",
"action": '/export_paths_to_remove.php'
}).append('<input type="hidden" name="pci" value="'+encodeURI(JSON.stringify(exceldata))+'"/>').appendTo($('body'));
$("#pciform").submit();
}

// Deletes the paths
$.post('',{pci: pci});

// Deletes the device
form.append('<input type="hidden" name="'+btn.attr("name")+'" value="'+btn.val()+'">');
form.validationEngine("detach");
form.submit();
$(this).dialog("close");
},
Cancel: function(){
$(this).dialog("close");
}
},
<?php echo ' ',__("No"),': function(){'; ?>
close: function(){
$("#deleteselectedpaths").empty();
$(this).dialog("destroy");
}
}
});

});
});
});

$('#Ports').change(function(){
Expand Down Expand Up @@ -2599,6 +2760,9 @@ function setPreferredLayout() {<?php if(isset($_COOKIE["layout"]) && strtolower(
<p><?php print __("Do you certify that you have completed an audit of this device?"); ?></p>
</div>


<div id="deleteselectedpaths" class="hide"></div>

<div id="pdutest" title="Testing SNMP Communications"></div>

</div><!-- END div.main -->
Expand Down
48 changes: 48 additions & 0 deletions export_paths_to_remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
require_once( "db.inc.php" );
require_once( "facilities.inc.php" );

if(isset($_REQUEST["pci"]) && $_REQUEST["pci"]!="" && $_REQUEST["pci"]!=null){
// Data is a device port so pci[0]=deviceid and pci[1]=portnumber

$data = json_decode(urldecode($_REQUEST['pci']));

// Creates a sheet which containes the labels of the connections/paths you wanted to remove when deleting a device
function generate_spreadsheet($pathLabels){

$sheet = new PHPExcel();

$sheet->getProperties()->setCreator("openDCIM");
$sheet->getProperties()->setLastModifiedBy("openDCIM");

$sheet->setActiveSheetIndex(0);
$sheet->getActiveSheet()->SetCellValue('A1',__("Device paths to remove"));

$sheet->getActiveSheet()->setTitle(__("Paths To Remove"));
$row = 2;

$dev = new Device();
$endDev = new Device();
$endPort = new DevicePorts();

foreach($pathLabels as $p){
$sheet->getActiveSheet()->SetCellValue('A' . $row, $p);
$row++;
}

foreach( range('A','B') as $columnID) {
$sheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}

return $sheet;
}

$writer = new PHPExcel_Writer_Excel2007(generate_spreadsheet($data));

ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header( "Content-Disposition: attachment;filename=\"openDCIM-Paths-To-Remove_".date( "Y-m-d H:i:s" ).".xlsx\"" );

$writer->save("php://output");
}
?>