/
events.php
68 lines (59 loc) · 2.87 KB
/
events.php
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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Notification;
use Concrete\Core\Notification\Events\MercureService;
use Concrete\Core\Notification\Events\ServerEvent\TestConnection;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Utility\Service\Identifier;
use Symfony\Component\HttpFoundation\JsonResponse;
class Events extends DashboardPageController
{
public function view()
{
$config = $this->app->make("config");
$dbConfig = $this->app->make('config/database');
$enable_server_sent_events = (bool) $config->get('concrete.notification.server_sent_events');
$this->set('enable_server_sent_events', $enable_server_sent_events);
if ($enable_server_sent_events) {
$this->set('publishUrl', $config->get('concrete.notification.mercure.default.publish_url'));
$this->set('jwtKey', $dbConfig->get('concrete.notification.mercure.default.jwt_key'));
}
}
public function test_connection()
{
$ping = $this->request->request->get('ping');
$service = $this->app->make(MercureService::class);
$service->sendUpdate(new TestConnection($ping));
return new JsonResponse([]); // This is just here for our ajax requets, it has nothing to do with mercure
}
public function submit()
{
$enable_server_sent_events = $this->request->request->get("enable_server_sent_events") ? true : false;
if (!$this->token->validate('submit')) {
$this->error->add($this->token->getErrorMessage());
}
if ($enable_server_sent_events) {
}
if (!$this->error->has()) {
$config = $this->app->make('config');
$dbConfig = $this->app->make('config/database');
$events_previously_enabled = (bool) $config->get('concrete.notification.server_sent_events');
if ($enable_server_sent_events) {
if (!$events_previously_enabled) {
// Generate a JWT key.
$jwtKey = $this->app->make(Identifier::class)->getString(96);
$dbConfig->save('concrete.notification.mercure.default.jwt_key', $jwtKey);
}
if ($this->request->request->has('publishUrl')) {
$config->save('concrete.notification.mercure.default.publish_url', (string)
$this->request->request->get('publishUrl'));
}
} else {
$config->save('concrete.notification.mercure', []);
$dbConfig->save('concrete.notification.mercure', []);
}
$config->save('concrete.notification.server_sent_events', $enable_server_sent_events);
$this->flash('success', t("Server-Sent Events Settings updated successfully."));
return $this->redirect('/dashboard/system/notification/events');
}
}
}