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 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 |
||
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 | View Code Duplication | 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 | 9 | 'S_SUNDAY_FIRST' => ($this->config['board3_sunday_first_' . $module_id]) ? true : false, |
|
196 | 1 | '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 | if (($cur_event['start_time'] + $this->time->getOffset()) >= $today_timestamp || |
|
285 | 1 | ($cur_event['end_time'] + $this->time->getOffset()) >= $today_timestamp || |
|
286 | 1 | (($cur_event['start_time'] + $this->time->getOffset() + self::TIME_DAY) >= $today_timestamp && $cur_event['all_day'])) |
|
287 | 1 | { |
|
288 | 1 | $cur_permissions = explode(',', $cur_event['permission']); |
|
289 | 1 | $permission_check = array_intersect($groups_ary, $cur_permissions); |
|
290 | |||
291 | 1 | if (!empty($permission_check) || $cur_event['permission'] == '') |
|
292 | 1 | { |
|
293 | // check if this is an external link |
||
294 | 1 | if (isset($cur_event['url']) && strpos($cur_event['url'], generate_board_url()) === false) |
|
295 | 1 | { |
|
296 | 1 | $is_external = true; |
|
297 | 1 | } |
|
298 | else |
||
299 | { |
||
300 | 1 | $is_external = false; |
|
301 | } |
||
302 | |||
303 | /** |
||
304 | * Current events |
||
305 | * |
||
306 | * Events are treated as current if the following is met: |
||
307 | * - We have an all day event and the start of that event is less than 1 day (86400 seconds) away |
||
308 | * - We have a normal event with a start that is less then 1 day away and that hasn't ended yet |
||
309 | */ |
||
310 | 1 | if ((($cur_event['start_time'] + $this->time->getOffset() - $today_timestamp) <= self::TIME_DAY && $cur_event['all_day']) || |
|
311 | 1 | (($cur_event['start_time'] + $this->time->getOffset() - $today_timestamp) <= self::TIME_DAY && ($cur_event['end_time'] + $this->time->getOffset()) >= $today_timestamp)) |
|
312 | 1 | { |
|
313 | 1 | $this->template->assign_block_vars('minical.cur_events', array( |
|
314 | 1 | 'EVENT_URL' => (isset($cur_event['url']) && $cur_event['url'] != '') ? $this->validate_url($cur_event['url']) : '', |
|
315 | 1 | 'EVENT_TITLE' => $cur_event['title'], |
|
316 | 1 | 'START_TIME' => $this->user->format_date($cur_event['start_time']), |
|
317 | 1 | 'END_TIME' => (!empty($cur_event['end_time'])) ? $this->user->format_date($cur_event['end_time']) : false, |
|
318 | 1 | 'EVENT_DESC' => (isset($cur_event['desc']) && $cur_event['desc'] != '') ? $cur_event['desc'] : '', |
|
319 | 1 | 'ALL_DAY' => ($cur_event['all_day']) ? true : false, |
|
320 | 1 | 'MODULE_ID' => $module_id, |
|
321 | 1 | 'EVENT_URL_NEW_WINDOW' => ($is_external && $this->config['board3_events_url_new_window_' . $module_id]) ? true : false, |
|
322 | 1 | )); |
|
323 | 1 | } |
|
324 | else |
||
325 | { |
||
326 | 1 | $this->template->assign_block_vars('minical.upcoming_events', array( |
|
327 | 1 | 'EVENT_URL' => (isset($cur_event['url']) && $cur_event['url'] != '') ? $this->validate_url($cur_event['url']) : '', |
|
328 | 1 | 'EVENT_TITLE' => $cur_event['title'], |
|
329 | 1 | 'START_TIME' => $this->user->format_date($cur_event['start_time']), |
|
330 | 1 | 'END_TIME' => (!$cur_event['all_day']) ? $this->user->format_date($cur_event['end_time']) : '', |
|
331 | 1 | 'EVENT_DESC' => (isset($cur_event['desc']) && $cur_event['desc'] != '') ? $cur_event['desc'] : '', |
|
332 | 1 | 'ALL_DAY' => (($cur_event['start_time'] - $cur_event['end_time']) == 1) ? true : false, |
|
333 | 1 | 'MODULE_ID' => $module_id, |
|
334 | 1 | 'EVENT_URL_NEW_WINDOW' => ($is_external && $this->config['board3_events_url_new_window_' . $module_id]) ? true : false, |
|
335 | 1 | )); |
|
336 | } |
||
337 | 1 | } |
|
338 | 1 | } |
|
339 | 1 | } |
|
340 | 1 | } |
|
341 | |||
342 | 1 | return 'calendar_side.html'; |
|
343 | } |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | 1 | public function get_template_acp($module_id) |
|
349 | { |
||
350 | return array( |
||
351 | 1 | 'title' => 'ACP_PORTAL_CALENDAR', |
|
352 | 'vars' => array( |
||
353 | 1 | 'legend1' => 'ACP_PORTAL_CALENDAR', |
|
354 | 1 | 'board3_calendar_today_color_' . $module_id => array('lang' => 'PORTAL_CALENDAR_TODAY_COLOR' , 'validate' => 'string', 'type' => 'text:10:10', 'explain' => true), |
|
355 | 1 | 'board3_calendar_sunday_color_' . $module_id => array('lang' => 'PORTAL_CALENDAR_SUNDAY_COLOR' , 'validate' => 'string', 'type' => 'text:10:10', 'explain' => true), |
|
356 | 1 | 'board3_long_month_' . $module_id => array('lang' => 'PORTAL_LONG_MONTH' , 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), |
|
357 | 1 | 'board3_sunday_first_' . $module_id => array('lang' => 'PORTAL_SUNDAY_FIRST' , 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), |
|
358 | 1 | 'board3_display_events_' . $module_id => array('lang' => 'PORTAL_DISPLAY_EVENTS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), |
|
359 | 1 | 'board3_events_url_new_window_' . $module_id => array('lang' => 'PORTAL_EVENTS_URL_NEW_WINDOW', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false), |
|
360 | 1 | 'board3_events_' . $module_id => array('lang' => 'PORTAL_EVENTS_MANAGE', 'validate' => 'string', 'type' => 'custom', 'explain' => false, 'method' => 'manage_events', 'submit' => 'update_events'), |
|
361 | 1 | ), |
|
362 | 1 | ); |
|
363 | } |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | 1 | public function install($module_id) |
|
369 | { |
||
370 | 1 | $this->config->set('board3_sunday_first_' . $module_id, 1); |
|
371 | 1 | $this->config->set('board3_calendar_today_color_' . $module_id, '#000000'); |
|
372 | 1 | $this->config->set('board3_calendar_sunday_color_' . $module_id, '#FF0000'); |
|
373 | 1 | $this->config->set('board3_long_month_' . $module_id, 0); |
|
374 | 1 | $this->config->set('board3_display_events_' . $module_id, 0); |
|
375 | 1 | $this->config->set('board3_events_' . $module_id, ''); |
|
376 | 1 | $this->config->set('board3_events_url_new_window_' . $module_id, 0); |
|
377 | |||
378 | 1 | set_portal_config('board3_calendar_events_' . $module_id, ''); |
|
379 | 1 | return true; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | 1 | public function uninstall($module_id, $db) |
|
386 | { |
||
387 | $del_config = array( |
||
388 | 1 | 'board3_calendar_events_' . $module_id, |
|
389 | 1 | ); |
|
390 | 1 | $sql = 'DELETE FROM ' . PORTAL_CONFIG_TABLE . ' |
|
391 | 1 | WHERE ' . $db->sql_in_set('config_name', $del_config); |
|
392 | |||
393 | 1 | $db->sql_query($sql); |
|
394 | |||
395 | $del_config = array( |
||
396 | 1 | 'board3_sunday_first_' . $module_id, |
|
397 | 1 | 'board3_calendar_today_color_' . $module_id, |
|
398 | 1 | 'board3_calendar_sunday_color_' . $module_id, |
|
399 | 1 | 'board3_long_month_' . $module_id, |
|
400 | 1 | 'board3_display_events_' . $module_id, |
|
401 | 1 | 'board3_events_' . $module_id, |
|
402 | 1 | 'board3_events_url_new_window_' . $module_id, |
|
403 | 1 | ); |
|
404 | 1 | $sql = 'DELETE FROM ' . CONFIG_TABLE . ' |
|
405 | 1 | WHERE ' . $db->sql_in_set('config_name', $del_config); |
|
406 | 1 | return $db->sql_query($sql); |
|
407 | } |
||
408 | |||
409 | /** |
||
410 | * Manage events |
||
411 | * |
||
412 | * @param mixed $value Value of input |
||
413 | * @param string $key Key name |
||
414 | * @param int $module_id Module ID |
||
415 | * |
||
416 | * @return null |
||
417 | */ |
||
418 | 12 | public function manage_events($value, $key, $module_id) |
|
419 | { |
||
420 | 12 | $action = $this->request->variable('action', ''); |
|
421 | 11 | $action = ($this->request->is_set_post('add')) ? 'add' : $action; |
|
422 | 11 | $action = ($this->request->is_set_post('save')) ? 'save' : $action; |
|
423 | 11 | $link_id = $this->request->variable('id', 99999999); // 0 will trigger unwanted behavior, therefore we set a number we should never reach |
|
424 | 11 | $portal_config = obtain_portal_config(); |
|
425 | |||
426 | 11 | $events = (strlen($portal_config['board3_calendar_events_' . $module_id]) >= 1) ? json_decode($portal_config['board3_calendar_events_' . $module_id], true) : array(); |
|
427 | |||
428 | // append_sid() adds adm/ already, no need to add it here |
||
429 | 12 | $u_action = append_sid('index.' . $this->php_ext, 'i=-board3-portal-acp-portal_module&mode=config&module_id=' . $module_id); |
|
430 | |||
431 | switch ($action) |
||
432 | { |
||
433 | // Save changes |
||
434 | 11 | case 'save': |
|
435 | 8 | View Code Duplication | if (!check_form_key('acp_portal')) |
436 | 8 | { |
|
437 | 1 | trigger_error($this->user->lang['FORM_INVALID']. adm_back_link($u_action), E_USER_WARNING); |
|
438 | } |
||
439 | |||
440 | 7 | $event_title = $this->request->variable('event_title', '', true); |
|
441 | 7 | $event_desc = $this->request->variable('event_desc', '', true); |
|
442 | 7 | $event_start_date = trim($this->request->variable('event_start_date', '')); |
|
443 | 7 | $event_end_date = trim($this->request->variable('event_end_date', '')); |
|
444 | 7 | $event_all_day = $this->request->variable('event_all_day', false); // default to false |
|
445 | 7 | $event_url = $this->request->variable('event_url', ''); |
|
446 | 7 | $event_permission = $this->request->variable('permission-setting-calendar', array(0 => '')); |
|
447 | 7 | $groups_ary = array(); |
|
448 | |||
449 | // Now get the unix timestamps out of the entered information |
||
450 | 7 | $start_time = $this->date_to_time($event_start_date); |
|
451 | 7 | $end_time = (!$event_all_day) ? $this->date_to_time($event_end_date) : ''; |
|
452 | |||
453 | 7 | if (!$start_time) |
|
454 | 7 | { |
|
455 | 1 | trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_START_INCORRECT']. adm_back_link($u_action), E_USER_WARNING); |
|
456 | } |
||
457 | 6 | View Code Duplication | else if (!$event_all_day && !$end_time) |
458 | 6 | { |
|
459 | 1 | trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_END_INCORRECT']. adm_back_link($u_action), E_USER_WARNING); |
|
460 | } |
||
461 | |||
462 | 5 | if (($end_time) <= time() && !(($start_time + self::TIME_DAY) >= time() && $event_all_day)) |
|
463 | 5 | { |
|
464 | 1 | trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_EVENT_PAST']. adm_back_link($u_action), E_USER_WARNING); |
|
465 | } |
||
466 | 4 | View Code Duplication | else if ($end_time < $start_time && !$event_all_day) |
467 | 4 | { |
|
468 | 1 | trigger_error($this->user->lang['ACP_PORTAL_CALENDAR_EVENT_START_FIRST']. adm_back_link($u_action), E_USER_WARNING); |
|
469 | } |
||
470 | |||
471 | // get groups and check if the selected groups actually exist |
||
472 | $sql = 'SELECT group_id |
||
473 | 3 | FROM ' . GROUPS_TABLE . ' |
|
474 | 3 | ORDER BY group_id ASC'; |
|
475 | 3 | $result = $this->db->sql_query($sql); |
|
476 | 3 | while ($row = $this->db->sql_fetchrow($result)) |
|
477 | { |
||
478 | 3 | $groups_ary[] = $row['group_id']; |
|
479 | 3 | } |
|
480 | 3 | $this->db->sql_freeresult($result); |
|
481 | |||
482 | 3 | $event_permission = array_intersect($event_permission, $groups_ary); |
|
483 | 3 | $event_permission = implode(',', $event_permission); |
|
484 | |||
485 | // Check for errors |
||
486 | 3 | if (!$event_title) |
|
487 | 3 | { |
|
488 | 1 | trigger_error($this->user->lang['NO_EVENT_TITLE'] . adm_back_link($u_action), E_USER_WARNING); |
|
489 | } |
||
490 | |||
491 | // overwrite already existing events and make sure we don't try to save an event outside of the normal array size of $events |
||
492 | 2 | if (isset($link_id) && $link_id < sizeof($events)) |
|
493 | 2 | { |
|
494 | 1 | $message = $this->user->lang['EVENT_UPDATED']; |
|
495 | |||
496 | 1 | $events[$link_id] = array( |
|
497 | 1 | 'title' => $event_title, |
|
498 | 1 | 'desc' => $event_desc, |
|
499 | 1 | 'start_time' => $start_time, |
|
500 | 1 | 'end_time' => $end_time, |
|
501 | 1 | 'all_day' => $event_all_day, |
|
502 | 1 | 'permission' => $event_permission, |
|
503 | 1 | 'url' => htmlspecialchars_decode($event_url), |
|
504 | ); |
||
505 | |||
506 | 1 | $this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_UPDATED', false, array($event_title)); |
|
507 | 1 | } |
|
508 | else |
||
509 | { |
||
510 | 1 | $message = $this->user->lang['EVENT_ADDED']; |
|
511 | |||
512 | 1 | $events[] = array( |
|
513 | 1 | 'title' => $event_title, |
|
514 | 1 | 'desc' => $event_desc, |
|
515 | 1 | 'start_time' => $start_time, |
|
516 | 1 | 'end_time' => $end_time, |
|
517 | 1 | 'all_day' => $event_all_day, |
|
518 | 1 | 'permission' => $event_permission, |
|
519 | 1 | 'url' => $event_url, // optional |
|
520 | ); |
||
521 | 1 | $this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_ADDED', false, array($event_title)); |
|
522 | } |
||
523 | |||
524 | 2 | $time_ary = array(); |
|
525 | // we sort the $events array by the start time |
||
526 | 2 | foreach ($events as $key => $cur_event) |
|
527 | { |
||
528 | 2 | $time_ary[$key] = $cur_event['start_time']; |
|
529 | 2 | } |
|
530 | 2 | array_multisort($time_ary, SORT_NUMERIC, $events); |
|
531 | 2 | $board3_events_array = json_encode($events); |
|
532 | 2 | set_portal_config('board3_calendar_events_' . $module_id, $board3_events_array); |
|
533 | |||
534 | 2 | trigger_error($message . adm_back_link($u_action)); |
|
535 | |||
536 | break; |
||
537 | |||
538 | // Delete link |
||
539 | 3 | View Code Duplication | case 'delete': |
540 | |||
541 | if (!isset($link_id) && $link_id >= sizeof($events)) |
||
542 | { |
||
543 | trigger_error($this->user->lang['NO_EVENT'] . adm_back_link($u_action), E_USER_WARNING); |
||
544 | } |
||
545 | |||
546 | if (confirm_box(true)) |
||
547 | { |
||
548 | $cur_event_title = $events[$link_id]['title']; |
||
549 | // delete the selected link and reset the array numbering afterwards |
||
550 | array_splice($events, $link_id, 1); |
||
551 | $events = array_merge($events); |
||
552 | |||
553 | $board3_events_array = json_encode($events); |
||
554 | set_portal_config('board3_calendar_events_' . $module_id, $board3_events_array); |
||
555 | |||
556 | $this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_EVENT_REMOVED', false, array($cur_event_title)); |
||
557 | } |
||
558 | else |
||
559 | { |
||
560 | confirm_box(false, $this->user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
561 | 'link_id' => $link_id, |
||
562 | 'action' => 'delete', |
||
563 | ))); |
||
564 | } |
||
565 | |||
566 | break; |
||
567 | |||
568 | // Edit or add menu item |
||
569 | 3 | case 'edit': |
|
570 | 3 | case 'add': |
|
571 | 1 | $event_all_day = (isset($events[$link_id]['all_day']) && $events[$link_id]['all_day'] == true) ? true : false; |
|
572 | 1 | $date_format = str_replace(array('D '), '', $this->user->data['user_dateformat']); |
|
573 | |||
574 | 1 | $this->template->assign_vars(array( |
|
575 | 1 | 'EVENT_TITLE' => (isset($events[$link_id]['title']) && $action != 'add') ? $events[$link_id]['title'] : '', |
|
576 | 1 | 'EVENT_DESC' => (isset($events[$link_id]['desc']) && $action != 'add') ? $events[$link_id]['desc'] : '', |
|
577 | 1 | 'EVENT_START_DATE' => ($action != 'add') ? $this->user->format_date($events[$link_id]['start_time'], $date_format) : '', |
|
578 | 1 | 'EVENT_END_DATE' => ($action != 'add' && !$event_all_day) ? $this->user->format_date($events[$link_id]['end_time'], $date_format) : '', |
|
579 | 1 | 'EVENT_ALL_DAY' => (isset($events[$link_id]['all_day']) && $events[$link_id]['all_day'] == true) ? true : false, |
|
580 | 1 | 'EVENT_URL' => (isset($events[$link_id]['url']) && $action != 'add') ? $events[$link_id]['url'] : '', |
|
581 | |||
582 | //'U_BACK' => $u_action, |
||
583 | 1 | 'B3P_U_ACTION' => $u_action . '&id=' . $link_id, |
|
584 | |||
585 | 1 | 'S_EDIT' => true, |
|
586 | 1 | )); |
|
587 | |||
588 | 1 | $groups_ary = (isset($events[$link_id]['permission'])) ? explode(',', $events[$link_id]['permission']) : array(); |
|
589 | |||
590 | // get group info from database and assign the block vars |
||
591 | $sql = 'SELECT group_id, group_name |
||
592 | 1 | FROM ' . GROUPS_TABLE . ' |
|
593 | 1 | ORDER BY group_id ASC'; |
|
594 | 1 | $result = $this->db->sql_query($sql); |
|
595 | 1 | View Code Duplication | while ($row = $this->db->sql_fetchrow($result)) |
596 | { |
||
597 | 1 | $this->template->assign_block_vars('permission_setting_calendar', array( |
|
598 | 1 | 'SELECTED' => (in_array($row['group_id'], $groups_ary)) ? true : false, |
|
599 | 1 | 'GROUP_NAME' => (isset($this->user->lang['G_' . $row['group_name']])) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name'], |
|
600 | 1 | 'GROUP_ID' => $row['group_id'], |
|
601 | 1 | )); |
|
602 | 1 | } |
|
603 | 1 | $this->db->sql_freeresult($result); |
|
604 | |||
605 | 1 | return; |
|
606 | } |
||
607 | |||
608 | 2 | for ($i = 0; $i < sizeof($events); $i++) |
|
609 | { |
||
610 | 1 | $event_all_day = ($events[$i]['all_day'] == true) ? true : false; |
|
611 | |||
612 | 1 | $this->template->assign_block_vars('events', array( |
|
613 | 1 | 'EVENT_TITLE' => ($action != 'add') ? ((isset($this->user->lang[$events[$i]['title']])) ? $this->user->lang[$events[$i]['title']] : $events[$i]['title']) : '', |
|
614 | 1 | 'EVENT_DESC' => ($action != 'add') ? $events[$i]['desc'] : '', |
|
615 | 1 | 'EVENT_START' => ($action != 'add') ? $this->user->format_date($events[$i]['start_time']) : '', |
|
616 | 1 | 'EVENT_END' => ($action != 'add' && !$event_all_day && !empty($end_time_format)) ? $this->user->format_date($events[$i]['end_time']) : '', |
|
617 | 1 | 'EVENT_URL' => ($action != 'add' && isset($events[$i]['url']) && !empty($events[$i]['url'])) ? $this->validate_url($events[$i]['url']) : '', |
|
618 | 1 | 'EVENT_URL_RAW' => ($action != 'add' && isset($events[$i]['url']) && !empty($events[$i]['url'])) ? $events[$i]['url'] : '', |
|
619 | 1 | 'U_EDIT' => $u_action . '&action=edit&id=' . $i, |
|
620 | 1 | 'U_DELETE' => $u_action . '&action=delete&id=' . $i, |
|
621 | 1 | 'EVENT_ALL_DAY' => $event_all_day, |
|
622 | 1 | )); |
|
623 | 1 | } |
|
624 | |||
625 | 2 | } |
|
626 | |||
627 | /** |
||
628 | * Update events |
||
629 | * |
||
630 | * @param string $key Key name |
||
631 | * @param int $module_id Module ID |
||
632 | * |
||
633 | * @return null |
||
634 | */ |
||
635 | 11 | public function update_events($key, $module_id) |
|
639 | |||
640 | /** |
||
641 | * Convert date->timestamp to time |
||
642 | * |
||
643 | * @param string $date Date to convert |
||
644 | * |
||
645 | * @return int Converted time |
||
646 | */ |
||
647 | 1 | protected function make_timestamp($date) |
|
648 | { |
||
649 | 1 | $this->stamp = strtotime($date); |
|
650 | 1 | return $this->stamp; |
|
651 | } |
||
652 | |||
653 | /** |
||
654 | * Get date listed in array |
||
655 | * |
||
656 | * @param string $call_date Date |
||
657 | * |
||
658 | * @return null |
||
659 | */ |
||
660 | 1 | protected function get_month($call_date) |
|
661 | { |
||
662 | 1 | $this->make_timestamp($call_date); |
|
663 | // last or first day of some months need to be treated in a special way |
||
664 | 1 | if (!empty($this->mini_cal_month)) |
|
665 | 1 | { |
|
666 | 1 | $time = $this->user->create_datetime(); |
|
667 | 1 | $now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset()); |
|
668 | 1 | $today_timestamp = $now[0]; |
|
669 | 1 | $cur_month = date("n", $today_timestamp); |
|
670 | 1 | $correct_month = $cur_month + $this->mini_cal_month; |
|
671 | |||
672 | // move back or forth the correct number of years |
||
673 | 1 | while ($correct_month < 1 || $correct_month > self::MONTHS_PER_YEAR) |
|
674 | { |
||
675 | $correct_month = ($correct_month < 1) ? $correct_month + self::MONTHS_PER_YEAR : $correct_month - self::MONTHS_PER_YEAR; |
||
676 | } |
||
677 | |||
678 | // fix incorrect months |
||
679 | 1 | while (date("n", $this->stamp) != $correct_month) |
|
680 | { |
||
681 | // Go back one day or move forward in order to |
||
682 | // get to the correct month |
||
683 | $this->stamp = (date("n", $this->stamp) > $correct_month) ? $this->stamp - self::TIME_DAY : $this->stamp + self::TIME_DAY; |
||
684 | } |
||
685 | 1 | } |
|
686 | 1 | $this->dateYYYY = (int) date("Y", $this->stamp); |
|
687 | 1 | $this->dateMM = (int) date("n", $this->stamp); |
|
688 | 1 | $this->ext_dateMM = date("F", $this->stamp); |
|
689 | 1 | $this->dateDD = (int) date("d", $this->stamp); |
|
690 | 1 | $this->daysMonth = (int) date("t", $this->stamp); |
|
691 | |||
692 | 1 | for ($i = 1; $i < $this->daysMonth + 1; $i++) |
|
693 | { |
||
694 | 1 | $this->make_timestamp("$i {$this->ext_dateMM} {$this->dateYYYY}"); |
|
695 | 1 | $this->day[] = array( |
|
696 | 1 | '0' => "$i", |
|
697 | 1 | '1' => $this->dateMM, |
|
698 | 1 | '2' => $this->dateYYYY, |
|
699 | 1 | '3' => date('w', $this->stamp) |
|
700 | 1 | ); |
|
701 | 1 | } |
|
702 | 1 | } |
|
703 | |||
704 | /** |
||
705 | * Validate URLs and execute apppend_sid if necessary |
||
706 | * |
||
707 | * @param string $url URL to process |
||
708 | * |
||
709 | * @return string Processed URL |
||
710 | */ |
||
711 | 2 | protected function validate_url($url) |
|
712 | { |
||
713 | 2 | $url = str_replace("\r\n", "\n", str_replace('\"', '"', trim($url))); |
|
714 | 2 | $url = str_replace(' ', '%20', $url); |
|
715 | 2 | $url = str_replace('&', '&', $url); |
|
716 | |||
717 | // if there is no scheme, then add http schema |
||
718 | 2 | if (!preg_match('#^[a-z][a-z\d+\-.]*:/{2}#i', $url)) |
|
719 | 2 | { |
|
720 | 2 | $url = 'http://' . $url; |
|
721 | 2 | } |
|
722 | |||
723 | // Is this a link to somewhere inside this board? If so then run reapply_sid() |
||
724 | 2 | if (strpos($url, generate_board_url()) !== false) |
|
725 | 2 | { |
|
726 | 2 | $url = reapply_sid($url); |
|
727 | 2 | } |
|
728 | |||
729 | 2 | return $url; |
|
730 | } |
||
731 | |||
732 | /** |
||
733 | * Returns timestamp from given date string |
||
734 | * |
||
735 | * @param string $date Date and time string. It can contain just the |
||
736 | * date or date and time info. The string should |
||
737 | * be in a similar format: 17.06.1990 18:06 |
||
738 | * @return int|bool The timestamp of the given date or false if |
||
739 | * given date does not match any known formats. |
||
740 | */ |
||
741 | 11 | public function date_to_time($date) |
|
745 | } |
||
746 |
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.