ManagePortalShoutbox_Controller::action_edit()   F
last analyzed

Complexity

Conditions 24
Paths 3252

Size

Total Lines 161
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 24
eloc 95
nc 3252
nop 0
dl 0
loc 161
rs 0
c 3
b 1
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * SimplePortal Shoutbox Administration controller class.
15
 *
16
 * - This class handles the administration of the shoutbox
17
 */
18
class ManagePortalShoutbox_Controller extends Action_Controller
19
{
20
	/**
21
	 * This method is executed before any action handler.
22
	 * Loads common things for all methods
23
	 */
24
	public function pre_dispatch()
25
	{
26
		// We'll need the utility functions from here.
27
		require_once(SUBSDIR . '/PortalAdmin.subs.php');
28
		require_once(SUBSDIR . '/Portal.subs.php');
29
		require_once(SUBSDIR . '/PortalShoutbox.subs.php');
30
	}
31
32
	/**
33
	 * Main dispatcher.
34
	 *
35
	 * - This function checks permissions and passes control through.
36
	 */
37
	public function action_index()
38
	{
39
		global $context, $txt;
40
41
		// You must have admin or shoutbox privileges to be here
42
		if (!allowedTo('sp_admin'))
43
		{
44
			isAllowedTo('sp_manage_shoutbox');
45
		}
46
47
		loadTemplate('PortalAdminShoutbox');
48
49
		// The actions allowed in the shoutbox
50
		$subActions = array(
51
			'list' => array($this, 'action_list'),
52
			'add' => array($this, 'action_edit'),
53
			'edit' => array($this, 'action_edit'),
54
			'prune' => array($this, 'action_prune'),
55
			'delete' => array($this, 'action_delete'),
56
			'status' => array($this, 'action_status'),
57
			'blockredirect' => array($this, 'action_block_redirect'),
58
		);
59
60
		// Start up the controller, provide a hook since we can
61
		$action = new Action('portal_shoutbox');
62
63
		$context[$context['admin_menu_name']]['tab_data'] = array(
64
			'title' => $txt['sp_admin_shoutbox_title'],
65
			'help' => 'sp_ShoutboxArea',
66
			'description' => $txt['sp_admin_shoutbox_desc'],
67
			'tabs' => array(
68
				'list' => array(),
69
				'add' => array(),
70
			),
71
		);
72
73
		// Default the action to list if none or no valid option is given
74
		$subAction = $action->initialize($subActions, 'list');
75
		$context['sub_action'] = $subAction;
76
77
		// Shout it on out
78
		$action->dispatch($subAction);
79
	}
80
81
	/**
82
	 * List all the shouts in the system
83
	 *
84
	 * Shout, shout, let it all out, these are the things I can do without
85
	 * Come on, I'm talking to you, come on
86
	 */
87
	public function action_list()
88
	{
89
		global $context, $scripturl, $txt, $modSettings;
90
91
		// Build the listoption array to display the categories
92
		$listOptions = array(
93
			'id' => 'portal_shout',
94
			'title' => $txt['sp_admin_shoutbox_list'],
95
			'items_per_page' => $modSettings['defaultMaxMessages'],
96
			'no_items_label' => $txt['error_sp_no_shoutbox'],
97
			'base_href' => $scripturl . '?action=admin;area=portalshoutbox;sa=list;',
98
			'default_sort_col' => 'name',
99
			'get_items' => array(
100
				'function' => array($this, 'list_spLoadShoutbox'),
101
			),
102
			'get_count' => array(
103
				'function' => array($this, 'list_spCountShoutbox'),
104
			),
105
			'columns' => array(
106
				'name' => array(
107
					'header' => array(
108
						'value' => $txt['sp_admin_shoutbox_col_name'],
109
					),
110
					'data' => array(
111
						'db' => 'name',
112
					),
113
					'sort' => array(
114
						'default' => 'name',
115
						'reverse' => 'name DESC',
116
					),
117
				),
118
				'shouts' => array(
119
					'header' => array(
120
						'value' => $txt['sp_admin_shoutbox_col_shouts'],
121
						'class' => 'centertext',
122
					),
123
					'data' => array(
124
						'db' => 'shouts',
125
						'class' => 'centertext',
126
					),
127
					'sort' => array(
128
						'default' => 'shouts',
129
						'reverse' => 'shouts DESC',
130
					),
131
				),
132
				'caching' => array(
133
					'header' => array(
134
						'value' => $txt['sp_admin_shoutbox_col_caching'],
135
						'class' => 'centertext',
136
					),
137
					'data' => array(
138
						'db' => 'caching',
139
						'class' => 'centertext',
140
					),
141
					'sort' => array(
142
						'default' => 'caching',
143
						'reverse' => 'caching DESC',
144
					),
145
				),
146
				'status' => array(
147
					'header' => array(
148
						'value' => $txt['sp_admin_shoutbox_col_status'],
149
						'class' => 'centertext',
150
					),
151
					'data' => array(
152
						'db' => 'status_image',
153
						'class' => 'centertext',
154
					),
155
					'sort' => array(
156
						'default' => 'status',
157
						'reverse' => 'status DESC',
158
					),
159
				),
160
				'action' => array(
161
					'header' => array(
162
						'value' => $txt['sp_admin_shoutbox_col_actions'],
163
						'class' => 'centertext',
164
					),
165
					'data' => array(
166
						'sprintf' => array(
167
							'format' => '
168
								<a href="?action=admin;area=portalshoutbox;sa=edit;shoutbox_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" accesskey="m">' . sp_embed_image('modify') . '</a>&nbsp;
169
								<a href="?action=admin;area=portalshoutbox;sa=prune;shoutbox_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" accesskey="p">' . sp_embed_image('bin') . '</a>&nbsp;
170
								<a href="?action=admin;area=portalshoutbox;sa=delete;shoutbox_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(' . JavaScriptEscape($txt['sp_admin_shoutbox_delete_confirm']) . ') && submitThisOnce(this);" accesskey="d">' . sp_embed_image('trash') . '</a>',
171
							'params' => array(
172
								'id' => true,
173
							),
174
						),
175
						'class' => 'centertext nowrap',
176
					),
177
				),
178
				'check' => array(
179
					'header' => array(
180
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
181
						'class' => 'centertext',
182
					),
183
					'data' => array(
184
						'function' => function ($row)
185
						{
186
							return '<input type="checkbox" name="remove[]" value="' . $row['id'] . '" class="input_check" />';
187
						},
188
						'class' => 'centertext',
189
					),
190
				),
191
			),
192
			'form' => array(
193
				'href' => $scripturl . '?action=admin;area=portalshoutbox;sa=remove',
194
				'include_sort' => true,
195
				'include_start' => true,
196
				'hidden_fields' => array(
197
					$context['session_var'] => $context['session_id'],
198
				),
199
			),
200
			'additional_rows' => array(
201
				array(
202
					'position' => 'below_table_data',
203
					'value' => '<input type="submit" name="remove_shoutbox" value="' . $txt['sp_admin_shoutbox_remove'] . '" class="right_submit" />',
204
				),
205
			),
206
		);
207
208
		// Set the context values
209
		$context['page_title'] = $txt['sp_admin_shoutbox_title'];
210
		$context['sub_template'] = 'show_list';
211
		$context['default_list'] = 'portal_shout';
212
213
		// Create the list.
214
		require_once(SUBSDIR . '/GenericList.class.php');
215
		createList($listOptions);
216
	}
217
218
	/**
219
	 * Callback for createList(),
220
	 * Returns the number of articles in the system
221
	 */
222
	public function list_spCountShoutbox()
223
	{
224
		return sp_count_shoutbox();
225
	}
226
227
	/**
228
	 * Callback for createList()
229
	 * Returns an array of articles
230
	 *
231
	 * @param int $start
232
	 * @param int $items_per_page
233
	 * @param string $sort
234
	 *
235
	 * @return array
236
	 */
237
	public function list_spLoadShoutbox($start, $items_per_page, $sort)
238
	{
239
		return sp_load_shoutbox($start, $items_per_page, $sort);
240
	}
241
242
	/**
243
	 * Edit an existing shoutbox or add a new one
244
	 */
245
	public function action_edit()
246
	{
247
		global $txt, $context, $modSettings, $editortxt;
248
249
		$context['SPortal']['is_new'] = empty($_REQUEST['shoutbox_id']);
250
251
		if (!empty($_POST['submit']))
252
		{
253
			checkSession();
254
255
			if (!isset($_POST['name']) || Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) === '')
256
			{
257
				throw new Elk_Exception('sp_error_shoutbox_name_empty', false);
258
			}
259
260
			// No two the same
261
			$has_duplicate = sp_check_duplicate_shoutbox($_POST['name'], $_POST['shoutbox_id']);
262
			if (!empty($has_duplicate))
263
			{
264
				throw new Elk_Exception('sp_error_shoutbox_name_duplicate', false);
265
			}
266
267
			if (isset($_POST['moderator_groups']) && is_array($_POST['moderator_groups']) && count($_POST['moderator_groups']) > 0)
268
			{
269
				foreach ($_POST['moderator_groups'] as $id => $group)
270
				{
271
					$_POST['moderator_groups'][$id] = (int) $group;
272
				}
273
274
				$_POST['moderator_groups'] = implode(',', $_POST['moderator_groups']);
275
			}
276
			else
277
			{
278
				$_POST['moderator_groups'] = '';
279
			}
280
281
			if (!empty($_POST['allowed_bbc']) && is_array($_POST['allowed_bbc']))
282
			{
283
				foreach ($_POST['allowed_bbc'] as $id => $tag)
284
				{
285
					$_POST['allowed_bbc'][$id] = Util::htmlspecialchars($tag, ENT_QUOTES);
286
				}
287
288
				$_POST['allowed_bbc'] = implode(',', $_POST['allowed_bbc']);
289
			}
290
			else
291
			{
292
				$_POST['allowed_bbc'] = '';
293
			}
294
295
			$shoutbox_info = array(
296
				'id' => (int) $_POST['shoutbox_id'],
297
				'name' => Util::htmlspecialchars($_POST['name'], ENT_QUOTES),
298
				'permissions' => (int) $_POST['permissions'],
299
				'moderator_groups' => $_POST['moderator_groups'],
300
				'warning' => Util::htmlspecialchars($_POST['warning'], ENT_QUOTES),
301
				'allowed_bbc' => $_POST['allowed_bbc'],
302
				'height' => (int) $_POST['height'],
303
				'num_show' => (int) $_POST['num_show'],
304
				'num_max' => (int) $_POST['num_max'],
305
				'reverse' => !empty($_POST['reverse']) ? 1 : 0,
306
				'caching' => !empty($_POST['caching']) ? 1 : 0,
307
				'refresh' => (int) $_POST['refresh'],
308
				'status' => !empty($_POST['status']) ? 1 : 0,
309
			);
310
311
			// Update existing or add a new shoutbox
312
			$shoutbox_info['id'] = sp_edit_shoutbox($shoutbox_info, $context['SPortal']['is_new']);
313
314
			sportal_update_shoutbox($shoutbox_info['id']);
315
316
			if ($context['SPortal']['is_new'] && (allowedTo(array('sp_admin', 'sp_manage_blocks'))))
317
			{
318
				redirectexit('action=admin;area=portalshoutbox;sa=blockredirect;shoutbox=' . $shoutbox_info['id']);
319
			}
320
			else
321
			{
322
				redirectexit('action=admin;area=portalshoutbox');
323
			}
324
		}
325
326
		if ($context['SPortal']['is_new'])
327
		{
328
			$context['SPortal']['shoutbox'] = array(
329
				'id' => 0,
330
				'name' => $txt['sp_shoutbox_default_name'],
331
				'permissions' => 3,
332
				'moderator_groups' => array(),
333
				'warning' => '',
334
				'allowed_bbc' => array('b', 'i', 'u', 's', 'url', 'code', 'quote', 'me'),
335
				'height' => 200,
336
				'num_show' => 20,
337
				'num_max' => 1000,
338
				'reverse' => 0,
339
				'caching' => 1,
340
				'refresh' => 0,
341
				'status' => 1,
342
			);
343
		}
344
		else
345
		{
346
			$_REQUEST['shoutbox_id'] = (int) $_REQUEST['shoutbox_id'];
347
			$context['SPortal']['shoutbox'] = sportal_get_shoutbox($_REQUEST['shoutbox_id']);
348
		}
349
350
		loadLanguage('Editor');
351
352
		$context['SPortal']['shoutbox']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
353
		sp_loadMemberGroups($context['SPortal']['shoutbox']['moderator_groups'], 'moderator', 'moderator_groups');
354
355
		if (empty($context['SPortal']['shoutbox']['permission_profiles']))
356
		{
357
			throw new Elk_Exception('error_sp_no_permission_profiles', false);
358
		}
359
360
		// We only allow some BBC in the shoutbox
361
		$context['allowed_bbc'] = array(
362
			'b' => $editortxt['Bold'],
363
			'i' => $editortxt['Italic'],
364
			'u' => $editortxt['Underline'],
365
			's' => $editortxt['Strikethrough'],
366
			'pre' => $editortxt['Preformatted Text'],
367
			'img' => $editortxt['Insert an image'],
368
			'url' => $editortxt['Insert a link'],
369
			'email' => $editortxt['Insert an email'],
370
			'sup' => $editortxt['Superscript'],
371
			'sub' => $editortxt['Subscript'],
372
			'tt' => $editortxt['Teletype'],
373
			'code' => $editortxt['Code'],
374
			'quote' => $editortxt['Insert a Quote'],
375
			'size' => $editortxt['Font Size'],
376
			'font' => $editortxt['Font Name'],
377
			'color' => $editortxt['Font Color'],
378
			'me' => 'me',
379
		);
380
381
		// Remove the ones the admin does not allow
382
		$disabled_tags = array();
383
		if (!empty($modSettings['disabledBBC']))
384
		{
385
			$disabled_tags = explode(',', $modSettings['disabledBBC']);
386
		}
387
		if (empty($modSettings['enableEmbeddedFlash']))
388
		{
389
			$disabled_tags[] = 'flash';
390
		}
391
392
		foreach ($disabled_tags as $tag)
393
		{
394
			if ($tag == 'list')
395
			{
396
				$context['disabled_tags']['orderlist'] = true;
397
			}
398
399
			$context['disabled_tags'][trim($tag)] = true;
400
		}
401
402
		$context['page_title'] = $context['SPortal']['is_new']
403
			? $txt['sp_admin_shoutbox_add']
404
			: $txt['sp_admin_shoutbox_edit'];
405
		$context['sub_template'] = 'shoutbox_edit';
406
	}
