diff --git a/app/Plugins/ExamplePlugin/DeviceOverview.php b/app/Plugins/ExamplePlugin/DeviceOverview.php index 28c3f11179..92d8f2dd6d 100644 --- a/app/Plugins/ExamplePlugin/DeviceOverview.php +++ b/app/Plugins/ExamplePlugin/DeviceOverview.php @@ -29,4 +29,31 @@ use App\Plugins\Hooks\DeviceOverviewHook; class DeviceOverview extends DeviceOverviewHook { + // point to the view for your plugin's settings + // this is the default name so you can create the blade file as in this plugin + // by ommitting the variable, or point to another one + +// public string $view = 'resources.views.device-overview'; + + public function authorize(\App\Models\User $user, \App\Models\Device $device): bool + { + // In this example, we check if the user has a custom role/permission and if it is member of any device groups +// return $user->can('view-extra-port-info') && $device->has('groups'); + + return true; + } + + // override the data function to add additional data to be accessed in the view + // title is a required attribute and will be shown above your returned html from your blade file + // inside the blade, all variables will be named based on the key in the returned array + public function data(\App\Models\Device $device): array + { + // here we pass a title string, url to notes, and the device to the blade view for display + + return [ + 'title' => 'Example Plugin: Device Notes', + 'device' => $device, + 'url' => url('device/' . $device->device_id . '/notes'), + ]; + } } diff --git a/app/Plugins/ExamplePlugin/Menu.php b/app/Plugins/ExamplePlugin/Menu.php index b45ab75be4..7a2101f7ad 100644 --- a/app/Plugins/ExamplePlugin/Menu.php +++ b/app/Plugins/ExamplePlugin/Menu.php @@ -4,6 +4,33 @@ namespace App\Plugins\ExamplePlugin; use App\Plugins\Hooks\MenuEntryHook; +// this will create a menu entry in the plugin menu +// it should generally just be a class Menu extends MenuEntryHook { + // point to the view for your plugin's settings + // this is the default name so you can create the blade file as in this plugin + // by ommitting the variable, or point to another one + +// public string $view = 'resources.views.menu'; + + // this will determine if the menu entry should be shown to the user + public function authorize(\App\Models\User $user, array $settings = []): bool + { + // menu entry shown if users has the global-read role and there is a setting that has > one entries in it +// return $user->can('global-read') && isset($settings['some_data']) && count($settings['some_data']) > 0; + + return true; // allow every logged in user + } + + // override the data function to add additional data to be accessed in the view + // inside the blade, all variables will be named based on the key in the returned array + public function data(array $settings = []): array + { + // inject settings and count how many we have so we can display it in the menu + + return [ + 'count' => count($settings), + ]; + } } diff --git a/app/Plugins/ExamplePlugin/Page.php b/app/Plugins/ExamplePlugin/Page.php index c356e145d3..57b06fb956 100644 --- a/app/Plugins/ExamplePlugin/Page.php +++ b/app/Plugins/ExamplePlugin/Page.php @@ -27,6 +27,40 @@ namespace App\Plugins\ExamplePlugin; use App\Plugins\Hooks\PageHook; +// this page will be shown when the user clicks on the plugin from the plugins menu. +// This allows you to output a full screen of whatever you want to the user class Page extends PageHook { + // point to the view for your plugin's settings + // this is the default name so you can create the blade file as in this plugin + // by ommitting the variable, or point to another one + +// public string $view = 'resources.views.page'; + + // The authorize method will determine if the user has access to this page. + // if you want all users to be able to access this page simple return true + public function authorize(\App\Models\User $user): bool + { + // you can check user's roles like this: +// return $user->can('admin'); + + // or use whatever you like +// return \Carbon\Carbon::now()->dayOfWeek == Carbon::THURSDAY; // only allowed access on Thursdays! + + return true; // allow every logged in user to access + } + + // override the data function to add additional data to be accessed in the view + // default just passes the stored data through + // inside the blade, all variables will be named based on the key in the returned array + public function data(): array + { + // run any calculations here + $username = auth()->user()->username; + + return [ + 'something' => 'this is a variable and can be accessed with {{ $something }}', + 'hello' => 'Hello: ' . $username, + ]; + } } diff --git a/app/Plugins/ExamplePlugin/PortTab.php b/app/Plugins/ExamplePlugin/PortTab.php index ed47d71b4d..aa91728f60 100644 --- a/app/Plugins/ExamplePlugin/PortTab.php +++ b/app/Plugins/ExamplePlugin/PortTab.php @@ -4,6 +4,28 @@ namespace App\Plugins\ExamplePlugin; use App\Plugins\Hooks\PortTabHook; +// this will insert a tab into every port view class PortTab extends PortTabHook { + // point to the view for your plugin's port plugin + // this is the default name so you can create the blade file as in this plugin + // by ommitting the variable, or point to another one + +// public string $view = 'resources.views.port-tab'; + + // override the data function to add additional data to be accessed in the view + // title is a required attribute and will be shown above your returned html from your blade file + // inside the blade, all variables will be named based on the key in the returned array + public function data(\App\Models\Port $port): array + { + // run any calculations here + $total_delta = $port->ifOutOctets_delta + $port->ifInOctets_delta; // nonsense calculation :) + + return [ + 'title' => 'Example Plugin', + 'port' => $port, + 'something' => 'this is a variable and can be accessed with {{ $something }}', + 'total' => $total_delta, + ]; + } } diff --git a/app/Plugins/ExamplePlugin/Settings.php b/app/Plugins/ExamplePlugin/Settings.php index 0ad995e75a..04b6ffbfe8 100644 --- a/app/Plugins/ExamplePlugin/Settings.php +++ b/app/Plugins/ExamplePlugin/Settings.php @@ -27,6 +27,29 @@ namespace App\Plugins\ExamplePlugin; use App\Plugins\Hooks\SettingsHook; +// In the plugins admin page, there will be a settings button if you implement this hook +// To save settings in your settings page, you should have a form that returns all variables +// you want to save in the database. class Settings extends SettingsHook { + // point to the view for your plugin's settings + // this is the default name so you can create the blade file as in this plugin + // by ommitting the variable, or point to another one + +// public string $view = 'resources.views.settings'; + + // override the data function to add additional data to be accessed in the view + // default just passes the stored data through + // inside the blade, all variables will be named based on the key in the returned array + public function data(array $settings = []): array + { + // run any calculations here + $total = array_sum([1, 2, 3, 4]); + + return [ + 'settings' => $settings, // this is an array of all the settings stored in the database + 'something' => 'this is a variable and can be accessed with {{ $something }}', + 'total' => $total, + ]; + } } diff --git a/app/Plugins/ExamplePlugin/resources/views/device-overview.blade.php b/app/Plugins/ExamplePlugin/resources/views/device-overview.blade.php index af279c6344..977bec1d4e 100644 --- a/app/Plugins/ExamplePlugin/resources/views/device-overview.blade.php +++ b/app/Plugins/ExamplePlugin/resources/views/device-overview.blade.php @@ -2,11 +2,13 @@