-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
100 lines (80 loc) · 3.25 KB
/
Controller.php
File metadata and controls
100 lines (80 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace HomeHeaderBanners;
use MapasCulturais\App;
use MapasCulturais\i;
class Controller extends \MapasCulturais\Controller
{
public function __construct()
{
$this->layout = 'panel';
}
protected function requireSaasAdmin(): void
{
$this->requireAuthentication();
$app = App::i();
if (!$app->user->is('saasAdmin') && !$app->user->is('superSaasAdmin')) {
$app->pass();
}
}
public function GET_index()
{
$this->requireSaasAdmin();
$app = App::i();
$plugin = Plugin::getInstance();
$this->render('index', [
'config' => $plugin ? $plugin->getConfig() : [],
'saved' => (bool) ($this->data['saved'] ?? false),
'actionUrl' => $app->createUrl('home-headers-banners', 'save'),
]);
}
public function POST_save()
{
$app = App::i();
$this->requireSaasAdmin();
$plugin = Plugin::getInstance();
if (!$plugin) {
$this->errorJson(i::__('Plugin não inicializado.'), 500);
}
$banners = (array) ($this->postData['banners'] ?? $this->data['banners'] ?? []);
// configuração base para os banners enviados no formulário
$config = [
'banners' => [
'first' => [
'image' => trim((string) (($banners['first']['image'] ?? '') ?: '')),
'link' => trim((string) (($banners['first']['link'] ?? '') ?: '')),
'alt' => trim((string) (($banners['first']['alt'] ?? '') ?: '')),
'newTab' => !empty($banners['first']['newTab']),
],
'second' => [
'image' => trim((string) (($banners['second']['image'] ?? '') ?: '')),
'link' => trim((string) (($banners['second']['link'] ?? '') ?: '')),
'alt' => trim((string) (($banners['second']['alt'] ?? '') ?: '')),
'newTab' => !empty($banners['second']['newTab']),
],
'third' => [
'image' => trim((string) (($banners['third']['image'] ?? '') ?: '')),
'link' => trim((string) (($banners['third']['link'] ?? '') ?: '')),
'alt' => trim((string) (($banners['third']['alt'] ?? '') ?: '')),
'newTab' => !empty($banners['third']['newTab']),
],
],
];
// identifica o subsite atual (ou 0 quando não houver subsite)
$subsite_id = $app->subsite ? $app->subsite->id : 0;
// carrega a configuração existente (possivelmente com vários subsites)
$current_config = $plugin->getConfig();
// garante que seja sempre um array
if (!is_array($current_config)) {
$current_config = [];
}
// sobrescreve apenas a configuração do subsite atual
$current_config[$subsite_id] = $config;
// persiste a configuração completa (todas as chaves de subsite)
$plugin->saveConfig($current_config);
if ($this->isAjax()) {
$this->json(['success' => true]);
} else {
App::i()->redirect(App::i()->createUrl('home-headers-banners', 'index', ['saved' => 1]));
}
}
}