407
408
	/**
409
	 * Who does not like prunes .. or maybe cut down the shout list
410
	 */
411
	public function action_prune()
412
	{
413
		global $context, $txt;
414
415
		$shoutbox_id = empty($_REQUEST['shoutbox_id']) ? 0 : (int) $_REQUEST['shoutbox_id'];
416
		$context['shoutbox'] = sportal_get_shoutbox($shoutbox_id);
417
418
		if (empty($context['shoutbox']))
419
		{
420
			throw new Elk_Exception('error_sp_shoutbox_not_exist', false);
421
		}
422
423
		// Time to remove some chitta-chatta
424
		if (!empty($_POST['submit']))
425
		{
426
			checkSession();
427
428
			if (!empty($_POST['type']))
429
			{
430
				$where = array('id_shoutbox = {int:shoutbox_id}');
431
				$parameters = array('shoutbox_id' => $shoutbox_id);
432
433
				// Prune by days
434
				if ($_POST['type'] === 'days' && !empty($_POST['days']))
435
				{
436
					$where[] = 'log_time < {int:time_limit}';
437
					$parameters['time_limit'] = time() - $_POST['days'] * 86400;
438
				}
439
				// Or maybe by member
440
				elseif ($_POST['type'] === 'member' && !empty($_POST['member']))
441
				{
442
					$member_id = sp_shoutbox_prune_member($_POST['member']);
443
					if (!empty($member_id))
444
					{
445
						$where[] = 'id_member = {int:member_id}';
446
						$parameters['member_id'] = $member_id;
447
					}
448
				}
449
450
				// Execute the selective prune or clear the entire box
451
				if ($_POST['type'] === 'all' || count($where) > 1)
452
				{
453
					sp_prune_shoutbox($shoutbox_id, $where, $parameters, $_POST['type'] === 'all');
454
					sportal_update_shoutbox($shoutbox_id);
455
				}
456
			}
457
458
			redirectexit('action=admin;area=portalshoutbox');
459
		}
460
461
		loadJavascriptFile('suggest.js');
462
		$context['page_title'] = $txt['sp_admin_shoutbox_prune'];
463
		$context['sub_template'] = 'shoutbox_prune';
464
	}
