Skip to content
11 changes: 11 additions & 0 deletions classes/ProjectMembership.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,16 @@ public static function RedactUser( $UserID ) {

return;
}

/**
* Remove all direct project associations for a specific Device
* Used before assigning a new project to the device.
*/
public static function RemoveDeviceMemberships( $DeviceID ) {
global $dbh;

$st = $dbh->prepare("DELETE FROM fac_ProjectMembership WHERE MemberType='Device' AND MemberID=:id");
$st->execute(array(":id"=>$DeviceID));
}
}
?>
83 changes: 63 additions & 20 deletions devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ function updatedevice($devid){
$pwrCords=null;
$chassis="";
$copy = false;
$copyerr=__("This device is a copy of an existing device. Remember to set the new location before saving.");
$copyerr=__("This device is a copy of an existing device. Remember to set the new location before saving.");
$CurrentProjectID = 0; // Default: no project selected for new devices
$childList=array();

// This page was called from somewhere so let's do stuff.
Expand Down Expand Up @@ -729,6 +730,9 @@ function updatedevice($devid){

// Put the device rights back just in case we had someone try to inject them
$dev->Rights=$devrights;

// Capture selected project from POST (if any)
$selectedProjectID=(isset($_POST['ProjectID']))?intval($_POST['ProjectID']):0;
// Stupid Cabinet vs CabinetID
$dev->Cabinet=$_POST['CabinetID'];
// Checkboxes don't work quite like normal inputs
Expand Down Expand Up @@ -769,6 +773,11 @@ function updatedevice($devid){
}else{
$dev->UpdateDevice();
}
// Update Project association (remove existing, then add if selected)
ProjectMembership::RemoveDeviceMemberships($dev->DeviceID);
if($selectedProjectID>0){
ProjectMembership::addMember($selectedProjectID,$dev->DeviceID,'Device');
}
break;
case 'Delete':
$dev->DeleteDevice();
Expand Down Expand Up @@ -822,6 +831,15 @@ function updatedevice($devid){
$dev->CreateDevice();
$dev->SetTags($tagarray);

// Update Project association (remove existing, then add if selected)
ProjectMembership::RemoveDeviceMemberships($dev->DeviceID);
if(isset($_POST['ProjectID'])){
$selectedProjectID=intval($_POST['ProjectID']);
if($selectedProjectID>0){
ProjectMembership::addMember($selectedProjectID,$dev->DeviceID,'Device');
}
}

// We've, hopefully, successfully created a new device. Force them to the new device page.
header('Location: '.redirect("devices.php?DeviceID=$dev->DeviceID"));
exit;
Expand Down Expand Up @@ -858,6 +876,18 @@ function updatedevice($devid){
$taginsert="\t\ttags: {items: ".json_encode($tags)."},\n";
}

// Get existing project association for this device (direct membership only)
$CurrentProjectID = 0;
if ( $dev->DeviceID > 0 ) {
$st = $dbh->prepare("SELECT ProjectID FROM fac_ProjectMembership WHERE MemberType='Device' AND MemberID=:id ORDER BY ProjectID ASC");
$st->execute(array(":id"=>$dev->DeviceID));
if($row = $st->fetch(PDO::FETCH_NUM)){
$CurrentProjectID = intval($row[0]);
}
}

// Build project options is now handled after this block to also fill on new devices

// Since a device exists we're gonna need some additional info, but only if it's not a copy
if(!$copy){
// clearing errors for now
Expand Down Expand Up @@ -906,15 +936,23 @@ function updatedevice($devid){
}
$cab->CabinetID=$dev->Cabinet;
$cab->GetCabinet();
}else{
/*
* Everything below here will get processed when no DeviceID is present
* aka adding a new device
*/

// sets install date to today when a new device is being created
$dev->InstallDate=date("Y-m-d");
}
}else{
/*
* Everything below here will get processed when no DeviceID is present
* aka adding a new device
*/

// sets install date to today when a new device is being created
$dev->InstallDate=date("Y-m-d");
}

// Build project options for the Project select (always available)
$projectOptions = '<option value="0">'.__("None").'</option>';
$projList = Projects::getProjectList();
foreach($projList as $proj){
$selected = ($proj->ProjectID == $CurrentProjectID) ? ' selected' : '';
$projectOptions .= "\t\t\t\t<option value=\"{$proj->ProjectID}\"{$selected}>{$proj->ProjectName}</option>\n";
}

// We don't want someone accidentally adding a chassis device inside of a chassis slot.
if($dev->ParentDevice>0){
Expand Down Expand Up @@ -1889,6 +1927,10 @@ function setPreferredLayout() {<?php if(isset($_COOKIE["layout"]) && strtolower(
<div><input type="text" name="AssetTag" id="AssetTag" size="20" value="'.$dev->AssetTag.'">
<button class="hide" type="button" onclick="getScan(\'AssetTag\')">',__("Scan Barcode"),'</button></div>
</div>
<div>
<div><label for="projectid">'.__("Project").'</label></div>
<div><select name="ProjectID" id="projectid">'.$projectOptions.'</select></div>
</div>
<div>
<div><label for="PrimaryIP">'.__("Primary IP / Host Name").'</label></div>
<div><input type="text" name="PrimaryIP" id="PrimaryIP" size="20" value="'.$dev->PrimaryIP.'">
Expand Down Expand Up @@ -2885,16 +2927,17 @@ function linkifyolog(){
});
});

// Make the cabinet and template selections smart comboboxes if user can write
<?php
if ($write) {
?>
$('#CabinetID').combobox();
$('#TemplateID').combobox();
$('select[name=ParentDevice]').combobox();
<?php
}
?>
// Make the cabinet and template selections smart comboboxes if user can write
<?php
if ($write) {
?>
$('#projectid').combobox();
$('#CabinetID').combobox();
$('#TemplateID').combobox();
$('select[name=ParentDevice]').combobox();
<?php
}
?>

// Hide this for now
$('#devicetype-limiter').parent('div').hide();
Expand Down