Skip to content

Commit

Permalink
UniFi API browser 2.0.4
Browse files Browse the repository at this point in the history
- added PHP version check
- corrected bug when displaying the error page upon start up
- clear active collection group after switching controllers
- added example custom sub menu
  • Loading branch information
malle-pietje committed Nov 26, 2019
1 parent cdf16e3 commit 476f72a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,60 @@ Alternatively you may choose to download the zip file and unzip it in your direc
- after following these steps, you can open the tool in your browser (assuming you installed it in the root folder of your web server as suggested above) by going to this url: `http(s)://<server IP address>/UniFi-API-browser/`


### Extending the dropdown menu

Since version 2.0.0 you can extend the dropdown menu with your own options by adding them to the `config.php` file. Here's an example:
```php
/**
* adding a custom sub menu example
*/
$collections = array_merge($collections, [
[
'label' => 'Custom Menu', // length of this string is limited due to dropdown menu width
'options' => [
[
'type' => 'collection', // either collection or divider
'label' => 'hourly site stats past 24 hours', // string that is dosplayed in the dropdown menu
'method' => 'stat_hourly_site', // the method/function in the API client class that is called
'params' => [(time() - (24 * 60 *60)) * 1000, time() * 1000], // an array containing the parameters that are passed to the method/function
'key' => 'custom_0' // unique key for this menu option, may be required for future versions
],
[
'type' => 'collection',
'label' => 'daily site stats past 31 days',
'method' => 'stat_daily_site',
'params' => [(time() - (31 * 24 * 60 *60)) * 1000, time() * 1000],
'key' => 'custom_1'
],
[
'type' => 'divider', // dividers have no other properties
],
[
'type' => 'collection',
'label' => 'enable the site LEDs',
'method' => 'site_leds', // don't go wild when adding such calls, this example is simply to show the flexibility
'params' => [true],
'key' => 'custom_2'
],
[
'type' => 'collection',
'label' => 'disable the site LEDs',
'method' => 'site_leds', // don't go wild when adding such calls, this example is simply to show the flexibility
'params' => [false],
'key' => 'custom_3'
],
],
],
]);
```

Note: for a `collection` type menu option the `type`, `label`, `method`, `params` and `key` "properties" are required.

This is what the result looks like:

![Custom sub menu](https://user-images.githubusercontent.com/12016131/69611554-4fb4a400-102e-11ea-9175-99618c1e1f98.png "Custom sub menu")


### Updates

If you have installed the tool using the `git clone` command, you can install updates by going into the directory where the tool has been installed, and running the `git pull` command from there.
Expand Down
2 changes: 1 addition & 1 deletion common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* with this package in the file LICENSE.md
*
*/
define('TOOL_VERSION', '2.0.3');
define('TOOL_VERSION', '2.0.4');

/**
* gather some basic information for the About modal
Expand Down
39 changes: 27 additions & 12 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
require_once('common.php');
require_once('collections.php');

/**
* initialize the Twig loader early on in case we need to render the error page
*/
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

/**
* load the configuration file, if readable
* - if not, stop and display an error message
Expand All @@ -49,6 +55,16 @@
exit();
}

/**
* inject Twig global variables for use across the templates
*/
$twig->addGlobal('tool_version', TOOL_VERSION);
$twig->addGlobal('debug', $debug);
$twig->addGlobal('session', $_SESSION);
$twig->addGlobal('navbar_class', $navbar_class);
$twig->addGlobal('navbar_bg_class', $navbar_bg_class);
$twig->addGlobal('about_modal_params', $about_modal_params);

/**
* check whether the required PHP curl module is available
* - if not, stop and display an error message
Expand All @@ -65,20 +81,19 @@
}

/**
* initialize the Twig loader
* check whether the minimum required PHP version (5.6.0) is met
* - if not, stop and display an error message
*/
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
/**
* render the config error page
*/
echo $twig->render('config_error.html.twig', [
'error_message' => 'The current PHP version (' . PHP_VERSION . ') does not meet the minimum required version which is 5.6.0. Please upgrade before proceeding!<br>',
]);

/**
* inject Twig global variables for use across the templates
*/
$twig->addGlobal('tool_version', TOOL_VERSION);
$twig->addGlobal('debug', $debug);
$twig->addGlobal('session', $_SESSION);
$twig->addGlobal('navbar_class', $navbar_class);
$twig->addGlobal('navbar_bg_class', $navbar_bg_class);
$twig->addGlobal('about_modal_params', $about_modal_params);
exit();
}

/**
* load the file containing user accounts, if readable
Expand Down
14 changes: 9 additions & 5 deletions js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ $('.controller_idx').on('click', function(){
$('.controller_idx').removeClass('active').children('i').remove();
$($(this)).addClass('active').append(' <i class="fas fa-check"></i>');


/**
* if not yet hidden, we hide the alert
* in #collection_dropdown menu we also clear any active elements
*/
$('#select_controller_alert_wrapper').hide();
$('#collection_dropdown .dropdown-item').parent().find('.dropdown-item').removeClass('active');

/**
* restore the label for the site dropdown "button"
* if not yet hidden, we hide the alert
*/
$('#navbar_site_dropdown_link').html('Sites');
$('#select_controller_alert_wrapper').hide();

/**
* hide the output card
Expand All @@ -92,6 +91,11 @@ $('.controller_idx').on('click', function(){
*/
$('.alert_wrapper').addClass('d-none');

/**
* restore the label for the site dropdown "button"
*/
$('#navbar_site_dropdown_link').html('Sites');

/**
* we also update the PHP $_SESSION variable with the new theme name using AJAX POST
*/
Expand Down
1 change: 0 additions & 1 deletion templates/collections_view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
{% block content %}
<br>
<div id="fetching_sites_alert_wrapper" class="col-sm-12 d-none alert_wrapper">
<br>
<div id="fetching_sites_alert" class="alert alert-primary" role="alert">
<span id="fetching_sites_alert_span">
Fetching sites from the controller <i class="fas fa-sync fa-spin"></i>
Expand Down

0 comments on commit 476f72a

Please sign in to comment.