From 673ad552a105eaf59059533270ce069e31f36849 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sun, 28 Aug 2022 21:45:52 -0500 Subject: [PATCH] Device settings: attempt to open related tab (#14250) * Device settings: attempt to open related tab * remove baseline --- app/Http/Controllers/DeviceController.php | 31 ++++++++++++++++++++--- phpstan-baseline.neon | 5 ---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index 47de6f69fa22..079a3235a1af 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -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); @@ -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, ]; } @@ -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")); + } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f1794b1910df..adc501dd7cac 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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