Skip to content

Commit

Permalink
Adding in new rack drawing functions
Browse files Browse the repository at this point in the history
Still to do is correct the row view so that it will render correctly.  It isn't tagging the racks for front/rear properly yet and in turn causing the images to render incorrectly.  The side view still needs to be fleshed out with the JS to mark the areas on the cabinet as used. Finally, still need to work out a way to build the legend like the old routines.  #526
  • Loading branch information
wilpig committed Aug 25, 2015
1 parent 321ac47 commit 173acf8
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 241 deletions.
33 changes: 33 additions & 0 deletions api/v1/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,39 @@ function authenticate(\Slim\Route $route) {
}
});

//
// URL: /api/v1/device/:deviceid/getpicture
// Method: GET
// Params:
// required: deviceid (passed in URL)
// optional: rear (will return the rear face of the device)
// Returns: HTML representation of a device
//

$app->get( '/device/:deviceid/getpicture', function($deviceid) {
$dev=new Device($deviceid);

$response['error']=true;
$response['errorcode']=404;
$response['message']=__("Unknown error");

if(!$dev->GetDevice(false)){
$response['message']=__("Device not found");
}else{
if($dev->Rights=="None"){
$response['errorcode']=403;
$response['message']=__("Permission denied");
}else{
$response['error']=false;
$response['errorcode']=200;
$response['message']="";
$response['picture']=$dev->GetDevicePicture(isset($_GET['rear']));
}
}

echoResponse(200,$response);
});