465
466
	/**
467
	 * Remove a shout from the system
468
	 */
469
	public function action_delete()
470
	{
471
		$shoutbox_ids = array();
472
473
		// Get the page id's to remove
474
		if (!empty($_POST['remove_shoutbox']) && !empty($_POST['remove']) && is_array($_POST['remove']))
475
		{
476
			checkSession();
477
478
			foreach ($_POST['remove'] as $index => $page_id)
479
			{
480
				$shoutbox_ids[(int) $index] = (int) $page_id;
481
			}
482
		}
483
		elseif (!empty($_REQUEST['shoutbox_id']))
484
		{
485
			checkSession('get');
486
			$shoutbox_ids[] = (int) $_REQUEST['shoutbox_id'];
487
		}
488
489
		// If we have some to remove ....
490
		if (!empty($shoutbox_ids))
491
		{
492
			sp_delete_shoutbox($shoutbox_ids);
493
		}
494
495
		redirectexit('action=admin;area=portalshoutbox');
496
	}
497
498
	/**
499
	 * Updates the system status of the shoutout, toggle on/off
500
	 */
501
	public function action_status()
502
	{
503
		checkSession('get');
504
505
		$shoutbox_id = !empty($_REQUEST['shoutbox_id']) ? (int) $_REQUEST['shoutbox_id'] : 0;
506
		sp_changeState('shout', $shoutbox_id);
507
		redirectexit('action=admin;area=portalshoutbox');
508
	}
509
510
	/**
511
	 * Used to gain access to the add a shoutbox block
512
	 */
513
	public function action_block_redirect()
514
	{
515
		global $context, $scripturl, $txt;
516
517
		if (!allowedTo('sp_admin'))
518
		{
519
			isAllowedTo('sp_manage_blocks');
520
		}
521
522
		$context['page_title'] = $txt['sp_admin_shoutbox_add'];
523
		$context['redirect_message'] = sprintf($txt['sp_admin_shoutbox_block_redirect_message'], $scripturl . '?action=admin;area=portalblocks;sa=add;selected_type=Shoutbox;parameters[]=shoutbox;shoutbox=' . $_GET['shoutbox'], $scripturl . '?action=admin;area=portalshoutbox');
524
		$context['sub_template'] = 'shoutbox_block_redirect';
525
	}
526
}
527