2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Widgets;
|
|
|
|
|
2016-04-18 13:48:09 +02:00
|
|
|
use App;
|
2015-01-28 18:03:35 +11:00
|
|
|
use Str;
|
|
|
|
use Lang;
|
|
|
|
use Input;
|
2014-05-14 23:24:20 +10:00
|
|
|
use System\Classes\PluginManager;
|
|
|
|
use Cms\Classes\ComponentHelpers;
|
2015-01-28 18:03:35 +11:00
|
|
|
use Backend\Classes\WidgetBase;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component list widget.
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class ComponentList extends WidgetBase
|
|
|
|
{
|
2016-03-28 15:47:29 +02:00
|
|
|
use \Backend\Traits\CollapsableWidget;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2016-03-28 15:47:29 +02:00
|
|
|
protected $searchTerm = false;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
protected $pluginComponentList;
|
|
|
|
|
|
|
|
public function __construct($controller, $alias)
|
|
|
|
{
|
|
|
|
$this->alias = $alias;
|
|
|
|
|
|
|
|
parent::__construct($controller, []);
|
|
|
|
$this->bindToController();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the widget.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return $this->makePartial('body', [
|
2015-04-29 19:50:30 +10:00
|
|
|
'data' => $this->getData()
|
2014-05-14 23:24:20 +10:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function onSearch()
|
|
|
|
{
|
|
|
|
$this->setSearchTerm(Input::get('search'));
|
|
|
|
|
|
|
|
return $this->updateList();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Methods for th internal use
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected function getData()
|
|
|
|
{
|
|
|
|
$searchTerm = Str::lower($this->getSearchTerm());
|
|
|
|
$searchWords = [];
|
2014-10-11 01:53:40 +02:00
|
|
|
if (strlen($searchTerm)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$searchWords = explode(' ', $searchTerm);
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$pluginManager = PluginManager::instance();
|
|
|
|
$plugins = $pluginManager->getPlugins();
|
|
|
|
|
|
|
|
$this->prepareComponentList();
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$components = $this->getPluginComponents($plugin);
|
2014-10-11 01:53:40 +02:00
|
|
|
if (!is_array($components)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$pluginDetails = $plugin->pluginDetails();
|
2014-10-19 10:58:18 +11:00
|
|
|
|
|
|
|
$pluginName = isset($pluginDetails['name'])
|
|
|
|
? $pluginDetails['name']
|
|
|
|
: Lang::get('system::lang.plugin.unnamed');
|
|
|
|
|
|
|
|
$pluginIcon = isset($pluginDetails['icon'])
|
|
|
|
? $pluginDetails['icon']
|
|
|
|
: 'icon-puzzle-piece';
|
|
|
|
|
|
|
|
$pluginDescription = isset($pluginDetails['description'])
|
|
|
|
? $pluginDetails['description']
|
|
|
|
: null;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
$pluginClass = get_class($plugin);
|
|
|
|
|
|
|
|
$pluginItems = [];
|
|
|
|
foreach ($components as $componentInfo) {
|
|
|
|
$className = $componentInfo->className;
|
|
|
|
$alias = $componentInfo->alias;
|
2016-04-18 13:48:09 +02:00
|
|
|
$component = App::make($className);
|
2015-01-10 10:59:58 +11:00
|
|
|
|
2015-01-08 09:42:41 -05:00
|
|
|
if ($component->isHidden) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-10 10:59:58 +11:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
$componentDetails = $component->componentDetails();
|
|
|
|
$component->alias = '--alias--';
|
|
|
|
|
|
|
|
$item = (object)[
|
2014-06-12 19:47:05 +10:00
|
|
|
'title' => ComponentHelpers::getComponentName($component),
|
|
|
|
'description' => ComponentHelpers::getComponentDescription($component),
|
|
|
|
'plugin' => $pluginName,
|
2014-05-14 23:24:20 +10:00
|
|
|
'propertyConfig' => ComponentHelpers::getComponentsPropertyConfig($component),
|
|
|
|
'propertyValues' => ComponentHelpers::getComponentPropertyValues($component, $alias),
|
2014-06-12 19:47:05 +10:00
|
|
|
'className' => get_class($component),
|
|
|
|
'pluginIcon' => $pluginIcon,
|
|
|
|
'alias' => $alias,
|
2014-10-19 10:58:18 +11:00
|
|
|
'name' => $componentInfo->duplicateAlias
|
|
|
|
? $componentInfo->className
|
|
|
|
: $componentInfo->alias
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
if ($searchWords && !$this->itemMatchesSearch($searchWords, $item)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
if (!array_key_exists($pluginClass, $items)) {
|
|
|
|
$group = (object)[
|
2014-06-12 19:47:05 +10:00
|
|
|
'title' => $pluginName,
|
2014-05-14 23:24:20 +10:00
|
|
|
'description' => $pluginDescription,
|
|
|
|
'pluginClass' => $pluginClass,
|
2014-06-12 19:47:05 +10:00
|
|
|
'icon' => $pluginIcon,
|
|
|
|
'items' => []
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
|
|
|
$items[$pluginClass] = $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pluginItems[] = $item;
|
|
|
|
}
|
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
usort($pluginItems, function ($a, $b) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return strcmp($a->title, $b->title);
|
|
|
|
});
|
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
if (isset($items[$pluginClass])) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$items[$pluginClass]->items = $pluginItems;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
uasort($items, function ($a, $b) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return strcmp($a->title, $b->title);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function prepareComponentList()
|
|
|
|
{
|
|
|
|
$pluginManager = PluginManager::instance();
|
|
|
|
$plugins = $pluginManager->getPlugins();
|
|
|
|
|
|
|
|
$componentList = [];
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$components = $plugin->registerComponents();
|
2014-10-11 01:53:40 +02:00
|
|
|
if (!is_array($components)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
foreach ($components as $className => $alias) {
|
|
|
|
$duplicateAlias = false;
|
|
|
|
foreach ($componentList as $componentInfo) {
|
|
|
|
if ($componentInfo->alias == $alias) {
|
|
|
|
$componentInfo->duplicateAlias = true;
|
|
|
|
$duplicateAlias = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$componentList[] = (object)[
|
2014-06-12 19:47:05 +10:00
|
|
|
'className' => $className,
|
|
|
|
'alias' => $alias,
|
2014-05-14 23:24:20 +10:00
|
|
|
'duplicateAlias' => $duplicateAlias,
|
2014-06-12 19:47:05 +10:00
|
|
|
'pluginClass' => get_class($plugin)
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pluginComponentList = $componentList;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getPluginComponents($plugin)
|
|
|
|
{
|
2016-03-29 14:55:25 +02:00
|
|
|
$result = [];
|
2014-05-14 23:24:20 +10:00
|
|
|
$pluginClass = get_class($plugin);
|
|
|
|
foreach ($this->pluginComponentList as $componentInfo) {
|
2014-10-11 01:53:40 +02:00
|
|
|
if ($componentInfo->pluginClass == $pluginClass) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$result[] = $componentInfo;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getSearchTerm()
|
|
|
|
{
|
|
|
|
return $this->searchTerm !== false ? $this->searchTerm : $this->getSession('search');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setSearchTerm($term)
|
|
|
|
{
|
|
|
|
$this->searchTerm = trim($term);
|
|
|
|
$this->putSession('search', $this->searchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateList()
|
|
|
|
{
|
2016-03-29 14:55:25 +02:00
|
|
|
return ['#'.$this->getId('component-list') => $this->makePartial('items', [
|
|
|
|
'items' => $this->getData()]
|
|
|
|
)];
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function itemMatchesSearch(&$words, $item)
|
|
|
|
{
|
|
|
|
foreach ($words as $word) {
|
|
|
|
$word = trim($word);
|
2014-10-11 01:53:40 +02:00
|
|
|
if (!strlen($word)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
continue;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
if (!$this->itemContainsWord($word, $item)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return false;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function itemContainsWord($word, $item)
|
|
|
|
{
|
2014-10-11 01:53:40 +02:00
|
|
|
if (Str::contains(Str::lower($item->title), $word)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return true;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
if (Str::contains(Str::lower($item->description), $word) && strlen($item->description)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return true;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-11 01:53:40 +02:00
|
|
|
if (Str::contains(Str::lower($item->plugin), $word) && strlen($item->plugin)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return true;
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-11 01:53:40 +02:00
|
|
|
}
|