return function () {
$self = url('/@object/@action/' . urlencode('dashboards/user'));
$board = is_string($_GET['board'] ?? null) && preg_match('~^[a-z0-9_-]{1,32}$~D', $_GET['board']) ? $_GET['board'] : 'main';
$base = $self . '?board=' . $board;
$guard = _class('csrf_guard');
$cls = get_class(_class('dashboard_user'));
$svc = new $cls();
$h = fn ($v) => $svc->h($v);
$token_ok = function () use ($guard) {
$sent = [$_POST[$guard->TOKEN_NAME] ?? '', $_SERVER[$guard->HEADER_NAME] ?? ''];
return is_post() && (bool) array_filter($sent, fn ($t) => is_string($t) && $t !== '' && hash_equals($guard->session_token(), $t));
};
// Two sample users; "signing in" here only picks one of them for this session (a POST with the session token), the cards then read
// the user from the service (user_id()), never from the request
$users = [101 => ['name' => 'Ann', 'group' => 2], 102 => ['name' => 'Bob', 'group' => 2]];
$_SESSION['docs_demo_user'] = (int) ($_SESSION['docs_demo_user'] ?? 0);
// a write answers with a redirect to the board (a reload never sends it again)
$back = function ($msg = '') use ($base) {
$_SESSION['docs_demo_user_msg'] = $msg;
no_graphics(true);
header('Location: ' . $base, true, 303);
return '';
};
if (isset($_GET['signin']) && $token_ok()) {
$who = (int) ($_POST['user'] ?? 0);
$_SESSION['docs_demo_user'] = isset($users[$who]) ? $who : 0;
return $back();
}
$uid = $_SESSION['docs_demo_user'];
$svc->_actor = ['id' => $uid, 'group' => $uid ? $users[$uid]['group'] : 0];
$svc->LEGACY = false;
$svc->_user_modules = [];
$svc->ENABLED = false;
// Sample cards: two personal ones over sample data per user, one public one over the shipped product list; plus the real Text and
// Links cards of the framework
$products = _class('docs')->_sample('products')->load();
load('user_dash_widgets', '', 'classes/user_dash/');
$demo = new class() extends yf_user_dash_widgets {
public $GROUP = 'shop';
public $data = [];
public $products = [];
public $WIDGETS = [
'orders' => ['title' => 'My orders', 'icon' => 'fa-shopping-cart', 'access' => 'user', 'personal' => true, 'kind' => 'list', 'size' => ['w' => 4, 'h' => 4, 'min_w' => 3],
'desc' => 'Your last orders (sample data of the signed-in user).', 'config' => ['limit' => ['type' => 'int', 'min' => 1, 'max' => 5, 'default' => 3, 'title' => 'Orders shown']]],
'balance' => ['title' => 'My balance', 'icon' => 'fa-money', 'access' => 'user', 'personal' => true, 'kind' => 'stat', 'size' => ['w' => 4, 'h' => 2, 'min_w' => 2],
'desc' => 'Your balance (sample data of the signed-in user).'],
'products' => ['title' => 'New in the shop', 'icon' => 'fa-cube', 'access' => 'guest', 'personal' => false, 'group' => 'content', 'kind' => 'list',
'size' => ['w' => 4, 'h' => 4, 'min_w' => 3], 'desc' => 'Products of the shop: public, guests see it too.'],
];
public function render_orders(array $w)
{
$rows = array_slice($this->data[$this->user_id()]['orders'] ?? [], 0, (int) ($w['conf']['limit'] ?? 3));
$list = array_map(fn ($o) => ['#' . $o[0] . ' · $' . number_format($o[1], 2), '', $o[2]], $rows);
return $list ? $this->rows_list($list) : '<p class="text-muted small">No orders yet.</p>';
}
public function render_balance(array $w)
{
return $this->big('$' . number_format((float) ($this->data[$this->user_id()]['balance'] ?? 0), 2), ' ' . t('on your account'));
}
public function render_products(array $w)
{
return $this->rows_list(array_map(fn ($p) => [(string) $p['name'], '', '$' . $p['price']], array_slice($this->products, 0, 5)));
}
};
$demo->data = [101 => ['orders' => [[1042, 58.0, 'shipped'], [1031, 12.5, 'done']], 'balance' => 120.5], 102 => ['orders' => [[2001, 999.0, 'new']], 'balance' => 7]];
$demo->products = $products;
$basic = clone _class('user_dash_widgets_basic', 'classes/user_dash/');
$svc->_providers = ['basic' => $basic, 'demo' => $demo];
$basic->_panel = $demo->_panel = $svc;
$a = $svc->actor();
$catalogue = $svc->catalogue($a);
$max = $svc->boards()->max_items('user');
// The shared board (as a super-admin would share it with `guest`), and each user's personal copy in this session
$grid = $svc->grid();
$shared = ['id' => 1, 'scope' => 'user', 'owner_id' => 0, 'title' => 'Welcome', 'version' => 1, 'layout' => $grid->normalize_v2(['items' => [
['i' => 'w1', 'type' => 'basic.text', 'x' => 0, 'y' => 0, 'w' => 8, 'h' => 2, 'conf' => ['heading' => 'Hello!', 'text' => 'Your **own** page: orders, balance and *news*. [Help](./?object=help)']],
['i' => 'w2', 'type' => 'demo.balance', 'x' => 8, 'y' => 0, 'w' => 4, 'h' => 2],
['i' => 'w3', 'type' => 'demo.orders', 'x' => 0, 'y' => 2, 'w' => 4, 'h' => 4],
['i' => 'w4', 'type' => 'demo.products', 'x' => 4, 'y' => 2, 'w' => 4, 'h' => 4],
['i' => 'w5', 'type' => 'basic.links', 'x' => 8, 'y' => 2, 'w' => 4, 'h' => 4, 'conf' => ['links' => "Shop | ./?object=shop\nNews | ./?object=news\nyf | https://yfix.net/"]],
]], null, ['max_items' => $max])];
$_SESSION['docs_demo_user_boards'] = is_array($_SESSION['docs_demo_user_boards'] ?? null) ? array_slice($_SESSION['docs_demo_user_boards'], -20, null, true) : [];
$copies = &$_SESSION['docs_demo_user_boards'][$board];
$copies = is_array($copies) ? $copies : [];
$msg = is_string($_SESSION['docs_demo_user_msg'] ?? null) ? $_SESSION['docs_demo_user_msg'] : '';
unset($_SESSION['docs_demo_user_msg']);
if (isset($_GET['do']) && $token_ok() && $uid > 0) {
if ($_GET['do'] === 'copy' && ! isset($copies[$uid])) {
$copies[$uid] = ['id' => 100 + $uid, 'scope' => 'user', 'owner_id' => $uid, 'title' => $shared['title'], 'version' => 1, 'layout' => $shared['layout']];
return $back('This is your personal copy: Edit layout to change it.');
} elseif ($_GET['do'] === 'reset') {
unset($copies[$uid]);
return $back('Your copy was deleted: this is the shared board.');
}
}
$own = $uid > 0 ? ($copies[$uid] ?? null) : null;
$b = $own ?: $shared;
$htmx = _class('htmx');
header('Vary: Cookie, HX-Request, HX-History-Restore-Request');
header('Cache-Control: private, no-store, max-age=0');
// one card of the board shown: its stored instance (?i=), or for the owner of the copy a card with the settings being tried
if (isset($_GET['card']) && $htmx->_wants_fragment()) {
return $htmx->_result($svc->rate_ok() ? $svc->board_card($b, $_GET['i'] ?? '', $_GET['type'] ?? '', $_GET['conf'] ?? '', $a) : '');
}
if (isset($_GET['save'])) {
$layout = null;
if ( ! is_post()) {
$r = ['ok' => false, 'error' => 'method', 'code' => 405];
} elseif ( ! $token_ok()) {
$r = ['ok' => false, 'error' => 'csrf', 'code' => 403];
} elseif ( ! $svc->write_ok()) {
$r = ['ok' => false, 'error' => 'rate', 'code' => 429];
} elseif ( ! $own) {
$r = ['ok' => false, 'error' => $uid > 0 ? 'not_found' : 'denied', 'code' => $uid > 0 ? 404 : 403];
} elseif (($layout = $grid->parse_layout_v2($_POST['layout'] ?? null, $catalogue, ['scope' => 'user', 'max_items' => $max])) === null) {
$r = ['ok' => false, 'error' => 'layout', 'code' => 400];
} elseif (isset($_POST['version']) && (int) $_POST['version'] > 0 && (int) $_POST['version'] !== $own['version']) {
$r = ['ok' => false, 'error' => 'conflict', 'code' => 409];
} else {
$copies[$uid]['layout'] = $layout;
$copies[$uid]['version']++;
$r = ['ok' => true, 'error' => '', 'version' => $copies[$uid]['version'], 'layout' => $layout, 'code' => 200];
}
no_graphics(true);
$code = $r['code'];
unset($r['code']);
return _class('api')->_response_raw((string) json_encode($r, JSON_UNESCAPED_SLASHES), $code, 'json');
}
$form = function ($url, $label, array $fields = [], $class = 'btn btn-default btn-outline-secondary btn-sm') use ($guard, $h) {
$hidden = '';
foreach ($fields as $k => $v) {
$hidden .= '<input type="hidden" name="' . $h($k) . '" value="' . $h($v) . '">';
}
return '<form method="post" action="' . $h($url) . '" class="yf-board-inline"><input type="hidden" name="' . $h($guard->TOKEN_NAME) . '" value="' . $h($guard->session_token()) . '">'
. $hidden . '<button type="submit" class="' . $h($class) . '">' . $h($label) . '</button></form>';
};
$who = '<div class="docs-ud-who"><span class="text-muted small">Signed in as:</span> <strong class="docs-ud-user">' . $h($uid ? $users[$uid]['name'] : 'a guest') . '</strong> ';
foreach ($users as $id => $u) {
$id !== $uid && $who .= $form($base . '&signin=1', 'Sign in as ' . $u['name'], ['user' => $id], 'btn btn-default btn-outline-secondary btn-sm docs-ud-signin-' . strtolower($u['name'])) . ' ';
}
$uid && $who .= $form($base . '&signin=1', 'Sign out', ['user' => 0], 'btn btn-default btn-outline-secondary btn-sm docs-ud-signout');
$who .= '</div>';
$notice = $msg !== '' ? '<div class="alert alert-success yf-board-msg" role="status">' . $h($msg) . '</div>' : '';
if ($uid && ! $own) {
$notice .= '<div class="alert alert-info yf-ud-copy-note">Make this board yours to change it; the shared board stays as it is. '
. $form($base . '&do=copy', 'Make it mine', [], 'btn btn-primary btn-xs btn-sm yf-ud-do-copy') . '</div>';
} elseif ($own) {
$notice .= '<p class="small text-muted yf-ud-copy">This is your copy of a shared board. ' . $form($base . '&do=reset', 'Reset to shared', [], 'btn btn-default btn-outline-secondary btn-xs btn-sm yf-ud-do-reset') . '</p>';
}
$intro = '<p class="docs-demo-intro">A user board (<code>yf_dashboard_user</code>, scope <code>user</code>): a board a super-admin shares with
<code>guest</code> shows a guest only its public cards (text, links, products); a signed-in user also gets the cards with their own
data, read from the session user and never from the request. <b>Make it mine</b> takes a personal copy that only its owner changes
(catalogue, settings, undo, save with the session token); <b>Reset to shared</b> drops it. Under 576 px the cards stack in one
column. On a site: <code>?object=dashboard</code> with <code>$PROJECT_CONF[\'dashboard_user\'][\'ENABLED\'] = true</code>.</p>';
return $intro . $who . $svc->render($b, $a, [
'title' => $b['title'],
'editable' => (bool) $own,
'save' => $own ? $base . '&save=1' : '',
'card' => $base . '&card=1',
'token' => $own ? $guard->session_token() : '',
'token_name' => $guard->TOKEN_NAME,
'notice' => $notice,
'id' => 'docs-user-board',
]);
};