• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lonnieezell / Bonfire2 / 13618451914

02 Mar 2025 07:07PM UTC coverage: 45.094% (-0.5%) from 45.594%
13618451914

Pull #564

github

web-flow
Merge 4efcc8f7f into 49088d21e
Pull Request #564: Prevent unnecessary queries related to widgets

47 of 88 new or added lines in 7 files covered. (53.41%)

20 existing lines in 1 file now uncovered.

1590 of 3526 relevant lines covered (45.09%)

63.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Widgets/Controllers/WidgetsSettingsController.php
1
<?php
2

3
/**
4
 * This file is part of Bonfire.
5
 *
6
 * (c) Lonnie Ezell <lonnieje@gmail.com>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11

12
namespace Bonfire\Widgets\Controllers;
13

14
use Bonfire\Core\AdminController;
15
use CodeIgniter\HTTP\RedirectResponse;
16

17
class WidgetsSettingsController extends AdminController
18
{
19
    protected $theme      = 'Admin';
20
    protected $viewPrefix = 'Bonfire\Widgets\Views\\';
21

22
    /**
23
     * Display the Widgets settings page.
24
     */
25
    public function index()
26
    {
27
        if (! auth()->user()->can('widgets.settings')) {
×
28
            return redirect()->to(ADMIN_AREA)->with('error', lang('Bonfire.notAuthorized'));
×
29
        }
30

31
        return $this->render($this->viewPrefix . 'settings', [
×
32
            'widgets' => setting('LineChart.widgets'),
×
33
            'manager' => service('widgets')->manager(),
×
34
        ]);
×
35
    }
36

37
    public function show(string $alias)
38
    {
39
        if (! auth()->user()->can('widgets.settings')) {
×
40
            return redirect()->to(ADMIN_AREA)->with('error', lang('Bonfire.notAuthorized'));
×
41
        }
42

43
        return $this->render($this->viewPrefix . '_' . $alias, [
×
44
            'widgets' => setting('LineChart.widgets'),
×
45
            'tab'     => $alias,
×
46
        ]);
×
47
    }
48

49
    /**
50
     * Saves the Widgets settings to the config file, where it
51
     * is automatically saved by our dynamic configuration system.
52
     */
53
    public function save(): RedirectResponse
54
    {
55
        if (! auth()->user()->can('widgets.settings')) {
×
56
            return redirect()->to(ADMIN_AREA)->with('error', lang('Bonfire.notAuthorized'));
×
57
        }
58

59
        match ($this->request->getVar('widget')) {
×
60
            'stats'          => $this->saveStatsSettings(),
×
61
            'linechart'      => $this->saveLineSettings(),
×
62
            'barchart'       => $this->saveBarSettings(),
×
63
            'doughnutchart'  => $this->saveDoughnutSettings(),
×
64
            'piechart'       => $this->savePieSettings(),
×
65
            'polarareachart' => $this->savePolarAreaSettings(),
×
66
            default          => $this->saveWidgetSettings(),
×
67
        };
×
68

69
        alert('success', 'The settings have been saved.');
×
70

71
        return redirect()->to($this->request->getUserAgent()->getReferrer());
×
72
    }
73

74
    /**
75
     * Return the image with preview scheme colors.
76
     */
77
    public function getColorSchemePreview()
78
    {
79
        if (! auth()->user()->can('widgets.settings')) {
×
80
            return redirect()->to(ADMIN_AREA)->with('error', lang('Bonfire.notAuthorized'));
×
81
        }
82

83
        $req = 'null';
×
84

85
        if ($this->request->getVar('bar_colorScheme')) {
×
86
            $req = $this->request->getVar('bar_colorScheme');
×
87
        }
88
        if ($this->request->getVar('doughnut_colorScheme')) {
×
89
            $req = $this->request->getVar('doughnut_colorScheme');
×
90
        }
91
        if ($this->request->getVar('pie_colorScheme')) {
×
92
            $req = $this->request->getVar('pie_colorScheme');
×
93
        }
94
        if ($this->request->getVar('polarArea_colorScheme')) {
×
95
            $req = $this->request->getVar('polarArea_colorScheme');
×
96
        }
97

98
        if ($req !== 'null') {
×
99
            return '<img src="/assets/admin/img/color_scheme/' . $req . '.png" style="height:40px !important; width:300px; ';
×
100
        }
101

102
        return '';
×
103
    }
104

105
    /**
106
     * Saves the Widgets settings to the config file, where it
107
     * is automatically saved by our dynamic configuration system.
108
     *
109
     * @return void
110
     */
111
    private function saveWidgetSettings()
