Dashboards

A board is a grid of cards: counts, charts, lists, links, text. The plugin dashboards keeps boards of two scopes that never mix: admin boards with the cards of the admin home, and user boards that site visitors see. Super-admins make them in the admin (Dashboards, manage_dashboards); code adds cards through card providers. Live examples: builder, grid, user board.

Boards

  • A board has a scope, an owner (0: a shared board, else the admin or user of a personal board), a slug, a visibility (the admin groups, or for a user board the user groups and guest; empty: super-admins only) and a layout (card types, positions, sizes, settings). A template is a starting point for new boards and is not listed as a board.
  • Super-admins create, edit, share and delete shared boards and templates of both scopes. A personal board is seen and changed by its owner only. Whoever sees a shared board may take a personal copy, change it and reset it; they are told when the shared board changed.
  • Every save keeps a version (the last 20, dashboard_boards.KEEP_VERSIONS) with a diff and restore; a board moves between sites as JSON (export / import, at most 128 KB).
  • A shared admin board can be marked the default board of admin groups. It is offered to the admins of the group (a notice on the admin home, the boards list); with dashboard_builder.GROUP_HOME on (default off) the admin home shows it to an admin who set no home board of their own.
  • Limits (dashboard_boards): MAX_BOARDS personal boards per admin (50), USER_MAX_BOARDS per user (5), MAX_SHARED shared boards and templates per scope (200), USER_MAX_ITEMS cards on a user board (20).

Every rights check is on the server (_class('dashboard_boards'): can_view(), can_edit(), can_delete(), can_copy(), can_create()), and every write is a POST with the session CSRF token. A layout holds data only: the card types come from code, and their settings are checked against each card's schema.

User boards on the site

The user module dashboard is off by default, so existing sites expose no new page; every action answers 404 until it is on:

$PROJECT_CONF['dashboard_user']['ENABLED'] = true;
  • ?object=dashboard lists the boards the visitor may see, &board=<slug> shows one (the visitor's own copy first). {execute(dashboard,display,name=<slug>)} places a board in a page template.
  • A guest sees the public cards of a board shared with guest (text, links, content); a signed-in user also sees the cards with their own data, read from the session user, never from the request. Guests only read.
  • Under 576 px (ONE_COLUMN_BELOW) the cards stack in one column.
  • Rate limits (dashboard_user): CARD_RATE card loads per session and minute (120), WRITE_RATE writes per session and minute (30), GUEST_IP_RATE requests per guest IP and minute (0: none). GUEST_IP_RATE needs the real client IP: behind a reverse proxy or CDN set TRUSTED_PROXIES and CLIENT_IP_HEADER too, or every guest shares the proxy's address and all are locked out at once.
  • A project or app module named dashboard replaces this one; extend yf_dashboard to keep its actions.

Card providers

Cards come from provider classes. Admin cards: a class *admin_home_widgets_* in classes/admin_home/ of a plugin, the app or the project, extending yf_admin_home_widgets; user cards: *user_dash_widgets_* in classes/user_dash/, extending yf_user_dash_widgets. Both are found by name (DISCOVER, on); SKIP hides cards (system.disk) or a whole provider (system.*).

class my_user_dash_widgets_orders extends yf_user_dash_widgets
{
    public $GROUP = 'other';
    public $WIDGETS = [
        'recent' => ['title' => 'My recent orders', 'icon' => 'fa-shopping-cart', 'kind' => 'list',
            'size' => [4, 3], 'access' => 'user', 'cache' => 60, 'tables' => ['my_orders'],
            'config' => ['limit' => ['type' => 'int', 'default' => 5, 'min' => 1, 'max' => 20]]],
    ];

    public function render_recent($w)
    {
        $limit = (int) ($w['conf']['limit'] ?? 5);
        $rows = $this->cached('recent_' . $limit, fn () => $this->db_all('SELECT id, title FROM ' . $this->table('my_orders')
            . ' WHERE user_id = ' . (int) $this->user_id() . ' ORDER BY id DESC LIMIT ' . $limit));
        $out = '';
        foreach ((array) $rows as $r) {
            $out .= '<li>' . $this->h($r['title']) . '</li>';
        }
        return $out ? '<ul>' . $out . '</ul>' : null;   // null or '' hides the card
    }
}

Keys of a card: title, icon (Font Awesome 4), group, kind (stat, chart, table, list, text, embed, links, data, other), size ([w, h] in grid cells, or w, h, min_w...), config (the settings: select, int, bool, text with options, default, min, max), refresh (seconds between reloads), cache (seconds the data is kept), tables (a card whose tables are missing is not offered), lazy and wide (admin home). access: on the admin side superadmin, admin or module:<object>; on the user side guest, user, module:<user module> or group:<id>[,<id>]; a list (any one) or a closure. A user card is personal by default: never shown to a guest, and a placeholder in an admin preview. render_<name>($w) draws the card; may_see_<name>() narrows who sees it. $TEMPLATES of a provider offers ready boards.

Rules for a provider: read the user only through user_id(); escape every value with h() and build links with link() / url(); keep queries bounded and cached through cached() (per user and site) or cached_public() (shared by every viewer); let the card's settings only narrow what is shown. Data and database cards (kind data) are for super-admins and never shown on a user board.

The old dashboards

The boards replace the old dashboards (manage_dashboards2 and the old rows of manage_dashboards). dashboards:migrate moves them: a dry run by default, --apply creates the boards. The old renderers (common()->dashboard_display() and the admin display()) show the old rows until dashboard_boards.LEGACY_RENDERER is false; then they show the migrated board, and the old renderer only for a row not migrated yet. The default flips in 4.0. The _hook_widget_* methods of old user modules stay available as legacy.* cards (dashboard_user.LEGACY).