Skip to content

Commit 01c60b5

Browse files
authored
Merge pull request #41 from iMattPro/sub-anywhere
Subscribe Toggle in Notification Dropdown
2 parents 08c7348 + 7fcf10d commit 01c60b5

15 files changed

Lines changed: 207 additions & 43 deletions

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ services:
1616
- '@phpbb.wpn.form_helper'
1717
- '@language'
1818
- '@template'
19+
- '@notification_manager'
1920
tags:
2021
- { name: event.listener }
2122

event/listener.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpbb\controller\helper as controller_helper;
1818
use phpbb\webpushnotifications\form\form_helper;
1919
use phpbb\language\language;
20+
use phpbb\notification\manager;
2021
use phpbb\template\template;
2122

2223
/**
@@ -27,10 +28,10 @@ class listener implements EventSubscriberInterface
2728
public static function getSubscribedEvents()
2829
{
2930
return [
30-
'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data',
3131
'core.ucp_display_module_before' => 'load_language',
3232
'core.acp_main_notice' => 'compatibility_notice',
33-
'core.page_header_after' => 'service_worker_url',
33+
'core.page_header_after' => 'load_template_data',
34+
'core.user_add_modify_notifications_data' => 'add_user_notification_data',
3435
];
3536
}
3637

@@ -46,32 +47,40 @@ public static function getSubscribedEvents()
4647
/* @var template */
4748
protected $template;
4849

50+
/* @var manager */
51+
protected $phpbb_notifications;
52+
4953
/**
5054
* Constructor
5155
*
5256
* @param controller_helper $controller_helper Controller helper object
5357
* @param form_helper $form_helper Form helper object
5458
* @param language $language Language object
5559
* @param template $template Template object
60+
* @param manager $phpbb_notifications Notifications manager object
5661
*/
57-
public function __construct(controller_helper $controller_helper, form_helper $form_helper, language $language, template $template)
62+
public function __construct(controller_helper $controller_helper, form_helper $form_helper, language $language, template $template, manager $phpbb_notifications)
5863
{
5964
$this->controller_helper = $controller_helper;
6065
$this->form_helper = $form_helper;
6166
$this->language = $language;
6267
$this->template = $template;
68+
$this->phpbb_notifications = $phpbb_notifications;
6369
}
6470

6571
/**
6672
* Load template data
67-
*
68-
* @param \phpbb\event\data $event
6973
*/
70-
public function load_template_data($event)
74+
public function load_template_data()
7175
{
72-
if ($event['method_data']['id'] === 'notification.method.phpbb.wpn.webpush')
76+
$methods = $this->phpbb_notifications->get_subscription_methods();
77+
if (array_key_exists('notification.method.phpbb.wpn.webpush', $methods))
7378
{
74-
$template_ary = $event['method_data']['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
79+
if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH'))
80+
{
81+
$this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications');
82+
}
83+
$template_ary = $methods['notification.method.phpbb.wpn.webpush']['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
7584
$this->template->assign_vars($template_ary);
7685
}
7786
}
@@ -93,13 +102,22 @@ public function compatibility_notice()
93102
}
94103

95104
/**
96-
* Generate service worker URL globally for update
105+
* Add default web push notification settings for new users
106+
*
107+
* @param \phpbb\event\data $event The event object
108+
* @return void
97109
*/
98-
public function service_worker_url()
110+
public function add_user_notification_data($event)
99111
{
100-
if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL'))
101-
{
102-
$this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'));
103-
}
112+
$notifications_data = $event['notifications_data'];
113+
$notifications_data[] = [
114+
'item_type' => 'notification.type.quote',
115+
'method' => 'notification.method.phpbb.wpn.webpush',
116+
];
117+
$notifications_data[] = [
118+
'item_type' => 'notification.type.pm',
119+
'method' => 'notification.method.phpbb.wpn.webpush',
120+
];
121+
$event['notifications_data'] = $notifications_data;
104122
}
105123
}

language/en/webpushnotifications_module_ucp.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939

4040
$lang = array_merge($lang, [
4141
'NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH' => 'Web Push',
42+
'NOTIFY_WEBPUSH_ENABLE_SHORT' => 'Enable Web Push notifications',
4243
'NOTIFY_WEBPUSH_ENABLE' => 'Enable receiving Web Push notifications',
4344
'NOTIFY_WEBPUSH_ENABLE_EXPLAIN' => 'Enable receiving browser-based push notifications.<br>The notifications can be turned off at any time in your browser settings, by unsubscribing, or by disabling the push notifications below.',
4445
'NOTIFY_WEBPUSH_SUBSCRIBE' => 'Subscribe',
4546
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Unsubscribe',
47+
'NOTIFY_WEBPUSH_SUBSCRIBED' => 'Subscribed',
4648
]);

language/ru/webpushnotifications_module_ucp.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939

