ORIENT/modules/backend/classes/WidgetManager.php

257 lines
6.5 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace Backend\Classes;
use Str;
use File;
use Lang;
use Closure;
use October\Rain\Support\Yaml;
use Illuminate\Container\Container;
use System\Classes\PluginManager;
use System\Classes\SystemException;
/**
* 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 08:20:55 +00:00
protected $formWidgetCallbacks = [];
2014-05-14 13:24:20 +00:00
/**
* @var array An array of report widgets.
*/
protected $formWidgetHints;
2014-05-14 13:24:20 +00:00
/**
* @var array An array of report widgets.
*/
protected $reportWidgets;
/**
* @var array Cache of report widget registration callbacks.
*/
2014-08-01 08:20:55 +00:00
protected $reportWidgetCallbacks = [];
2014-05-14 13:24:20 +00:00
/**
* @var System\Classes\PluginManager
*/
protected $pluginManager;
/**
* Initialize this singleton.
*/
protected function init()
{
$this->pluginManager = PluginManager::instance();
}
/**
* Makes a widget object with configuration set.
2014-05-17 16:08:01 +00:00
* @param string $className A widget class name.
2014-05-14 13:24:20 +00:00
* @param Controller $controller The Backend controller that spawned this widget.
* @param array $configuration Configuration values.
* @return WidgetBase The widget object.
*/
public function makeWidget($className, $controller = null, $configuration = null)
{
/*
* Build configuration
*/
2014-10-10 21:12:50 +00:00
if ($configuration === null) {
2014-05-14 13:24:20 +00:00
$configuration = [];
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
/*
* Create widget object
*/
if (!class_exists($className)) {
throw new SystemException(Lang::get('backend::lang.widget.not_registered', [
2014-05-17 16:08:01 +00:00
'name' => $className
2014-05-14 13:24:20 +00:00
]));
}
return new $className($controller, $configuration);
}
//
// 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 21:12:50 +00:00
if (!is_array($widgets = $plugin->registerFormWidgets())) {
2014-05-14 13:24:20 +00:00
continue;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
2014-10-10 21:12:50 +00:00
foreach ($widgets as $className => $widgetInfo) {
2014-05-14 13:24:20 +00:00
$this->registerFormWidget($className, $widgetInfo);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
}
}
return $this->formWidgets;
}
2014-09-20 10:33:09 +00:00
/**
2014-05-14 13:24:20 +00:00
* Registers a single form form widget.
2014-09-20 10:33:09 +00:00
* @param string $className Widget class name.
* @param array $widgetInfo Registration information, can contain an 'code' key.
2014-09-20 10:33:09 +00:00
* @return void
2014-05-14 13:24:20 +00:00
*/
public function registerFormWidget($className, $widgetInfo = null)
{
$widgetCode = isset($widgetInfo['code']) ? $widgetInfo['code'] : null;
/* @todo Remove line if year >= 2015 */
if (!$widgetCode) {
$widgetCode = isset($widgetInfo['alias']) ? $widgetInfo['alias'] : null;
}
if (!$widgetCode) {
$widgetCode = Str::getClassId($className);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$this->formWidgets[$className] = $widgetInfo;
$this->formWidgetHints[$widgetCode] = $className;
2014-05-14 13:24:20 +00:00
}
/**
* Manually registers form widget for consideration.
* Usage:
* <pre>
* WidgetManager::registerFormWidgets(function($manager){
* $manager->registerFormWidget('Backend\FormWidgets\CodeEditor', [
* 'name' => 'Code editor',
* 'code' => 'codeeditor'
* ]);
2014-05-14 13:24:20 +00:00
* });
* </pre>
*/
public function registerFormWidgets(callable $definitions)
{
$this->formWidgetCallbacks[] = $definitions;
}
/**
* 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-20 23:37:58 +00:00
* @return string The class name resolved, or the original name.
2014-05-14 13:24:20 +00:00
*/
public function resolveFormWidget($name)
{
2014-10-10 21:12:50 +00:00
if ($this->formWidgets === null) {
2014-05-14 13:24:20 +00:00
$this->listFormWidgets();
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$hints = $this->formWidgetHints;
2014-05-14 13:24:20 +00:00
if (isset($hints[$name])) {
return $hints[$name];
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$_name = Str::normalizeClassName($name);
2014-10-10 21:12:50 +00:00
if (isset($this->formWidgets[$_name])) {
2014-05-14 13:24:20 +00:00
return $_name;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00: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 21:12:50 +00:00
if (!is_array($widgets = $plugin->registerReportWidgets())) {
2014-05-14 13:24:20 +00:00
continue;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
2014-10-10 21:12:50 +00:00
foreach ($widgets as $className => $widgetInfo) {
2014-05-14 13:24:20 +00:00
$this->registerReportWidget($className, $widgetInfo);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00: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:
* <pre>
* WidgetManager::registerReportWidgets(function($manager){
* $manager->registerReportWidget('RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview', [
* 'name'=>'Google Analytics traffic overview',
* 'context'=>'dashboard'
* ]);
* });
* </pre>
*/
public function registerReportWidgets(callable $definitions)
{
$this->reportWidgetCallbacks[] = $definitions;
}
2014-10-10 21:12:50 +00:00
}