Completed
Push — master ( 3853fd...e6bfe7 )
by Marc
9s
created

calendar   D

Complexity

Total Complexity 124

Size/Duplication

Total Lines 734
Duplicated Lines 6.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.37%

Importance

Changes 0
Metric Value
dl 48
loc 734
ccs 352
cts 377
cp 0.9337
rs 4.4444
c 0
b 0
f 0
wmc 124
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
F get_template_side() 0 193 48
A get_template_acp() 0 16 1
A install() 0 13 1
A uninstall() 0 23 1
F manage_events() 48 208 58
A update_events() 0 4 1
A make_timestamp() 0 5 1
A validate_url() 0 20 3
A date_to_time() 0 4 1
C get_month() 0 43 8

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like calendar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use calendar, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
*
4
* @package Board3 Portal v2.1
5
* @copyright (c) 2014 Board3 Group ( www.board3.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace board3\portal\modules;
11
12
/**
13
* @package Calendar
14
*/
15
class calendar extends module_base
16
{
17
	/**
18
	* Allowed columns: Just sum up your options (Exp: left + right = 10)
19
	* top		1
20
	* left		2
21
	* center	4
22
	* right		8
23
	* bottom	16
24
	*/
25
	public $columns = 10;
26
27
	/**
28
	* Default modulename
29
	*/
30
	public $name = 'PORTAL_CALENDAR';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_calendar.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_calendar_module';
43
44
	/**
45
	* custom acp template
46
	* file must be in "adm/style/portal/"
47
	*/
48
	public $custom_acp_tpl = 'acp_portal_calendar';
49
50
	/**
51
	* additional variables
52
	*/
53
	protected $mini_cal_fdow;
54
	protected $mini_cal_month;
55
56
	/**
57
	* User datetime object
58
	*/
59
	protected $time;
60
61
	/**
62
	* constants
63
	*/
64
	const TIME_DAY = 86400;
65
	const DAYS_PER_WEEK = 6; // indexes start at 0
66
	const MONTHS_PER_YEAR = 12;
67
68
	/** @var int year in numeric format (YYYY) */
69
	protected $dateYYYY;
70
71
	/** @var int month in numeric format (MM) */
72
	protected $dateMM;
73
74
	/** @var int day in numeric format (DD) */
75
	protected $dateDD;
76
77
	/** @var string extended month (e.g. February) */
78
	protected $ext_dateMM;
79
80
	/** @var int count of days in month */
81
	protected $daysMonth;
82
83
	/** @var int timestamp */
84
	protected $stamp;
85
86
	/** @var array return array s.a. */
87
	protected $day;
88
89
	/** @var \phpbb\config\config */
90
	protected $config;
91
92
	/** @var \phpbb\template\template */
93
	protected $template;
94
95
	/** @var \phpbb\db\driver\driver_interface */
96
	protected $db;
97
98
	/** @var \phpbb\request\request_interface */
99
	protected $request;
100
101
	/** @var string PHP file extension */
102
	protected $php_ext;
103
104
	/** @var string phpBB root path */
105
	protected $phpbb_root_path;
106
107
	/** @var \phpbb\user */
108
	protected $user;
109
110
	/** @var \phpbb\path_helper */
111
	protected $path_helper;
112
113
	/** @var string Portal root path */
114
	protected $portal_root_path;
115
116
	/** @var \phpbb\log\log phpBB log */
117
	protected $log;
118
119
	/** @var \board3\portal\includes\modules_helper */
120
	protected $modules_helper;
121
122
	/**
123
	* Construct a calendar object
124
	*
125
	* @param \phpbb\config\config $config phpBB config
126
	* @param \board3\portal\includes\modules_helper $modules_helper Modules helper
127
	* @param \phpbb\template\template $template phpBB template
128
	* @param \phpbb\db\driver\driver_interface $db Database driver
129
	* @param \phpbb\request\request_interface $request phpBB request
130
	* @param string $phpEx php file extension
131
	* @param string $phpbb_root_path phpBB root path
132
	* @param \phpbb\user $user phpBB user object
133
	* @param \phpbb\path_helper $path_helper phpBB path helper
134
	* @param \phpbb\log\log $log phpBB log object
135
	*/
136 19
	public function __construct($config, $modules_helper, $template, $db, $request, $phpbb_root_path, $phpEx, $user, $path_helper, $log)
137
	{
138 19
		$this->config = $config;
139 19
		$this->modules_helper = $modules_helper;
140 19
		$this->template = $template;
141 19
		$this->db = $db;
142 19
		$this->request = $request;
143 19
		$this->php_ext = $phpEx;
144 19
		$this->phpbb_root_path = $phpbb_root_path;
145 19
		$this->user = $user;
146 19
		$this->path_helper = $path_helper;
147 19
		$this->log = $log;
148 19
		$this->portal_root_path = $this->path_helper->get_web_root_path() . $this->phpbb_root_path . 'ext/board3/portal/';
149 19
	}
150
151
	/**
152
	* {@inheritdoc}
153
	*/
154 9
	public function get_template_side($module_id)
155
	{
156 1
		$portal_config = obtain_portal_config();
157
158
		// 0 = Sunday first - 1 = Monday first. ;-)
159 1
		if ($this->config['board3_sunday_first_' . $module_id])
160 1
		{
161 1
			$this->mini_cal_fdow = 0;
162 1
		}
163
		else
164
		{
165 1
			$this->mini_cal_fdow = 1;
166
		}
167
168
		// get the calendar month
169 1
		$this->mini_cal_month = 0;
170 1
		if ($this->request->is_set('m' . $module_id))
171 1
		{
172 1
			$this->mini_cal_month = $this->request->variable('m' . $module_id, 0);
173 1
		}
174
175
		// initialise some variables
176 1
		$this->time = $this->user->create_datetime();
177 1
		$now = phpbb_gmgetdate($this->time->getTimestamp() + $this->time->getOffset());
178 1
		$today_timestamp = $now[0];
179 1
		$mini_cal_today = date('Ymd', $today_timestamp);
180 1
		$this->stamp = (int) $today_timestamp;
181 1
		$s_cal_month = ($this->mini_cal_month != 0) ? $this->mini_cal_month . ' month' : $mini_cal_today;
182 1
		$this->get_month($s_cal_month);
183 1
		$mini_cal_count = $this->mini_cal_fdow;
184 1
		$mini_cal_this_year = $this->dateYYYY;
185 1
		$mini_cal_this_month = $this->dateMM;
186 1
		$mini_cal_month_days = $this->daysMonth;
187
188
		// output our general calendar bits
189 1
		$down = $this->mini_cal_month - 1;
190 1
		$up = $this->mini_cal_month + 1;
191 1
		$prev_month = '<a href="' . $this->modules_helper->route('board3_portal_controller') . "?m$module_id=$down#minical$module_id" . '" rel="nofollow"><span class="portal-arrow-left-icon" title="' . $this->user->lang['VIEW_PREVIOUS_MONTH'] . '"></span></a>';
192 1
		$next_month = '<a href="' . $this->modules_helper->route('board3_portal_controller') . "?m$module_id=$up#minical$module_id" . '" rel="nofollow"><span class="portal-arrow-right-icon" title="' . $this->user->lang['VIEW_NEXT_MONTH'] . '"></span></a>';
193
194 1
		$this->template->assign_block_vars('minical', array(
195 1
			'S_SUNDAY_FIRST'	=> ($this->config['board3_sunday_first_' . $module_id]) ? true : false,
196 9
			'L_MINI_CAL_MONTH'	=> (($this->config['board3_long_month_' . $module_id]) ? $this->user->lang['mini_cal']['long_month'][$this->day[0][1]] : $this->user->lang['mini_cal']['month'][$this->day[0][1]]) . " " . $this->day[0][2],
197 1
			'L_MINI_CAL_SUN'	=> '<span style="color: ' . $this->config['board3_calendar_sunday_color_' . $module_id] . ';">' . $this->user->lang['mini_cal']['day'][1] . '</span>',
198 1
			'L_MINI_CAL_MON'	=> $this->user->lang['mini_cal']['day'][2],
199 1
			'L_MINI_CAL_TUE'	=> $this->user->lang['mini_cal']['day'][3],
200 1
			'L_MINI_CAL_WED'	=> $this->user->lang['mini_cal']['day'][4],
201 1
			'L_MINI_CAL_THU'	=> $this->user->lang['mini_cal']['day'][5],
202 1
			'L_MINI_CAL_FRI'	=> $this->user->lang['mini_cal']['day'][6],
203 1
			'L_MINI_CAL_SAT'	=> $this->user->lang['mini_cal']['day'][7],
204 1
			'U_PREV_MONTH'		=> $prev_month,
205 1
			'U_NEXT_MONTH'		=> $next_month,
206 1
			'S_DISPLAY_EVENTS'	=> ($this->config['board3_display_events_' . $module_id]) ? true : false,
207 1
			'MODULE_ID'			=> $module_id,
208 1
		));
209
210
		// output the days for the current month
211 1
		for ($i = 0; $i < $mini_cal_month_days;)
212
		{
213
			// is this the first day of the week?
214 1
			if ($mini_cal_count == $this->mini_cal_fdow)
215 1
			{
216 1
				$this->template->assign_block_vars('minical.mini_cal_row', array(
217 1
					'MODULE_ID'		=> $module_id,
218 1
				));
219 1
			}
220
221
			// is this a valid weekday?
222 1
			if ($mini_cal_count == ($this->day[$i][3]))
223 1
			{
224 1
				$mini_cal_this_day = $this->day[$i][0];
225
226 1
				$d_mini_cal_today = $mini_cal_this_year . (($mini_cal_this_month <= 9) ? '0' . $mini_cal_this_month : $mini_cal_this_month) . (($mini_cal_this_day <= 9) ? '0' . $mini_cal_this_day : $mini_cal_this_day);
227 1
				$mini_cal_day = ($mini_cal_today == $d_mini_cal_today) ? '<span style="font-weight: bold; color: ' . $this->config['board3_calendar_today_color_' . $module_id] . ';">' . $mini_cal_this_day . '</span>' : $mini_cal_this_day;
228
229 1
				$this->template->assign_block_vars('minical.mini_cal_row.mini_cal_days', array(
230 1
					'MINI_CAL_DAY'		=> ($mini_cal_count == 0) ? '<span style="color: ' . $this->config['board3_calendar_sunday_color_' . $module_id] . ';">' . $mini_cal_day . '</span>' : $mini_cal_day)
231 1
				);
232 1
				$i++;
233 1
			}
234
			// no day
235
			else
236
			{
237 1
				$this->template->assign_block_vars('minical.mini_cal_row.mini_cal_days', array(
238 1
					'MINI_CAL_DAY'		=> ' ')
239 1
				);
240
			}
241
242
			// is this the last day of the week?
243 1
			if ($mini_cal_count == self::DAYS_PER_WEEK)
244 1
			{
245
				// if so then reset the count
246 1
				$mini_cal_count = 0;
247 1
			}
248
			else
249
			{
250
				// otherwise increment the count
251 1
				$mini_cal_count++;
252
			}
253 1
		}
254
255
		// fill table with empty strings
256 1
		while ($mini_cal_count <= self::DAYS_PER_WEEK)
257
		{
258 1
			$this->template->assign_block_vars('minical.mini_cal_row.mini_cal_days', array(
259 1
				'MINI_CAL_DAY'		=> ' ')
260 1
			);
261 1
			$mini_cal_count++;
262 1
		}
263
264
		/*
265
		* Let's start displaying the events
266
		* make sure we only display events in the future
267
		*/
268 1
		$events = json_decode($portal_config['board3_calendar_events_' . $module_id], true);
269
270 1
		if (!empty($events) && $this->config['board3_display_events_' . $module_id])
271 1
		{
272 1
			$time_ary = array();
273
			// we sort the $events array by the start time
274 1
			foreach ($events as $key => $cur_event)
275
			{
276 1
				$time_ary[$key] = $cur_event['start_time'];
277 1
			}
278 1
			array_multisort($time_ary, SORT_NUMERIC, $events);
279
280 1
			$groups_ary = get_user_groups();
281
282 1
			foreach ($events as $key => $cur_event)
283
			{
284 1
				$cur_event['start_time'] = (int) $cur_event['start_time'];
285 1
				$cur_event['end_time'] = (int) $cur_event['end_time'];
286
287 1
				if (($cur_event['start_time'] + $this->time->getOffset()) >= $today_timestamp ||
288 1
					($cur_event['end_time'] + $this->time->getOffset()) >= $today_timestamp ||
289 1
					(($cur_event['start_time'] + $this->time->getOffset() + self::TIME_DAY) >= $today_timestamp && $cur_event['all_day']))
290 1
				{
291 1
					$cur_permissions = explode(',', $cur_event['permission']);
292 1
					$permission_check = array_intersect($groups_ary, $cur_permissions);
293
294 1
					if (!empty($permission_check) || $cur_event['permission'] == '')
295 1
					{
296
						// check if this is an external link
297 1
						if (isset($cur_event['url']) && strpos($cur_event['url'], generate_board_url()) === false)
298 1
						{
299 1
							$is_external = true;
300 1
						}
301
						else
302
						{
303 1
							$is_external = false;
304
						}
305
306
						/**
307
						* Current events
308
						*
309
						* Events are treated as current if the following is met:
310
						* - We have an all day event and the start of that event is less than 1 day (86400 seconds) away
311
						* - We have a normal event with a start that is less then 1 day away and that hasn't ended yet
312
						*/
313 1
						if ((($cur_event['start_time'] + $this->time->getOffset() - $today_timestamp) <= self::TIME_DAY && $cur_event['all_day']) ||
314 1
						(($cur_event['start_time'] + $this->time->getOffset() - $today_timestamp) <= self::TIME_DAY && ($cur_event['end_time'] + $this->time->getOffset()) >= $today_timestamp))
315 1
						{
316 1
							$this->template->assign_block_vars('minical.cur_events', array(
317 1
								'EVENT_URL'		=> (isset($cur_event['url']) && $cur_event['url'] != '') ? $this->validate_url($cur_event['url']) : '',
318 1
								'EVENT_TITLE'	=> $cur_event['title'],
319 1
								'START_TIME'	=> $this->user->format_date($cur_event['start_time']),
320 1
								'END_TIME'		=> (!empty($cur_event['end_time'])) ? $this->user->format_date($cur_event['end_time']) : false,
321 1
								'EVENT_DESC'	=> (isset($cur_event['desc']) && $cur_event['desc'] != '') ? $cur_event['desc'] : '',
322 1
								'ALL_DAY'	=> ($cur_event['all_day']) ? true : false,
323 1
								'MODULE_ID'		=> $module_id,
324 1
								'EVENT_URL_NEW_WINDOW'	=> ($is_external && $this->config['board3_events_url_new_window_' . $module_id]) ? true : false,
325 1
							));
326 1
						}
327
						else
328
						{
329 1
							$this->template->assign_block_vars('minical.upcoming_events', array(
330 1
								'EVENT_URL'		=> (isset($cur_event['url']) && $cur_event['url'] != '') ? $this->validate_url($cur_event['url']) : '',
331 1
								'EVENT_TITLE'	=> $cur_event['title'],
332 1
								'START_TIME'	=> $this->user->format_date($cur_event['start_time']),
333 1
								'END_TIME'		=> (!$cur_event['all_day']) ? $this->user->format_date($cur_event['end_time']) : '',
334 1
								'EVENT_DESC'	=> (isset($cur_event['desc']) && $cur_event['desc'] != '') ? $cur_event['desc'] : '',
335 1
								'ALL_DAY'	=> (($cur_event['start_time'] - $cur_event['end_time']) == 1) ? true : false,
336 1
								'MODULE_ID'		=> $module_id,
337 1
								'EVENT_URL_NEW_WINDOW'	=> ($is_external && $this->config['board3_events_url_new_window_' . $module_id]) ? true : false,
338 1
							));
339
						}
340 1
					}
341 1
				}
342 1
			}
343 1
		}
344
345 1
		return 'calendar_side.html';
346
	}
347
348
	/**
349
	* {@inheritdoc}
350
	*/
351 1
	public function get_template_acp($module_id)
352
	{
353
		return array(
354 1
			'title'	=> 'ACP_PORTAL_CALENDAR',
355
			'vars'	=> array(
356 1
				'legend1'								=> 'ACP_PORTAL_CALENDAR',
357 1
				'board3_calendar_today_color_' . $module_id		=> array('lang' => 'PORTAL_CALENDAR_TODAY_COLOR'	 ,	'validate' => 'string', 'type' => 'text:10:10',	 'explain' => true),
358 1
				'board3_calendar_sunday_color_' . $module_id	=> array('lang' => 'PORTAL_CALENDAR_SUNDAY_COLOR' ,	'validate' => 'string', 'type' => 'text:10:10',	 'explain' => true),
359 1
				'board3_long_month_' . $module_id				=> array('lang' => 'PORTAL_LONG_MONTH' ,	'validate' => 'bool',	'type' => 'radio:yes_no',	 'explain' => true),
360 1
				'board3_sunday_first_' . $module_id				=> array('lang' => 'PORTAL_SUNDAY_FIRST' ,	'validate' => 'bool',	'type' => 'radio:yes_no',	 'explain' => true),
361 1
				'board3_display_events_' . $module_id			=> array('lang' => 'PORTAL_DISPLAY_EVENTS',	'validate' => 'bool',	'type' => 'radio:yes_no',	 'explain' => true),
362 1
				'board3_events_url_new_window_' . $module_id	=> array('lang' => 'PORTAL_EVENTS_URL_NEW_WINDOW', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
363 1
				'board3_events_' . $module_id					=> array('lang' => 'PORTAL_EVENTS_MANAGE', 'validate' => 'string',	'type' => 'custom',	'explain' => false, 'method' => 'manage_events', 'submit' => 'update_events'),
364 1
			),
365 1
		);
366
	}
367
368
	/**
369
	* {@inheritdoc}
370
	*/
371 1
	public function install($module_id)
372
	{
373 1
		$this->config->set('board3_sunday_first_' . $module_id, 1);
374 1
		$this->config->set('board3_calendar_today_color_' . $module_id, '#000000');
375 1
		$this->config->set('board3_calendar_sunday_color_' . $module_id, '#FF0000');
376 1
		$this->config->set('board3_long_month_' . $module_id, 0);
377 1
		$this->config->set('board3_display_events_' . $module_id, 0);
378 1
		$this->config->set('board3_events_' . $module_id, '');
379 1
		$this->config->set('board3_events_url_new_window_' . $module_id, 0);
380
381 1
		set_portal_config('board3_calendar_events_' . $module_id, '');
382 1
		return true;
383
	}
384
385
	/**
386
	* {@inheritdoc}
387
	*/
388 1
	public function uninstall($module_id, $db)
389
	{
390
		$del_config = array(
391 1
			'board3_calendar_events_' . $module_id,
392 1
		);
393 1
		$sql = 'DELETE FROM ' . PORTAL_CONFIG_TABLE . '
394 1
			WHERE ' . $db->sql_in_set('config_name', $del_config);
395
396 1
		$db->sql_query($sql);
397
398
		$del_config = array(
399 1
			'board3_sunday_first_' . $module_id,
400 1
			'board3_calendar_today_color_' . $module_id,
401 1
			'board3_calendar_sunday_color_' . $module_id,
402 1
			'board3_long_month_' . $module_id,
403 1
			'board3_display_events_' . $module_id,
404 1
			'board3_events_' . $module_id,
405 1
			'board3_events_url_new_window_' . $module_id,
406 1
		);
407 1
		$sql = 'DELETE FROM ' . CONFIG_TABLE . '
408 1
			WHERE ' . $db->sql_in_set('config_name', $del_config);
409 1
		return $db->sql_query($sql);
410
	}
411
412
	/**
413
	* Manage events
414
	*
415
	* @param mixed $value Value of input
416
	* @param string $key Key name
417
	* @param int $module_id Module ID
418
	*
419
	* @return null
420
	*/
421 12
	public function manage_events($value, $key, $module_id)
422
	{
423 11
		$action = $this->request->variable('action', '');
424 11
		$action = ($this->request->is_set_post('add')) ? 'add' : $action;
425 11
		$action = ($this->request->is_set_post('save')) ? 'save' : $action;
426 11
		$link_id = $this->request->variable('id', 99999999); // 0 will trigger unwanted behavior, therefore we set a number we should never reach
427 11
		$portal_config = obtain_portal_config();
428
429 12
		$events = (strlen($portal_config['board3_calendar_events_' . $module_id]) >= 1) ? json_decode($portal_config['board3_calendar_events_' . $module_id], true) : array();
430
431
		// append_sid() adds adm/ already, no need to add it here
432 11
		$u_action = append_sid('index.' . $this->php_ext, 'i=-board3-portal-acp-portal_module&amp;mode=config&amp;module_id=' . $module_id);
433
434
		switch ($action)
435
		{
436
			// Save changes
437 11
			case 'save':
438 8 View Code Duplication
				if (!check_form_key('acp_portal'))
439 9
				{
440 1
					trigger_error($this->user->lang['FORM_INVALID']. adm_back_link($u_action), E_USER_WARNING);
441
				}
442
443 7
				$event_title = $this->request->variable('event_title', '', true);
444 7
				$event_desc = $this->request->variable('event_desc', '', true);
445 7
				$event_start_date = trim($this->request->variable('event_start_date', ''));
446 7
				$event_end_date = trim($this->request->variable('event_end_date', ''));
447 7
				$event_all_day = $this->request->variable('event_all_day', false); // default to false
448 7
				$event_url = $this->request->variable('event_url', '');
449 7
				$event_permission = $this->request->variable('permission-setting-calendar', array(0 => ''));
450 7
				$groups_ary = array();
451
452
				// Now get the unix timestamps out of the entered information
453 7
				$start_time = $this->date_to_time($event_start_date);
454 7
				$end_time = (!$event_all_day) ? $this->date_to_time($event_end_date) : '';
455
456 7
				if (!$start_time)
457 7
				{
458 1
					trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_START_INCORRECT']. adm_back_link($u_action), E_USER_WARNING);
459
				}
460 6 View Code Duplication
				else if (!$event_all_day && !$end_time)
461 6
				{
462 1
					trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_END_INCORRECT']. adm_back_link($u_action), E_USER_WARNING);
463
				}
464
465 5
				if (($end_time) <= time() && !(($start_time + self::TIME_DAY) >= time() && $event_all_day))
466 5
				{
467 1
					trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_EVENT_PAST']. adm_back_link($u_action), E_USER_WARNING);
468
				}
469 4 View Code Duplication
				else if ($end_time < $start_time && !$event_all_day)
470 4
				{
471 1
					trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_EVENT_START_FIRST']. adm_back_link($u_action), E_USER_WARNING);
472
				}
473
474
				// get groups and check if the selected groups actually exist
475
				$sql = 'SELECT group_id
476 3
						FROM ' . GROUPS_TABLE . '
477 3
						ORDER BY group_id ASC';
478 3
				$result = $this->db->sql_query($sql);
479 3
				while ($row = $this->db->sql_fetchrow($result))
480
				{
481 3
					$groups_ary[] = $row['group_id'];
482 3
				}
483 3
				$this->db->sql_freeresult($result);
484
485 3
				$event_permission = array_intersect($event_permission, $groups_ary);
486 3
				$event_permission = implode(',', $event_permission);
487
488
				// Check for errors
489 3
				if (!$event_title)
490 3
				{
491 1
					trigger_error($this->user->lang['NO_EVENT_TITLE'] . adm_back_link($u_action), E_USER_WARNING);
492
				}
493
494
				// overwrite already existing events and make sure we don't try to save an event outside of the normal array size of $events
495 2
				if (isset($link_id) && $link_id < sizeof($events))
496 2
				{
497 1
					$message = $this->user->lang['EVENT_UPDATED'];
498
499 1
					$events[$link_id] = array(
500 1
						'title' 		=> $event_title,
501 1
						'desc'			=> $event_desc,
502 1
						'start_time'	=> $start_time,
503 1
						'end_time'		=> $end_time,
504 1
						'all_day'		=> $event_all_day,
505 1
						'permission'	=> $event_permission,
506 1
						'url'			=> htmlspecialchars_decode($event_url),
507
					);
508
509 1
					$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_UPDATED', false, array($event_title));
510 1
				}
511
				else
512
				{
513 1
					$message = $this->user->lang['EVENT_ADDED'];
514
515 1
					$events[] = array(
516 1
						'title'			=> $event_title,
517 1
						'desc'			=> $event_desc,
518 1
						'start_time'	=> $start_time,
519 1
						'end_time'		=> $end_time,
520 1
						'all_day'		=> $event_all_day,
521 1
						'permission'	=> $event_permission,
522 1
						'url'			=> $event_url, // optional
523
					);
524 1
					$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_ADDED', false, array($event_title));
525
				}
526
527 2
				$time_ary = array();
528
				// we sort the $events array by the start time
529 2
				foreach ($events as $key => $cur_event)
530
				{
531 2
					$time_ary[$key] = $cur_event['start_time'];
532 2
				}
533 2
				array_multisort($time_ary, SORT_NUMERIC, $events);
534 2
				$board3_events_array = json_encode($events);
535 2
				set_portal_config('board3_calendar_events_' . $module_id, $board3_events_array);
536
537 2
				trigger_error($message . adm_back_link($u_action));
538
539
			break;
540
541
			// Delete link
542 3 View Code Duplication
			case 'delete':
543
544
				if (!isset($link_id) && $link_id >= sizeof($events))
545
				{
546
					trigger_error($this->user->lang['NO_EVENT'] . adm_back_link($u_action), E_USER_WARNING);
547
				}
548
549
				if (confirm_box(true))
550
				{
551
					$cur_event_title = $events[$link_id]['title'];
552
					// delete the selected link and reset the array numbering afterwards
553
					array_splice($events, $link_id, 1);
554
					$events = array_merge($events);
555
556
					$board3_events_array = json_encode($events);
557
					set_portal_config('board3_calendar_events_' . $module_id, $board3_events_array);
558
559
					$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_REMOVED', false, array($cur_event_title));
560
				}
561
				else
562
				{
563
					confirm_box(false, $this->user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
564
						'link_id'	=> $link_id,
565
						'action'	=> 'delete',
566
					)));
567
				}
568
569
			break;
570
571
			// Edit or add menu item
572 3
			case 'edit':
573 3
			case 'add':
574 1
				$event_all_day = (isset($events[$link_id]['all_day']) && $events[$link_id]['all_day'] == true) ? true : false;
575 1
				$date_format = str_replace(array('D '), '', $this->user->data['user_dateformat']);
576
577 1
				$this->template->assign_vars(array(
578 1
					'EVENT_TITLE'		=> (isset($events[$link_id]['title']) && $action != 'add') ? $events[$link_id]['title'] : '',
579 1
					'EVENT_DESC'		=> (isset($events[$link_id]['desc']) && $action != 'add') ? $events[$link_id]['desc'] : '',
580 1
					'EVENT_START_DATE'	=> ($action != 'add') ? $this->user->format_date($events[$link_id]['start_time'], $date_format) : '',
581 1
					'EVENT_END_DATE'	=> ($action != 'add' && !$event_all_day) ? $this->user->format_date($events[$link_id]['end_time'], $date_format) : '',
582 1
					'EVENT_ALL_DAY'		=> (isset($events[$link_id]['all_day']) && $events[$link_id]['all_day'] == true) ? true : false,
583 1
					'EVENT_URL'			=> (isset($events[$link_id]['url']) && $action != 'add') ? $events[$link_id]['url'] : '',
584
585
					//'U_BACK'	=> $u_action,
586 1
					'B3P_U_ACTION'	=> $u_action . '&amp;id=' . $link_id,
587
588 1
					'S_EDIT'				=> true,
589 1
				));
590
591 1
				$groups_ary = (isset($events[$link_id]['permission'])) ? explode(',', $events[$link_id]['permission']) : array();
592
593
				// get group info from database and assign the block vars
594
				$sql = 'SELECT group_id, group_name
595 1
						FROM ' . GROUPS_TABLE . '
596 1
						ORDER BY group_id ASC';
597 1
				$result = $this->db->sql_query($sql);
598 1 View Code Duplication
				while ($row = $this->db->sql_fetchrow($result))
599
				{
600 1
					$this->template->assign_block_vars('permission_setting_calendar', array(
601 1
						'SELECTED'		=> (in_array($row['group_id'], $groups_ary)) ? true : false,
602 1
						'GROUP_NAME'	=> (isset($this->user->lang['G_' . $row['group_name']])) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name'],
603 1
						'GROUP_ID'		=> $row['group_id'],
604 1
					));
605 1
				}
606 1
				$this->db->sql_freeresult($result);
607
608 1
				return;
609
		}
610
611 2
		for ($i = 0; $i < sizeof($events); $i++)
612
		{
613 1
			$event_all_day = ($events[$i]['all_day'] == true) ? true : false;
614
615 1
			$this->template->assign_block_vars('events', array(
616 1
				'EVENT_TITLE'	=> ($action != 'add') ? ((isset($this->user->lang[$events[$i]['title']])) ? $this->user->lang[$events[$i]['title']] : $events[$i]['title']) : '',
617 1
				'EVENT_DESC'	=> ($action != 'add') ? $events[$i]['desc'] : '',
618 1
				'EVENT_START'	=> ($action != 'add') ? $this->user->format_date($events[$i]['start_time']) : '',
619 1
				'EVENT_END'		=> ($action != 'add' && !$event_all_day && !empty($end_time_format)) ? $this->user->format_date($events[$i]['end_time']) : '',
620 1
				'EVENT_URL'		=> ($action != 'add' && isset($events[$i]['url']) && !empty($events[$i]['url'])) ? $this->validate_url($events[$i]['url']) : '',
621 1
				'EVENT_URL_RAW'	=> ($action != 'add' && isset($events[$i]['url']) && !empty($events[$i]['url'])) ? $events[$i]['url'] : '',
622 1
				'U_EDIT'		=> $u_action . '&amp;action=edit&amp;id=' . $i,
623 1
				'U_DELETE'		=> $u_action . '&amp;action=delete&amp;id=' . $i,
624 1
				'EVENT_ALL_DAY'	=> $event_all_day,
625 1
			));
626 1
		}
627
628 2
	}
629
630
	/**
631
	* Update events
632
	*
633
	* @param string $key Key name
634
	* @param int $module_id Module ID
635
	*
636
	* @return null
637
	*/
638 11
	public function update_events($key, $module_id)
639
	{
640 11
		$this->manage_events('', $key, $module_id);
641 3
	}
642
643
	/**
644
	* Convert date->timestamp to time
645
	*
646
	* @param string $date Date to convert
647
	*
648
	* @return int Converted time
649
	*/
650 1
	protected function make_timestamp($date)
651
	{
652 1
		$this->stamp = strtotime($date);
653 1
		return $this->stamp;
654
	}
655
656
	/**
657
	* Get date listed in array
658
	*
659
	* @param string $call_date Date
660
	*
661
	* @return null
662
	*/
663 1
	protected function get_month($call_date)
664
	{
665 1
		$this->make_timestamp($call_date);
666
		// last or first day of some months need to be treated in a special way
667 1
		if (!empty($this->mini_cal_month))
668 1
		{
669 1
			$time = $this->user->create_datetime();
670 1
			$now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset());
671 1
			$today_timestamp = $now[0];
672 1
			$cur_month = date("n", $today_timestamp);
673 1
			$correct_month = $cur_month + $this->mini_cal_month;
674
675
			// move back or forth the correct number of years
676 1
			while ($correct_month < 1 || $correct_month > self::MONTHS_PER_YEAR)
677
			{
678 1
				$correct_month = ($correct_month < 1) ? $correct_month + self::MONTHS_PER_YEAR : $correct_month - self::MONTHS_PER_YEAR;
679 1
			}
680
681
			// fix incorrect months
682 1
			while (date("n", $this->stamp) != $correct_month)
683
			{
684
				// Go back one day or move forward in order to
685
				// get to the correct month
686 1
				$this->stamp = (date("n", $this->stamp) > $correct_month) ? $this->stamp - self::TIME_DAY : $this->stamp + self::TIME_DAY;
687 1
			}
688 1
		}
689 1
		$this->dateYYYY = (int) date("Y", $this->stamp);
690 1
		$this->dateMM = (int) date("n", $this->stamp);
691 1
		$this->ext_dateMM = date("F", $this->stamp);
692 1
		$this->dateDD = (int) date("d", $this->stamp);
693 1
		$this->daysMonth = (int) date("t", $this->stamp);
694
695 1
		for ($i = 1; $i < $this->daysMonth + 1; $i++)
696
		{
697 1
			$this->make_timestamp("$i {$this->ext_dateMM} {$this->dateYYYY}");
698 1
			$this->day[] = array(
699 1
				'0' => "$i",
700 1
				'1' => $this->dateMM,
701 1
				'2' => $this->dateYYYY,
702 1
				'3' => date('w', $this->stamp)
703 1
				);
704 1
		}
705 1
	}