4040
$lang = array_merge($lang, [
4141
'NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH' => 'Браузерные push—уведомления',
42+
'NOTIFY_WEBPUSH_ENABLE_SHORT' => 'Браузерные push—уведомления',
4243
'NOTIFY_WEBPUSH_ENABLE' => 'Включить браузерные push—уведомления',
4344
'NOTIFY_WEBPUSH_ENABLE_EXPLAIN' => 'Включение возможности получения браузерных push—уведомлений.<br>Данная возможность может быть отключена в любое время в настройках браузера, с помощью отписки от браузерных push—уведомлений в личном разделе или путём отключения данной функции ниже.',
4445
'NOTIFY_WEBPUSH_SUBSCRIBE' => 'Подписаться',
4546
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Отписаться',
47+
'NOTIFY_WEBPUSH_SUBSCRIBED' => 'Включено',
4648
]);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
*
4+
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2024, phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\webpushnotifications\migrations;
12+
13+
use phpbb\db\migration\migration;
14+
15+
class update_user_notifications extends migration
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public static function depends_on()
21+
{
22+
return ['\phpbb\webpushnotifications\migrations\add_webpush'];
23+
}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function effectively_installed()
29+
{
30+
$sql = 'SELECT method
31+
FROM ' . $this->table_prefix . "user_notifications
32+
WHERE method = '" . $this->db->sql_escape('notification.method.phpbb.wpn.webpush') . "'";
33+
$result = $this->db->sql_query_limit($sql, 1);
34+
$row = $this->db->sql_fetchrow($result);
35+
$this->db->sql_freeresult($result);
36+
37+
return $row !== false;
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function update_data()
44+
{
45+
return [
46+
['custom', [[$this, 'update_notifications']]],
47+
];
48+
}
49+
50+
/**
51+
* Add default push notifications for users in chunks
52+
*
53+
* @param $start int Start value for the update
54+
* @return int|true Next start value or true if complete
55+
*/
56+
public function update_notifications($start)
57+
{
58+
$start = (int) $start;
59+
$limit = 500;
60+
$updated = 0;
61+
62+
$sql_ary = [];
63+
64+
$sql = 'SELECT user_id
65+
FROM ' . $this->table_prefix . 'users
66+
WHERE user_type <> ' . USER_IGNORE . '
67+
ORDER BY user_id ASC';
68+
$result = $this->db->sql_query_limit($sql, $limit, $start);
69+
70+
while ($row = $this->db->sql_fetchrow($result))
71+
{
72+
$sql_ary[] = [
73+
'item_type' => 'notification.type.pm',
74+
'item_id' => 0,
75+
'user_id' => (int) $row['user_id'],
76+
'notify' => 1,
77+
'method' => 'notification.method.phpbb.wpn.webpush',
78+
];
79+
$sql_ary[] = [
80+
'item_type' => 'notification.type.quote',
81+
'item_id' => 0,
82+
'user_id' => (int) $row['user_id'],
83+
'notify' => 1,
84+
'method' => 'notification.method.phpbb.wpn.webpush',
85+
];
86+
$updated++;
87+
}
88+
$this->db->sql_freeresult($result);
89+
90+
$this->db->sql_multi_insert($this->table_prefix . 'user_notifications', $sql_ary);
91+
92+
return ($updated === $limit) ? $start + $limit : true;
93+
}
94+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if NOTIFICATIONS_WEBPUSH_ENABLE and not notification_types|default and not notification_list|default %}
2+
<div class="wpn-notification-dropdown-footer">
3+
<span class="ellipsis-text">{{ lang('NOTIFY_WEBPUSH_ENABLE_SHORT') ~ lang('COLON') }}</span>
4+
<button id="subscribe_webpush" name="subscribe_webpush"><i class="icon fa-toggle-off fa-fw icon-lightgray"></i><span>{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}</span></button>
5+
<button id="unsubscribe_webpush" name="unsubscribe_webpush" class="hidden"><i class="icon fa-toggle-on fa-fw icon-blue"></i><span>{{ lang('NOTIFY_WEBPUSH_SUBSCRIBED') }}</span></button>
6+
</div>
7+
{% endif %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
2+
{% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %}
3+
{% endif %}

styles/prosilver/template/ucp_notifications_webpush.html renamed to styles/all/template/ucp_notifications_webpush.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
</script>
1919

2020
{% INCLUDEJS '@phpbb_webpushnotifications/webpush.js' %}
21+
{% INCLUDEJS '@phpbb_webpushnotifications/update_worker.js' %}
2122
{% INCLUDECSS '@phpbb_webpushnotifications/phpbb_wpn.css' %}

styles/all/template/update_worker.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3+
/* global phpbbWebpushOptions, domReady */
34
function webpushWorkerUpdate() {
45
if ('serviceWorker' in navigator) {
5-
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
6+
navigator.serviceWorker.getRegistration(phpbbWebpushOptions.serviceWorkerUrl)
67
.then((registration) => {
78
registration.update();
89
})
@@ -13,7 +14,6 @@ function webpushWorkerUpdate() {
1314
}
1415
}
1516
// Do not redeclare function if exist
16-
/* global domReady */
1717
if (typeof domReady === 'undefined') {
1818
window.domReady = function(callBack) {
1919
if (document.readyState === 'loading') {
@@ -25,6 +25,5 @@ if (typeof domReady === 'undefined') {
2525
}
2626

2727
domReady(() => {
28-
/* global serviceWorkerUrl */
2928
webpushWorkerUpdate();
3029
});

styles/all/theme/phpbb_wpn.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.button[disabled],
2+
.button[disabled]:hover,
3+
.button.disabled,
4+
.button.disabled:hover {
5+
background: #e0e0e0;
6+
border-color: #9e9e9e;
7+
color: #9e9e9e;
8+
}
9+
10+
.button.hidden {
11+
display: none;
12+
}
13+
14+
.wpn-notification-dropdown-footer {
15+
font-size: 12px;
16+
white-space: nowrap;
17+
border-top: solid 1px #b9b9b9;
18+
display: flex;
19+
flex-wrap: nowrap;
20+
justify-content: space-between;
21+
padding: 5px 25px;
22+
}
23+
24+
.wpn-notification-dropdown-footer button:disabled {
25+
opacity: 0.7;
26+
}

0 commit comments

Comments
 (0)