Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ucp_notifications
0.00% covered (danger)
0.00%
0 / 125
0.00% covered (danger)
0.00%
0 / 3
1332
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 1
702
 output_notification_types
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
56
 output_notification_methods
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @ignore
16*/
17
18use phpbb\controller\helper;
19use phpbb\form\form_helper;
20use phpbb\notification\method\extended_method_interface;
21
22if (!defined('IN_PHPBB'))
23{
24    exit;
25}
26
27class ucp_notifications
28{
29    public $u_action;
30
31    private const FORM_TOKEN_NAME = 'ucp_notification';
32
33    /** @var helper */
34    private helper $controller_helper;
35
36    /** @var form_helper */
37    private form_helper $form_helper;
38
39    public function main($id, $mode)
40    {
41        global $config, $template, $user, $request, $phpbb_container, $phpbb_dispatcher;
42        global $phpbb_root_path, $phpEx;
43
44        add_form_key(self::FORM_TOKEN_NAME);
45
46        $start = $request->variable('start', 0);
47        $form_time = $request->variable('form_time', 0);
48        $form_time = ($form_time <= 0 || $form_time > time()) ? time() : $form_time;
49
50        $this->controller_helper = $phpbb_container->get('controller.helper');
51        $this->form_helper = $phpbb_container->get('form_helper');
52
53        /* @var $phpbb_notifications \phpbb\notification\manager */
54        $phpbb_notifications = $phpbb_container->get('notification_manager');
55
56        /* @var $pagination \phpbb\pagination */
57        $pagination = $phpbb_container->get('pagination');
58
59        switch ($mode)
60        {
61            case 'notification_options':
62                $subscriptions = $phpbb_notifications->get_global_subscriptions(false);
63
64                // Add/remove subscriptions
65                if ($request->is_set_post('submit'))
66                {
67                    if (!check_form_key(self::FORM_TOKEN_NAME))
68                    {
69                        trigger_error('FORM_INVALID');
70                    }
71
72                    $notification_methods = $phpbb_notifications->get_subscription_methods();
73
74                    foreach ($phpbb_notifications->get_subscription_types() as $group => $subscription_types)
75                    {
76                        foreach ($subscription_types as $type => $type_data)
77                        {
78                            foreach ($notification_methods as $method => $method_data)
79                            {
80                                $is_set_notify = $request->is_set_post(str_replace('.', '_', $type . '_' . $method_data['id']));
81                                $is_available = $method_data['method']->is_available($type_data['type']);
82
83                                /**
84                                * Event to perform additional actions before ucp_notifications is submitted
85                                *
86                                * @event core.ucp_notifications_submit_notification_is_set
87                                * @var    array    type_data        The notification type data
88                                * @var    array    method_data        The notification method data
89                                * @var    bool    is_set_notify    The notification is set or not
90                                * @var    bool    is_available    The notification is available or not
91                                * @var    array    subscriptions    The subscriptions data
92                                *
93                                * @since 3.2.10-RC1
94                                * @since 3.3.1-RC1
95                                */
96                                $vars = [
97                                    'type_data',
98                                    'method_data',
99                                    'is_set_notify',
100                                    'is_available',
101                                    'subscriptions',
102                                ];
103                                extract($phpbb_dispatcher->trigger_event('core.ucp_notifications_submit_notification_is_set', compact($vars)));
104
105                                if ($is_set_notify && $is_available && (!isset($subscriptions[$type]) || !in_array($method_data['id'], $subscriptions[$type])))
106                                {
107                                    $phpbb_notifications->add_subscription($type, 0, $method_data['id']);
108                                }
109                                else if ((!$is_set_notify || !$is_available) && isset($subscriptions[$type]) && in_array($method_data['id'], $subscriptions[$type]))
110                                {
111                                    $phpbb_notifications->delete_subscription($type, 0, $method_data['id']);
112                                }
113                            }
114                        }
115                    }
116
117                    meta_refresh(3, $this->u_action);
118                    $message = $user->lang['PREFERENCES_UPDATED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
119                    trigger_error($message);
120                }
121
122                $this->output_notification_methods($phpbb_notifications, $template, $user);
123
124                $this->output_notification_types($subscriptions, $phpbb_notifications, $template, $user, $phpbb_dispatcher, 'notification_types');
125
126                $template->assign_vars([
127                    'FORM_TOKENS'            => $this->form_helper->get_form_tokens(self::FORM_TOKEN_NAME),
128                ]);
129
130                $this->tpl_name = 'ucp_notifications_options';
131                $this->page_title = 'UCP_NOTIFICATION_OPTIONS';
132            break;
133
134            case 'notification_list':
135            default:
136                // Mark all items read
137                if ($request->variable('mark', '') == 'all' && check_link_hash($request->variable('token', ''), 'mark_all_notifications_read'))
138                {
139                    $phpbb_notifications->mark_notifications(false, false, $user->data['user_id'], $form_time);
140
141                    meta_refresh(3, $this->u_action);
142                    $message = $user->lang['NOTIFICATIONS_MARK_ALL_READ_SUCCESS'];
143
144                    if ($request->is_ajax())
145                    {
146                        $json_response = new \phpbb\json_response();
147                        $json_response->send(array(
148                            'MESSAGE_TITLE'    => $user->lang['INFORMATION'],
149                            'MESSAGE_TEXT'    => $message,
150                            'success'        => true,
151                        ));
152                    }
153                    $message .= '<br /><br />' . $user->lang('RETURN_UCP', '<a href="' . $this->u_action . '">', '</a>');
154
155                    trigger_error($message);
156                }
157
158                // Mark specific notifications read
159                if ($request->is_set_post('submit'))
160                {
161                    if (!check_form_key(self::FORM_TOKEN_NAME))
162                    {
163                        trigger_error('FORM_INVALID');
164                    }
165
166                    $mark_read = $request->variable('mark', array(0));
167
168                    if (!empty($mark_read))
169                    {
170                        $phpbb_notifications->mark_notifications_by_id('notification.method.board', $mark_read, $form_time);
171                    }
172                }
173
174                $notifications = $phpbb_notifications->load_notifications('notification.method.board', array(
175                    'start'            => $start,
176                    'limit'            => $config['topics_per_page'],
177                    'count_total'    => true,
178                ));
179
180                foreach ($notifications['notifications'] as $notification)
181                {
182                    $template->assign_block_vars('notification_list', $notification->prepare_for_display());
183                }
184
185                $base_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=ucp_notifications&amp;mode=notification_list");
186                $start = $pagination->validate_start($start, $config['topics_per_page'], $notifications['total_count']);
187                $pagination->generate_template_pagination($base_url, 'pagination', 'start', $notifications['total_count'], $config['topics_per_page'], $start);
188
189                $template->assign_vars(array(
190                    'TOTAL_COUNT'    => $notifications['total_count'],
191                    'U_MARK_ALL'    => $base_url . '&amp;mark=all&amp;token=' . generate_link_hash('mark_all_notifications_read'),
192                ));
193
194                $this->tpl_name = 'ucp_notifications';
195                $this->page_title = 'UCP_NOTIFICATION_LIST';
196            break;
197        }
198
199        $template->assign_vars(array(
200            'TITLE'                => $user->lang($this->page_title),
201            'TITLE_EXPLAIN'        => $user->lang($this->page_title . '_EXPLAIN'),
202
203            'MODE'                => $mode,
204
205            'FORM_TIME'            => time(),
206        ));
207    }
208
209    /**
210    * Output all the notification types to the template
211    *
212    * @param array $subscriptions Array containing global subscriptions
213    * @param \phpbb\notification\manager $phpbb_notifications
214    * @param \phpbb\template\template $template
215    * @param \phpbb\user $user
216    * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher
217    * @param string $block
218    */
219    public function output_notification_types($subscriptions, \phpbb\notification\manager $phpbb_notifications, \phpbb\template\template $template, \phpbb\user $user, \phpbb\event\dispatcher_interface $phpbb_dispatcher, $block = 'notification_types')
220    {
221        $notification_methods = $phpbb_notifications->get_subscription_methods();
222
223        foreach ($phpbb_notifications->get_subscription_types() as $group => $subscription_types)
224        {
225            $template->assign_block_vars($block, array(
226                'GROUP_NAME'    => $user->lang($group),
227            ));
228
229            foreach ($subscription_types as $type => $type_data)
230            {
231                $template->assign_block_vars($block, array(
232                    'TYPE'                => $type,
233
234                    'NAME'                => $user->lang($type_data['lang']),
235                    'EXPLAIN'            => (isset($user->lang[$type_data['lang'] . '_EXPLAIN'])) ? $user->lang($type_data['lang'] . '_EXPLAIN') : '',
236                ));
237
238                foreach ($notification_methods as $method => $method_data)
239                {
240                    $tpl_ary = [
241                        'METHOD'            => $method_data['id'],
242                        'NAME'                => $user->lang($method_data['lang']),
243                        'AVAILABLE'            => $method_data['method']->is_available($type_data['type']),
244                        'SUBSCRIBED'        => (isset($subscriptions[$type]) && in_array($method_data['id'], $subscriptions[$type])) ? true : false,
245                    ];
246
247                    /**
248                    * Event to perform additional actions before ucp_notifications is displayed
249                    *
250                    * @event core.ucp_notifications_output_notification_types_modify_template_vars
251                    * @var    array    type_data        The notification type data
252                    * @var    array    method_data        The notification method data
253                    * @var    array    tpl_ary            The template variables
254                    * @var    array    subscriptions    The subscriptions data
255                    *
256                    * @since 3.2.10-RC1
257                    * @since 3.3.1-RC1
258                    */
259                    $vars = [
260                        'type_data',
261                        'method_data',
262                        'tpl_ary',
263                        'subscriptions',
264                    ];
265                    extract($phpbb_dispatcher->trigger_event('core.ucp_notifications_output_notification_types_modify_template_vars', compact($vars)));
266
267                    $template->assign_block_vars($block . '.notification_methods', $tpl_ary);
268                }
269            }
270        }
271
272        $template->assign_vars(array(
273            strtoupper($block) . '_COLS' => count($notification_methods) + 1,
274        ));
275    }
276
277    /**
278    * Output all the notification methods to the template
279    *
280    * @param \phpbb\notification\manager $phpbb_notifications
281    * @param \phpbb\template\template $template
282    * @param \phpbb\user $user
283    * @param string $block
284    */
285    public function output_notification_methods(\phpbb\notification\manager $phpbb_notifications, \phpbb\template\template $template, \phpbb\user $user, $block = 'notification_methods')
286    {
287        $notification_methods = $phpbb_notifications->get_subscription_methods();
288
289        foreach ($notification_methods as $method_data)
290        {
291            if ($method_data['method'] instanceof extended_method_interface)
292            {
293                $ucp_template_data = $method_data['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
294                $template->assign_vars($ucp_template_data);
295            }
296
297            $template->assign_block_vars($block, array(
298                'METHOD'            => $method_data['id'],
299                'NAME'                => $user->lang($method_data['lang']),
300            ));
301        }
302    }
303}