AppCtrlConfiguration::getDisplayedInterfaceMenu()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 38
rs 9.504
cc 4
nc 6
nop 1
1
<?php
2
3
// -------------------------------------------------------------------------
4
// OVIDENTIA http://www.ovidentia.org
5
// Ovidentia is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
8
// any later version.
9
//
10
// This program is distributed in the hope that it will be useful, but
11
// WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
// USA.
19
// -------------------------------------------------------------------------
20
/**
21
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
22
 * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com})
23
 */
24
namespace Capwelton\LibApp\Ctrl;
25
26
use Capwelton\Widgets\Widgets\Helpers\WidgetAction;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Helpers\WidgetAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Capwelton\Widgets\Widgets\Item\WidgetLink;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetLink was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Capwelton\LibApp\Func_App;
29
use Capwelton\LibApp\Interfaces\AppComponentDefinition;
30
31
/**
32
 * This controller manages configuration of components
33
 *
34
 * @method Func_App App()
35
 * @method self proxy()
36
 */
37
class AppCtrlConfiguration extends AppController
38
{
39
    
40
    public function __construct(Func_App $app = null)
41
    {
42
        parent::__construct($app);
43
        if(! bab_isUserAdministrator()){
44
            $action = WidgetAction::fromRequest();
45
            list (, $method) = explode('.', $action->getMethod());
46
            if($method !== 'accessDenied'){
47
                /**
48
                 * @var WidgetAction
49
                 */
50
                $action = $this->proxy()->accessDenied();
51
                $this->redirect($action);
52
            }
53
        }
54
    }
55
    
56
    public function setDisplayTab($tab)
57
    {
58
        $_SESSION['libapp']['configuration']['currentTab'] = $tab;
59
        $this->addReloadSelector('.reload-configuration-page');
60
        return true;
61
    }
62
    
63
    public function resetCurrentTab()
64
    {
65
        $_SESSION['libapp']['configuration']['currentTab'] = 'addon';
66
        return $this;
67
    }
68
    
69
    public function getDisplayTab()
70
    {
71
        return isset($_SESSION['libapp']['configuration']['currentTab']) ? $_SESSION['libapp']['configuration']['currentTab'] : 'addon';
72
    }
73
    
74
    public function display()
75
    {
76
        $App = $this->App();
77
        $Ui = $App->Ui();
78
        $W = bab_Widgets();
79
        
80
        $page = $Ui->Page();
81
        
82
        $layout = $W->FlowLayout();
83
        $layout->addClass('panel panel-default libapp-configuration');
84
        
85
        $layout->addItem($this->getDisplayedInterfaceMenu()
86
            ->setSizePolicy('widget-100pc panel-heading'));
87
        $layout->addItem($this->getDisplayedInterfacePage()
88
            ->setSizePolicy('widget-100pc panel-body'));
89
        
90
        $page->addItem($layout);
91
        return $page;
92
    }
93
    
94
    public function getDisplayedInterfaceMenu($itemId = null)
95
    {
96
        $W = bab_Widgets();
97
        
98
        $box = $W->FlowLayout($itemId);
99
        $box->setReloadAction($this->proxy()
0 ignored issues
show
Bug introduced by
$this->proxy()->getDispl...faceMenu($box->getId()) of type Widget_FlowLayout is incompatible with the type Widget_Action expected by parameter $reloadAction of Widget_Item::setReloadAction(). ( Ignorable by Annotation )

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

99
        $box->setReloadAction(/** @scrutinizer ignore-type */ $this->proxy()
Loading history...
100
            ->getDisplayedInterfaceMenu($box->getId()));
101
        $box->addClass('reload-configuration-page');
102
        $box->addClass('widget-100pc');
103
        
104
        if(! isset($itemId)){
105
            $this->resetCurrentTab();
106
        }
107
        
108
        $currentTab = $this->getDisplayTab();
109
        $configurationItems = $this->getAvailableConfigurationItems();
110
        
111
        $navBoxHtml = '<ul class="nav nav-tabs">';
112
        $htmlCanvas = $W->HtmlCanvas();
113
        
114
        foreach ($configurationItems as $tabId => $tab){
115
            $tabLink = $W->Link($tab['tabTitle'], $this->proxy()
0 ignored issues
show
Bug introduced by
$this->proxy()->setDisplayTab($tabId) of type true is incompatible with the type Widget_Action|bab_url|string expected by parameter $url of Func_Widgets::Link(). ( Ignorable by Annotation )

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

115
            $tabLink = $W->Link($tab['tabTitle'], /** @scrutinizer ignore-type */ $this->proxy()
Loading history...
116
                ->setDisplayTab($tabId))
117
                ->setOpenMode(WidgetLink::OPEN_DIALOG);
118
            $tabLinkHtml = '<li role="configuration"';
119
            if($tabId == $currentTab){
120
                $tabLinkHtml .= ' class="active"';
121
            }
122
            $tabLinkHtml .= '>';
123
            $tabLinkHtml .= $tabLink->display($htmlCanvas) . '</li>';
124
            $navBoxHtml .= $tabLinkHtml;
125
        }
126
        
127
        $navBoxHtml .= '</ul>';
128
        $box->addItem($W->Html($navBoxHtml)
129
            ->setSizePolicy('widget-100pc libapp-navbox'));
130
        
131
        return $box;
132
    }
133
    
134
    public function getDisplayedInterfacePage($itemId = null)
135
    {
136
        $W = bab_Widgets();
137
        
138
        $box = $W->FlowLayout($itemId);
139
        $box->setReloadAction($this->proxy()
0 ignored issues
show
Bug introduced by
$this->proxy()->getDispl...facePage($box->getId()) of type Widget_FlowLayout is incompatible with the type Widget_Action expected by parameter $reloadAction of Widget_Item::setReloadAction(). ( Ignorable by Annotation )

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

139
        $box->setReloadAction(/** @scrutinizer ignore-type */ $this->proxy()
Loading history...
140
            ->getDisplayedInterfacePage($box->getId()));
141
        $box->addClass('reload-configuration-page');
142
        $box->addClass('widget-100pc');
143
        
144
        $currentTab = $this->getDisplayTab();
145
        $configurationItems = $this->getAvailableConfigurationItems();
146
        
147
        $configBox = $W->VBoxItems()
148
            ->addClass('panel-body')
149
            ->addClass('icon-left icon-48x48 icon-left-48');
150
        $box->addItem($configBox);
151
        
152
        if(! isset($configurationItems[$currentTab])){
153
            return $box;
154
        }
155
        
156
        $tab = $configurationItems[$currentTab];
157
        
158
        foreach ($tab['tabContent'] as $tabSection){
159
            if(! isset($tabSection['sectionContent']) || empty($tabSection['sectionContent'])){
160
                continue;
161
            }
162
            $configBox->addItem($W->Section($tabSection['sectionName'], $sectionContentBox = $W->HBoxItems())
163
                ->setFoldable(true));
164
            foreach ($tabSection['sectionContent'] as $tabSection){
0 ignored issues
show
Comprehensibility Bug introduced by
$tabSection is overwriting a variable from outer foreach loop.
Loading history...
165
                $sectionContentBox->addItem($tabSection);
166
            }
167
        }
168
        
169
        return $box;
170
    }
171
    
172
    protected function getAvailableConfigurationItems()
173
    {
174
        $App = $this->App();
175
        $addonName = $App->getAddonName();
176
        $configurationItems = array();
177
        
178
        $configurationItems['addon'] = array(
179
            'tabTitle' => $addonName,
180
            'tabContent' => array(
181
                array(
182
                    'sectionName' => 'Main',
183
                    'sectionContent' => array()
184
                )
185
            )
186
        );
187
        
188
        $definitions = $App->getComponentDefinitions();
189
        foreach ($definitions as $packageName => $definition){
190
            /* @var $definition AppComponentDefinition */
191
            if(! method_exists($definition, 'getConfiguration')){
192
                continue;
193
            }
194
            $configurationItems[$packageName] = array(
195
                'tabTitle' => $packageName,
196
                'tabContent' => $definition->getConfiguration($App)
197
            );
198
        }
199
        
200
        return $configurationItems;
201
    }
202
}