-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheme.php
More file actions
203 lines (179 loc) · 7.14 KB
/
Theme.php
File metadata and controls
203 lines (179 loc) · 7.14 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace redemapas;
use MapasCulturais\App;
use MapasCulturais\Definitions\Metadata;
use MapasCulturais\Entities\Notification;
use MapasCulturais\Entities\User;
use MapasCulturais\Themes\RedeMapas\Controllers\Push;
use MapasCulturais\Themes\RedeMapas\Jobs\SendWebPushNotification;
use MapasCulturais\Themes\RedeMapas\Push\PushConfigBuilder;
use MapasCulturais\Themes\RedeMapas\Pwa\HeadTagsBuilder;
use MapasCulturais\Themes\RedeMapas\Pwa\WebmanifestBuilder;
class Theme extends \MapasCulturais\Themes\BaseV2\Theme
{
static function getThemeFolder()
{
return __DIR__;
}
function _init()
{
parent::_init();
$app = App::i();
$theme = $this;
if (!$app->getController('push')) {
$app->registerController('push', Push::class);
}
// Copy service worker to public directory on theme init
$swSource = __DIR__ . '/assets/js/sw.js';
$swDest = '/var/www/html/sw.js';
if (is_file($swSource) && (!is_file($swDest) || filemtime($swSource) > filemtime($swDest))) {
copy($swSource, $swDest);
chmod($swDest, 0644);
}
$app->registerMetadata(
new Metadata(Push::USER_METADATA_KEY, ['label' => 'Web Push Subscriptions', 'type' => 'json', 'private' => true]),
User::class
);
$app->hook('view.render(site/index):before', function () {
$this->enqueueStyle('redemapas-home', 'redemapas-home-css', 'css/home.css');
$this->enqueueScript('redemapas-home', 'redemapas-home-js', 'js/home.js');
$this->enqueueScript('redemapas-home', 'redemapas-pwa-launcher', 'js/push-notifications.js');
$this->bodyClasses[] = 'redemapas-home';
});
$app->hook('view.render(<<*>>):before', function () {
$this->enqueueScript('app-v2', 'redemapas-pwa-launcher', 'js/push-notifications.js');
});
$app->hook('mapas.printJsObject:before', function () use ($theme) {
$config = $theme->getPushClientConfig();
$config['strings'] = [
'enable' => \MapasCulturais\i::__('Ativar notificações'),
'enabled' => \MapasCulturais\i::__('Notificações ativadas'),
'blocked' => \MapasCulturais\i::__('Notificações bloqueadas'),
'unsupported' => \MapasCulturais\i::__('Notificações não suportadas'),
'unavailable' => \MapasCulturais\i::__('Notificações indisponíveis'),
'installApp' => \MapasCulturais\i::__('Instalar aplicativo'),
];
$this->jsObject['redemapasPush'] = $config;
});
$app->hook('GET(site.webmanifest)', function () use ($theme) {
/** @var \MapasCulturais\Controller $this */
$this->json($theme->getWebmanifestData());
});
$app->hook('template(<<*>>.head):end', function () use ($theme) {
$theme->printPwaHeadTags();
});
$app->hook('entity(Notification).insert:after', function () use ($app) {
/** @var Notification $this */
if (($app->config['redemapas.push.enabled'] ?? false) && $this->user) {
$app->enqueueJob(SendWebPushNotification::SLUG, ['notification' => $this]);
}
});
}
function register()
{
parent::register();
$app = App::i();
if (!$app->getRegisteredJobType(SendWebPushNotification::SLUG)) {
$app->registerJobType(new SendWebPushNotification(SendWebPushNotification::SLUG));
}
}
public static function buildWebmanifestData(
string $siteName,
string $siteDescription,
string $startUrl,
string $icon192,
string $icon512,
string $wideScreenshot = '',
string $mobileScreenshot = '',
string $themeColor = '#0f172a',
string $backgroundColor = '#ffffff'
): array {
return WebmanifestBuilder::build(
siteName: $siteName,
siteDescription: $siteDescription,
startUrl: $startUrl,
icon192: $icon192,
icon512: $icon512,
wideScreenshot: $wideScreenshot,
mobileScreenshot: $mobileScreenshot,
themeColor: $themeColor,
backgroundColor: $backgroundColor
);
}
public static function buildPwaHeadTags(
string $siteName,
string $manifestUrl,
string $appleTouchIcon,
string $themeColor = '#0f172a'
): array {
return HeadTagsBuilder::build(
siteName: $siteName,
manifestUrl: $manifestUrl,
appleTouchIcon: $appleTouchIcon,
themeColor: $themeColor
);
}
public function getWebmanifestData(): array
{
$app = App::i();
$startUrl = rtrim($app->baseUrl, '/') ?: '/';
return self::buildWebmanifestData(
siteName: (string) $app->siteName,
siteDescription: (string) $app->siteDescription,
startUrl: $startUrl,
icon192: $this->asset($app->config['favicon.192'], false),
icon512: $this->asset($app->config['favicon.512'], false),
wideScreenshot: $this->asset('img/home/home-circuits/circuits.jpg', false),
mobileScreenshot: $this->asset('img/home/home-main-header/banner.png', false)
);
}
public static function buildPushClientConfig(
bool $enabled,
string $publicKey,
string $subscribeUrl,
string $unsubscribeUrl,
string $serviceWorkerUrl
): array {
return PushConfigBuilder::buildClientConfig(
enabled: $enabled,
publicKey: $publicKey,
subscribeUrl: $subscribeUrl,
unsubscribeUrl: $unsubscribeUrl,
serviceWorkerUrl: $serviceWorkerUrl
);
}
public function getPushClientConfig(): array
{
$app = App::i();
return self::buildPushClientConfig(
enabled: (bool) ($app->config['redemapas.push.enabled'] ?? false),
publicKey: (string) ($app->config['redemapas.push.vapid.publicKey'] ?? ''),
subscribeUrl: $app->createUrl('push', 'subscribe'),
unsubscribeUrl: $app->createUrl('push', 'unsubscribe'),
serviceWorkerUrl: '/sw.js'
);
}
public function printPwaHeadTags(): void
{
$app = App::i();
$tags = self::buildPwaHeadTags(
siteName: (string) $app->siteName,
manifestUrl: $app->createUrl('site', 'webmanifest'),
appleTouchIcon: $this->asset($app->config['favicon.180'], false)
);
foreach ($tags['links'] as $linkCfg) {
$attrs = [];
foreach ($linkCfg as $prop => $value) {
$attrs[] = sprintf('%s="%s"', $prop, htmlentities((string) $value));
}
echo "\n<link " . implode(' ', $attrs) . ">";
}
foreach ($tags['metas'] as $metaCfg) {
$attrs = [];
foreach ($metaCfg as $prop => $value) {
$attrs[] = sprintf('%s="%s"', $prop, htmlentities((string) $value));
}
echo "\n<meta " . implode(' ', $attrs) . " />";
}
}
}