PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/rainlab/builder/widgets/PluginList.php

https://gitlab.com/gideonmarked/newlifetrainingcenter-v2
PHP | 206 lines | 149 code | 34 blank | 23 comment | 10 complexity | 40fc296ce4b2b856e1369cdf7d7613a0 MD5 | raw file
  1. <?php namespace RainLab\Builder\Widgets;
  2. use Str;
  3. use Input;
  4. use File;
  5. use Backend\Classes\WidgetBase;
  6. use System\Classes\PluginManager;
  7. use RainLab\Builder\Classes\PluginCode;
  8. use RainLab\Builder\Models\Settings as PluginSettings;
  9. use RainLab\Builder\Classes\PluginVector;
  10. use Exception;
  11. /**
  12. * Plugin list widget.
  13. *
  14. * @package rainlab\builder
  15. * @author Alexey Bobkov, Samuel Georges
  16. */
  17. class PluginList extends WidgetBase
  18. {
  19. use \Backend\Traits\SearchableWidget;
  20. protected $theme;
  21. public $noRecordsMessage = 'rainlab.builder::lang.plugin.no_records';
  22. public function __construct($controller, $alias)
  23. {
  24. $this->alias = $alias;
  25. parent::__construct($controller, []);
  26. $this->bindToController();
  27. }
  28. /**
  29. * Renders the widget.
  30. * @return string
  31. */
  32. public function render()
  33. {
  34. return $this->makePartial('body', $this->getRenderData());
  35. }
  36. /**
  37. * Returns information about this widget, including name and description.
  38. */
  39. public function widgetDetails() {}
  40. public function setActivePlugin($pluginCode)
  41. {
  42. $pluginCodeObj = new PluginCode($pluginCode);
  43. $this->putSession('activePlugin', $pluginCodeObj->toCode());
  44. }
  45. public function getActivePluginVector()
  46. {
  47. $pluginCode = $this->getActivePluginCode();
  48. try {
  49. if (strlen($pluginCode)) {
  50. $pluginCodeObj = new PluginCode($pluginCode);
  51. $path = $pluginCodeObj->toPluginInformationFilePath();
  52. if (!File::isFile(File::symbolizePath($path))) {
  53. return null;
  54. }
  55. $plugins = PluginManager::instance()->getPlugins();
  56. foreach ($plugins as $code=>$plugin) {
  57. if ($code == $pluginCode) {
  58. return new PluginVector($plugin, $pluginCodeObj);
  59. }
  60. }
  61. }
  62. }
  63. catch (Exception $ex) {
  64. return null;
  65. }
  66. return null;
  67. }
  68. public function updateList()
  69. {
  70. return ['#'.$this->getId('plugin-list') => $this->makePartial('items', $this->getRenderData())];
  71. }
  72. /*
  73. * Event handlers
  74. */
  75. public function onUpdate()
  76. {
  77. return $this->updateList();
  78. }
  79. public function onSearch()
  80. {
  81. $this->setSearchTerm(Input::get('search'));
  82. return $this->updateList();
  83. }
  84. public function onToggleFilter()
  85. {
  86. $mode = $this->getFilterMode();
  87. $this->setFilterMode($mode == 'my' ? 'all' : 'my');
  88. $result = $this->updateList();
  89. $result['#'.$this->getId('toolbar-buttons')] = $this->makePartial('toolbar-buttons');
  90. return $result;
  91. }
  92. /*
  93. * Methods for the internal use
  94. */
  95. protected function getData()
  96. {
  97. $plugins = $this->getPluginList();
  98. $searchTerm = Str::lower($this->getSearchTerm());
  99. // Apply the search
  100. //
  101. if (strlen($searchTerm)) {
  102. $words = explode(' ', $searchTerm);
  103. $result = [];
  104. foreach ($plugins as $code=>$plugin) {
  105. if ($this->textMatchesSearch($words, $plugin['full-text'])) {
  106. $result[$code] = $plugin;
  107. }
  108. }
  109. $plugins = $result;
  110. }
  111. // Apply the my plugins / all plugins filter
  112. //
  113. $mode = $this->getFilterMode();
  114. if ($mode == 'my') {
  115. $namespace = PluginSettings::instance()->author_namespace;
  116. $result = [];
  117. foreach ($plugins as $code=>$plugin) {
  118. if (strcasecmp($plugin['namespace'], $namespace) === 0) {
  119. $result[$code] = $plugin;
  120. }
  121. }
  122. $plugins = $result;
  123. }
  124. return $plugins;
  125. }
  126. protected function getPluginList()
  127. {
  128. $plugins = PluginManager::instance()->getPlugins();
  129. $result = [];
  130. foreach ($plugins as $code=>$plugin) {
  131. $pluginInfo = $plugin->pluginDetails();
  132. $itemInfo = [
  133. 'name' => isset($pluginInfo['name']) ? $pluginInfo['name'] : 'rainlab.builder::lang.plugin.no_name',
  134. 'description' => isset($pluginInfo['description']) ? $pluginInfo['description'] : 'rainlab.builder::lang.plugin.no_description',
  135. 'icon' => isset($pluginInfo['icon']) ? $pluginInfo['icon'] : null
  136. ];
  137. list($namespace) = explode('\\', get_class($plugin));
  138. $itemInfo['namespace'] = trim($namespace);
  139. $itemInfo['full-text'] = trans($itemInfo['name']).' '.trans($itemInfo['description']);
  140. $result[$code] = $itemInfo;
  141. }
  142. uasort($result, function($a, $b) {
  143. return strcmp(trans($a['name']), trans($b['name']));
  144. });
  145. return $result;
  146. }
  147. protected function setFilterMode($mode)
  148. {
  149. $this->putSession('filter', $mode);
  150. }
  151. protected function getFilterMode()
  152. {
  153. return $this->getSession('filter', 'my');
  154. }
  155. protected function getActivePluginCode()
  156. {
  157. return $this->getSession('activePlugin');
  158. }
  159. protected function getRenderData()
  160. {
  161. return [
  162. 'items'=>$this->getData()
  163. ];
  164. }
  165. }