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 | 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 | 1 | 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 | 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) |
|
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) |
|
636 | { |
||
637 | 11 | $this->manage_events('', $key, $module_id); |
|
638 | 3 | } |
|
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 | 1 | $this->stamp = (date("n", $this->stamp) > $correct_month) ? $this->stamp - self::TIME_DAY : $this->stamp + self::TIME_DAY; |
|
684 | 1 | } |
|
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) |
|
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 |