Passed
Push — master ( 4954b5...87483c )
by Georgi
03:11
created

ModuleAdministration::addInstallButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 18
rs 9.9666
1
<?php
2
3
namespace Epesi\Core\System;
4
5
use Epesi\Core\System\Integration\Modules\ModuleView;
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\System\Integration\Modules\ModuleManager;
8
use Epesi\Core\Layout\Seeds\ActionBar;
9
use atk4\ui\jsFunction;
10
use atk4\ui\jsExpression;
11
12
class ModuleAdministration extends ModuleView
13
{
14
	protected $label = 'Module Administration';
15
	
16
	public static function access()
17
	{
18
		return Auth::user()->can('modify system settings');
19
	}
20
	
21
	public function body()
22
	{
23
		$this->addControlButtons();
24
25
		$this->addAccordion($this, $this->topLevelModules());
26
	}
27
	
28
	public function topLevelModules()
29
	{
30
		$modules = ModuleManager::getAll();
31
		
32
		return $modules->filter(function ($subModuleClass) use ($modules) {
33
			return ! $modules->map(function($moduleClass){
34
				return $moduleClass::namespace();
35
			})->contains($subModuleClass::parentNamespace());
36
		})->sort();
37
	}
38
	
39
	public function addAccordion($container, $modules)
40
	{
41
		$accordion = $container->add(['Accordion', 'type' => ['styled', 'fluid'], 'settings' => ['animateChildren' => false]])->setStyle(['max-width' => '800px', 'margin-left' => 'auto', 'margin-right' => 'auto']);
42
		
43
		foreach ($modules as $moduleClass) {
44
			$section = $accordion->addSection($moduleClass::label());
45
			
46
			
47
			$section->add(['Message', 'ui' => 'tiny message'])->template->appendHTML('Content', $this->formatModuleInfo($moduleClass));
48
49
			if (ModuleManager::isInstalled($moduleClass)) {
50
				$label = ['Label', __('Installed'), 'green'];
51
				
52
				$this->addUninstallButton($section, $moduleClass);
53
				
54
				$this->addReinstallButton($section, $moduleClass);
55
			}
56
			else {
57
				$label = ['Label', __('Available'), 'yellow'];
58
				
59
				$this->addInstallButton($section, $moduleClass);
60
			}
61
			
62
			$section->add($label, 'title')->setStyle('float', 'right');
63
			
64
			$submodules = ModuleManager::getAll()->filter(function ($subModuleClass) use ($moduleClass) {
65
				return $subModuleClass::isSubModuleOf($moduleClass);
66
			});
67
			
68
			if ($submodules->isEmpty()) continue;
69
			
70
			$this->addAccordion($section, $submodules);
71
		}
72
	}
73
	
74
	public function formatModuleInfo($moduleClass)
75
	{
76
		$moduleInfo = (array) ($moduleClass::info()?: __(' No details provided by author'));
77
		
78
		$ret = [];
79
		foreach ($moduleInfo as $label => $text) {
80
			$ret[] = (is_string($label)? "<strong>$label</strong>: ": '') . $text;
81
		}
82
		
83
		return implode('<br>', $ret);
84
	}
85
	
86
	public function addInstallButton($container, $moduleClass)
87
	{
88
		$installCallback = $this->add('jsCallback')->set(function() use ($moduleClass) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $arg1 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$installCallback = $this->add('jsCallback')->set(/** @scrutinizer ignore-type */ function() use ($moduleClass) {
Loading history...
89
			ob_start();
90
			ModuleManager::install($moduleClass);
91
			
92
			return ob_get_clean();
93
		});
94
			
95
		$modal = $this->add(['Modal', 'title' => __('Module Installation')])->set(function($view) use ($installCallback) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $arg1 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
		$modal = $this->add(['Modal', 'title' => __('Module Installation')])->set(/** @scrutinizer ignore-type */ function($view) use ($installCallback) {
Loading history...
96
			$view->add('Header')->set('Module has following dependencies which will be installed');
97
				
98
			$view->add(['Button', __('Install'), 'primary'])->on('click', [
99
					$installCallback
100
			]);
101
		});
102
				
103
		$container->add(['Button', __('Install'), 'class' => ['green']])->on('click', $modal->show());
104
	}
105
	
106
	public function addUninstallButton($container, $moduleClass)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
	public function addUninstallButton($container, /** @scrutinizer ignore-unused */ $moduleClass)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
	{
108
		$container->add(['Button', __('Uninstall'), 'class' => ['red']]);
109
	}
110
	
111
	public function addReinstallButton($container, $moduleClass)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
	public function addReinstallButton($container, /** @scrutinizer ignore-unused */ $moduleClass)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
	{
113
		$container->add(['Button', __('Re-install')]);
114
	}
115
	
116
	public function addControlButtons()
117
	{
118
		ActionBar::addButton('back')->link(url('view/system'));
119
		
120
		$this->addClearCacheButton();
121
	}
122
	
123
	public function addClearCacheButton()
124
	{
125
		ActionBar::addButton(['icon' => 'refresh', 'label' => __('Clear Cache')])->callback(function($callback) {
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
		ActionBar::addButton(['icon' => 'refresh', 'label' => __('Clear Cache')])->callback(function(/** @scrutinizer ignore-unused */ $callback) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
			ModuleManager::clearCache();
127
			
128
			return $this->notify(__('Cache cleared!'));
129
		});
130
	}
131
	
132
}
133