Skip to content

Commit

Permalink
Fix map down color
Browse files Browse the repository at this point in the history
Use the user configured down settings on custom maps for down linked maps
A little clean up too
  • Loading branch information
murrant committed Oct 11, 2024
1 parent b4f7425 commit 1d6417e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
60 changes: 23 additions & 37 deletions app/Http/Controllers/Maps/CustomMapDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,9 @@ public function get(Request $request, CustomMap $map): JsonResponse
}
}

/** @var CustomMapNode $node */
foreach ($map->nodes as $node) {
$nodeid = $node->custom_map_node_id;
if ($node->linked_custom_map_id > 0) {
$nodes_down = CustomMapNode::where('custom_map_id', $node->linked_custom_map_id)->whereRelation('device', 'status', 0)->get();
if (count($nodes_down) > 0) {
$node->colour_bg = 'darkred';
}
}
$nodes[$nodeid] = [
'custom_map_node_id' => $node->custom_map_node_id,
'device_id' => $node->device_id,
Expand All @@ -166,29 +161,22 @@ public function get(Request $request, CustomMap $map): JsonResponse
'x_pos' => $node->x_pos,
'y_pos' => $node->y_pos,
];

// set status for linked map
if ($node->linkedMapIsDown()) {
$this->setNodeDownStyle($nodes[$nodeid], $request);
}

// set up linked device and status
if ($node->device) {
$nodes[$nodeid]['device_name'] = $node->device->hostname . '(' . $node->device->sysName . ')';
$nodes[$nodeid]['device_image'] = $node->device->icon;
$nodes[$nodeid]['device_info'] = Url::deviceLink($node->device, null, [], 0, 0, 0, 0);

if ($node->device->disabled) {
$device_style = $this->nodeDisabledStyle();
$this->setNodeDisabledStyle($nodes[$nodeid]);
} elseif (! $node->device->status) {
$device_style = $this->nodeDownStyle();
// Change the text colour as long as we have not been requested by the editor
if ($request->headers->get('referer') && ! str_ends_with(parse_url($request->headers->get('referer'), PHP_URL_PATH), '/edit')) {
$nodes[$nodeid]['text_colour'] = 'darkred';
}
} else {
$device_style = $this->nodeUpStyle();
}

if ($device_style['background']) {
$nodes[$nodeid]['colour_bg_view'] = $device_style['background'];
}

if ($device_style['border']) {
$nodes[$nodeid]['colour_bdr_view'] = $device_style['border'];
$this->setNodeDownStyle($nodes[$nodeid], $request);
}
}
}
Expand Down Expand Up @@ -347,27 +335,25 @@ private function speedWidth(int $speed): float
return (strlen((string) $speed) - 5) / 2.0;
}

protected function nodeDisabledStyle(): array
protected function setNodeDisabledStyle(array &$node_data_array): void
{
return [
'border' => Config::get('network_map_legend.di.border'),
'background' => Config::get('network_map_legend.di.node'),
];
$node_data_array['colour_bg_view'] = Config::get('network_map_legend.di.border');
$node_data_array['colour_bdr_view'] = Config::get('network_map_legend.di.node');
}

protected function nodeDownStyle(): array
protected function setNodeDownStyle(array &$node_data_array, Request $request): void
{
return [
'border' => Config::get('network_map_legend.dn.border'),
'background' => Config::get('network_map_legend.dn.node'),
];
$node_data_array['colour_bg_view'] = Config::get('network_map_legend.dn.node');
$node_data_array['colour_bdr_view'] = Config::get('network_map_legend.dn.border');
// Change the text colour as long as we have not been requested by the editor
if ($request->headers->get('referer') && ! str_ends_with(parse_url($request->headers->get('referer'), PHP_URL_PATH), '/edit')) {
$node_data_array['text_colour'] = 'darkred';
}
}

protected function nodeUpStyle(): array
protected function setNodeUpStyle(array &$node_data_array, CustomMapNode $node): void
{
return [
'border' => null,
'background' => null,
];
$node_data_array['colour_bg_view'] = $node->colour_bg;
$node_data_array['colour_bdr_view'] = $node->colour_bdr;
}
}
7 changes: 7 additions & 0 deletions app/Models/CustomMapNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class CustomMapNode extends BaseModel
use HasFactory;
protected $primaryKey = 'custom_map_node_id';

public function linkedMapIsDown(): bool
{
return $this->linked_custom_map_id &&
CustomMapNode::where('custom_map_id', $this->linked_custom_map_id)
->whereRelation('device', fn ($q) => $q->isDown())->exists();
}

public function scopeHasAccess($query, User $user)
{
if ($user->hasGlobalRead()) {
Expand Down

0 comments on commit 1d6417e

Please sign in to comment.