706
707
	/**
708
	* Validate URLs and execute apppend_sid if necessary
709
	*
710
	* @param string $url URL to process
711
	*
712
	* @return string Processed URL
713
	*/
714 2
	protected function validate_url($url)
715
	{
716 2
		$url = str_replace("\r\n", "\n", str_replace('\"', '"', trim($url)));
717 2
		$url = str_replace(' ', '%20', $url);
718 2
		$url = str_replace('&', '&amp;', $url);
719
720
		// if there is no scheme, then add http schema
721 2
		if (!preg_match('#^[a-z][a-z\d+\-.]*:/{2}#i', $url))
722 2
		{
723 2
			$url = 'http://' . $url;
724 2
		}
725
726
		// Is this a link to somewhere inside this board? If so then run reapply_sid()
727 2
		if (strpos($url, generate_board_url()) !== false)
728 2
		{
729 2
			$url = reapply_sid($url);
730 2
		}
731
732 2
		return $url;
733
	}
734
735
	/**
736
	* Returns timestamp from given date string
737
	*
738
	* @param string $date	Date and time string. It can contain just the
739
	*			date or date and time info. The string should
740
	*			be in a similar format: 17.06.1990 18:06
741
	* @return int|bool	The timestamp of the given date or false if
742
	*			given date does not match any known formats.
743
	*/
744 11
	public function date_to_time($date)
745
	{
746 11
		return strtotime($date);
747
	}
748
}
749