2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Classes;
|
|
|
|
|
|
|
|
use Str;
|
|
|
|
use System\Classes\PluginManager;
|
2017-05-30 16:49:35 -05:00
|
|
|
use Event;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget manager
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class WidgetManager
|
|
|
|
{
|
|
|
|
use \October\Rain\Support\Traits\Singleton;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array An array of report widgets.
|
|
|
|
*/
|
|
|
|
protected $formWidgets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Cache of report widget registration callbacks.
|
|
|
|
*/
|
2014-08-01 18:20:55 +10:00
|
|
|
protected $formWidgetCallbacks = [];
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array An array of report widgets.
|
|
|
|
*/
|
2014-10-18 10:47:36 +11:00
|
|
|
protected $formWidgetHints;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array An array of report widgets.
|
|
|
|
*/
|
|
|
|
protected $reportWidgets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Cache of report widget registration callbacks.
|
|
|
|
*/
|
2014-08-01 18:20:55 +10:00
|
|
|
protected $reportWidgetCallbacks = [];
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var System\Classes\PluginManager
|
|
|
|
*/
|
|
|
|
protected $pluginManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this singleton.
|
|
|
|
*/
|
|
|
|
protected function init()
|
|
|
|
{
|
|
|
|
$this->pluginManager = PluginManager::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Form Widgets
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of registered form widgets.
|
|
|
|
* @return array Array keys are class names.
|
|
|
|
*/
|
|
|
|
public function listFormWidgets()
|
|
|
|
{
|
|
|
|
if ($this->formWidgets === null) {
|
|
|
|
$this->formWidgets = [];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load module widgets
|
|
|
|
*/
|
|
|
|
foreach ($this->formWidgetCallbacks as $callback) {
|
|
|
|
$callback($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load plugin widgets
|
|
|
|
*/
|
|
|
|
$plugins = $this->pluginManager->getPlugins();
|
|
|
|
|
|
|
|
foreach ($plugins as $plugin) {
|
2014-10-10 23:12:50 +02:00
|
|
|
if (!is_array($widgets = $plugin->registerFormWidgets())) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-10 23:12:50 +02:00
|
|
|
foreach ($widgets as $className => $widgetInfo) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->registerFormWidget($className, $widgetInfo);
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->formWidgets;
|
|
|
|
}
|
|
|
|
|
2014-09-20 10:33:09 +00:00
|
|
|
/**
|
2014-05-14 23:24:20 +10:00
|
|
|
* Registers a single form form widget.
|
2014-09-20 10:33:09 +00:00
|
|
|
* @param string $className Widget class name.
|
2017-03-16 17:08:20 +11:00
|
|
|
* @param array $widgetInfo Registration information, can contain a `code` key.
|
2014-09-20 10:33:09 +00:00
|
|
|
* @return void
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function registerFormWidget($className, $widgetInfo = null)
|
|
|
|
{
|
2016-11-01 08:14:25 +11:00
|
|
|
if (!is_array($widgetInfo)) {
|
|
|
|
$widgetInfo = ['code' => $widgetInfo];
|
|
|
|
}
|
|
|
|
|
2014-10-18 10:47:36 +11:00
|
|
|
$widgetCode = isset($widgetInfo['code']) ? $widgetInfo['code'] : null;
|
|
|
|
|
2014-10-18 11:06:54 +02:00
|
|
|
if (!$widgetCode) {
|
2014-10-18 10:47:36 +11:00
|
|
|
$widgetCode = Str::getClassId($className);
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$this->formWidgets[$className] = $widgetInfo;
|
2014-10-18 10:47:36 +11:00
|
|
|
$this->formWidgetHints[$widgetCode] = $className;
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually registers form widget for consideration.
|
|
|
|
* Usage:
|
2017-03-16 17:08:20 +11:00
|
|
|
*
|
|
|
|
* WidgetManager::registerFormWidgets(function($manager){
|
|
|
|
* $manager->registerFormWidget('Backend\FormWidgets\CodeEditor', 'codeeditor');
|
|
|
|
* });
|
|
|
|
*
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function registerFormWidgets(callable $definitions)
|
|
|
|
{
|
|
|
|
$this->formWidgetCallbacks[] = $definitions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-18 10:47:36 +11:00
|
|
|
* Returns a class name from a form widget code
|
|
|
|
* Normalizes a class name or converts an code to it's class name.
|
|
|
|
* @param string $name Class name or form widget code.
|
2014-05-21 09:37:58 +10:00
|
|
|
* @return string The class name resolved, or the original name.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function resolveFormWidget($name)
|
|
|
|
{
|
2014-10-10 23:12:50 +02:00
|
|
|
if ($this->formWidgets === null) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->listFormWidgets();
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-18 10:47:36 +11:00
|
|
|
$hints = $this->formWidgetHints;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-18 11:06:54 +02:00
|
|
|
if (isset($hints[$name])) {
|
2014-10-18 10:47:36 +11:00
|
|
|
return $hints[$name];
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$_name = Str::normalizeClassName($name);
|
2014-10-10 23:12:50 +02:00
|
|
|
if (isset($this->formWidgets[$_name])) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return $_name;
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Report Widgets
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of registered report widgets.
|
|
|
|
* @return array Array keys are class names.
|
|
|
|
*/
|
|
|
|
public function listReportWidgets()
|
|
|
|
{
|
|
|
|
if ($this->reportWidgets === null) {
|
|
|
|
$this->reportWidgets = [];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load module widgets
|
|
|
|
*/
|
|
|
|
foreach ($this->reportWidgetCallbacks as $callback) {
|
|
|
|
$callback($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load plugin widgets
|
|
|
|
*/
|
|
|
|
$plugins = $this->pluginManager->getPlugins();
|
|
|
|
|
|
|
|
foreach ($plugins as $plugin) {
|
2014-10-10 23:12:50 +02:00
|
|
|
if (!is_array($widgets = $plugin->registerReportWidgets())) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-10 23:12:50 +02:00
|
|
|
foreach ($widgets as $className => $widgetInfo) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->registerReportWidget($className, $widgetInfo);
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 16:49:35 -05:00
|
|
|
/*
|
|
|
|
* Extensibility
|
|
|
|
*/
|
|
|
|
Event::fire('system.reportwidgets.extendItems', [$this]);
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
return $this->reportWidgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Registers a single report widget.
|
|
|
|
*/
|
|
|
|
public function registerReportWidget($className, $widgetInfo)
|
|
|
|
{
|
|
|
|
$this->reportWidgets[$className] = $widgetInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually registers report widget for consideration.
|
|
|
|
* Usage:
|
2017-03-16 17:08:20 +11:00
|
|
|
*
|
|
|
|
* WidgetManager::registerReportWidgets(function($manager){
|
|
|
|
* $manager->registerReportWidget('RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview', [
|
|
|
|
* 'name'=>'Google Analytics traffic overview',
|
|
|
|
* 'context'=>'dashboard'
|
|
|
|
* ]);
|
|
|
|
* });
|
|
|
|
*
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function registerReportWidgets(callable $definitions)
|
|
|
|
{
|
|
|
|
$this->reportWidgetCallbacks[] = $definitions;
|
|
|
|
}
|
2017-05-30 16:49:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a single report widget item
|
|
|
|
*/
|
|
|
|
public function removeReportWidgetItem($className)
|
|
|
|
{
|
|
|
|
if (!$this->reportWidgets) {
|
|
|
|
throw new SystemException('Unable to remove a widget before widgets are loaded.');
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->reportWidgets[$className]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|