Completed
Push — patch_1-1-4 ( 567711...ded5b7 )
by Emanuele
17s
created

Notify_Controller::pre_dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains just the functions that turn on and off notifications
5
 * to topics or boards.
6
 *
7
 * @name      ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
10
 *
11
 * This file contains code covered by:
12
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
13
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
14
 *
15
 * @version 1.1.4
16
 *
17
 */
18
19
/**
20
 * Notify_Controller Class
21
 * Functions that turn on and off various member notifications
22
 */
23
class Notify_Controller extends Action_Controller
24
{
25
	/**
26
	 * Pre Dispatch, called before other methods, used to load common needs.
27
	 */
28
	public function pre_dispatch()
29
	{
30
		// Our topic functions are here
31
		require_once(SUBSDIR . '/Topic.subs.php');
32
	}
33
34
	/**
35
	 * Dispatch to the right action.
36
	 *
37
	 * @see Action_Controller::action_index()
38
	 */
39
	public function action_index()
40
	{
41
		// forward to our respective method.
42
		// $this->action_notify();
43
	}
44
45
	/**
46
	 * Turn off/on notification for a particular topic.
47
	 *
48
	 * What it does:
49
	 *
50
	 * - Must be called with a topic specified in the URL.
51
	 * - The sub-action can be 'on', 'off', or nothing for what to do.
52
	 * - Requires the mark_any_notify permission.
53
	 * - Upon successful completion of action will direct user back to topic.
54
	 * - Accessed via ?action=notify.
55
	 *
56
	 * @uses Notify.template, main sub-template
57
	 */
58 View Code Duplication
	public function action_notify()
59
	{
60
		global $topic, $scripturl, $txt, $user_info, $context;
61
62
		// Make sure they aren't a guest or something - guests can't really receive notifications!
63
		is_not_guest();
64
		isAllowedTo('mark_any_notify');
65
66
		// Make sure the topic has been specified.
67
		if (empty($topic))
68
			throw new Elk_Exception('not_a_topic', false);
69
70
		// What do we do?  Better ask if they didn't say..
71
		if (empty($this->_req->query->sa))
72
		{
73
			// Load the template, but only if it is needed.
74
			loadTemplate('Notify');
75
76
			// Find out if they have notification set for this topic already.
77
			$context['notification_set'] = hasTopicNotification($user_info['id'], $topic);
78
79
			// Set the template variables...
80
			$context['topic_href'] = $scripturl . '?topic=' . $topic . '.' . $this->_req->query->start;
81
			$context['start'] = $this->_req->query->start;
82
			$context['page_title'] = $txt['notifications'];
83
			$context['sub_template'] = 'notification_settings';
84
85
			return true;
86
		}
87
		else
88
		{
89
			checkSession('get');
90
91
			$this->_toggle_topic_notification();
92
		}
93
94
		// Send them back to the topic.
95
		redirectexit('topic=' . $topic . '.' . $this->_req->query->start);
96
97
		return true;
98
	}
99
100
	/**
101
	 * Turn off/on notifications for a particular topic
102
	 *
103
	 * - Intended for use in XML or JSON calls
104
	 */
105
	public function action_notify_api()
106
	{
107
		global $topic, $txt, $scripturl, $context, $user_info;
108
109
		loadTemplate('Xml');
110
111
		Template_Layers::instance()->removeAll();
112
		$context['sub_template'] = 'generic_xml_buttons';
113
114
		// Even with Ajax, guests still can't do this
115 View Code Duplication
		if ($user_info['is_guest'])
116
		{
117
			loadLanguage('Errors');
118
			$context['xml_data'] = array(
119
				'error' => 1,
120
				'text' => $txt['not_guests']
121
			);
122
123
			return;
124
		}
125
126
		// And members still need the right permissions
127 View Code Duplication
		if (!allowedTo('mark_any_notify') || empty($topic) || empty($this->_req->query->sa))
128
		{
129
			loadLanguage('Errors');
130
			$context['xml_data'] = array(
131
				'error' => 1,
132
				'text' => $txt['cannot_mark_any_notify']
133
			);
134
135
			return;
136
		}
137
138
		// And sessions still matter, so you better have a valid one
139 View Code Duplication
		if (checkSession('get', '', false))
140
		{
141
			loadLanguage('Errors');
142
			$context['xml_data'] = array(
143
				'error' => 1,
144
				'url' => $scripturl . '?action=notify;sa=' . ($this->_req->query->sa === 'on' ? 'on' : 'off') . ';topic=' . $topic . '.' . $this->_req->query->start . ';' . $context['session_var'] . '=' . $context['session_id'],
145
			);
146
			return;
147
		}
148
149
		$this->_toggle_topic_notification();
150
151
		// Return the results so the UI can be updated properly
152
		$context['xml_data'] = array(
153
			'text' => $this->_req->query->sa === 'on' ? $txt['unnotify'] : $txt['notify'],
154
			'url' => $scripturl . '?action=notify;sa=' . ($this->_req->query->sa === 'on' ? 'off' : 'on') . ';topic=' . $topic . '.' . $this->_req->query->start . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api',
155
			'confirm' => $this->_req->query->sa === 'on' ? $txt['notification_disable_topic'] : $txt['notification_enable_topic']
156
		);
157
	}
158
159
	/**
160
	 * Toggle a topic notification on/off
161
	 */
162
	private function _toggle_topic_notification()
163
	{
164
		global $user_info, $topic;
165
166
		// Attempt to turn notifications on/off.
167
		setTopicNotification($user_info['id'], $topic, $this->_req->query->sa === 'on');
168
	}
169
170
	/**
171
	 * Turn off/on notification for a particular board.
172
	 *
173
	 * What it does:
174
	 *
175
	 * - Must be called with a board specified in the URL.
176
	 * - Only uses the template if no sub action is used. (on/off)
177
	 * - Requires the mark_notify permission.
178
	 * - Redirects the user back to the board after it is done.
179
	 * - Accessed via ?action=notifyboard.
180
	 *
181
	 * @uses template_notify_board() sub-template in Notify.template
182
	 */
183 View Code Duplication
	public function action_notifyboard()
184
	{
185
		global $scripturl, $txt, $board, $user_info, $context;
186
187
		// Permissions are an important part of anything ;).
188
		is_not_guest();
189
		isAllowedTo('mark_notify');
190
191
		// You have to specify a board to turn notifications on!
192
		if (empty($board))
193
			throw new Elk_Exception('no_board', false);
194
195
		// No subaction: find out what to do.
196
		if (empty($this->_req->query->sa))
197
		{
198
			// We're gonna need the notify template...
199
			loadTemplate('Notify');
200
201
			// Find out if they have notification set for this board already.
202
			$context['notification_set'] = hasBoardNotification($user_info['id'], $board);
203
204
			// Set the template variables...
205
			$context['board_href'] = $scripturl . '?board=' . $board . '.' . $this->_req->query->start;
206
			$context['start'] = $this->_req->query->start;
207
			$context['page_title'] = $txt['notifications'];
208
			$context['sub_template'] = 'notify_board';
209
210
			return;
211
		}
212
		// Turn the board level notification on/off?
213
		else
214
		{
215
			checkSession('get');
216
217
			// Turn notification on/off for this board.
218
			$this->_toggle_board_notification();
219
		}
220
221
		// Back to the board!
222
		redirectexit('board=' . $board . '.' . $this->_req->query->start);
223
	}
224
225
	/**
226
	 * Turn off/on notification for a particular board.
227
	 *
228
	 * - Intended for use in XML or JSON calls
229
	 * - Performs the same actions as action_notifyboard but provides ajax responses
230
	 */
231
	public function action_notifyboard_api()
232
	{
233
		global $scripturl, $txt, $board, $user_info, $context;
234
235
		loadTemplate('Xml');
236
237
		Template_Layers::instance()->removeAll();
238
		$context['sub_template'] = 'generic_xml_buttons';
239
240
		// Permissions are an important part of anything ;).
241 View Code Duplication
		if ($user_info['is_guest'])
242
		{
243
			loadLanguage('Errors');
244
			$context['xml_data'] = array(
245
				'error' => 1,
246
				'text' => $txt['not_guests']
247
			);
248
249
			return;
250
		}
251
252
		// Have to have provided the right information
253 View Code Duplication
		if (!allowedTo('mark_notify') || empty($board) || empty($this->_req->query->sa))
254
		{
255
			loadLanguage('Errors');
256
			$context['xml_data'] = array(
257
				'error' => 1,
258
				'text' => $txt['cannot_mark_notify'],
259
			);
260
261
			return;
262
		}
263
264
		// Sessions are always verified
265 View Code Duplication
		if (checkSession('get', '', false))
266
		{
267
			loadLanguage('Errors');
268
			$context['xml_data'] = array(
269
				'error' => 1,
270
				'url' => $scripturl . '?action=notifyboard;sa=' . ($this->_req->query->sa === 'on' ? 'on' : 'off') . ';board=' . $board . '.' . $this->_req->query->start . ';' . $context['session_var'] . '=' . $context['session_id'],
271
			);
272
273
			return;
274
		}
275
276
		$this->_toggle_board_notification();
277
278
		$context['xml_data'] = array(
279
			'text' => $this->_req->query->sa === 'on' ? $txt['unnotify'] : $txt['notify'],
280
			'url' => $scripturl . '?action=notifyboard;sa=' . ($this->_req->query->sa === 'on' ? 'off' : 'on') . ';board=' . $board . '.' . $this->_req->query->start . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : ''),
281
			'confirm' => $this->_req->query->sa === 'on' ? $txt['notification_disable_board'] : $txt['notification_enable_board']
282
		);
283
	}