112
    {
113
        $manager = service('widgets')->manager();
×
114

115
        foreach ($manager as $elem) {
×
NEW
116
            setting('Stats.' . $elem['widget'] . '_' . $elem['id'], $this->request->getPost($elem['widget'] . '_' . $elem['id']) ?? false);
×
117
        }
118
    }
119

120
    /**
121
     * Saves the Statistic widgets settings to the config file, where it
122
     * is automatically saved by our dynamic configuration system.
123
     *
124
     * @return void
125
     */
126
    private function saveStatsSettings()
127
    {
128
        setting('Stats.stats_showLink', $this->request->getPost('stats_showLink') ?? false);
×
129
    }
130

131
    /**
132
     * Saves the Chart Line settings to the config file, where it
133
     * is automatically saved by our dynamic configuration system.
134
     *
135
     * @return void
136
     */
137
    private function saveLineSettings()
138
    {
139
        setting('LineChart.line_showTitle', $this->request->getPost('line_showTitle') ?? false);
×
140
        setting('LineChart.line_showSubTitle', $this->request->getPost('line_showSubTitle') ?? false);
×
141
        setting('LineChart.line_showLegend', $this->request->getPost('line_showLegend') ?? false);
×
142
        setting('LineChart.line_legendPosition', $this->request->getPost('line_legendPosition') ?? false);
×
143
        setting('LineChart.line_enableAnimation', $this->request->getPost('line_enableAnimation') ?? false);
×
144
        setting('LineChart.line_usePermission', $this->request->getPost('line_usePermission') ?? false);
×
145
        setting('LineChart.line_tension', $this->request->getPost('line_tension'));
×
146
        setting('LineChart.useCustomSettings', $this->request->getPost('useCustomSettings') ?? false);
×
147

148
        if (setting('LineChart.useCustomSettings')) {
×
149
            setting('LineChart.line_borderColor', $this->request->getPost('line_borderColor') ?? '#000000');
×
150
            setting('LineChart.line_borderWidth', $this->request->getPost('line_borderWidth') ?? 1);
×
151
            setting('LineChart.line_pointBackgroundColor', $this->request->getPost('line_pointBackgroundColor'));
×
152
            setting('LineChart.line_pointBorderColor', $this->request->getPost('line_pointBorderColor'));
×
153
            setting('LineChart.line_pointBorderWidth', $this->request->getPost('line_pointBorderWidth'));
×
154
        } else {
155
            setting()->forget('LineChart.line_borderColor');
×
156
            setting()->forget('LineChart.line_borderWidth');
×
157
            setting()->forget('LineChart.line_pointBackgroundColor');
×
158
            setting()->forget('LineChart.line_pointBorderColor');
×
159
            setting()->forget('LineChart.line_pointBorderWidth');
×
160
        }
161
    }
162

163
    /**
164
     * Saves the Chart Bar settings to the config file, where it
165
     * is automatically saved by our dynamic configuration system.
166
     *
167
     * @return void
168
     */
169
    private function saveBarSettings()
170
    {
171
        setting('BarChart.bar_showTitle', $this->request->getPost('bar_showTitle') ?? false);
×
172
        setting('BarChart.bar_showLegend', $this->request->getPost('bar_showLegend') ?? false);
×
173
        setting('BarChart.bar_legendPosition', $this->request->getPost('bar_legendPosition') ?? false);
×
174
        setting('BarChart.bar_enableAnimation', $this->request->getPost('bar_enableAnimation') ?? false);
×
175
        setting('BarChart.bar_colorScheme', $this->request->getPost('bar_colorScheme') ?? 'null');
×
176
    }
177

178
    /**
179
     * Saves the Chart Doughnut settings to the config file, where it
180
     * is automatically saved by our dynamic configuration system.
181
     *
182
     * @return void
183
     */
184
    private function saveDoughnutSettings()
185
    {
186
        setting('DoughnutChart.doughnut_showTitle', $this->request->getPost('doughnut_showTitle') ?? false);
×
187
        setting('DoughnutChart.doughnut_showLegend', $this->request->getPost('doughnut_showLegend') ?? false);
×
188
        setting('DoughnutChart.doughnut_legendPosition', $this->request->getPost('doughnut_legendPosition') ?? false);
×
189
        setting('DoughnutChart.doughnut_enableAnimation', $this->request->getPost('doughnut_enableAnimation') ?? false);
×
190
        setting('DoughnutChart.doughnut_colorScheme', $this->request->getPost('doughnut_colorScheme') ?? 'null');
×
191
    }
192

193
    /**
194
     * Saves the Chart Pie settings to the config file, where it
195
     * is automatically saved by our dynamic configuration system.
196
     *
197
     * @return void
198
     */
199
    private function savePieSettings()
