Fixes #1766 - support 3rd party dashboard widgets

This commit is contained in:
Andy Miller
2019-10-24 21:39:17 -06:00
parent 1fa9888b16
commit 858d27103a
6 changed files with 100 additions and 76 deletions

View File

@@ -3,6 +3,7 @@
1. [](#improved)
* Improved delete button UI [#1769](https://github.com/getgrav/grav-plugin-admin/issues/1769)
* Ability to configure display of 3rd party dashboard widgets [#1766](https://github.com/getgrav/grav-plugin-admin/issues/1766)
1. [](#bugfix)
* Fixed administrator user creation when `Flex Users` is enabled
* Fixed minor button alignment in FF [#1760](https://github.com/getgrav/grav-plugin-admin/issues/1760)

View File

@@ -376,11 +376,27 @@ class AdminPlugin extends Plugin
*/
public function onAdminDashboard()
{
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = ['template' => 'dashboard-maintenance'];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = ['template' => 'dashboard-statistics'];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = ['template' => 'dashboard-notifications'];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = ['template' => 'dashboard-feed'];
$this->grav['twig']->plugins_hooked_dashboard_widgets_main[] = ['template' => 'dashboard-pages'];
$lang = $this->grav['language'];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = [
'name' => $lang->translate('PLUGIN_ADMIN.MAINTENANCE'),
'template' => 'dashboard-maintenance',
];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = [
'name' => $lang->translate('PLUGIN_ADMIN.STATISTICS'),
'template' => 'dashboard-statistics',
];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = [
'name' => $lang->translate('PLUGIN_ADMIN.NOTIFICATIONS'),
'template' => 'dashboard-notifications',
];
$this->grav['twig']->plugins_hooked_dashboard_widgets_top[] = [
'name' => $lang->translate('PLUGIN_ADMIN.NEWS_FEED'),
'template' => 'dashboard-feed',
];
$this->grav['twig']->plugins_hooked_dashboard_widgets_main[] = [
'name' => $lang->translate('PLUGIN_ADMIN.LATEST_PAGE_UPDATES'),
'template' => 'dashboard-pages',
];
}
@@ -393,12 +409,13 @@ class AdminPlugin extends Plugin
*/
public function onAdminTools(Event $event)
{
$lang = $this->grav['language'];
$event['tools'] = array_merge($event['tools'], [
'backups' => [['admin.maintenance', 'admin.super'], $this->grav['language']->translate('PLUGIN_ADMIN.BACKUPS')],
'scheduler' => [['admin.super'], $this->grav['language']->translate('PLUGIN_ADMIN.SCHEDULER')],
'logs' => [['admin.super'], $this->grav['language']->translate('PLUGIN_ADMIN.LOGS')],
'reports' => [['admin.super'], $this->grav['language']->translate('PLUGIN_ADMIN.REPORTS')],
'direct-install' => [['admin.super'], $this->grav['language']->translate('PLUGIN_ADMIN.DIRECT_INSTALL')],
'backups' => [['admin.maintenance', 'admin.super'], $lang->translate('PLUGIN_ADMIN.BACKUPS')],
'scheduler' => [['admin.super'], $lang->translate('PLUGIN_ADMIN.SCHEDULER')],
'logs' => [['admin.super'], $lang->translate('PLUGIN_ADMIN.LOGS')],
'reports' => [['admin.super'], $lang->translate('PLUGIN_ADMIN.REPORTS')],
'direct-install' => [['admin.super'], $lang->translate('PLUGIN_ADMIN.DIRECT_INSTALL')],
]);
return $event;
@@ -613,13 +630,12 @@ class AdminPlugin extends Plugin
return $bc <=> $ac;
});
switch ($this->template) {
case 'dashboard':
$twig->twig_vars['popularity'] = $this->popularity;
// Gather Plugin-hooked dashboard items
$this->grav->fireEvent('onAdminDashboard');
switch ($this->template) {
case 'dashboard':
$twig->twig_vars['popularity'] = $this->popularity;
break;
}

View File

@@ -13,7 +13,7 @@ sidebar:
size: auto
dashboard:
days_of_stats: 7
widgets:
widgets_display:
dashboard-maintenance: true
dashboard-statistics: true
dashboard-notifications: true

View File

@@ -361,65 +361,11 @@ form:
title: Dashboard
underline: true
widgets.dashboard-maintenance:
type: toggle
label: Maintenance Widget
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
widgets_display:
type: widgets
label: Widget Display Status
validate:
type: bool
help: Display dashboard maintenance widget
widgets.dashboard-statistics:
type: toggle
label: Statistics Widget
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
help: Display dashboard statistics widget
widgets.dashboard-notifications:
type: toggle
label: Notifications Feed Widget
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
help: Display dashboard notifications feed widget
widgets.dashboard-feed:
type: toggle
label: News Feed Widget
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
help: Display dashboard news feed widget
widgets.dashboard-pages:
type: toggle
label: Latest Pages Widget
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
help: Display dashboard latest pages widget
type: array
Notifications:
type: section

View File

@@ -37,7 +37,7 @@
<div id="admin-dashboard">
{% if grav.twig.plugins_hooked_dashboard_widgets_top %}
{% for widget in grav.twig.plugins_hooked_dashboard_widgets_top %}
{% if attribute(config.plugins.admin.widgets, widget.template) == true %}
{% if attribute(config.plugins.admin.widgets_display, widget.template)|string in ['1', 'true'] %}
<div class="dashboard-item-flex">
{% include 'partials/' ~ widget.template ~ '.html.twig' %}
</div>
@@ -50,7 +50,7 @@
{% block content %}
{%- if grav.twig.plugins_hooked_dashboard_widgets_main -%}
{%- for widget in grav.twig.plugins_hooked_dashboard_widgets_main -%}
{%- if attribute(config.plugins.admin.widgets, widget.template) == true %}
{% if attribute(config.plugins.admin.widgets_display, widget.template)|string in ['1', 'true'] %}
{% include 'partials/' ~ widget.template ~ '.html.twig' %}
{% endif -%}
{%- endfor -%}
@@ -61,7 +61,7 @@
<div id="admin-dashboard">
{%- if grav.twig.plugins_hooked_dashboard_widgets_bottom -%}
{%- for widget in grav.twig.plugins_hooked_dashboard_widgets_bottom -%}
{%- if attribute(config.plugins.admin.widgets, widget.template) == true %}
{% if attribute(config.plugins.admin.widgets_display, widget.template)|string in ['1', 'true'] %}
{% include 'partials/' ~ widget.template ~ '.html.twig' %}
{% endif -%}
{%- endfor -%}

View File

@@ -0,0 +1,61 @@
{#{% extends "forms/field.html.twig" %}#}
{% import _self as macro %}
{% macro spanToggle(input, length) %}
{% set space = repeat('&nbsp;&nbsp;', (length - input|length) / 2) %}
{{ (space ~ input ~ space)|raw }}
{% endmacro %}
{% macro outputWidget(widget) %}
{% endmacro %}
{% set widgets = grav.twig.plugins_hooked_dashboard_widgets_top|array|merge(grav.twig.plugins_hooked_dashboard_widgets_main|array)|merge(grav.twig.plugins_hooked_dashboard_widgets_bottom|array) %}
{% set widgets_display = grav.config.plugins.admin.widgets_display %}
{% for widget in widgets %}
{% set widget_name = widget.name|default(widget.template|titleize) %}
{% set widget_id = widget.template %}
{% set widget_value = widgets_display[widget_id]|string in ['1', 'true'] ? 'true' : 'false' %}
{% set name = 'data['~field.name~']['~widget_id~']' %}
<div class="block block-toggle">
<div class="form-field grid">
<div class="form-label block size-1-3">
<label>
<span>{{ widget_name }} Widget</span>
</label>
</div>
<div class="form-data block size-2-3" data-grav-field="toggle" data-grav-default="1">
<div class="switch-toggle switch-grav medium switch-2">
{% set options = { true: 'PLUGIN_ADMIN.ENABLED', false: 'PLUGIN_ADMIN.DISABLED' } %}
{% set maxLen = 0 %}
{% for value, text in options %}
{% set translation = grav.twig.twig.filters['tu'] is defined ? text|tu : text|t %}
{% set maxLen = max(translation|length, maxLen) %}
{% endfor %}
{% for key, text in options %}
{% set id = "toggle_" ~ field.name ~ "." ~ widget_id ~ '.' ~ key %}
{% set translation = (grav.twig.twig.filters['tu'] is defined ? text|tu : text|t)|trim %}
<input type="radio"
value="{{ key }}"
id="{{ id }}"
name="{{ name }}"
class="{{ 'true' == '' ~ key ? 'highlight' : '' }}"
{% if key|string == widget_value|string %}
checked="checked"
{% endif %}
/>
<label for="{{ id }}">{{ (macro.spanToggle(translation, maxLen)|trim)|raw }}</label>
{% endfor %}
</div>
</div>
</div>
</div>
{% endfor %}