function GetChildDevicePicture($parentDetails, $rear = false) { /* * The following section will make a few assumptions * - All dimensions will be given back as a percentage of the whole for scalability * -- Labels will be the exception to that, we're just going to assign them values * - Child devices will only have one face, front * -- This makes the pictures on the templates easier to manage * --- Children of an HTRAY or VTRAY will be treated as any other device with a front * and a rear image. This makes this just stupidly complicated but has to be done * -- Child devices defined with rear slots will have the rear slots ignored * --- This logic needs to be applied to the functions that figure power usage and weight * so we don't end up with phantom sources * - Child devices shouldn't need to conform to the 1.75:19 ratio we use for devices * directly in a cabinet they will target the slot that they are inside */ $resp = ""; $templ = new DeviceTemplate(); $templ->TemplateID = $this->TemplateID; $templ->GetTemplateByID(); $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($path . $picturefile)) { $picturefile = "pictures/P_ERROR.png"; } @(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); $noimage = false; if ($width == 0 || $height == 0) { $noimage = true; if ($parentTempl->Model == 'HTRAY') { $height = $parentDetails->targetWidth; $width = $parentDetails->targetHeight; } elseif ($parentTempl->Model == 'VTRAY') { $width = $parentDetails->targetWidth; $height = $parentDetails->targetHeight; } } // In the event of read error this will rotate a horizontal text label $hor_blade = $width == "" || $height == "" ? true : $width > $height; // 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 ? $height : 1; $kidsHavingKids->Width = $width ? $width : 1; $slot = new Slot(); $slotOK = false; //get slot from DB $slot->TemplateID = $parentDev->TemplateID; $slot->Position = $this->Position; $slot->BackSide = $this->BackSide; if ($parentTempl->Model == 'HTRAY' || $parentTempl->Model == 'VTRAY' || $slot->GetSlot()) { // If we're dealing with a shelf mimic what GetSlot() would have done for our fake slot if ($parentTempl->Model == 'HTRAY' || $parentTempl->Model == 'VTRAY') { $imageratio = $hor_blade || !$hor_blade && $parentTempl->Model == 'HTRAY' ? $width / $height : $height / $width; // If we don't have an image this will make the text box fit correctly, hopefully if ($noimage) { $imageratio = $parentTempl->Model == 'HTRAY' ? $height / $width : $width / $height; } $slot->W = $parentTempl->Model == 'HTRAY' ? $parentDetails->targetWidth / $parentDev->ChassisSlots : $parentDetails->targetWidth; $slot->H = $parentTempl->Model == 'HTRAY' ? $parentDetails->targetHeight : $parentDetails->targetHeight / $parentDev->ChassisSlots; $slot->X = $parentTempl->Model == 'HTRAY' ? $rear ? ($parentDev->ChassisSlots - $this->Position - $this->Height + 1) * $slot->W : ($slot->Position - 1) * $slot->W : 0; $slot->Y = $parentTempl->Model == 'HTRAY' ? 0 : $parentDetails->targetHeight - $parentDetails->targetHeight / $parentDev->ChassisSlots * ($this->Position + $this->Height - 1); // Enlarge the slot if needed $slot->H = $parentTempl->Model == 'HTRAY' ? $parentDetails->targetHeight : $parentDetails->targetHeight / $parentDev->ChassisSlots * $this->Height; $slot->W = $parentTempl->Model == 'HTRAY' ? $parentDetails->targetWidth / $parentDev->ChassisSlots * $this->Height : $slot->H * $imageratio; // To center the devices in the slot we first needed to know the width figured just above $slot->X = $parentTempl->Model == 'VTRAY' ? ($parentDetails->targetWidth - $slot->W) / 2 : $slot->X; // This covers the event that an image scaled properly will be too wide for the slot. // Recalculate all the things! Shelves are stupid. if ($parentTempl->Model == 'VTRAY' && $slot->W > $parentDetails->targetWidth) { $originalH = $slot->H; $slot->W = $parentDetails->targetWidth; $slot->H = $slot->W / $imageratio; $slot->X = 0; $slot->Y = $originalH - $slot->H; } if ($parentTempl->Model == 'HTRAY' && $slot->W > $slot->H * $imageratio) { $originalW = $slot->W; $originalX = $slot->X; $slot->W = $slot->H * $imageratio; $slot->X = $rear ? $originalX + ($originalW - $slot->W) : $slot->X; } elseif ($parentTempl->Model == 'HTRAY' && $slot->H > $slot->W * $this->Height / $imageratio && !$noimage) { $originalH = $slot->H; $slot->H = $hor_blade ? $slot->W * $imageratio : $slot->W / $imageratio; $slot->Y = $originalH - $slot->H; } // Reset the zoome on the parent to 1 just for trays $parentDetails->zoomX = 1; $parentDetails->zoomY = 1; } // Check for slot orientation before we possibly modify it via height $hor_slot = $slot->W > $slot->H; // We dealt with the slot sizing above for trays this will bypass the next bit if ($parentTempl->Model == 'HTRAY' || $parentTempl->Model == 'VTRAY') { $slotOK = true; $this->Height = 0; } // This will prevent the freak occurance of a child device with a 0 height if ($this->Height >= 1) { // If height==1 then just accept the defined slot as is if ($this->Height > 1) { //get last slot $lslot = new Slot(); $lslot->TemplateID = $slot->TemplateID; $lslot->Position = $slot->Position + $this->Height - 1; // If the height extends past the defined slots then just get the last slot if ($lslot->Position > ($slot->BackSide ? $parentDev->RearChassisSlots : $parentDev->ChassisSlots)) { $lslot->Position = $slot->BackSide ? $parentDev->RearChassisSlots : $parentDev->ChassisSlots; } $lslot->BackSide = $slot->BackSide; if ($lslot->GetSlot()) { //calculate total size $xmin = min($slot->X, $lslot->X); $ymin = min($slot->Y, $lslot->Y); $xmax = max($slot->X + $slot->W, $lslot->X + $lslot->W); $ymax = max($slot->Y + $slot->H, $lslot->Y + $lslot->H); //put new size in $slot $slot->X = $xmin; $slot->Y = $ymin; $slot->W = $xmax - $xmin; $slot->H = $ymax - $ymin; } else { // Last slot isn't defined so just error out break; } } $slotOK = true; } } if ($slotOK) { // Determine if the element needs to be rotated or not // This only evaluates if we have a horizontal image in a vertical slot $rotar = !$hor_slot && $hor_blade ? "rotar_d" : ""; // Scale the slot to fit the forced aspect ratio $zoomX = $parentDetails->zoomX; $zoomY = $parentDetails->zoomY; $slot->X = $slot->X * $zoomX; $slot->Y = $slot->Y * $zoomY; $slot->W = $slot->W * $zoomX; $slot->H = $slot->H * $zoomY; if ($rotar) { $left = $slot->X - abs($slot->W - $slot->H) / 2; $top = $slot->Y + abs($slot->W - $slot->H) / 2; $height = $slot->W; $width = $slot->H; } else { $left = $slot->X; $top = $slot->Y; $height = $slot->H; $width = $slot->W; } $left = intval(round($left)); $top = intval(round($top)); $height = intval(round($height)); $width = intval(round($width)); // If they have rights to the device then make the picture clickable $clickable = $this->Rights != "None" ? "\t\t\t<a href=\"devices.php?DeviceID={$this->DeviceID}\">\n" : ""; $clickableend = $this->Rights != "None" ? "\t\t\t</a>\n" : ""; // Add in flags for missing ownership // Device pictures are set on the template so always assume template has been set $flags = $this->Owner == 0 ? '(O) ' : ''; $flags = $this->TemplateID == 0 ? $flags . '(T) ' : $flags; $flags = $flags != '' ? '<span class="hlight">' . $flags . '</span>' : ''; $label = ""; $resp .= "\t\t<div class=\"dept{$this->Owner} {$rotar}\" style=\"left: " . number_format(round($left / $parentDetails->targetWidth * 100, 2), 2, '.', '') . "%; top: " . number_format(round($top / $parentDetails->targetHeight * 100, 2), 2, '.', '') . "%; width: " . number_format(round($width / $parentDetails->targetWidth * 100, 2), 2, '.', '') . "%; height:" . number_format(round($height / $parentDetails->targetHeight * 100, 2), 2, '.', '') . "%;\">\n{$clickable}"; // if(($templ->FrontPictureFile!="" && !$rear) || ($templ->RearPictureFile!="" && $rear)){ if ($picturefile != 'pictures/') { // IMAGE // this rotate should only happen for a horizontal slot with a vertical image $rotateimage = $hor_slot && !$hor_blade ? " class=\"rotar_d rlt\" style=\"height: " . number_format(round($width / $height * 100, 2), 2, '.', '') . "%; left: 100%; width: " . number_format(round($height / $width * 100, 2), 2, '.', '') . "%; top: 0; position: absolute;\"" : ""; $resp .= "\t\t\t\t<img data-deviceid={$this->DeviceID} src=\"{$picturefile}\"{$rotateimage} alt=\"{$this->Label}\">\n"; // LABEL FOR IMAGE if ($hor_slot || $rotar && !$hor_slot) { $label = "\t\t\t<div class=\"label\" style=\"line-height:" . $height . "px; height:" . $height . "px;" . ($height * 0.8 < 13 ? " font-size: " . intval($height * 0.8) . "px;" : "") . "\">"; } else { // This is a vertical slot with a vertical picture so we have to rotate the label $label = "\t\t\t<div class=\"rotar_d rlt label\" style=\"top: calc(" . $height . "px * 0.05); left: " . $width . "px; width: calc(" . $height . "px * 0.9); line-height:" . $width . "px; height:" . $width . "px;" . ($width * 0.8 < 13 ? " font-size: " . intval($width * 0.8) . "px; " : "") . "\">"; } $label .= "<div>{$flags}{$this->Label}" . ($rear ? " (" . __("Rear") . ")" : "") . "</div></div>\n"; } else { //LABEL for child device without image - Always show $resp .= "\t\t\t\t<div class=\"label noimage\" data-deviceid={$this->DeviceID} style='height: " . $height . "px; line-height:" . $height . "px; " . ($height * 0.8 < 13 ? " font-size: " . intval($height * 0.8) . "px;" : "") . "'>"; $resp .= "<div>{$flags}{$this->Label}" . ($rear ? " (" . __("Rear") . ")" : "") . "</div></div>\n"; } $resp .= $clickableend . $label; // If the label on a nested chassis device proves to be a pita remove the label // above and uncomment the following if // if($this->ChassisSlots<4){$resp.=$label;} if ($this->ChassisSlots > 0) { $kidsHavingKids->targetWidth = $width; $kidsHavingKids->targetHeight = $height; $kidsHavingKids->zoomX = $width / $kidsHavingKids->Width; $kidsHavingKids->zoomY = $height / $kidsHavingKids->Height; $kidsHavingKids->parentDev = $this; $kidsHavingKids->parentTempl = $templ; //multichassis $childList = $this->GetDeviceChildren(); foreach ($childList as $tmpDev) { if (!$tmpDev->BackSide && !$rear || $tmpDev->BackSide && $rear) { $resp .= $tmpDev->GetChildDevicePicture($kidsHavingKids, $rear); } } } $resp .= "\t\t</div>\n"; } return $resp; }
// Method: POST // Params: // Required: templateid, slutnum // Optional: everything else // Returns: true/false on update operation // $app->post('/devicetemplate/:templateid/slot/:slotnum', function ($templateid, $slotnum) use($app, $person) { $s = new Slot(); $s->TemplateID = $templateid; $s->PortNumber = $slotnum; if (!$person->WriteAccess) { $response['error'] = true; $response['errorcode'] = 403; $response['message'] = __("Unauthorized"); } else { if (!$s->GetSlot()) { $response['error'] = true; $response['errorcode'] = 404; $response['message'] = __("Template slot not found with id: ") . " {$templateid}:{$slotnum}"; } else { foreach ($app->request->post() as $prop => $val) { $s->{$prop} = $val; } // Just to make sure $s->TemplateID = $templateid; $s->PortNumber = $slotnum; if (!$s->UpdateSlot()) { $response['error'] = true; $response['errorcode'] = 404; $response['message'] = __("Template slot update failed"); } else {
function UpdateSlot() { $this->MakeSafe(); $oldslot = new Slot(); $oldslot->TemplateID = $this->TemplateID; $oldslot->Position = $this->Position; $oldslot->BackSide = $this->BackSide; $oldslot->GetSlot(); $sql = "UPDATE fac_Slots SET X={$this->X}, Y={$this->Y}, W={$this->W}, H={$this->H} \n\t\t\tWHERE TemplateID={$this->TemplateID} AND Position={$this->Position} AND \n\t\t\tBackSide={$this->BackSide};"; if (!$this->query($sql)) { return false; } class_exists('LogActions') ? LogActions::LogThis($this, $oldslot) : ''; return true; }