drago-ex / project-backend-ui
Integration of a basic template for administration.
Package info
github.com/drago-ex/project-backend-ui
Type:drago-tools-resource
pkg:composer/drago-ex/project-backend-ui
Requires
- php: >=8.3 <9
- drago-ex/application: ^5.0
- drago-ex/component: ^4.0
- drago-ex/form: ^4.0
- drago-ex/permission: ^2.0
- drago-ex/project-account: ^1.0 || ^2.0
- drago-ex/project-backend: ^1.0
- drago-ex/project-user: ^1.0
- drago-ex/translator: ^4.0.1
Requires (Dev)
- drago-ex/project-tools: ^1.0
- nette/tester: ^2.5
- phpstan/phpstan: ^2.0
- phpstan/phpstan-nette: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The administration layout of Drago Project in the look of compostrap: a sidebar menu of the installed packages filtered by permissions (also compact, with the icons only) with its editor, a navbar with the breadcrumb, the search, notifications, the language and the user menu with the theme (light, dark, the one of the system), and the dashboard.
Requirements
- PHP >= 8.3, < 9
- Drago Project
Installation
composer require drago-ex/project-backend-ui
Project Tools copies the files of the package into the project once,
replacing the layout and the admin.* assets of Project Backend.
Its npm dependencies (compostrap, Bootstrap Icons, Perfect Scrollbar, Chart.js) are installed by
npm install of the project; then build the assets again (npm run vite:build).
Assets
The copied assets/admin.js initializes the frame (sidebar, menu, theme, scrollbar; a click outside does not close a
modal or an offcanvas), the charts of the dashboard, the menu editor and the search dialog (Ctrl+K), and registers the
Naja extensions of the administration: the spinner, the submit button, Tom Select, the modal, the offcanvas, the
dropdowns and the toasts of flash messages. Add the extensions of other packages to the same call:
registerNajaExtensions( Spinner, SubmitDisableHandler, TomSelectHandler, ModalHandler, OffcanvasHandler, DropdownHandler, ToastHandler // the extensions of other packages );
assets/admin.scss imports the theme, core/admin-theme (icons and the compostrap modules the administration uses:
theme switch, tooltip, elements, sortable, empty state; the dialogs come with core/base) and, last,
core/admin-layout (the frame: sidebar, menu, dashboard). The styles of other packages go between them:
@import 'core/base'; @import 'core/admin-theme'; @import 'naja/spinner'; @import 'drago-form/styles/tom-select'; // the styles of other packages go here, before the frame of the administration @import 'compostrap/src/scss/chart'; @import 'core/admin-layout';
Menu
A package adds its links to the sidebar by a class implementing MenuProvider; the class is all it takes, the
search of Accessory/backend.neon registers it. A link is a presenter destination, an item with children is a group:
use App\Presentation\Backend\Accessory\Menu\MenuItem; use App\Presentation\Backend\Accessory\Menu\MenuProvider; final class BlogMenu implements MenuProvider { public function getItems(): array { return [ new MenuItem('blog', 'Blog', icon: 'bi-journal-text', order: 10, children: [ new MenuItem('blog.articles', 'Articles', 'Backend:Blog:default'), new MenuItem('blog.categories', 'Categories', 'Backend:Blog:categories'), ]), ]; } }
MenuItem: key (unique), label (translated), link (absolute destination, none for a group), icon (a class
of Bootstrap Icons without bi), section (content by default), order (lower first), allow and children. A
key given twice keeps the last item, so a project replaces an item of a package. Sections are listed with their
labels in the parameter backend.menu.sections; a package adds its own section the same way:
parameters: backend: menu: sections: shop: Shop
A link is current on every page of its presenter, a group opens by itself when one of its links is current. The breadcrumb in the navbar is made from the menu; a page deeper than its item adds its steps:
$this['breadcrumb']->add('Edit'); $this['breadcrumb']->add($article->title, translate: false);
Menu editor
The administrator puts the menu in order on the page Menu (Backend:Menu): sections and items are moved by dragging,
also into and out of a group, and Save stores the order in temp/menu.json (cleaning temp/ brings back the default
order). The items still come from the packages:
a removed package leaves only its keys, which are skipped, and a new one adds its items at their default place. A
project that wants the order in the database registers its own MenuStorage.
Notifications
What waits for the user (comments to approve, new orders) comes from classes implementing NotificationProvider. The
bell in the navbar lists them, and the menu item named by item shows the number (a group sums its links); the bell
is hidden when nothing waits.
use App\Presentation\Backend\Accessory\Notification\Notification; use App\Presentation\Backend\Accessory\Notification\NotificationProvider; final class CommentNotifications implements NotificationProvider { public function getNotifications(): array { return [new Notification('Comments waiting for approval', 'Backend:Blog:default', 3, item: 'blog.articles')]; } }
The bell and the numbers are sent again after every AJAX request, so they follow e.g. an approved comment.
Brand
The widget brand is the logo and the name of the website in the layouts of the sign-in, the account and the website
(the administration draws its own brand in Backend/@brand.latte): blocks brand-logo and brand. Here it is static, "Project";
Project Settings replaces it with the name of the website from its
settings.
{import '../Accessory/Widget/@brand.latte'} <a n:href="Home:">{include brand-logo}<span>{include brand}</span></a>
Content box
The widget box is the content box of the administration with its heading: the title and the subtitle on the left,
the buttons on the right. The header is left out when the title, the subtitle and the buttons are empty, so a box with
only content is a plain box.
{embed 'path/to/@box.latte'} {block title}{_'Blog articles'}{/block} {block subtitle}{_'Articles of the website.'}{/block} {block buttons}<a n:href="add!" class="btn btn-primary btn-sm ajax">{_'Add item'}</a>{/block} {block content}{control articles}{/block} {/embed}
The blocks inside the {embed} belong to the box: a page that also needs the title for <title> defines it outside,
{define title}{_'Blog articles'}{/define}.
Dashboard
The dashboard shows what the installed packages bring: tiles with numbers on top, charts and rankings under them. A
package returns them from a class implementing DashboardProvider:
use App\Presentation\Backend\Accessory\Dashboard\Chart; use App\Presentation\Backend\Accessory\Dashboard\DashboardProvider; use App\Presentation\Backend\Accessory\Dashboard\Ranking; use App\Presentation\Backend\Accessory\Dashboard\RankingItem; use App\Presentation\Backend\Accessory\Dashboard\Series; use App\Presentation\Backend\Accessory\Dashboard\Tile; final class BlogDashboard implements DashboardProvider { public function getWidgets(): array { return [ new Tile('Comments to approve', 'Backend:Blog:default', 3, icon: 'bi-chat-dots', theme: 'warning'), new Chart('Comments in the last 30 days', 'Backend:Blog:default', 'bar', ['1. 1.', '2. 1.'], [new Series('Comments', [3, 5])], wide: true), new Ranking('Most commented articles', 'Backend:Blog:default', [new RankingItem('Welcome', 4)], empty: 'No comments yet.'), ]; } }
Tile:label,link,value,icon,theme(a color of compostrap),order.Chart:title,link,type(area,line,bar,bar-horizontal,sparkline),labels,series(Serieswith a translatedname, itsdataand an optionalcolor),subtitle,wide(8 of 12 columns),format(options ofIntl.NumberFormat),order.Ranking:title,link,items(RankingItemwith alabel, avalueand an optionallinkwithargs),subtitle,empty(shown without items),wide,order.
Titles and labels are translation keys, the labels of a ranking are data. Tiles and the other widgets are sorted
apart by order.
Search
The search in the navbar (also Ctrl+K) finds the pages of the menu the user sees and the results of the classes
implementing SearchProvider, each in a group under its title:
use App\Presentation\Backend\Accessory\Search\Search; use App\Presentation\Backend\Accessory\Search\SearchProvider; use App\Presentation\Backend\Accessory\Search\SearchResult; final class ArticleSearch implements SearchProvider { public function getTitle(): string { return 'Articles'; } public function search(string $query, int $limit): array { return [new SearchResult('Welcome', 'Backend:Blog:edit', ['id' => 1], detail: 'News', icon: 'bi-journal-text')]; } }
Search::matches($query, ...$texts) compares the words of the query without regard to case and diacritics.
Permissions
Everything shows only what the user may open, asked by User::isAllowed() of Nette: the presenter of the link is the
resource and its action the privilege (Backend:Blog:categories → Backend:Blog, categories; an empty action is
default). The menu, the notifications, the dashboard and the search use the same rule. For a link to a page that
only redirects, an item sets the privileges with allow (any of them is enough):
new MenuItem('permission', 'Permissions', 'Backend:Permission:default', allow: ['Backend:Permission', 'users', 'roles']);
A group is shown when one of its links is, a section when one of its items is. A resource the ACL does not know is an
error, not a hidden item. The dashboard is the privilege default of Backend:Admin; a role without it goes to the
front page after signing in. The menu editor (Backend:Menu) is for the administrator.