200
    {
201
        setting('PieChart.pie_showTitle', $this->request->getPost('pie_showTitle') ?? false);
×
202
        setting('PieChart.pie_showLegend', $this->request->getPost('pie_showLegend') ?? false);
×
203
        setting('PieChart.pie_legendPosition', $this->request->getPost('pie_legendPosition') ?? false);
×
204
        setting('PieChart.pie_enableAnimation', $this->request->getPost('pie_enableAnimation') ?? false);
×
205
        setting('PieChart.pie_colorScheme', $this->request->getPost('pie_colorScheme') ?? 'null');
×
206
    }
207

208
    /**
209
     * Saves the Chart Pola Area settings to the config file, where it
210
     * is automatically saved by our dynamic configuration system.
211
     *
212
     * @return void
213
     */
214
    private function savePolarAreaSettings()
215
    {
216
        setting('PolarAreaChart.polarArea_showTitle', $this->request->getPost('polarArea_showTitle') ?? false);
×
217
        setting('PolarAreaChart.polarArea_showLegend', $this->request->getPost('polarArea_showLegend') ?? false);
×
218
        setting('PolarAreaChart.polarArea_legendPosition', $this->request->getPost('polarArea_legendPosition') ?? false);
×
219
        setting('PolarAreaChart.polarArea_enableAnimation', $this->request->getPost('polarArea_enableAnimation') ?? false);
×
220
        setting('PolarAreaChart.polarArea_colorScheme', $this->request->getPost('polarArea_colorScheme') ?? 'null');
×
221
    }
222

223
    /**
224
     * Reset all the widget settings to their default values
225
     */
226
    public function resetSettings(): RedirectResponse
227
    {
228
        if (! auth()->user()->can('widgets.settings')) {
×
229
            return redirect()->to(ADMIN_AREA)->with('error', lang('Bonfire.notAuthorized'));
×
230
        }
231

232
        $manager = service('widgets')->manager();
×
233

234
        foreach ($manager as $elem) {
×
NEW
235
            setting()->forget('Stats.' . $elem['widget'] . '_' . $elem['id']);
×
236
        }
237

238
        setting()->forget('Stats.stats_showLink');
×
239

240
        setting()->forget('LineChart.line_showTitle');
×
241
        setting()->forget('LineChart.line_showSubTitle');
×
242
        setting()->forget('LineChart.line_showLegend');
×
243
        setting()->forget('LineChart.line_legendPosition');
×
244
        setting()->forget('LineChart.line_enableAnimation');
×
245
        setting()->forget('LineChart.line_usePermission');
×
246
        setting()->forget('LineChart.line_tension');
×
247
        setting()->forget('LineChart.useCustomSettings');
×
248
        setting()->forget('LineChart.line_borderColor');
×
249
        setting()->forget('LineChart.line_borderWidth');
×
250
        setting()->forget('LineChart.line_pointBackgroundColor');
×
251
        setting()->forget('LineChart.line_pointBorderColor');
×
252
        setting()->forget('LineChart.line_pointBorderWidth');
×
253

254
        setting()->forget('BarChart.bar_showTitle');
×
255
        setting()->forget('BarChart.bar_showLegend');
×
256
        setting()->forget('BarChart.bar_legendPosition');
×
257
        setting()->forget('BarChart.bar_enableAnimation');
×
258
        setting()->forget('BarChart.bar_colorScheme');
×
259

260
        setting()->forget('DoughnutChart.doughnut_showTitle');
×
261
        setting()->forget('DoughnutChart.doughnut_showLegend');
×
262
        setting()->forget('DoughnutChart.doughnut_legendPosition');
×
263
        setting()->forget('DoughnutChart.doughnut_enableAnimation');
×
264
        setting()->forget('DoughnutChart.doughnut_colorScheme');
×
265

266
        setting()->forget('PieChart.pie_showTitle');
×
267
        setting()->forget('PieChart.pie_showLegend');
×
268
        setting()->forget('PieChart.pie_legendPosition');
×
269
        setting()->forget('PieChart.pie_enableAnimation');
×
270
        setting()->forget('PieChart.pie_colorScheme');
×
271

272
        setting()->forget('PolarAreaChart.polarArea_showTitle');
×
273
        setting()->forget('PolarAreaChart.polarArea_showLegend');
×
274
        setting()->forget('PolarAreaChart.polarArea_legendPosition');
×
275
        setting()->forget('PolarAreaChart.polarArea_enableAnimation');
×
276
        setting()->forget('PolarAreaChart.polarArea_colorScheme');
×
277

278
        alert('success', 'The settings have been reset.');
×
279

280
        return redirect()->route('widgets-settings');
×
281
    }
282
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc