Skip to content

Commit

Permalink
Merge branch '3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sampoyigi committed Jun 14, 2023
2 parents ccd5cf5 + f3e83c8 commit 3514a8b
Show file tree
Hide file tree
Showing 36 changed files with 812 additions and 595 deletions.
12 changes: 6 additions & 6 deletions app/admin/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,33 @@ protected function registerDashboardWidgets()
{
Widgets::instance()->registerDashboardWidgets(function (Widgets $manager) {
$manager->registerDashboardWidget(\System\DashboardWidgets\Activities::class, [
'code' => 'recent-activities',
'label' => 'Recent activities',
'context' => 'dashboard',
]);

$manager->registerDashboardWidget(\System\DashboardWidgets\Cache::class, [
'code' => 'cache',
'label' => 'Cache Usage',
'context' => 'dashboard',
]);

$manager->registerDashboardWidget(\System\DashboardWidgets\News::class, [
'code' => 'news',
'label' => 'Latest News',
'context' => 'dashboard',
]);

$manager->registerDashboardWidget(\Admin\DashboardWidgets\Statistics::class, [
'code' => 'stats',
'label' => 'Statistics widget',
'context' => 'dashboard',
]);

$manager->registerDashboardWidget(\Admin\DashboardWidgets\Onboarding::class, [
'code' => 'onboarding',
'label' => 'Onboarding widget',
'context' => 'dashboard',
]);

$manager->registerDashboardWidget(\Admin\DashboardWidgets\Charts::class, [
'code' => 'charts',
'label' => 'Charts widget',
'context' => 'dashboard',
]);
});
}
Expand Down
48 changes: 47 additions & 1 deletion app/admin/classes/BaseDashboardWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,54 @@ class BaseDashboardWidget extends BaseWidget

public function __construct($controller, $properties = [])
{
$this->setConfig($properties, ['widget']);

parent::__construct($controller, $properties);

$this->properties = $this->validateProperties($properties);
$this->fillFromConfig($properties);
}

public function getPropertiesToSave()
{
return array_except($this->properties, ['startDate', 'endDate']);
}

parent::__construct($controller);
public function getPropertyRules()
{
$rules = $attributes = [];
foreach ($this->defineProperties() as $name => $params) {
if (strlen($rule = array_get($params, 'validationRule', ''))) {
$rules[$name] = $rule;
$attributes[$name] = array_get($params, 'label', $name);
}
}

return [$rules, $attributes];
}

public function getWidth()
{
return $this->property('width');
}

public function getCssClass()
{
return $this->property('cssClass', 'bg-light');
}

public function getPriority()
{
return $this->property('priority', 9999);
}

public function getStartDate()
{
return $this->property('startDate');
}

public function getEndDate()
{
return $this->property('endDate');
}
}
29 changes: 29 additions & 0 deletions app/admin/classes/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Widgets
*/
protected $dashboardWidgetCallbacks = [];

protected $dashboardWidgetHints;

/**
* @var ExtensionManager
*/
Expand Down Expand Up @@ -275,7 +277,14 @@ public function listDashboardWidgets()
*/
public function registerDashboardWidget($className, $widgetInfo)
{
$widgetCode = $widgetInfo['code'] ?? null;

if (!$widgetCode) {
$widgetInfo['code'] = $widgetCode = get_class_id($className);
}

$this->dashboardWidgets[$className] = $widgetInfo;
$this->dashboardWidgetHints[$widgetCode] = $className;
}

/**
Expand All @@ -296,4 +305,24 @@ public function registerDashboardWidgets(callable $definitions)
{
$this->dashboardWidgetCallbacks[] = $definitions;
}

public function resolveDashboardWidget($name)
{
if ($this->dashboardWidgets === null) {
$this->listDashboardWidgets();
}

$hints = $this->dashboardWidgetHints;

if (isset($hints[$name])) {
return $hints[$name];
}

$_name = normalize_class_name($name);
if (isset($this->dashboardWidgets[$_name])) {
return $_name;
}

return $name;
}
}
81 changes: 43 additions & 38 deletions app/admin/controllers/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Dashboard extends \Admin\Classes\AdminController
{
public $containerConfig = [];

protected $callbacks = [];

public function __construct()
{
parent::__construct();
Expand All @@ -38,59 +40,57 @@ public function initDashboardContainer()
$this->containerConfig['canSetDefault'] = array_get($this->containerConfig, 'canSetDefault', AdminAuth::isSuperUser());
$this->containerConfig['defaultWidgets'] = array_get($this->containerConfig, 'defaultWidgets', $this->getDefaultWidgets());

new DashboardContainer($this, $this->containerConfig);
$widget = new DashboardContainer($this, $this->containerConfig);

foreach ($this->callbacks as $callback) {
$callback($widget);
}

$widget->bindToController();
}

protected function getDefaultWidgets()
{
return [
'onboarding' => [
'class' => \Admin\DashboardWidgets\Onboarding::class,
'priority' => 1,
'config' => [
'title' => 'admin::lang.dashboard.onboarding.title',
'width' => '6',
],
'priority' => 10,
'width' => '6',
],
'news' => [
'class' => \System\DashboardWidgets\News::class,
'priority' => 2,
'config' => [
'title' => 'admin::lang.dashboard.text_news',
'width' => '6',
],
'priority' => 10,
'width' => '6',
],
'order_stats' => [
'class' => \Admin\DashboardWidgets\Statistics::class,
'priority' => 3,
'config' => [
'context' => 'sale',
'width' => '4',
],
'widget' => 'stats',
'priority' => 20,
'card' => 'sale',
'width' => '4',
],
'reservation_stats' => [
'class' => \Admin\DashboardWidgets\Statistics::class,
'priority' => 4,
'config' => [
'context' => 'lost_sale',
'width' => '4',
],
'widget' => 'stats',
'priority' => 20,
'card' => 'lost_sale',
'width' => '4',
],
'customer_stats' => [
'class' => \Admin\DashboardWidgets\Statistics::class,
'priority' => 5,
'config' => [
'context' => 'cash_payment',
'width' => '4',
],
'widget' => 'stats',
'priority' => 20,
'card' => 'cash_payment',
'width' => '4',
],
'reports' => [
'widget' => 'charts',
'priority' => 30,
'width' => '12',
],
'charts' => [
'class' => \Admin\DashboardWidgets\Charts::class,
'priority' => 6,
'config' => [
'title' => 'admin::lang.dashboard.text_reports_chart',
'width' => '12',
],
'recent-activities' => [
'widget' => 'recent-activities',
'priority' => 40,
'width' => '6',
],
'cache' => [
'priority' => 90,
'width' => '6',
],
];
}
Expand All @@ -99,4 +99,9 @@ protected function canManageWidgets()
{
return $this->getUser()->hasPermission('Admin.Dashboard');
}

public function extendDashboard(callable $callback)
{
$this->callbacks[] = $callback;
}
}
Loading

0 comments on commit 3514a8b

Please sign in to comment.