284
285
	/**
286
	 * Toggle a board notification on/off
287
	 */
288
	private function _toggle_board_notification()
289
	{
290
		global $user_info, $board;
291
292
		// Our board functions are here
293
		require_once(SUBSDIR . '/Boards.subs.php');
294
295
		// Turn notification on/off for this board.
296
		setBoardNotification($user_info['id'], $board, $this->_req->query->sa === 'on');
297
	}
298
299
	/**
300
	 * Turn off/on unread replies subscription for a topic
301
	 *
302
	 * What it does:
303
	 *
304
	 * - Must be called with a topic specified in the URL.
305
	 * - The sub-action can be 'on', 'off', or nothing for what to do.
306
	 * - Requires the mark_any_notify permission.
307
	 * - Upon successful completion of action will direct user back to topic.
308
	 * - Accessed via ?action=unwatchtopic.
309
	 */
310
	public function action_unwatchtopic()
311
	{
312
		global $user_info, $topic, $modSettings;
313
314
		is_not_guest();
315
316
		// Let's do something only if the function is enabled
317
		if (!$user_info['is_guest'] && !empty($modSettings['enable_unwatch']))
318
		{
319
			checkSession('get');
320
321
			$this->_toggle_topic_watch();
322
		}
323
324
		// Back to the topic.
325
		redirectexit('topic=' . $topic . '.' . $this->_req->query->start);
326
	}
