| Conditions | 54 |
| Paths | 1 |
| Total Lines | 231 |
| Code Lines | 130 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 32 | function CalendarMain() |
||
| 33 | { |
||
| 34 | global $txt, $context, $modSettings, $scripturl, $options, $sourcedir, $user_info, $smcFunc; |
||
| 35 | |||
| 36 | // Permissions, permissions, permissions. |
||
| 37 | isAllowedTo('calendar_view'); |
||
| 38 | |||
| 39 | // Some global template resources. |
||
| 40 | $context['calendar_resources'] = array( |
||
| 41 | 'min_year' => $modSettings['cal_minyear'], |
||
| 42 | 'max_year' => $modSettings['cal_maxyear'], |
||
| 43 | ); |
||
| 44 | |||
| 45 | // Doing something other than calendar viewing? |
||
| 46 | $subActions = array( |
||
| 47 | 'ical' => 'iCalDownload', |
||
| 48 | 'post' => 'CalendarPost', |
||
| 49 | ); |
||
| 50 | |||
| 51 | if (isset($_GET['sa']) && isset($subActions[$_GET['sa']])) |
||
| 52 | return call_helper($subActions[$_GET['sa']]); |
||
| 53 | |||
| 54 | // You can't do anything if the calendar is off. |
||
| 55 | if (empty($modSettings['cal_enabled'])) |
||
| 56 | fatal_lang_error('calendar_off', false); |
||
| 57 | |||
| 58 | // This is gonna be needed... |
||
| 59 | loadTemplate('Calendar'); |
||
| 60 | loadCSSFile('calendar.css', array('force_current' => false, 'validate' => true, 'rtl' => 'calendar.rtl.css'), 'smf_calendar'); |
||
| 61 | |||
| 62 | // Did the specify an individual event ID? If so, let's splice the year/month in to what we would otherwise be doing. |
||
| 63 | if (isset($_GET['event'])) |
||
| 64 | { |
||
| 65 | $evid = (int) $_GET['event']; |
||
| 66 | if ($evid > 0) |
||
| 67 | { |
||
| 68 | $request = $smcFunc['db_query']('', ' |
||
| 69 | SELECT start_date |
||
| 70 | FROM {db_prefix}calendar |
||
| 71 | WHERE id_event = {int:event_id}', |
||
| 72 | array( |
||
| 73 | 'event_id' => $evid, |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | if ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 77 | { |
||
| 78 | $_REQUEST['start_date'] = $row['start_date']; |
||
| 79 | |||
| 80 | // We might use this later. |
||
| 81 | $context['selected_event'] = $evid; |
||
| 82 | } |
||
| 83 | $smcFunc['db_free_result']($request); |
||
| 84 | } |
||
| 85 | unset ($_GET['event']); |
||
| 86 | } |
||
| 87 | |||
| 88 | // Set the page title to mention the calendar ;). |
||
| 89 | $context['page_title'] = $txt['calendar']; |
||
| 90 | |||
| 91 | // Ensure a default view is defined |
||
| 92 | if (empty($options['calendar_default_view'])) |
||
| 93 | $options['calendar_default_view'] = 'viewlist'; |
||
| 94 | |||
| 95 | // What view do we want? |
||
| 96 | if (isset($_GET['viewweek'])) |
||
| 97 | $context['calendar_view'] = 'viewweek'; |
||
| 98 | elseif (isset($_GET['viewmonth'])) |
||
| 99 | $context['calendar_view'] = 'viewmonth'; |
||
| 100 | elseif (isset($_GET['viewlist'])) |
||
| 101 | $context['calendar_view'] = 'viewlist'; |
||
| 102 | else |
||
| 103 | $context['calendar_view'] = $options['calendar_default_view']; |
||
| 104 | |||
| 105 | // Don't let search engines index the non-default calendar pages |
||
| 106 | if ($context['calendar_view'] !== $options['calendar_default_view']) |
||
| 107 | $context['robot_no_index'] = true; |
||
| 108 | |||
| 109 | // Get the current day of month... |
||
| 110 | require_once($sourcedir . '/Subs-Calendar.php'); |
||
| 111 | $today = getTodayInfo(); |
||
| 112 | |||
| 113 | // Need a start date for all views |
||
| 114 | if (!empty($_REQUEST['start_date'])) |
||
| 115 | { |
||
| 116 | $start_parsed = date_parse(str_replace(',', '', convertDateToEnglish($_REQUEST['start_date']))); |
||
| 117 | if (empty($start_parsed['error_count']) && empty($start_parsed['warning_count'])) |
||
| 118 | { |
||
| 119 | $_REQUEST['year'] = $start_parsed['year']; |
||
| 120 | $_REQUEST['month'] = $start_parsed['month']; |
||
| 121 | $_REQUEST['day'] = $start_parsed['day']; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | $year = !empty($_REQUEST['year']) ? (int) $_REQUEST['year'] : $today['year']; |
||
| 125 | $month = !empty($_REQUEST['month']) ? (int) $_REQUEST['month'] : $today['month']; |
||
| 126 | $day = !empty($_REQUEST['day']) ? (int) $_REQUEST['day'] : (!empty($_REQUEST['month']) ? 1 : $today['day']); |
||
| 127 | |||
| 128 | $start_object = checkdate($month, $day, $year) === true ? date_create(implode('-', array($year, $month, $day)) . ' ' . getUserTimezone()) : date_create(implode('-', array($today['year'], $today['month'], $today['day'])) . ' ' . getUserTimezone()); |
||
| 129 | |||
| 130 | // Need an end date for the list view |
||
| 131 | if (!empty($_REQUEST['end_date'])) |
||
| 132 | { |
||
| 133 | $end_parsed = date_parse(str_replace(',', '', convertDateToEnglish($_REQUEST['end_date']))); |
||
| 134 | if (empty($end_parsed['error_count']) && empty($end_parsed['warning_count'])) |
||
| 135 | { |
||
| 136 | $_REQUEST['end_year'] = $end_parsed['year']; |
||
| 137 | $_REQUEST['end_month'] = $end_parsed['month']; |
||
| 138 | $_REQUEST['end_day'] = $end_parsed['day']; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $end_year = !empty($_REQUEST['end_year']) ? (int) $_REQUEST['end_year'] : null; |
||
| 142 | $end_month = !empty($_REQUEST['end_month']) ? (int) $_REQUEST['end_month'] : null; |
||
| 143 | $end_day = !empty($_REQUEST['end_day']) ? (int) $_REQUEST['end_day'] : null; |
||
| 144 | |||
| 145 | $end_object = null; |
||
| 146 | |||
| 147 | if (isset($end_month, $end_day, $end_year) && checkdate($end_month, $end_day, $end_year)) |
||
| 148 | { |
||
| 149 | $end_object = date_create(implode('-', array($end_year, $end_month, $end_day)) . ' ' . getUserTimezone()); |
||
| 150 | } |
||
| 151 | |||
| 152 | if (empty($end_object) || $start_object >= $end_object) |
||
| 153 | { |
||
| 154 | $num_days_shown = empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index']; |
||
| 155 | |||
| 156 | $end_object = date_create(date_format($start_object, 'Y-m-d') . ' ' . getUserTimezone()); |
||
| 157 | |||
| 158 | date_add($end_object, date_interval_create_from_date_string($num_days_shown . ' days')); |
||
| 159 | } |
||
| 160 | |||
| 161 | $curPage = array( |
||
| 162 | 'year' => date_format($start_object, 'Y'), |
||
| 163 | 'month' => date_format($start_object, 'n'), |
||
| 164 | 'day' => date_format($start_object, 'j'), |
||
| 165 | 'start_date' => date_format($start_object, 'Y-m-d'), |
||
| 166 | 'end_year' => date_format($end_object, 'Y'), |
||
| 167 | 'end_month' => date_format($end_object, 'n'), |
||
| 168 | 'end_day' => date_format($end_object, 'j'), |
||
| 169 | 'end_date' => date_format($end_object, 'Y-m-d'), |
||
| 170 | ); |
||
| 171 | |||
| 172 | // Make sure the year and month are in valid ranges. |
||
| 173 | if ($curPage['month'] < 1 || $curPage['month'] > 12) |
||
| 174 | fatal_lang_error('invalid_month', false); |
||
| 175 | if ($curPage['year'] < $modSettings['cal_minyear'] || $curPage['year'] > $modSettings['cal_maxyear']) |
||
| 176 | fatal_lang_error('invalid_year', false); |
||
| 177 | // If we have a day clean that too. |
||
| 178 | if ($context['calendar_view'] != 'viewmonth') |
||
| 179 | { |
||
| 180 | $isValid = checkdate($curPage['month'], $curPage['day'], $curPage['year']); |
||
| 181 | if (!$isValid) |
||
| 182 | fatal_lang_error('invalid_day', false); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Load all the context information needed to show the calendar grid. |
||
| 186 | $calendarOptions = array( |
||
| 187 | 'start_day' => !empty($options['calendar_start_day']) ? $options['calendar_start_day'] : 0, |
||
| 188 | 'show_birthdays' => in_array($modSettings['cal_showbdays'], array(1, 2)), |
||
| 189 | 'show_events' => in_array($modSettings['cal_showevents'], array(1, 2)), |
||
| 190 | 'show_holidays' => in_array($modSettings['cal_showholidays'], array(1, 2)), |
||
| 191 | 'show_week_num' => true, |
||
| 192 | 'short_day_titles' => !empty($modSettings['cal_short_days']), |
||
| 193 | 'short_month_titles' => !empty($modSettings['cal_short_months']), |
||
| 194 | 'show_next_prev' => !empty($modSettings['cal_prev_next_links']), |
||
| 195 | 'show_week_links' => isset($modSettings['cal_week_links']) ? $modSettings['cal_week_links'] : 0, |
||
| 196 | ); |
||
| 197 | |||
| 198 | // Load up the main view. |
||
| 199 | if ($context['calendar_view'] == 'viewlist') |
||
| 200 | $context['calendar_grid_main'] = getCalendarList($curPage['start_date'], $curPage['end_date'], $calendarOptions); |
||
| 201 | elseif ($context['calendar_view'] == 'viewweek') |
||
| 202 | $context['calendar_grid_main'] = getCalendarWeek($curPage['start_date'], $calendarOptions); |
||
| 203 | else |
||
| 204 | $context['calendar_grid_main'] = getCalendarGrid($curPage['start_date'], $calendarOptions); |
||
| 205 | |||
| 206 | // Load up the previous and next months. |
||
| 207 | $context['calendar_grid_current'] = getCalendarGrid($curPage['start_date'], $calendarOptions, false, false); |
||
| 208 | |||
| 209 | // Only show previous month if it isn't pre-January of the min-year |
||
| 210 | if ($context['calendar_grid_current']['previous_calendar']['year'] > $modSettings['cal_minyear'] || $curPage['month'] != 1) |
||
| 211 | $context['calendar_grid_prev'] = getCalendarGrid($context['calendar_grid_current']['previous_calendar']['start_date'], $calendarOptions, true, false); |
||
| 212 | |||
| 213 | // Only show next month if it isn't post-December of the max-year |
||
| 214 | if ($context['calendar_grid_current']['next_calendar']['year'] < $modSettings['cal_maxyear'] || $curPage['month'] != 12) |
||
| 215 | $context['calendar_grid_next'] = getCalendarGrid($context['calendar_grid_current']['next_calendar']['start_date'], $calendarOptions, false, false); |
||
| 216 | |||
| 217 | // Basic template stuff. |
||
| 218 | $context['allow_calendar_event'] = allowedTo('calendar_post'); |
||
| 219 | |||
| 220 | // If you don't allow events not linked to posts and you're not an admin, we have more work to do... |
||
| 221 | if ($context['allow_calendar_event'] && empty($modSettings['cal_allow_unlinked']) && !$user_info['is_admin']) |
||
| 222 | { |
||
| 223 | $boards_can_post = boardsAllowedTo('post_new'); |
||
| 224 | $context['allow_calendar_event'] &= !empty($boards_can_post); |
||
| 225 | } |
||
| 226 | |||
| 227 | $context['can_post'] = $context['allow_calendar_event']; |
||
| 228 | $context['current_day'] = $curPage['day']; |
||
| 229 | $context['current_month'] = $curPage['month']; |
||
| 230 | $context['current_year'] = $curPage['year']; |
||
| 231 | $context['show_all_birthdays'] = isset($_GET['showbd']); |
||
| 232 | $context['blocks_disabled'] = !empty($modSettings['cal_disable_prev_next']) ? 1 : 0; |
||
| 233 | |||
| 234 | // Set the page title to mention the month or week, too |
||
| 235 | if ($context['calendar_view'] != 'viewlist') |
||
| 236 | $context['page_title'] .= ' - ' . ($context['calendar_view'] == 'viewweek' ? $context['calendar_grid_main']['week_title'] : $txt['months_titles'][$context['current_month']] . ' ' . $context['current_year']); |
||
| 237 | |||
| 238 | // Load up the linktree! |
||
| 239 | $context['linktree'][] = array( |
||
| 240 | 'url' => $scripturl . '?action=calendar', |
||
| 241 | 'name' => $txt['calendar'] |
||
| 242 | ); |
||
| 243 | // Add the current month to the linktree. |
||
| 244 | $context['linktree'][] = array( |
||
| 245 | 'url' => $scripturl . '?action=calendar;year=' . $context['current_year'] . ';month=' . $context['current_month'], |
||
| 246 | 'name' => $txt['months_titles'][$context['current_month']] . ' ' . $context['current_year'] |
||
| 247 | ); |
||
| 248 | // If applicable, add the current week to the linktree. |
||
| 249 | if ($context['calendar_view'] == 'viewweek') |
||
| 250 | $context['linktree'][] = array( |
||
| 251 | 'url' => $scripturl . '?action=calendar;viewweek;year=' . $context['current_year'] . ';month=' . $context['current_month'] . ';day=' . $context['current_day'], |
||
| 252 | 'name' => $context['calendar_grid_main']['week_title'], |
||
| 253 | ); |
||
| 254 | |||
| 255 | // Build the calendar button array. |
||
| 256 | $context['calendar_buttons'] = array(); |
||
| 257 | |||
| 258 | if ($context['can_post']) |
||
| 259 | $context['calendar_buttons']['post_event'] = array('text' => 'calendar_post_event', 'image' => 'calendarpe.png', 'url' => $scripturl . '?action=calendar;sa=post;month=' . $context['current_month'] . ';year=' . $context['current_year'] . ';' . $context['session_var'] . '=' . $context['session_id']); |
||
| 260 | |||
| 261 | // Allow mods to add additional buttons here |
||
| 262 | call_integration_hook('integrate_calendar_buttons'); |
||
| 263 | } |
||
| 736 | ?> |