| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 251 |
| Code Lines | 129 |
| Lines | 21 |
| Ratio | 8.37 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 220 | function CalendarPost() |
||
| 221 | { |
||
| 222 | global $context, $txt, $user_info, $sourcedir, $scripturl; |
||
| 223 | global $modSettings, $topic, $smcFunc, $settings; |
||
| 224 | |||
| 225 | // Well - can they? |
||
| 226 | isAllowedTo('calendar_post'); |
||
| 227 | |||
| 228 | // We need these for all kinds of useful functions. |
||
| 229 | require_once($sourcedir . '/Subs-Calendar.php'); |
||
| 230 | require_once($sourcedir . '/Subs.php'); |
||
| 231 | |||
| 232 | // Cast this for safety... |
||
| 233 | if (isset($_REQUEST['eventid'])) |
||
| 234 | $_REQUEST['eventid'] = (int) $_REQUEST['eventid']; |
||
| 235 | |||
| 236 | // We want a fairly compact version of the time, but as close as possible to the user's settings. |
||
| 237 | View Code Duplication | if (preg_match('~%[HkIlMpPrRSTX](?:[^%]*%[HkIlMpPrRSTX])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0])) |
|
|
1 ignored issue
–
show
|
|||
| 238 | $time_string = '%k:%M'; |
||
| 239 | else |
||
| 240 | $time_string = str_replace(array('%I', '%H', '%S', '%r', '%R', '%T'), array('%l', '%k', '', '%l:%M %p', '%k:%M', '%l:%M'), $matches[0]); |
||
| 241 | |||
| 242 | $js_time_string = str_replace( |
||
| 243 | array('%H', '%k', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X'), |
||
| 244 | array('H', 'G', 'h', 'g', 'i', 'A', 'a', 'h:i:s A', 'H:i', 's', 'H:i:s', 'H:i:s'), |
||
| 245 | $time_string |
||
| 246 | ); |
||
| 247 | |||
| 248 | // Submitting? |
||
| 249 | if (isset($_POST[$context['session_var']], $_REQUEST['eventid'])) |
||
| 250 | { |
||
| 251 | checkSession(); |
||
| 252 | |||
| 253 | // Validate the post... |
||
| 254 | if (!isset($_POST['link_to_board'])) |
||
| 255 | validateEventPost(); |
||
| 256 | |||
| 257 | // If you're not allowed to edit any events, you have to be the poster. |
||
| 258 | if ($_REQUEST['eventid'] > 0 && !allowedTo('calendar_edit_any')) |
||
| 259 | isAllowedTo('calendar_edit_' . (!empty($user_info['id']) && getEventPoster($_REQUEST['eventid']) == $user_info['id'] ? 'own' : 'any')); |
||
| 260 | |||
| 261 | // New - and directing? |
||
| 262 | if (isset($_POST['link_to_board']) || empty($modSettings['cal_allow_unlinked'])) |
||
| 263 | { |
||
| 264 | $_REQUEST['calendar'] = 1; |
||
| 265 | require_once($sourcedir . '/Post.php'); |
||
| 266 | return Post(); |
||
| 267 | } |
||
| 268 | // New... |
||
| 269 | elseif ($_REQUEST['eventid'] == -1) |
||
| 270 | { |
||
| 271 | $eventOptions = array( |
||
| 272 | 'board' => 0, |
||
| 273 | 'topic' => 0, |
||
| 274 | 'title' => $smcFunc['substr']($_REQUEST['evtitle'], 0, 100), |
||
| 275 | 'location' => $smcFunc['substr']($_REQUEST['event_location'], 0, 255), |
||
| 276 | 'member' => $user_info['id'], |
||
| 277 | ); |
||
| 278 | insertEvent($eventOptions); |
||
| 279 | } |
||
| 280 | |||
| 281 | // Deleting... |
||
| 282 | elseif (isset($_REQUEST['deleteevent'])) |
||
| 283 | removeEvent($_REQUEST['eventid']); |
||
| 284 | |||
| 285 | // ... or just update it? |
||
| 286 | else |
||
| 287 | { |
||
| 288 | $eventOptions = array( |
||
| 289 | 'title' => $smcFunc['substr']($_REQUEST['evtitle'], 0, 100), |
||
| 290 | 'location' => $smcFunc['substr']($_REQUEST['event_location'], 0, 255), |
||
| 291 | ); |
||
| 292 | modifyEvent($_REQUEST['eventid'], $eventOptions); |
||
| 293 | } |
||
| 294 | |||
| 295 | updateSettings(array( |
||
| 296 | 'calendar_updated' => time(), |
||
| 297 | )); |
||
| 298 | |||
| 299 | // No point hanging around here now... |
||
| 300 | if (isset($_POST['start_date'])) |
||
| 301 | { |
||
| 302 | $d = date_parse($_POST['start_date']); |
||
| 303 | $year = $d['year']; |
||
| 304 | $month = $d['month']; |
||
| 305 | } |
||
| 306 | elseif (isset($_POST['start_datetime'])) |
||
| 307 | { |
||
| 308 | $d = date_parse($_POST['start_datetime']); |
||
| 309 | $year = $d['year']; |
||
| 310 | $month = $d['month']; |
||
| 311 | } |
||
| 312 | else |
||
| 313 | { |
||
| 314 | $today = getdate(); |
||
| 315 | $year = isset($_POST['year']) ? $_POST['year'] : $today['year']; |
||
| 316 | $month = isset($_POST['month']) ? $_POST['month'] : $today['mon']; |
||
| 317 | } |
||
| 318 | redirectexit($scripturl . '?action=calendar;month=' . $month . ';year=' . $year); |
||
| 319 | } |
||
| 320 | |||
| 321 | // If we are not enabled... we are not enabled. |
||
| 322 | if (empty($modSettings['cal_allow_unlinked']) && empty($_REQUEST['eventid'])) |
||
| 323 | { |
||
| 324 | $_REQUEST['calendar'] = 1; |
||
| 325 | require_once($sourcedir . '/Post.php'); |
||
| 326 | return Post(); |
||
| 327 | } |
||
| 328 | |||
| 329 | // New? |
||
| 330 | if (!isset($_REQUEST['eventid'])) |
||
| 331 | { |
||
| 332 | $context['event'] = array( |
||
| 333 | 'boards' => array(), |
||
| 334 | 'board' => 0, |
||
| 335 | 'new' => 1, |
||
| 336 | 'eventid' => -1, |
||
| 337 | 'title' => '', |
||
| 338 | 'location' => '', |
||
| 339 | ); |
||
| 340 | |||
| 341 | $eventDatetimes = getNewEventDatetimes(); |
||
| 342 | $context['event'] = array_merge($context['event'], $eventDatetimes); |
||
| 343 | |||
| 344 | $context['event']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['event']['month'] == 12 ? 1 : $context['event']['month'] + 1, 0, $context['event']['month'] == 12 ? $context['event']['year'] + 1 : $context['event']['year'])); |
||
| 345 | } |
||
| 346 | else |
||
| 347 | { |
||
| 348 | $context['event'] = getEventProperties($_REQUEST['eventid']); |
||
| 349 | |||
| 350 | if ($context['event'] === false) |
||
| 351 | fatal_lang_error('no_access', false); |
||
| 352 | |||
| 353 | // If it has a board, then they should be editing it within the topic. |
||
| 354 | if (!empty($context['event']['topic']['id']) && !empty($context['event']['topic']['first_msg'])) |
||
| 355 | { |
||
| 356 | // We load the board up, for a check on the board access rights... |
||
| 357 | $topic = $context['event']['topic']['id']; |
||
| 358 | loadBoard(); |
||
| 359 | } |
||
| 360 | |||
| 361 | // Make sure the user is allowed to edit this event. |
||
| 362 | if ($context['event']['member'] != $user_info['id']) |
||
| 363 | isAllowedTo('calendar_edit_any'); |
||
| 364 | elseif (!allowedTo('calendar_edit_any')) |
||
| 365 | isAllowedTo('calendar_edit_own'); |
||
| 366 | } |
||
| 367 | |||
| 368 | // An all day event? Set up some nice defaults in case the user wants to change that |
||
| 369 | View Code Duplication | if ($context['event']['allday'] == true) |
|
|
1 ignored issue
–
show
|
|||
| 370 | { |
||
| 371 | $context['event']['tz'] = getUserTimezone(); |
||
| 372 | $context['event']['start_time'] = timeformat(time(), $time_string); |
||
| 373 | $context['event']['end_time'] = timeformat(time() + 3600, $time_string); |
||
| 374 | } |
||
| 375 | // Otherwise, just adjust these to look nice on the input form |
||
| 376 | else |
||
| 377 | { |
||
| 378 | $context['event']['start_time'] = timeformat(strtotime($context['event']['start_iso_gmdate']), $time_string); |
||
| 379 | $context['event']['end_time'] = timeformat(strtotime($context['event']['end_iso_gmdate']), $time_string); |
||
| 380 | } |
||
| 381 | |||
| 382 | // Need this so the user can select a timezone for the event. |
||
| 383 | $context['all_timezones'] = smf_list_timezones($context['event']['start_date']); |
||
| 384 | unset($context['all_timezones']['']); |
||
| 385 | |||
| 386 | // If the event's timezone is not in SMF's standard list of time zones, prepend it to the list |
||
| 387 | View Code Duplication | if (!in_array($context['event']['tz'], array_keys($context['all_timezones']))) |
|
|
1 ignored issue
–
show
|
|||
| 388 | { |
||
| 389 | $d = date_create($context['event']['tz']); |
||
| 390 | $context['all_timezones'] = array($context['event']['tz'] => date_format($d, 'T') . ' - ' . $context['event']['tz'] . ' [UTC' . date_format($d, 'P') . ']') + $context['all_timezones']; |
||
| 391 | } |
||
| 392 | |||
| 393 | // Get list of boards that can be posted in. |
||
| 394 | $boards = boardsAllowedTo('post_new'); |
||
| 395 | if (empty($boards)) |
||
| 396 | { |
||
| 397 | // You can post new events but can't link them to anything... |
||
| 398 | $context['event']['categories'] = array(); |
||
| 399 | } |
||
| 400 | else |
||
| 401 | { |
||
| 402 | // Load the list of boards and categories in the context. |
||
| 403 | require_once($sourcedir . '/Subs-MessageIndex.php'); |
||
| 404 | $boardListOptions = array( |
||
| 405 | 'included_boards' => in_array(0, $boards) ? null : $boards, |
||
| 406 | 'not_redirection' => true, |
||
| 407 | 'use_permissions' => true, |
||
| 408 | 'selected_board' => $modSettings['cal_defaultboard'], |
||
| 409 | ); |
||
| 410 | $context['event']['categories'] = getBoardList($boardListOptions); |
||
| 411 | } |
||
| 412 | |||
| 413 | // Template, sub template, etc. |
||
| 414 | loadTemplate('Calendar'); |
||
| 415 | $context['sub_template'] = 'event_post'; |
||
| 416 | |||
| 417 | $context['page_title'] = isset($_REQUEST['eventid']) ? $txt['calendar_edit'] : $txt['calendar_post_event']; |
||
| 418 | $context['linktree'][] = array( |
||
| 419 | 'name' => $context['page_title'], |
||
| 420 | ); |
||
| 421 | |||
| 422 | loadCSSFile('jquery-ui.datepicker.css', array('defer' => false), 'smf_datepicker'); |
||
| 423 | loadCSSFile('jquery.timepicker.css', array('defer' => false), 'smf_timepicker'); |
||
| 424 | loadJavaScriptFile('jquery-ui.datepicker.min.js', array('defer' => true), 'smf_datepicker'); |
||
| 425 | loadJavaScriptFile('jquery.timepicker.min.js', array('defer' => true), 'smf_timepicker'); |
||
| 426 | loadJavaScriptFile('datepair.min.js', array('defer' => true), 'smf_datepair'); |
||
| 427 | addInlineJavaScript(' |
||
| 428 | $("#allday").click(function(){ |
||
| 429 | $("#start_time").attr("disabled", this.checked); |
||
| 430 | $("#end_time").attr("disabled", this.checked); |
||
| 431 | $("#tz").attr("disabled", this.checked); |
||
| 432 | }); |
||
| 433 | $("#event_time_input .date_input").datepicker({ |
||
| 434 | dateFormat: "yy-mm-dd", |
||
| 435 | autoSize: true, |
||
| 436 | isRTL: ' . ($context['right_to_left'] ? 'true' : 'false') . ', |
||
| 437 | constrainInput: true, |
||
| 438 | showAnim: "", |
||
| 439 | showButtonPanel: false, |
||
| 440 | minDate: "' . $modSettings['cal_minyear'] . '-01-01", |
||
| 441 | maxDate: "' . $modSettings['cal_maxyear'] . '-12-31", |
||
| 442 | yearRange: "' . $modSettings['cal_minyear'] . ':' . $modSettings['cal_maxyear'] . '", |
||
| 443 | hideIfNoPrevNext: true, |
||
| 444 | monthNames: ["' . implode('", "', $txt['months_titles']) . '"], |
||
| 445 | monthNamesShort: ["' . implode('", "', $txt['months_short']) . '"], |
||
| 446 | dayNames: ["' . implode('", "', $txt['days']) . '"], |
||
| 447 | dayNamesShort: ["' . implode('", "', $txt['days_short']) . '"], |
||
| 448 | dayNamesMin: ["' . implode('", "', $txt['days_short']) . '"], |
||
| 449 | prevText: "' . $txt['prev_month'] . '", |
||
| 450 | nextText: "' . $txt['next_month'] . '", |
||
| 451 | }); |
||
| 452 | $(".time_input").timepicker({ |
||
| 453 | timeFormat: "' . $js_time_string . '", |
||
| 454 | showDuration: true, |
||
| 455 | maxTime: "23:59:59", |
||
| 456 | }); |
||
| 457 | var date_entry = document.getElementById("event_time_input"); |
||
| 458 | var date_entry_pair = new Datepair(date_entry, { |
||
| 459 | timeClass: "time_input", |
||
| 460 | dateClass: "date_input", |
||
| 461 | parseDate: function (el) { |
||
| 462 | var utc = new Date($(el).datepicker("getDate")); |
||
| 463 | return utc && new Date(utc.getTime() + (utc.getTimezoneOffset() * 60000)); |
||
| 464 | }, |
||
| 465 | updateDate: function (el, v) { |
||
| 466 | $(el).datepicker("setDate", new Date(v.getTime() - (v.getTimezoneOffset() * 60000))); |
||
| 467 | } |
||
| 468 | }); |
||
| 469 | ', true); |
||
| 470 | } |
||
| 471 | |||
| 701 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: