n_oct/plugins/rainlab/builder/widgets/ControllerList.php

105 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2023-06-17 20:52:33 +00:00
<?php namespace RainLab\Builder\Widgets;
use Str;
use Input;
use Backend\Classes\WidgetBase;
2023-06-18 12:28:15 +00:00
use RainLab\Builder\Classes\ControllerModel;
2023-06-17 20:52:33 +00:00
/**
2023-06-18 12:28:15 +00:00
* Plugin controller list widget.
2023-06-17 20:52:33 +00:00
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class ControllerList extends WidgetBase
{
use \Backend\Traits\SearchableWidget;
public $noRecordsMessage = 'rainlab.builder::lang.controller.no_records';
public function __construct($controller, $alias)
{
$this->alias = $alias;
parent::__construct($controller, []);
$this->bindToController();
}
/**
2023-06-18 12:28:15 +00:00
* Renders the widget.
2023-06-17 20:52:33 +00:00
* @return string
*/
public function render()
{
return $this->makePartial('body', $this->getRenderData());
}
public function updateList()
{
2023-06-18 12:28:15 +00:00
return ['#'.$this->getId('plugin-controller-list') => $this->makePartial('items', $this->getRenderData())];
2023-06-17 20:52:33 +00:00
}
public function refreshActivePlugin()
{
2023-06-18 12:28:15 +00:00
return ['#'.$this->getId('body') => $this->makePartial('widget-contents', $this->getRenderData())];
2023-06-17 20:52:33 +00:00
}
2023-06-18 12:28:15 +00:00
/*
* Event handlers
2023-06-17 20:52:33 +00:00
*/
2023-06-18 12:28:15 +00:00
2023-06-17 20:52:33 +00:00
public function onUpdate()
{
return $this->updateList();
}
public function onSearch()
{
$this->setSearchTerm(Input::get('search'));
return $this->updateList();
}
2023-06-18 12:28:15 +00:00
/*
* Methods for the internal use
2023-06-17 20:52:33 +00:00
*/
2023-06-18 12:28:15 +00:00
2023-06-17 20:52:33 +00:00
protected function getControllerList($pluginCode)
{
$result = ControllerModel::listPluginControllers($pluginCode);
return $result;
}
protected function getRenderData()
{
$activePluginVector = $this->controller->getBuilderActivePluginVector();
if (!$activePluginVector) {
return [
2023-06-18 12:28:15 +00:00
'pluginVector'=>null,
2023-06-17 20:52:33 +00:00
'items' => []
];
}
$items = $this->getControllerList($activePluginVector->pluginCodeObj);
$searchTerm = Str::lower($this->getSearchTerm());
if (strlen($searchTerm)) {
$words = explode(' ', $searchTerm);
$result = [];
foreach ($items as $controller) {
if ($this->textMatchesSearch($words, $controller)) {
$result[] = $controller;
}
}
$items = $result;
}
return [
2023-06-18 12:28:15 +00:00
'pluginVector'=>$activePluginVector,
'items'=>$items
2023-06-17 20:52:33 +00:00
];
}
}