Completed
Push — release-2.1 ( aa21c4...7040ad )
by Mathias
09:20
created

ManageCalendar.php ➔ EditHoliday()   D

Complexity

Conditions 15
Paths 56

Size

Total Lines 101
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 56
nc 56
nop 0
dl 0
loc 101
rs 4.9121
c 0
b 0
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
 * This file allows you to manage the calendar.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2017 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * The main controlling function doesn't have much to do... yet.
21
 * Just check permissions and delegate to the rest.
22
 *
23
 * @uses ManageCalendar language file.
24
 */
25
function ManageCalendar()
26
{
27
	global $context, $txt, $modSettings;
28
29
	isAllowedTo('admin_forum');
30
31
	// Everything's gonna need this.
32
	loadLanguage('ManageCalendar');
33
34
	// Default text.
35
	$context['explain_text'] = $txt['calendar_desc'];
36
37
	// Little short on the ground of functions here... but things can and maybe will change...
38
	if (!empty($modSettings['cal_enabled']))
39
	{
40
		$subActions = array(
41
			'editholiday' => 'EditHoliday',
42
			'holidays' => 'ModifyHolidays',
43
			'settings' => 'ModifyCalendarSettings'
44
		);
45
		$default = 'holidays';
46
	}
47
	else
48
	{
49
		$subActions = array(
50
			'settings' => 'ModifyCalendarSettings'
51
		);
52
		$default = 'settings';
53
	}
54
55
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : $default;
56
57
	// Set up the two tabs here...
58
	$context[$context['admin_menu_name']]['tab_data'] = array(
59
		'title' => $txt['manage_calendar'],
60
		'help' => 'calendar',
61
		'description' => $txt['calendar_settings_desc'],
62
	);
63 View Code Duplication
	if (!empty($modSettings['cal_enabled']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
		$context[$context['admin_menu_name']]['tab_data']['tabs'] = array(
65
			'holidays' => array(
66
				'description' => $txt['manage_holidays_desc'],
67
			),
68
			'settings' => array(
69
				'description' => $txt['calendar_settings_desc'],
70
			),
71
		);
72
73
	call_integration_hook('integrate_manage_calendar', array(&$subActions));
74
75
	call_helper($subActions[$_REQUEST['sa']]);
76
}
77
78
/**
79
 * The function that handles adding, and deleting holiday data
80
 */
81
function ModifyHolidays()
82
{
83
	global $sourcedir, $scripturl, $txt, $context, $modSettings;
84
85
	// Submitting something...
86
	if (isset($_REQUEST['delete']) && !empty($_REQUEST['holiday']))
87
	{
88
		checkSession();
89
		validateToken('admin-mc');
90
91
		foreach ($_REQUEST['holiday'] as $id => $value)
92
			$_REQUEST['holiday'][$id] = (int) $id;
93
94
		// Now the IDs are "safe" do the delete...
95
		require_once($sourcedir . '/Subs-Calendar.php');
96
		removeHolidays($_REQUEST['holiday']);
97
	}
98
99
	createToken('admin-mc');
100
	$listOptions = array(
101
		'id' => 'holiday_list',
102
		'title' => $txt['current_holidays'],
103
		'items_per_page' => $modSettings['defaultMaxListItems'],
104
		'base_href' => $scripturl . '?action=admin;area=managecalendar;sa=holidays',
105
		'default_sort_col' => 'name',
106
		'get_items' => array(
107
			'file' => $sourcedir . '/Subs-Calendar.php',
108
			'function' => 'list_getHolidays',
109
		),
110
		'get_count' => array(
111
			'file' => $sourcedir . '/Subs-Calendar.php',
112
			'function' => 'list_getNumHolidays',
113
		),
114
		'no_items_label' => $txt['holidays_no_entries'],
115
		'columns' => array(
116
			'name' => array(
117
				'header' => array(
118
					'value' => $txt['holidays_title'],
119
				),
120
				'data' => array(
121
					'sprintf' => array(
122
						'format' => '<a href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday;holiday=%1$d">%2$s</a>',
123
						'params' => array(
124
							'id_holiday' => false,
125
							'title' => false,
126
						),
127
					),
128
				),
129
				'sort' => array(
130
					'default' => 'title ASC, event_date ASC',
131
					'reverse' => 'title DESC, event_date ASC',
132
				)
133
			),
134
			'date' => array(
135
				'header' => array(
136
					'value' => $txt['date'],
137
				),
138
				'data' => array(
139
					'function' => function ($rowData) use ($txt)
140
					{
141
						// Recurring every year or just a single year?
142
						$year = $rowData['year'] == '1004' ? sprintf('(%1$s)', $txt['every_year']) : $rowData['year'];
143
144
						// Construct the date.
145
						return sprintf('%1$d %2$s %3$s', $rowData['day'], $txt['months'][(int) $rowData['month']], $year);
146
					},
147
				),
148
				'sort' => array(
149
					'default' => 'event_date',
150
					'reverse' => 'event_date DESC',
151
				),
152
			),
153
			'check' => array(
154
				'header' => array(
155
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check">',
156
					'class' => 'centercol',
157
				),
158
				'data' => array(
159
					'sprintf' => array(
160
						'format' => '<input type="checkbox" name="holiday[%1$d]" class="input_check">',
161
						'params' => array(
162
							'id_holiday' => false,
163
						),
164
					),
165
					'class' => 'centercol',
166
				),
167
			),
168
		),
169
		'form' => array(
170
			'href' => $scripturl . '?action=admin;area=managecalendar;sa=holidays',
171
			'token' => 'admin-mc',
172
		),
173
		'additional_rows' => array(
174
			array(
175
				'position' => 'above_column_headers',
176
				'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button_submit">
177
					<a class="button_link" href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday">' . $txt['holidays_add'] . '</a>',
178
			),
179
			array(
180
				'position' => 'below_table_data',
181
				'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button_submit">
182
					<a class="button_link" href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday">' . $txt['holidays_add'] . '</a>',
183
			),
184
		),
185
	);
186
187
	require_once($sourcedir . '/Subs-List.php');
188
	createList($listOptions);
189
190
	//loadTemplate('ManageCalendar');
191
	$context['page_title'] = $txt['manage_holidays'];
192
193
	// Since the list is the only thing to show, use the default list template.
194
	$context['default_list'] = 'holiday_list';
195
	$context['sub_template'] = 'show_list';
196
}
197
198
/**
199
 * This function is used for adding/editing a specific holiday
200
 */
201
function EditHoliday()
202
{
203
	global $txt, $context, $smcFunc;
204
205
	loadTemplate('ManageCalendar');
206
207
	$context['is_new'] = !isset($_REQUEST['holiday']);
208
	$context['page_title'] = $context['is_new'] ? $txt['holidays_add'] : $txt['holidays_edit'];
209
	$context['sub_template'] = 'edit_holiday';
210
211
	// Cast this for safety...
212
	if (isset($_REQUEST['holiday']))
213
		$_REQUEST['holiday'] = (int) $_REQUEST['holiday'];
214
215
	// Submitting?
216
	if (isset($_POST[$context['session_var']]) && (isset($_REQUEST['delete']) || $_REQUEST['title'] != ''))
217
	{
218
		checkSession();
219
220
		// Not too long good sir?
221
		$_REQUEST['title'] =  $smcFunc['substr']($_REQUEST['title'], 0, 60);
222
		$_REQUEST['holiday'] = isset($_REQUEST['holiday']) ? (int) $_REQUEST['holiday'] : 0;
223
224
		if (isset($_REQUEST['delete']))
225
			$smcFunc['db_query']('', '
226
				DELETE FROM {db_prefix}calendar_holidays
227
				WHERE id_holiday = {int:selected_holiday}',
228
				array(
229
					'selected_holiday' => $_REQUEST['holiday'],
230
				)
231
			);
232
		else
233
		{
234
			$date = strftime($_REQUEST['year'] <= 1004 ? '1004-%m-%d' : '%Y-%m-%d', mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']));
235
			if (isset($_REQUEST['edit']))
236
				$smcFunc['db_query']('', '
237
					UPDATE {db_prefix}calendar_holidays
238
					SET event_date = {date:holiday_date}, title = {string:holiday_title}
239
					WHERE id_holiday = {int:selected_holiday}',
240
					array(
241
						'holiday_date' => $date,
242
						'selected_holiday' => $_REQUEST['holiday'],
243
						'holiday_title' => $_REQUEST['title'],
244
					)
245
				);
246
			else
247
				$smcFunc['db_insert']('',
248
					'{db_prefix}calendar_holidays',
249
					array(
250
						'event_date' => 'date', 'title' => 'string-60',
251
					),
252
					array(
253
						$date, $_REQUEST['title'],
254
					),
255
					array('id_holiday')
256
				);
257
		}
258
259
		updateSettings(array(
260
			'calendar_updated' => time(),
261
			'settings_updated' => time(),
262
		));
263
264
		redirectexit('action=admin;area=managecalendar;sa=holidays');
265
	}
266
267
	// Default states...
268
	if ($context['is_new'])
269
		$context['holiday'] = array(
270
			'id' => 0,
271
			'day' => date('d'),
272
			'month' => date('m'),
273
			'year' => '0000',
274
			'title' => ''
275
		);
276
	// If it's not new load the data.
277
	else
278
	{
279
		$request = $smcFunc['db_query']('', '
280
			SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
281
			FROM {db_prefix}calendar_holidays
282
			WHERE id_holiday = {int:selected_holiday}
283
			LIMIT 1',
284
			array(
285
				'selected_holiday' => $_REQUEST['holiday'],
286
			)
287
		);
288
		while ($row = $smcFunc['db_fetch_assoc']($request))
289
			$context['holiday'] = array(
290
				'id' => $row['id_holiday'],
291
				'day' => $row['day'],
292
				'month' => $row['month'],
293
				'year' => $row['year'] <= 4 ? 0 : $row['year'],
294
				'title' => $row['title']
295
			);
296
		$smcFunc['db_free_result']($request);
297
	}
298
299
	// Last day for the drop down?
300
	$context['holiday']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['holiday']['month'] == 12 ? 1 : $context['holiday']['month'] + 1, 0, $context['holiday']['month'] == 12 ? $context['holiday']['year'] + 1 : $context['holiday']['year']));
301
}
302
303
/**
304
 * Show and allow to modify calendar settings. Obviously.
305
 *
306
 * @param bool $return_config Whether to return the $config_vars array (used for admin search)
307
 * @return void|array Returns nothing or returns $config_vars if $return_config is true
308
 */
309
function ModifyCalendarSettings($return_config = false)
310
{
311
	global $context, $txt, $sourcedir, $scripturl, $smcFunc, $modSettings;
312
313
	// Load the boards list.
314
	$boards = array('');
315
	$request = $smcFunc['db_query']('order_by_board_order', '
316
		SELECT b.id_board, b.name AS board_name, c.name AS cat_name
317
		FROM {db_prefix}boards AS b
318
			LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)',
319
		array(
320
		)
321
	);
322 View Code Duplication
	while ($row = $smcFunc['db_fetch_assoc']($request))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
		$boards[$row['id_board']] = $row['cat_name'] . ' - ' . $row['board_name'];
324
	$smcFunc['db_free_result']($request);
325
326
	require_once($sourcedir . '/Subs-Boards.php');
327
	sortBoards($boards);
328
329
	// Look, all the calendar settings - of which there are many!
330
	if (!empty($modSettings['cal_enabled']))
331
		$config_vars = array(
332
				array('check', 'cal_enabled'),
333
			'',
334
				// All the permissions:
335
				array('permissions', 'calendar_view', 'help' => 'cal_enabled'),
336
				array('permissions', 'calendar_post'),
337
				array('permissions', 'calendar_edit_own'),
338
				array('permissions', 'calendar_edit_any'),
339
			'',
340
				// How many days to show on board index, and where to display events etc?
341
				array('select', 'calendar_default_view', array('viewlist' => $txt['setting_cal_viewlist'], 'viewmonth' => $txt['setting_cal_viewmonth'], 'viewweek' => $txt['setting_cal_viewweek'])),
342
				array('int', 'cal_days_for_index', 6, 'postinput' => $txt['days_word']),
343
				array('select', 'cal_showholidays', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
344
				array('select', 'cal_showbdays', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
345
				array('select', 'cal_showevents', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
346
				array('check', 'cal_export'),
347
			'',
348
				// Linking events etc...
349
				array('select', 'cal_defaultboard', $boards),
350
				array('check', 'cal_daysaslink'),
351
				array('check', 'cal_allow_unlinked'),
352
				array('check', 'cal_showInTopic'),
353
			'',
354
				// Dates of calendar...
355
				array('int', 'cal_minyear'),
356
				array('int', 'cal_maxyear'),
357
			'',
358
				// Calendar spanning...
359
				array('int', 'cal_maxspan', 6, 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_for_no_limit']),
360
			'',
361
				// A comment is like a dog marking its territory. ;)
362
				array('select', 'cal_highlight_events', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
363
				array('select', 'cal_highlight_holidays', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
364
				array('select', 'cal_highlight_birthdays', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
365
			'',
366
				// Miscellaneous layout settings...
367
				array('check', 'cal_disable_prev_next'),
368
				array('select', 'cal_display_type', array(0 => $txt['setting_cal_display_comfortable'], 1 => $txt['setting_cal_display_compact'])),
369
				array('select', 'cal_week_links', array(0 => $txt['setting_cal_week_links_none'], 1 => $txt['setting_cal_week_links_mini'], 2 => $txt['setting_cal_week_links_main'], 3 => $txt['setting_cal_week_links_both'])),
370
				array('check', 'cal_prev_next_links'),
371
				array('check', 'cal_short_days'),
372
				array('check', 'cal_short_months'),
373
		);
374
	else
375
		$config_vars = array(
376
			array('check', 'cal_enabled'),
377
		);
378
379
	call_integration_hook('integrate_modify_calendar_settings', array(&$config_vars));
380
	if ($return_config)
381
		return $config_vars;
382
383
	// Get the settings template fired up.
384
	require_once($sourcedir . '/ManageServer.php');
385
386
	// Some important context stuff
387
	$context['page_title'] = $txt['calendar_settings'];
388
	$context['sub_template'] = 'show_settings';
389
390
	// Get the final touches in place.
391
	$context['post_url'] = $scripturl . '?action=admin;area=managecalendar;save;sa=settings';
392
	$context['settings_title'] = $txt['calendar_settings'];
393
394
	// Saving the settings?
395 View Code Duplication
	if (isset($_GET['save']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
396
	{
397
		checkSession();
398
		call_integration_hook('integrate_save_calendar_settings');
399
		saveDBSettings($config_vars);
400
401
		// Update the stats in case.
402
		updateSettings(array(
403
			'calendar_updated' => time(),
404
		));
405
406
		$_SESSION['adm-save'] = true;
407
		redirectexit('action=admin;area=managecalendar;sa=settings');
408
	}
409
410
	// We need this for the inline permissions
411
	createToken('admin-mp');
412
413
	// Prepare the settings...
414
	prepareDBSettingContext($config_vars);
415
}
416
417
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...