$app->get( '/device/:deviceid/getsensorreadings', function($deviceid) {
$dev=new Device();
$dev->DeviceID=intval($deviceid);
Expand Down
22 changes: 16 additions & 6 deletions assets.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2781,16 +2781,22 @@ function GetChildDevicePicture($parentDetails, $rear=false){
$parentDev=$parentDetails->parentDev;
$parentTempl=$parentDetails->parentTempl;

// API path correction
$path="";
if(preg_match('/api\//',str_replace(DIRECTORY_SEPARATOR, '/',getcwd()))){
$path="../../";
}

// We'll only consider checking a rear image on a child if it is sitting on a shelf
if(($parentTempl->Model=='HTRAY' || $parentTempl->Model=='VTRAY') && $rear){
$picturefile="pictures/$templ->RearPictureFile";
}else{
$picturefile="pictures/$templ->FrontPictureFile";
}
if (!file_exists($picturefile)){
if (!file_exists($path.$picturefile)){
$picturefile="pictures/P_ERROR.png";
}
@list($width, $height)=getimagesize($picturefile);
@list($width, $height)=getimagesize($path.$picturefile);
// Make sure there is an image! DOH! If either is 0 then use a text box
$width=intval($width);
$height=intval($height);
Expand All @@ -2812,8 +2818,8 @@ function GetChildDevicePicture($parentDetails, $rear=false){
// We only need these numbers in the event that we have a nested device
// and need to scale the coordinates based off the original image size
$kidsHavingKids=new stdClass();
$kidsHavingKids->Height=$height;
$kidsHavingKids->Width=$width;
$kidsHavingKids->Height=($height)?$height:1;
$kidsHavingKids->Width=($width)?$width:1;

$slot=new Slot();
$slotOK=false;
Expand Down Expand Up @@ -3001,13 +3007,17 @@ function GetDevicePicture($rear=false,$targetWidth=220,$nolinks=false){

if(($templ->FrontPictureFile!="" && !$rear) || ($templ->RearPictureFile!="" && $rear)){
$picturefile="pictures/";
$path="";
if(preg_match('/api\//',str_replace(DIRECTORY_SEPARATOR, '/',getcwd()))){
$path="../../";
}
$picturefile.=($rear)?$templ->RearPictureFile:$templ->FrontPictureFile;
if (!file_exists($picturefile)){
if (!file_exists($path.$picturefile)){
$picturefile="pictures/P_ERROR.png";
}

// Get the true size of the template image
list($pictW, $pictH)=getimagesize($picturefile);
list($pictW, $pictH)=getimagesize($path.$picturefile);

// adjusted height = targetWidth * height:width ratio for 1u * height of device in U
$targetHeight=$targetWidth*1.75/19*$this->Height;
Expand Down
118 changes: 3 additions & 115 deletions cabnavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,127 +219,15 @@ function renderUnassignedTemplateOwnership($noTemplFlag, $noOwnerFlag, $device)
$noOwnerFlag=false;
$noTemplFlag=false;
$noReservationFlag=false;
$backside=false;
$backside=true;

// This function with no argument will build the front cabinet face. Specify
// rear and it will build the back.
function BuildCabinet($rear=false){
// This is fucking horrible, there has to be a better way to accomplish this.
global $cab_color, $cab, $device, $body, $currentHeight, $heighterr,
$devList, $templ, $tempDept, $backside, $deptswithcolor, $tempDept,
$totalWeight, $totalWatts, $totalMoment, $zeroheight,
$noTemplFlag, $noOwnerFlag, $noReservationFlag;

$currentHeight=$cab->CabinetHeight;

$body.="<div class=\"cabinet\">\n\t<table>
<tr><th id=\"cabid\" data-cabinetid=$cab->CabinetID colspan=2 $cab_color>".__("Cabinet")." $cab->Location".($rear?" (".__("Rear").")":"")."</th></tr>
<tr><td class=\"cabpos\">".__("Pos")."</td><td>".__("Device")."</td></tr>\n";

$heighterr="";
while(list($dev_index,$device)=each($devList)){
list($noTemplFlag, $noOwnerFlag, $highlight) =
renderUnassignedTemplateOwnership($noTemplFlag, $noOwnerFlag, $device);
if($device->Height<1 && !$rear && $device->DeviceType!="CDU" && $device->DeviceType!="Sensor"){
if($device->Rights!="None"){
$zeroheight.="\t\t\t<a href=\"devices.php?DeviceID=$device->DeviceID\" data-deviceid=$device->DeviceID>$highlight $device->Label</a>\n";
}else{
// empty html anchor for a line break
$zeroheight.="\t\t\t$highlight $device->Label\n<a></a>";
}
}

// This entire function needs to be reworked.
if($device->DeviceType=='Chassis' && $device->RearChassisSlots>0){
$backside=true;
}

if ((!$device->HalfDepth || !$device->BackSide)&&!$rear || (!$device->HalfDepth || $device->BackSide)&&$rear){
$backside=($device->HalfDepth || $device->BackSide)?true:$backside;
$devTop=$device->Position + $device->Height - 1;

$templ->TemplateID=$device->TemplateID;
$templ->GetTemplateByID();

$tempDept->DeptID=$device->Owner;
$tempDept->GetDeptByID();

// If a dept has been changed from white then it needs to be added to the stylesheet, legend, and device
if(!$device->Reservation && strtoupper($tempDept->DeptColor)!="#FFFFFF"){
// Fill array with deptid and color so we can process the list once for the legend and style information
$deptswithcolor[$device->Owner]["color"]=$tempDept->DeptColor;
$deptswithcolor[$device->Owner]["name"]=$tempDept->Name;
}

//only computes this device if it is its front side
if (!$device->BackSide && !$rear || $device->BackSide && $rear){
$totalWatts+=$device->GetDeviceTotalPower();
$DeviceTotalWeight=$device->GetDeviceTotalWeight();
$totalWeight+=$DeviceTotalWeight;
$totalMoment+=($DeviceTotalWeight*($device->Position+($device->Height/2)));
}

$reserved="";
if($device->Reservation==true){
$reserved=" reserved";
$noReservationFlag=true;
}
if($devTop<$currentHeight && $currentHeight>0){
for($i=$currentHeight;($i>$devTop);$i--){
$errclass=($i>$cab->CabinetHeight)?' error':'';
if($errclass!=''){$heighterr="yup";}
if($i==$currentHeight && $i>1){
$blankHeight=$currentHeight-$devTop;
if($devTop==-1){--$blankHeight;}
$body.="\t\t<tr><td class=\"cabpos freespace$errclass\">$i</td><td class=\"freespace\" rowspan=$blankHeight>&nbsp;</td></tr>\n";
} else {
$fix=($i==1)?'<td></td>':'';
$body.="\t\t<tr><td class=\"cabpos freespace$errclass\">$i</td>$fix</tr>\n";
if($i==1){break;}
}
}
}
for($i=$devTop;$i>=$device->Position;$i--){
$errclass=($i>$cab->CabinetHeight)?' error':'';
if($errclass!=''){$heighterr="yup";}
if($i==$devTop){
// Create the filler for the rack either text or a picture
$picture=(!$device->BackSide && !$rear || $device->BackSide && $rear)?$device->GetDevicePicture():$device->GetDevicePicture("rear");
$devlabel=$device->Label.(((!$device->BackSide && $rear || $device->BackSide && !$rear) && !$device->HalfDepth)?"(".__("Rear").")":"");
$text=($device->Rights!="None")?"<a href=\"devices.php?DeviceID=$device->DeviceID\">$highlight $devlabel</a>":$devlabel;

// Put the device in the rack
$body.="\t\t<tr><td class=\"cabpos$reserved dept$device->Owner$errclass\">$i</td><td class=\"dept$device->Owner$reserved\" rowspan=$device->Height data-deviceid=$device->DeviceID>";
$body.=($picture)?$picture:$text;
$body.="</td></tr>\n";
}else{
$body.="\t\t<tr><td class=\"cabpos$reserved dept$device->Owner$errclass\">$i</td></tr>\n";
}
}
$currentHeight=$device->Position - 1;
}elseif(!$rear){
$backside=true;
}
}

// Fill in to the bottom
for($i=$currentHeight;$i>0;$i--){
if($i==$currentHeight){
$blankHeight=$currentHeight;

$body.="\t\t<tr><td class=\"cabpos freespace\">$i</td><td class=\"freespace\" rowspan=$blankHeight>&nbsp;</td></tr>\n";
}else{
$body.="\t\t<tr><td class=\"cabpos freespace\">$i</td></tr>\n";
}
}
$body.="\t</table>\n</div>\n";
reset($devList);
} //END OF BuildCabinet

// Generate rack view
BuildCabinet();
$body.=BuildCabinet($cab->CabinetID);
// Generate rear rack view if needed
($backside)?BuildCabinet('rear'):'';
$body.=($backside)?BuildCabinet($cab->CabinetID,'rear'):'';

if($heighterr!=''){
$legend.='<div class="legenditem"><span style="background-color:'
Expand Down
7 changes: 5 additions & 2 deletions css/inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@
margin-right: 20px;
}

.cabinet td + td {vertical-align: middle;padding: 0.25em 0.5em;width: 220px; }
.cabinet #servercontainer, .cabinet #servercontainer-rear { background-image: url("../images/racku-background.png"); position: relative; padding: 0px; margin: 0px;}
.genericdevice {display: flex;justify-content: center; align-items: center; height: 100%; border: 2px black solid;}

.cabinet td + td {vertical-align: middle;width: 220px; }
.cabinet td.cabpos {text-align: center; vertical-align: middle;padding: 0.25em 0.5em;width: 10%;}
.cabinet th{font-size: 1.5em;padding: 0.25em;text-align: center;}
#zerou a{display: block;}
Expand Down Expand Up @@ -503,7 +506,7 @@

/* PICTURES */
.disabled {pointer-events: none;cursor: default;}
.cabnavigator div.picture {position:relative; left:0px; top:0px; margin: -0.25em -0.5em -0.4em; z-index: 5;}
.cabnavigator div.picture {position:relative; left:0px; top:0px; z-index: 5;}
.picture div {position:absolute; z-index: 10; padding: 0 !important;}
.picture .label {
z-index: 11;
Expand Down
Binary file added images/racku-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions misc.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,4 +883,112 @@ function download_file_from_string($string, $downloadfilename) {
echo $string;
}

/*
* In an attempt to keep html generation out of the primary class definitions
* this function is being put here to make a quick convenient method of drawing
* racks. This will NOT put the devices in the rack.
*
* Example usage: echo BuildCabinet(123);
*
* @param int $cabid
* @param string $face (front,rear,side)
* @return html table
*
*/

if(!function_exists("BuildCabinet")){
function BuildCabinet($cabid,$face="front"){
$cab=new Cabinet($cabid);
$cab->GetCabinet();
$order=($cab->U1Position=="Top")?false:true;
$dev=new Device();
$dev->Cabinet=$cab->CabinetID;
$dev->ParentDevice=0;
$bounds=array(
'max'=>array('position'=>0,'height'=>0),
'min'=>array('position'=>0,'height'=>0),
);

// Read in all the devices and make sure they fit the cabinet. If not expand it
foreach($dev->Search() as $device){
if($device->Position==0){
continue;
}
$pos=($order)?$device->Position:$device->Position-$device->Height;

if($device->Position>$bounds['max']['position']){
$bounds['max']['position']=$device->Position;
$bounds['max']['height']=$device->Height;
}
if($pos<$bounds['min']['position']){
$bounds['min']['position']=$pos;
$bounds['min']['height']=1;
}
}
if($order){
$top=max($cab->CabinetHeight,$bounds['max']['position']+$bounds['max']['height']-1);
$bottom=min(0,$bounds['min']['position']);
}else{
// Reverse order
$top=min(1,$bounds['min']['position']-$bounds['min']['height']);
$bottom=max($cab->CabinetHeight,$bounds['max']['position']);
}

// Build cabinet HTML
$cab->Location=($face=="rear")?"$cab->Location (".__("Rear").")":$cab->Location;

// helper function to print the rows of the cabinet table
if(!function_exists("printrow")){
function printrow($i,$top,$bottom,$order,$face,&$htmlcab){
if($order){
$x=($i<=0)?$i-1:$i;
}else{
$x=($i>=0)?$i+1:$i;
}
if($i==$top){
if($face=="rear"){
$rs="-rear";
}elseif($face=="side"){
$rs="-side";
}else{
$rs="";
}
$rowspan=abs($top)+abs($bottom);
$height=(((abs($top)+abs($bottom))*ceil(220*(1.75/19))))."px";
$htmlcab.="\t<tr id=\"pos$x\"><td>$x</td><td rowspan=$rowspan><div id=\"servercontainer$rs\" style=\"width: 220px; height: $height\" data-face=\"$face\"></div></td></tr>\n";
}else{
$htmlcab.="\t<tr id=\"pos$x\"><td>$x</td></tr>\n";
}
}
}

$htmlcab="<table class=\"cabinet\" id=\"cabinet$cab->CabinetID\">
<tr><th colspan=2>$cab->Location</th></tr>
<tr><td>Pos</td><td>Device</td></tr>\n";

// loop here for the height
// numbered high to low, top to bottom
if($order){
for($i=$top;$i>$bottom;$i--){
printrow($i,$top,$bottom,$order,$face,$htmlcab);
}
}else{ // numbered low to high, top to bottom
for($i=$top;$bottom>$i;$i++){
printrow($i,$top,$bottom,$order,$face,$htmlcab);
}
}

$htmlcab.="</table>\n";

// Wrap it in a nice div
$htmlcab='<div class="cabinet">'.$htmlcab.'</div>';

// debug information
// print "Cabinet: $cab->CabinetID Top: $top Bottom: $bottom<br>\n";

return $htmlcab;
}
}


?>
Loading

0 comments on commit 173acf8

Please sign in to comment.