Skip to content

Commit

Permalink
Device settings: attempt to open related tab (#14250)
Browse files Browse the repository at this point in the history
* Device settings: attempt to open related tab

* remove baseline
  • Loading branch information
murrant committed Aug 29, 2022
1 parent 323f0ea commit 673ad55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
31 changes: 27 additions & 4 deletions app/Http/Controllers/DeviceController.php
Expand Up @@ -90,7 +90,7 @@ public function index(Request $request, $device, $current_tab = 'overview', $var
$data = $tabs[$current_tab]->data($device);

// Device Link Menu, select the primary link
$device_links = $this->deviceLinkMenu($device);
$device_links = $this->deviceLinkMenu($device, $current_tab);
$primary_device_link_name = Config::get('html.device.primary_link', 'edit');
if (! isset($device_links[$primary_device_link_name])) {
$primary_device_link_name = array_key_first($device_links);
Expand Down Expand Up @@ -149,15 +149,33 @@ private function buildDeviceGraphArrays($device)
return $graphs;
}

private function deviceLinkMenu(Device $device)
private function deviceLinkMenu(Device $device, string $current_tab): array
{
$device_links = [];

if (Gate::allows('update', $device)) {
$suffix = 'edit';
$title = __('Edit');

// check if metric has more specific edit page
if (preg_match('#health/metric=(\w+)#', \Request::path(), $matches)) {
if ($this->editTabExists($matches[1])) {
$current_tab = $matches[1];
} elseif ($this->editTabExists($matches[1] . 's')) {
$current_tab = $matches[1] . 's';
}
}

// check if edit page exists
if ($this->editTabExists($current_tab)) {
$suffix .= "/section=$current_tab";
$title .= ' ' . ucfirst($current_tab);
}

$device_links['edit'] = [
'icon' => 'fa-gear',
'url' => route('device', [$device->device_id, 'edit']),
'title' => __('Edit'),
'url' => route('device', [$device->device_id, $suffix]),
'title' => $title,
'external' => false,
];
}
Expand Down Expand Up @@ -211,4 +229,9 @@ private function deviceLinkMenu(Device $device)

return $device_links;
}

private function editTabExists(string $tab): bool
{
return is_file(base_path("includes/html/pages/device/edit/$tab.inc.php"));
}
}
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -7115,11 +7115,6 @@ parameters:
count: 1
path: app/Http/Controllers/DeviceController.php

-
message: "#^Method App\\\\Http\\\\Controllers\\\\DeviceController\\:\\:deviceLinkMenu\\(\\) has no return type specified\\.$#"
count: 1
path: app/Http/Controllers/DeviceController.php

-
message: "#^Method App\\\\Http\\\\Controllers\\\\DeviceController\\:\\:index\\(\\) has no return type specified\\.$#"
count: 1
Expand Down

0 comments on commit 673ad55

Please sign in to comment.