327
328
	/**
329
	 * Turn off/on unread replies subscription for a topic
330
	 *
331
	 * - Intended for use in XML or JSON calls
332
	 */
333
	public function action_unwatchtopic_api()
334
	{
335
		global $user_info, $topic, $modSettings, $txt, $context, $scripturl;
336
337
		loadTemplate('Xml');
338
339
		Template_Layers::instance()->removeAll();
340
		$context['sub_template'] = 'generic_xml_buttons';
341
342
		// Sorry guests just can't do this
343 View Code Duplication
		if ($user_info['is_guest'])
344
		{
345
			loadLanguage('Errors');
346
			$context['xml_data'] = array(
347
				'error' => 1,
348
				'text' => $txt['not_guests']
349
			);
350
351
			return;
352
		}
353
354
		// Let's do something only if the function is enabled
355
		if (empty($modSettings['enable_unwatch']))
356
		{
357
			loadLanguage('Errors');
358
			$context['xml_data'] = array(
359
				'error' => 1,
360
				'text' => $txt['feature_disabled'],
361
			);
362
363
			return;
364
		}
365
366
		// Sessions need to be validated
367 View Code Duplication
		if (checkSession('get', '', false))
368
		{
369
			loadLanguage('Errors');
370
			$context['xml_data'] = array(
371
				'error' => 1,
372
				'url' => $scripturl . '?action=unwatchtopic;sa=' . ($this->_req->query->sa === 'on' ? 'on' : 'off') . ';topic=' . $topic . '.' . $this->_req->query->start . ';' . $context['session_var'] . '=' . $context['session_id'],
373
			);
374
375
			return;
376
		}
377
378
		$this->_toggle_topic_watch();
379
380
		$context['xml_data'] = array(
381
			'text' => $this->_req->query->sa === 'on' ? $txt['watch'] : $txt['unwatch'],
382
			'url' => $scripturl . '?action=unwatchtopic;topic=' . $context['current_topic'] . '.' . $this->_req->query->start . ';sa=' . ($this->_req->query->sa === 'on' ? 'off' : 'on') . ';' . $context['session_var'] . '=' . $context['session_id'] . ';api' . (isset($_REQUEST['json']) ? ';json' : ''),
383
		);
384
	}
385
386
	/**
387
	 * Toggle a watch topic on/off
388
	 */
389
	private function _toggle_topic_watch()
390
	{
391
		global $user_info, $topic;
392
393
		setTopicWatch($user_info['id'], $topic, $this->_req->query->sa === 'on');
394
	}
395
}
396