1010
1111namespace phpbb \webpushnotifications \event ;
1212
13- /**
14- * @ignore
15- */
16-
1713use FastImageSize \FastImageSize ;
1814use phpbb \config \config ;
1915use phpbb \controller \helper as controller_helper ;
2016use phpbb \language \language ;
2117use phpbb \notification \manager ;
2218use phpbb \template \template ;
19+ use phpbb \user ;
2320use phpbb \webpushnotifications \form \form_helper ;
2421use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2522
@@ -46,6 +43,9 @@ class listener implements EventSubscriberInterface
4643 /* @var template */
4744 protected $ template ;
4845
46+ /** @var user */
47+ protected $ user ;
48+
4949 /* @var manager */
5050 protected $ phpbb_notifications ;
5151
@@ -61,27 +61,29 @@ class listener implements EventSubscriberInterface
6161 * @param form_helper $form_helper Form helper object
6262 * @param language $language Language object
6363 * @param template $template Template object
64+ * @param user $user
6465 * @param manager $phpbb_notifications Notifications manager object
6566 * @param $root_path
6667 */
67- public function __construct (config $ config , controller_helper $ controller_helper , FastImageSize $ imagesize , form_helper $ form_helper , language $ language , template $ template , manager $ phpbb_notifications , $ root_path )
68+ public function __construct (config $ config , controller_helper $ controller_helper , FastImageSize $ imagesize , form_helper $ form_helper , language $ language , template $ template , user $ user , manager $ phpbb_notifications , $ root_path )
6869 {
6970 $ this ->config = $ config ;
7071 $ this ->controller_helper = $ controller_helper ;
7172 $ this ->imagesize = $ imagesize ;
7273 $ this ->form_helper = $ form_helper ;
7374 $ this ->language = $ language ;
7475 $ this ->template = $ template ;
76+ $ this ->user = $ user ;
7577 $ this ->phpbb_notifications = $ phpbb_notifications ;
7678 $ this ->root_path = $ root_path ;
7779 }
7880
7981 public static function getSubscribedEvents ()
8082 {
8183 return [
84+ 'core.page_header_after ' => [['load_template_data ' ], ['pwa_manifest ' ]],
8285 'core.ucp_display_module_before ' => 'load_language ' ,
8386 'core.acp_main_notice ' => 'compatibility_notice ' ,
84- 'core.page_header_after ' => 'load_template_data ' ,
8587 'core.acp_board_config_edit_add ' => 'acp_pwa_options ' ,
8688 'core.validate_config_variable ' => 'validate_pwa_options ' ,
8789 ];
@@ -92,23 +94,30 @@ public static function getSubscribedEvents()
9294 */
9395 public function load_template_data ()
9496 {
97+ if (!$ this ->can_use_notifications ())
98+ {
99+ return ;
100+ }
101+
95102 $ methods = $ this ->phpbb_notifications ->get_subscription_methods ();
96103 $ webpush_method = $ methods ['notification.method.phpbb.wpn.webpush ' ] ?? null ;
97104
98- if ($ webpush_method ! == null )
105+ if ($ webpush_method = == null )
99106 {
100- if (!$ this ->language ->is_set ('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH ' ))
101- {
102- $ this ->language ->add_lang ('webpushnotifications_module_ucp ' , 'phpbb/webpushnotifications ' );
103- }
107+ return ;
108+ }
104109
105- $ template_ary = $ webpush_method ['method ' ]->get_ucp_template_data ($ this ->controller_helper , $ this ->form_helper );
106- $ this ->template ->assign_vars ($ template_ary );
110+ if (!$ this ->language ->is_set ('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH ' ))
111+ {
112+ $ this ->load_language ();
107113 }
114+
115+ $ template_ary = $ webpush_method ['method ' ]->get_ucp_template_data ($ this ->controller_helper , $ this ->form_helper );
116+ $ this ->template ->assign_vars ($ template_ary );
108117 }
109118
110119 /**
111- * Load language file
120+ * Load language file (this is required for the UCP)
112121 */
113122 public function load_language ()
114123 {
@@ -123,6 +132,19 @@ public function compatibility_notice()
123132 $ this ->template ->assign_var ('S_WPN_COMPATIBILITY_NOTICE ' , phpbb_version_compare (PHPBB_VERSION , '4.0.0-dev ' , '>= ' ));
124133 }
125134
135+ /**
136+ * Assign template data for web manifest support
137+ *
138+ * @return void
139+ */
140+ public function pwa_manifest ()
141+ {
142+ $ this ->template ->assign_vars ([
143+ 'U_MANIFEST_URL ' => $ this ->controller_helper ->route ('phpbb_webpushnotifications_manifest_controller ' ),
144+ 'U_TOUCH_ICON ' => $ this ->config ['pwa_icon_small ' ],
145+ ]);
146+ }
147+
126148 /**
127149 * Progressive web app options for the ACP
128150 *
@@ -217,4 +239,16 @@ protected function add_error($event, $error_key, $param = null)
217239 $ error [] = $ this ->language ->lang ($ error_key , $ param );
218240 $ event ['error ' ] = $ error ;
219241 }
242+
243+ /**
244+ * Can notifications be used by the user?
245+ *
246+ * @return bool
247+ */
248+ protected function can_use_notifications ()
249+ {
250+ return $ this ->config ['wpn_webpush_enable ' ]
251+ && ANONYMOUS !== $ this ->user ->id ()
252+ && USER_IGNORE !== (int ) $ this ->user ->data ['user_type ' ];
253+ }
220254}
0 commit comments