PHP code example of kalakotra / silverstripe-dashboard

1. Go to this page and download the library: Download kalakotra/silverstripe-dashboard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kalakotra / silverstripe-dashboard example snippets




namespace App\Widgets;

use Kalakotra\Dashboard\Widgets\StatsWidget;
use Kalakotra\Dashboard\Widgets\WidgetWidth;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;

class BookingStatsWidget extends StatsWidget
{
    protected string    $title         = 'Bookings Overview';
    protected int       $order         = 15;
    protected WidgetWidth $width       = WidgetWidth::Full;
    protected int       $cacheLifetime = 300;

    public function canView(Member $member): bool
    {
        return Permission::checkMember($member, 'CMS_ACCESS_BookingAdmin');
    }

    public function getStats(): array
    {
        return [
            [
                'label' => 'Total Bookings',
                'value' => Booking::get()->count(),
                'icon'  => 'font-icon-calendar',
                'color' => 'blue',
            ],
            [
                'label' => 'This Month',
                'value' => Booking::get()
                    ->filter('Created:GreaterThan', date('Y-m-01'))
                    ->count(),
                'icon'  => 'font-icon-chart-line',
                'color' => 'green',
                'delta' => '+12%',
                'trend' => 'up',
            ],
        ];
    }
}

// Any logged-in CMS user (default)
public function canView(Member $member): bool
{
    return Permission::checkMember($member, 'CMS_ACCESS');
}

// Admin only
public function canView(Member $member): bool
{
    return Permission::checkMember($member, 'ADMIN');
}

// Specific group
public function canView(Member $member): bool
{
    return $member->inGroup('editors');
}

// 5-minute cache (default)
protected int $cacheLifetime = 300;

// Disable caching (static widgets)
protected int $cacheLifetime = 0;

// Custom cache key variance (e.g. per locale)
public function getCacheKey(?Member $member = null): string
{
    return parent::getCacheKey($member) . '_' . i18n::get_locale();
}

protected bool $supportsRefresh = true;

public function supportsRefresh(): bool
{
    return $this->supportsRefresh;
}