59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
namespace App\Handlers\Registrators;
|
|
|
|
use App\Enumerations\WidgetTypesWithHandlers;
|
|
use App\Models\WidgetType;
|
|
|
|
class WidgetRegistrator
|
|
{
|
|
protected $registeredWidgets = [];
|
|
|
|
|
|
/**
|
|
* Method used for registering new widgets
|
|
*
|
|
* @param $classname
|
|
* @param null $templateId
|
|
* @param null $moduleId
|
|
*/
|
|
public function register($classname, $templateId = null, $moduleId = null)
|
|
{
|
|
$this->registeredWidgets[] = [
|
|
'class' => $classname,
|
|
'templateId' => $templateId,
|
|
'moduleId' => $moduleId
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Install all non-installed widgets
|
|
*/
|
|
public function installWidgets()
|
|
{
|
|
$installed = WidgetTypesWithHandlers::getSimple();
|
|
foreach ($this->registeredWidgets as $registeredWidget) {
|
|
$classname = $registeredWidget['class'];
|
|
$templateId = $registeredWidget['templateId'];
|
|
$moduleId = $registeredWidget['moduleId'];
|
|
|
|
// register widget as new widget type
|
|
if (!in_array($classname, $installed)) {
|
|
$widgetName = call_user_func($classname . '::getName');
|
|
$widgetDescription = call_user_func($classname . '::getDescription');
|
|
|
|
// create row in widget types
|
|
WidgetType::create([
|
|
'handler' => $classname,
|
|
'name' => $widgetName,
|
|
'module_id' => $moduleId,
|
|
'template_id' => $templateId,
|
|
'description' => $widgetDescription
|
|
]);
|
|
|
|
// install widget (if neccessary)
|
|
call_user_func($classname . '::install');
|
|
}
|
|
}
|
|
}
|
|
}
|