Chris@76: 1, Chris@76: 'no_month' => 0, Chris@76: 'no_day' => 0, Chris@76: 'year_one' => '0001', Chris@76: 'year_low' => $year_low . '-%m-%d', Chris@76: 'year_high' => $year_high . '-%m-%d', Chris@76: 'low_date' => $low_date, Chris@76: 'high_date' => $high_date, Chris@76: 'max_year' => $year_high, Chris@76: ) Chris@76: ); Chris@76: $bday = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: if ($year_low != $year_high) Chris@76: $age_year = substr($row['birthdate'], 5) < substr($high_date, 5) ? $year_high : $year_low; Chris@76: else Chris@76: $age_year = $year_low; Chris@76: Chris@76: $bday[$age_year . substr($row['birthdate'], 4)][] = array( Chris@76: 'id' => $row['id_member'], Chris@76: 'name' => $row['real_name'], Chris@76: 'age' => $row['birth_year'] > 4 && $row['birth_year'] <= $age_year ? $age_year - $row['birth_year'] : null, Chris@76: 'is_last' => false Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // Set is_last, so the themes know when to stop placing separators. Chris@76: foreach ($bday as $mday => $array) Chris@76: $bday[$mday][count($array) - 1]['is_last'] = true; Chris@76: Chris@76: return $bday; Chris@76: } Chris@76: Chris@76: // Get all events within the given time range. Chris@76: function getEventRange($low_date, $high_date, $use_permissions = true) Chris@76: { Chris@76: global $scripturl, $modSettings, $user_info, $smcFunc, $context; Chris@76: Chris@76: $low_date_time = sscanf($low_date, '%04d-%02d-%02d'); Chris@76: $low_date_time = mktime(0, 0, 0, $low_date_time[1], $low_date_time[2], $low_date_time[0]); Chris@76: $high_date_time = sscanf($high_date, '%04d-%02d-%02d'); Chris@76: $high_date_time = mktime(0, 0, 0, $high_date_time[1], $high_date_time[2], $high_date_time[0]); Chris@76: Chris@76: // Find all the calendar info... Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT Chris@76: cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic, Chris@76: cal.id_board, b.member_groups, t.id_first_msg, t.approved, b.id_board Chris@76: FROM {db_prefix}calendar AS cal Chris@76: LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board) Chris@76: LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic) Chris@76: WHERE cal.start_date <= {date:high_date} Chris@76: AND cal.end_date >= {date:low_date}' . ($use_permissions ? ' Chris@76: AND (cal.id_board = {int:no_board_link} OR {query_wanna_see_board})' : ''), Chris@76: array( Chris@76: 'high_date' => $high_date, Chris@76: 'low_date' => $low_date, Chris@76: 'no_board_link' => 0, Chris@76: ) Chris@76: ); Chris@76: $events = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: // If the attached topic is not approved then for the moment pretend it doesn't exist Chris@76: //!!! This should be fixed to show them all and then sort by approval state later? Chris@76: if (!empty($row['id_first_msg']) && $modSettings['postmod_active'] && !$row['approved']) Chris@76: continue; Chris@76: Chris@76: // Force a censor of the title - as often these are used by others. Chris@76: censorText($row['title'], $use_permissions ? false : true); Chris@76: Chris@76: $start_date = sscanf($row['start_date'], '%04d-%02d-%02d'); Chris@76: $start_date = max(mktime(0, 0, 0, $start_date[1], $start_date[2], $start_date[0]), $low_date_time); Chris@76: $end_date = sscanf($row['end_date'], '%04d-%02d-%02d'); Chris@76: $end_date = min(mktime(0, 0, 0, $end_date[1], $end_date[2], $end_date[0]), $high_date_time); Chris@76: Chris@76: $lastDate = ''; Chris@76: for ($date = $start_date; $date <= $end_date; $date += 86400) Chris@76: { Chris@76: // Attempt to avoid DST problems. Chris@76: //!!! Resolve this properly at some point. Chris@76: if (strftime('%Y-%m-%d', $date) == $lastDate) Chris@76: $date += 3601; Chris@76: $lastDate = strftime('%Y-%m-%d', $date); Chris@76: Chris@76: // If we're using permissions (calendar pages?) then just ouput normal contextual style information. Chris@76: if ($use_permissions) Chris@76: $events[strftime('%Y-%m-%d', $date)][] = array( Chris@76: 'id' => $row['id_event'], Chris@76: 'title' => $row['title'], Chris@76: 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')), Chris@76: 'modify_href' => $scripturl . '?action=' . ($row['id_board'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['id_first_msg'] . ';topic=' . $row['id_topic'] . '.0;calendar;') . 'eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'], Chris@76: 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0', Chris@76: 'link' => $row['id_board'] == 0 ? $row['title'] : '' . $row['title'] . '', Chris@76: 'start_date' => $row['start_date'], Chris@76: 'end_date' => $row['end_date'], Chris@76: 'is_last' => false, Chris@76: 'id_board' => $row['id_board'], Chris@76: ); Chris@76: // Otherwise, this is going to be cached and the VIEWER'S permissions should apply... just put together some info. Chris@76: else Chris@76: $events[strftime('%Y-%m-%d', $date)][] = array( Chris@76: 'id' => $row['id_event'], Chris@76: 'title' => $row['title'], Chris@76: 'topic' => $row['id_topic'], Chris@76: 'msg' => $row['id_first_msg'], Chris@76: 'poster' => $row['id_member'], Chris@76: 'start_date' => $row['start_date'], Chris@76: 'end_date' => $row['end_date'], Chris@76: 'is_last' => false, Chris@76: 'allowed_groups' => explode(',', $row['member_groups']), Chris@76: 'id_board' => $row['id_board'], Chris@76: 'href' => $row['id_topic'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0', Chris@76: 'link' => $row['id_topic'] == 0 ? $row['title'] : '' . $row['title'] . '', Chris@76: 'can_edit' => false, Chris@76: ); Chris@76: } Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // If we're doing normal contextual data, go through and make things clear to the templates ;). Chris@76: if ($use_permissions) Chris@76: { Chris@76: foreach ($events as $mday => $array) Chris@76: $events[$mday][count($array) - 1]['is_last'] = true; Chris@76: } Chris@76: Chris@76: return $events; Chris@76: } Chris@76: Chris@76: // Get all holidays within the given time range. Chris@76: function getHolidayRange($low_date, $high_date) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // Get the lowest and highest dates for "all years". Chris@76: if (substr($low_date, 0, 4) != substr($high_date, 0, 4)) Chris@76: $allyear_part = 'event_date BETWEEN {date:all_year_low} AND {date:all_year_dec} Chris@76: OR event_date BETWEEN {date:all_year_jan} AND {date:all_year_high}'; Chris@76: else Chris@76: $allyear_part = 'event_date BETWEEN {date:all_year_low} AND {date:all_year_high}'; Chris@76: Chris@76: // Find some holidays... ;). Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT event_date, YEAR(event_date) AS year, title Chris@76: FROM {db_prefix}calendar_holidays Chris@76: WHERE event_date BETWEEN {date:low_date} AND {date:high_date} Chris@76: OR ' . $allyear_part, Chris@76: array( Chris@76: 'low_date' => $low_date, Chris@76: 'high_date' => $high_date, Chris@76: 'all_year_low' => '0004' . substr($low_date, 4), Chris@76: 'all_year_high' => '0004' . substr($high_date, 4), Chris@76: 'all_year_jan' => '0004-01-01', Chris@76: 'all_year_dec' => '0004-12-31', Chris@76: ) Chris@76: ); Chris@76: $holidays = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: if (substr($low_date, 0, 4) != substr($high_date, 0, 4)) Chris@76: $event_year = substr($row['event_date'], 5) < substr($high_date, 5) ? substr($high_date, 0, 4) : substr($low_date, 0, 4); Chris@76: else Chris@76: $event_year = substr($low_date, 0, 4); Chris@76: Chris@76: $holidays[$event_year . substr($row['event_date'], 4)][] = $row['title']; Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: return $holidays; Chris@76: } Chris@76: Chris@76: // Does permission checks to see if an event can be linked to a board/topic. Chris@76: function canLinkEvent() Chris@76: { Chris@76: global $user_info, $topic, $board, $smcFunc; Chris@76: Chris@76: // If you can't post, you can't link. Chris@76: isAllowedTo('calendar_post'); Chris@76: Chris@76: // No board? No topic?!? Chris@76: if (empty($board)) Chris@76: fatal_lang_error('missing_board_id', false); Chris@76: if (empty($topic)) Chris@76: fatal_lang_error('missing_topic_id', false); Chris@76: Chris@76: // Administrator, Moderator, or owner. Period. Chris@76: if (!allowedTo('admin_forum') && !allowedTo('moderate_board')) Chris@76: { Chris@76: // Not admin or a moderator of this board. You better be the owner - or else. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_member_started Chris@76: FROM {db_prefix}topics Chris@76: WHERE id_topic = {int:current_topic} Chris@76: LIMIT 1', Chris@76: array( Chris@76: 'current_topic' => $topic, Chris@76: ) Chris@76: ); Chris@76: if ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: // Not the owner of the topic. Chris@76: if ($row['id_member_started'] != $user_info['id']) Chris@76: fatal_lang_error('not_your_topic', 'user'); Chris@76: } Chris@76: // Topic/Board doesn't exist..... Chris@76: else Chris@76: fatal_lang_error('calendar_no_topic', 'general'); Chris@76: $smcFunc['db_free_result']($result); Chris@76: } Chris@76: } Chris@76: Chris@76: // Returns date information about 'today' relative to the users time offset. Chris@76: function getTodayInfo() Chris@76: { Chris@76: return array( Chris@76: 'day' => (int) strftime('%d', forum_time()), Chris@76: 'month' => (int) strftime('%m', forum_time()), Chris@76: 'year' => (int) strftime('%Y', forum_time()), Chris@76: 'date' => strftime('%Y-%m-%d', forum_time()), Chris@76: ); Chris@76: } Chris@76: Chris@76: // Returns the information needed to show a calendar grid for the given month. Chris@76: function getCalendarGrid($month, $year, $calendarOptions) Chris@76: { Chris@76: global $scripturl, $modSettings; Chris@76: Chris@76: // Eventually this is what we'll be returning. Chris@76: $calendarGrid = array( Chris@76: 'week_days' => array(), Chris@76: 'weeks' => array(), Chris@76: 'short_day_titles' => !empty($calendarOptions['short_day_titles']), Chris@76: 'current_month' => $month, Chris@76: 'current_year' => $year, Chris@76: 'show_next_prev' => !empty($calendarOptions['show_next_prev']), Chris@76: 'show_week_links' => !empty($calendarOptions['show_week_links']), Chris@76: 'previous_calendar' => array( Chris@76: 'year' => $month == 1 ? $year - 1 : $year, Chris@76: 'month' => $month == 1 ? 12 : $month - 1, Chris@76: 'disabled' => $modSettings['cal_minyear'] > ($month == 1 ? $year - 1 : $year), Chris@76: ), Chris@76: 'next_calendar' => array( Chris@76: 'year' => $month == 12 ? $year + 1 : $year, Chris@76: 'month' => $month == 12 ? 1 : $month + 1, Chris@76: 'disabled' => $modSettings['cal_maxyear'] < ($month == 12 ? $year + 1 : $year), Chris@76: ), Chris@76: //!!! Better tweaks? Chris@76: 'size' => isset($calendarOptions['size']) ? $calendarOptions['size'] : 'large', Chris@76: ); Chris@76: Chris@76: // Get todays date. Chris@76: $today = getTodayInfo(); Chris@76: Chris@76: // Get information about this month. Chris@76: $month_info = array( Chris@76: 'first_day' => array( Chris@76: 'day_of_week' => (int) strftime('%w', mktime(0, 0, 0, $month, 1, $year)), Chris@76: 'week_num' => (int) strftime('%U', mktime(0, 0, 0, $month, 1, $year)), Chris@76: 'date' => strftime('%Y-%m-%d', mktime(0, 0, 0, $month, 1, $year)), Chris@76: ), Chris@76: 'last_day' => array( Chris@76: 'day_of_month' => (int) strftime('%d', mktime(0, 0, 0, $month == 12 ? 1 : $month + 1, 0, $month == 12 ? $year + 1 : $year)), Chris@76: 'date' => strftime('%Y-%m-%d', mktime(0, 0, 0, $month == 12 ? 1 : $month + 1, 0, $month == 12 ? $year + 1 : $year)), Chris@76: ), Chris@76: 'first_day_of_year' => (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year)), Chris@76: 'first_day_of_next_year' => (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year + 1)), Chris@76: ); Chris@76: Chris@76: // The number of days the first row is shifted to the right for the starting day. Chris@76: $nShift = $month_info['first_day']['day_of_week']; Chris@76: Chris@76: $calendarOptions['start_day'] = empty($calendarOptions['start_day']) ? 0 : (int) $calendarOptions['start_day']; Chris@76: Chris@76: // Starting any day other than Sunday means a shift... Chris@76: if (!empty($calendarOptions['start_day'])) Chris@76: { Chris@76: $nShift -= $calendarOptions['start_day']; Chris@76: if ($nShift < 0) Chris@76: $nShift = 7 + $nShift; Chris@76: } Chris@76: Chris@76: // Number of rows required to fit the month. Chris@76: $nRows = floor(($month_info['last_day']['day_of_month'] + $nShift) / 7); Chris@76: if (($month_info['last_day']['day_of_month'] + $nShift) % 7) Chris@76: $nRows++; Chris@76: Chris@76: // Fetch the arrays for birthdays, posted events, and holidays. Chris@76: $bday = $calendarOptions['show_birthdays'] ? getBirthdayRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array(); Chris@76: $events = $calendarOptions['show_events'] ? getEventRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array(); Chris@76: $holidays = $calendarOptions['show_holidays'] ? getHolidayRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array(); Chris@76: Chris@76: // Days of the week taking into consideration that they may want it to start on any day. Chris@76: $count = $calendarOptions['start_day']; Chris@76: for ($i = 0; $i < 7; $i++) Chris@76: { Chris@76: $calendarGrid['week_days'][] = $count; Chris@76: $count++; Chris@76: if ($count == 7) Chris@76: $count = 0; Chris@76: } Chris@76: Chris@76: // An adjustment value to apply to all calculated week numbers. Chris@76: if (!empty($calendarOptions['show_week_num'])) Chris@76: { Chris@76: // If the first day of the year is a Sunday, then there is no Chris@76: // adjustment to be made. However, if the first day of the year is not Chris@76: // a Sunday, then there is a partial week at the start of the year Chris@76: // that needs to be accounted for. Chris@76: if ($calendarOptions['start_day'] === 0) Chris@76: $nWeekAdjust = $month_info['first_day_of_year'] === 0 ? 0 : 1; Chris@76: // If we are viewing the weeks, with a starting date other than Sunday, Chris@76: // then things get complicated! Basically, as PHP is calculating the Chris@76: // weeks with a Sunday starting date, we need to take this into account Chris@76: // and offset the whole year dependant on whether the first day in the Chris@76: // year is above or below our starting date. Note that we offset by Chris@76: // two, as some of this will get undone quite quickly by the statement Chris@76: // below. Chris@76: else Chris@76: $nWeekAdjust = $calendarOptions['start_day'] > $month_info['first_day_of_year'] && $month_info['first_day_of_year'] !== 0 ? 2 : 1; Chris@76: Chris@76: // If our week starts on a day greater than the day the month starts Chris@76: // on, then our week numbers will be one too high. So we need to Chris@76: // reduce it by one - all these thoughts of offsets makes my head Chris@76: // hurt... Chris@76: if ($month_info['first_day']['day_of_week'] < $calendarOptions['start_day'] || $month_info['first_day_of_year'] > 4) Chris@76: $nWeekAdjust--; Chris@76: } Chris@76: else Chris@76: $nWeekAdjust = 0; Chris@76: Chris@76: // Iterate through each week. Chris@76: $calendarGrid['weeks'] = array(); Chris@76: for ($nRow = 0; $nRow < $nRows; $nRow++) Chris@76: { Chris@76: // Start off the week - and don't let it go above 52, since that's the number of weeks in a year. Chris@76: $calendarGrid['weeks'][$nRow] = array( Chris@76: 'days' => array(), Chris@76: 'number' => $month_info['first_day']['week_num'] + $nRow + $nWeekAdjust Chris@76: ); Chris@76: // Handle the dreaded "week 53", it can happen, but only once in a blue moon ;) Chris@76: if ($calendarGrid['weeks'][$nRow]['number'] == 53 && $nShift != 4 && $month_info['first_day_of_next_year'] < 4) Chris@76: $calendarGrid['weeks'][$nRow]['number'] = 1; Chris@76: Chris@76: // And figure out all the days. Chris@76: for ($nCol = 0; $nCol < 7; $nCol++) Chris@76: { Chris@76: $nDay = ($nRow * 7) + $nCol - $nShift + 1; Chris@76: Chris@76: if ($nDay < 1 || $nDay > $month_info['last_day']['day_of_month']) Chris@76: $nDay = 0; Chris@76: Chris@76: $date = sprintf('%04d-%02d-%02d', $year, $month, $nDay); Chris@76: Chris@76: $calendarGrid['weeks'][$nRow]['days'][$nCol] = array( Chris@76: 'day' => $nDay, Chris@76: 'date' => $date, Chris@76: 'is_today' => $date == $today['date'], Chris@76: 'is_first_day' => !empty($calendarOptions['show_week_num']) && (($month_info['first_day']['day_of_week'] + $nDay - 1) % 7 == $calendarOptions['start_day']), Chris@76: 'holidays' => !empty($holidays[$date]) ? $holidays[$date] : array(), Chris@76: 'events' => !empty($events[$date]) ? $events[$date] : array(), Chris@76: 'birthdays' => !empty($bday[$date]) ? $bday[$date] : array() Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Set the previous and the next month's links. Chris@76: $calendarGrid['previous_calendar']['href'] = $scripturl . '?action=calendar;year=' . $calendarGrid['previous_calendar']['year'] . ';month=' . $calendarGrid['previous_calendar']['month']; Chris@76: $calendarGrid['next_calendar']['href'] = $scripturl . '?action=calendar;year=' . $calendarGrid['next_calendar']['year'] . ';month=' . $calendarGrid['next_calendar']['month']; Chris@76: Chris@76: return $calendarGrid; Chris@76: } Chris@76: Chris@76: // Returns the information needed to show a calendar for the given week. Chris@76: function getCalendarWeek($month, $year, $day, $calendarOptions) Chris@76: { Chris@76: global $scripturl, $modSettings; Chris@76: Chris@76: // Get todays date. Chris@76: $today = getTodayInfo(); Chris@76: Chris@76: // What is the actual "start date" for the passed day. Chris@76: $calendarOptions['start_day'] = empty($calendarOptions['start_day']) ? 0 : (int) $calendarOptions['start_day']; Chris@76: $day_of_week = (int) strftime('%w', mktime(0, 0, 0, $month, $day, $year)); Chris@76: if ($day_of_week != $calendarOptions['start_day']) Chris@76: { Chris@76: // Here we offset accordingly to get things to the real start of a week. Chris@76: $date_diff = $day_of_week - $calendarOptions['start_day']; Chris@76: if ($date_diff < 0) Chris@76: $date_diff += 7; Chris@76: $new_timestamp = mktime(0, 0, 0, $month, $day, $year) - $date_diff * 86400; Chris@76: $day = (int) strftime('%d', $new_timestamp); Chris@76: $month = (int) strftime('%m', $new_timestamp); Chris@76: $year = (int) strftime('%Y', $new_timestamp); Chris@76: } Chris@76: Chris@76: // Now start filling in the calendar grid. Chris@76: $calendarGrid = array( Chris@76: 'show_next_prev' => !empty($calendarOptions['show_next_prev']), Chris@76: // Previous week is easy - just step back one day. Chris@76: 'previous_week' => array( Chris@76: 'year' => $day == 1 ? ($month == 1 ? $year - 1 : $year) : $year, Chris@76: 'month' => $day == 1 ? ($month == 1 ? 12 : $month - 1) : $month, Chris@76: 'day' => $day == 1 ? 28 : $day - 1, Chris@76: 'disabled' => $day < 7 && $modSettings['cal_minyear'] > ($month == 1 ? $year - 1 : $year), Chris@76: ), Chris@76: 'next_week' => array( Chris@76: 'disabled' => $day > 25 && $modSettings['cal_maxyear'] < ($month == 12 ? $year + 1 : $year), Chris@76: ), Chris@76: ); Chris@76: Chris@76: // The next week calculation requires a bit more work. Chris@76: $curTimestamp = mktime(0, 0, 0, $month, $day, $year); Chris@76: $nextWeekTimestamp = $curTimestamp + 604800; Chris@76: $calendarGrid['next_week']['day'] = (int) strftime('%d', $nextWeekTimestamp); Chris@76: $calendarGrid['next_week']['month'] = (int) strftime('%m', $nextWeekTimestamp); Chris@76: $calendarGrid['next_week']['year'] = (int) strftime('%Y', $nextWeekTimestamp); Chris@76: Chris@76: // Fetch the arrays for birthdays, posted events, and holidays. Chris@76: $startDate = strftime('%Y-%m-%d', $curTimestamp); Chris@76: $endDate = strftime('%Y-%m-%d', $nextWeekTimestamp); Chris@76: $bday = $calendarOptions['show_birthdays'] ? getBirthdayRange($startDate, $endDate) : array(); Chris@76: $events = $calendarOptions['show_events'] ? getEventRange($startDate, $endDate) : array(); Chris@76: $holidays = $calendarOptions['show_holidays'] ? getHolidayRange($startDate, $endDate) : array(); Chris@76: Chris@76: // An adjustment value to apply to all calculated week numbers. Chris@76: if (!empty($calendarOptions['show_week_num'])) Chris@76: { Chris@76: $first_day_of_year = (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year)); Chris@76: $first_day_of_next_year = (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year + 1)); Chris@76: $last_day_of_last_year = (int) strftime('%w', mktime(0, 0, 0, 12, 31, $year - 1)); Chris@76: Chris@76: // All this is as getCalendarGrid. Chris@76: if ($calendarOptions['start_day'] === 0) Chris@76: $nWeekAdjust = $first_day_of_year === 0 && $first_day_of_year > 3 ? 0 : 1; Chris@76: else Chris@76: $nWeekAdjust = $calendarOptions['start_day'] > $first_day_of_year && $first_day_of_year !== 0 ? 2 : 1; Chris@76: Chris@76: $calendarGrid['week_number'] = (int) strftime('%U', mktime(0, 0, 0, $month, $day, $year)) + $nWeekAdjust; Chris@76: Chris@76: // If this crosses a year boundry and includes january it should be week one. Chris@76: if ((int) strftime('%Y', $curTimestamp + 518400) != $year && $calendarGrid['week_number'] > 53 && $first_day_of_next_year < 5) Chris@76: $calendarGrid['week_number'] = 1; Chris@76: } Chris@76: Chris@76: // This holds all the main data - there is at least one month! Chris@76: $calendarGrid['months'] = array(); Chris@76: $lastDay = 99; Chris@76: $curDay = $day; Chris@76: $curDayOfWeek = $calendarOptions['start_day']; Chris@76: for ($i = 0; $i < 7; $i++) Chris@76: { Chris@76: // Have we gone into a new month (Always happens first cycle too) Chris@76: if ($lastDay > $curDay) Chris@76: { Chris@76: $curMonth = $lastDay == 99 ? $month : ($month == 12 ? 1 : $month + 1); Chris@76: $curYear = $lastDay == 99 ? $year : ($curMonth == 1 && $month == 12 ? $year + 1 : $year); Chris@76: $calendarGrid['months'][$curMonth] = array( Chris@76: 'current_month' => $curMonth, Chris@76: 'current_year' => $curYear, Chris@76: 'days' => array(), Chris@76: ); Chris@76: } Chris@76: Chris@76: // Add todays information to the pile! Chris@76: $date = sprintf('%04d-%02d-%02d', $curYear, $curMonth, $curDay); Chris@76: Chris@76: $calendarGrid['months'][$curMonth]['days'][$curDay] = array( Chris@76: 'day' => $curDay, Chris@76: 'day_of_week' => $curDayOfWeek, Chris@76: 'date' => $date, Chris@76: 'is_today' => $date == $today['date'], Chris@76: 'holidays' => !empty($holidays[$date]) ? $holidays[$date] : array(), Chris@76: 'events' => !empty($events[$date]) ? $events[$date] : array(), Chris@76: 'birthdays' => !empty($bday[$date]) ? $bday[$date] : array() Chris@76: ); Chris@76: Chris@76: // Make the last day what the current day is and work out what the next day is. Chris@76: $lastDay = $curDay; Chris@76: $curTimestamp += 86400; Chris@76: $curDay = (int) strftime('%d', $curTimestamp); Chris@76: Chris@76: // Also increment the current day of the week. Chris@76: $curDayOfWeek = $curDayOfWeek >= 6 ? 0 : ++$curDayOfWeek; Chris@76: } Chris@76: Chris@76: // Set the previous and the next week's links. Chris@76: $calendarGrid['previous_week']['href'] = $scripturl . '?action=calendar;viewweek;year=' . $calendarGrid['previous_week']['year'] . ';month=' . $calendarGrid['previous_week']['month'] . ';day=' . $calendarGrid['previous_week']['day']; Chris@76: $calendarGrid['next_week']['href'] = $scripturl . '?action=calendar;viewweek;year=' . $calendarGrid['next_week']['year'] . ';month=' . $calendarGrid['next_week']['month'] . ';day=' . $calendarGrid['next_week']['day']; Chris@76: Chris@76: return $calendarGrid; Chris@76: } Chris@76: Chris@76: // Retrieve all events for the given days, independently of the users offset. Chris@76: function cache_getOffsetIndependentEvents($days_to_index) Chris@76: { Chris@76: global $sourcedir; Chris@76: Chris@76: $low_date = strftime('%Y-%m-%d', forum_time(false) - 24 * 3600); Chris@76: $high_date = strftime('%Y-%m-%d', forum_time(false) + $days_to_index * 24 * 3600); Chris@76: Chris@76: return array( Chris@76: 'data' => array( Chris@76: 'holidays' => getHolidayRange($low_date, $high_date), Chris@76: 'birthdays' => getBirthdayRange($low_date, $high_date), Chris@76: 'events' => getEventRange($low_date, $high_date, false), Chris@76: ), Chris@76: 'refresh_eval' => 'return \'' . strftime('%Y%m%d', forum_time(false)) . '\' != strftime(\'%Y%m%d\', forum_time(false)) || (!empty($modSettings[\'calendar_updated\']) && ' . time() . ' < $modSettings[\'calendar_updated\']);', Chris@76: 'expires' => time() + 3600, Chris@76: ); Chris@76: } Chris@76: Chris@76: // Called from the BoardIndex to display the current day's events on the board index. Chris@76: function cache_getRecentEvents($eventOptions) Chris@76: { Chris@76: global $modSettings, $user_info, $scripturl; Chris@76: Chris@76: // With the 'static' cached data we can calculate the user-specific data. Chris@76: $cached_data = cache_quick_get('calendar_index', 'Subs-Calendar.php', 'cache_getOffsetIndependentEvents', array($eventOptions['num_days_shown'])); Chris@76: Chris@76: // Get the information about today (from user perspective). Chris@76: $today = getTodayInfo(); Chris@76: Chris@76: $return_data = array( Chris@76: 'calendar_holidays' => array(), Chris@76: 'calendar_birthdays' => array(), Chris@76: 'calendar_events' => array(), Chris@76: ); Chris@76: Chris@76: // Set the event span to be shown in seconds. Chris@76: $days_for_index = $eventOptions['num_days_shown'] * 86400; Chris@76: Chris@76: // Get the current member time/date. Chris@76: $now = forum_time(); Chris@76: Chris@76: // Holidays between now and now + days. Chris@76: for ($i = $now; $i < $now + $days_for_index; $i += 86400) Chris@76: { Chris@76: if (isset($cached_data['holidays'][strftime('%Y-%m-%d', $i)])) Chris@76: $return_data['calendar_holidays'] = array_merge($return_data['calendar_holidays'], $cached_data['holidays'][strftime('%Y-%m-%d', $i)]); Chris@76: } Chris@76: Chris@76: // Happy Birthday, guys and gals! Chris@76: for ($i = $now; $i < $now + $days_for_index; $i += 86400) Chris@76: { Chris@76: $loop_date = strftime('%Y-%m-%d', $i); Chris@76: if (isset($cached_data['birthdays'][$loop_date])) Chris@76: { Chris@76: foreach ($cached_data['birthdays'][$loop_date] as $index => $dummy) Chris@76: $cached_data['birthdays'][strftime('%Y-%m-%d', $i)][$index]['is_today'] = $loop_date === $today['date']; Chris@76: $return_data['calendar_birthdays'] = array_merge($return_data['calendar_birthdays'], $cached_data['birthdays'][$loop_date]); Chris@76: } Chris@76: } Chris@76: Chris@76: $duplicates = array(); Chris@76: for ($i = $now; $i < $now + $days_for_index; $i += 86400) Chris@76: { Chris@76: // Determine the date of the current loop step. Chris@76: $loop_date = strftime('%Y-%m-%d', $i); Chris@76: Chris@76: // No events today? Check the next day. Chris@76: if (empty($cached_data['events'][$loop_date])) Chris@76: continue; Chris@76: Chris@76: // Loop through all events to add a few last-minute values. Chris@76: foreach ($cached_data['events'][$loop_date] as $ev => $event) Chris@76: { Chris@76: // Create a shortcut variable for easier access. Chris@76: $this_event = &$cached_data['events'][$loop_date][$ev]; Chris@76: Chris@76: // Skip duplicates. Chris@76: if (isset($duplicates[$this_event['topic'] . $this_event['title']])) Chris@76: { Chris@76: unset($cached_data['events'][$loop_date][$ev]); Chris@76: continue; Chris@76: } Chris@76: else Chris@76: $duplicates[$this_event['topic'] . $this_event['title']] = true; Chris@76: Chris@76: // Might be set to true afterwards, depending on the permissions. Chris@76: $this_event['can_edit'] = false; Chris@76: $this_event['is_today'] = $loop_date === $today['date']; Chris@76: $this_event['date'] = $loop_date; Chris@76: } Chris@76: Chris@76: if (!empty($cached_data['events'][$loop_date])) Chris@76: $return_data['calendar_events'] = array_merge($return_data['calendar_events'], $cached_data['events'][$loop_date]); Chris@76: } Chris@76: Chris@76: // Mark the last item so that a list separator can be used in the template. Chris@76: for ($i = 0, $n = count($return_data['calendar_birthdays']); $i < $n; $i++) Chris@76: $return_data['calendar_birthdays'][$i]['is_last'] = !isset($return_data['calendar_birthdays'][$i + 1]); Chris@76: for ($i = 0, $n = count($return_data['calendar_events']); $i < $n; $i++) Chris@76: $return_data['calendar_events'][$i]['is_last'] = !isset($return_data['calendar_events'][$i + 1]); Chris@76: Chris@76: return array( Chris@76: 'data' => $return_data, Chris@76: 'expires' => time() + 3600, Chris@76: 'refresh_eval' => 'return \'' . strftime('%Y%m%d', forum_time(false)) . '\' != strftime(\'%Y%m%d\', forum_time(false)) || (!empty($modSettings[\'calendar_updated\']) && ' . time() . ' < $modSettings[\'calendar_updated\']);', Chris@76: 'post_retri_eval' => ' Chris@76: global $context, $scripturl, $user_info; Chris@76: Chris@76: foreach ($cache_block[\'data\'][\'calendar_events\'] as $k => $event) Chris@76: { Chris@76: // Remove events that the user may not see or wants to ignore. Chris@76: if ((count(array_intersect($user_info[\'groups\'], $event[\'allowed_groups\'])) === 0 && !allowedTo(\'admin_forum\') && !empty($event[\'id_board\'])) || in_array($event[\'id_board\'], $user_info[\'ignoreboards\'])) Chris@76: unset($cache_block[\'data\'][\'calendar_events\'][$k]); Chris@76: else Chris@76: { Chris@76: // Whether the event can be edited depends on the permissions. Chris@76: $cache_block[\'data\'][\'calendar_events\'][$k][\'can_edit\'] = allowedTo(\'calendar_edit_any\') || ($event[\'poster\'] == $user_info[\'id\'] && allowedTo(\'calendar_edit_own\')); Chris@76: Chris@76: // The added session code makes this URL not cachable. Chris@76: $cache_block[\'data\'][\'calendar_events\'][$k][\'modify_href\'] = $scripturl . \'?action=\' . ($event[\'topic\'] == 0 ? \'calendar;sa=post;\' : \'post;msg=\' . $event[\'msg\'] . \';topic=\' . $event[\'topic\'] . \'.0;calendar;\') . \'eventid=\' . $event[\'id\'] . \';\' . $context[\'session_var\'] . \'=\' . $context[\'session_id\']; Chris@76: } Chris@76: } Chris@76: Chris@76: if (empty($params[0][\'include_holidays\'])) Chris@76: $cache_block[\'data\'][\'calendar_holidays\'] = array(); Chris@76: if (empty($params[0][\'include_birthdays\'])) Chris@76: $cache_block[\'data\'][\'calendar_birthdays\'] = array(); Chris@76: if (empty($params[0][\'include_events\'])) Chris@76: $cache_block[\'data\'][\'calendar_events\'] = array(); Chris@76: Chris@76: $cache_block[\'data\'][\'show_calendar\'] = !empty($cache_block[\'data\'][\'calendar_holidays\']) || !empty($cache_block[\'data\'][\'calendar_birthdays\']) || !empty($cache_block[\'data\'][\'calendar_events\']);', Chris@76: ); Chris@76: } Chris@76: Chris@76: // Makes sure the calendar post is valid. Chris@76: function validateEventPost() Chris@76: { Chris@76: global $modSettings, $txt, $sourcedir, $smcFunc; Chris@76: Chris@76: if (!isset($_POST['deleteevent'])) Chris@76: { Chris@76: // No month? No year? Chris@76: if (!isset($_POST['month'])) Chris@76: fatal_lang_error('event_month_missing', false); Chris@76: if (!isset($_POST['year'])) Chris@76: fatal_lang_error('event_year_missing', false); Chris@76: Chris@76: // Check the month and year... Chris@76: if ($_POST['month'] < 1 || $_POST['month'] > 12) Chris@76: fatal_lang_error('invalid_month', false); Chris@76: if ($_POST['year'] < $modSettings['cal_minyear'] || $_POST['year'] > $modSettings['cal_maxyear']) Chris@76: fatal_lang_error('invalid_year', false); Chris@76: } Chris@76: Chris@76: // Make sure they're allowed to post... Chris@76: isAllowedTo('calendar_post'); Chris@76: Chris@76: if (isset($_POST['span'])) Chris@76: { Chris@76: // Make sure it's turned on and not some fool trying to trick it. Chris@76: if (empty($modSettings['cal_allowspan'])) Chris@76: fatal_lang_error('no_span', false); Chris@76: if ($_POST['span'] < 1 || $_POST['span'] > $modSettings['cal_maxspan']) Chris@76: fatal_lang_error('invalid_days_numb', false); Chris@76: } Chris@76: Chris@76: // There is no need to validate the following values if we are just deleting the event. Chris@76: if (!isset($_POST['deleteevent'])) Chris@76: { Chris@76: // No day? Chris@76: if (!isset($_POST['day'])) Chris@76: fatal_lang_error('event_day_missing', false); Chris@76: if (!isset($_POST['evtitle']) && !isset($_POST['subject'])) Chris@76: fatal_lang_error('event_title_missing', false); Chris@76: elseif (!isset($_POST['evtitle'])) Chris@76: $_POST['evtitle'] = $_POST['subject']; Chris@76: Chris@76: // Bad day? Chris@76: if (!checkdate($_POST['month'], $_POST['day'], $_POST['year'])) Chris@76: fatal_lang_error('invalid_date', false); Chris@76: Chris@76: // No title? Chris@76: if ($smcFunc['htmltrim']($_POST['evtitle']) === '') Chris@76: fatal_lang_error('no_event_title', false); Chris@76: if ($smcFunc['strlen']($_POST['evtitle']) > 30) Chris@76: $_POST['evtitle'] = $smcFunc['substr']($_POST['evtitle'], 0, 30); Chris@76: $_POST['evtitle'] = str_replace(';', '', $_POST['evtitle']); Chris@76: } Chris@76: } Chris@76: Chris@76: // Get the event's poster. Chris@76: function getEventPoster($event_id) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // A simple database query, how hard can that be? Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member Chris@76: FROM {db_prefix}calendar Chris@76: WHERE id_event = {int:id_event} Chris@76: LIMIT 1', Chris@76: array( Chris@76: 'id_event' => $event_id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // No results, return false. Chris@76: if ($smcFunc['db_num_rows'] === 0) Chris@76: return false; Chris@76: Chris@76: // Grab the results and return. Chris@76: list ($poster) = $smcFunc['db_fetch_row']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: return $poster; Chris@76: } Chris@76: Chris@76: // Consolidating the various INSERT statements into this function. Chris@76: function insertEvent(&$eventOptions) Chris@76: { Chris@76: global $modSettings, $smcFunc; Chris@76: Chris@76: // Add special chars to the title. Chris@76: $eventOptions['title'] = $smcFunc['htmlspecialchars']($eventOptions['title'], ENT_QUOTES); Chris@76: Chris@76: // Add some sanity checking to the span. Chris@76: $eventOptions['span'] = isset($eventOptions['span']) && $eventOptions['span'] > 0 ? (int) $eventOptions['span'] : 0; Chris@76: Chris@76: // Make sure the start date is in ISO order. Chris@76: if (($num_results = sscanf($eventOptions['start_date'], '%d-%d-%d', $year, $month, $day)) !== 3) Chris@76: trigger_error('modifyEvent(): invalid start date format given', E_USER_ERROR); Chris@76: Chris@76: // Set the end date (if not yet given) Chris@76: if (!isset($eventOptions['end_date'])) Chris@76: $eventOptions['end_date'] = strftime('%Y-%m-%d', mktime(0, 0, 0, $month, $day, $year) + $eventOptions['span'] * 86400); Chris@76: Chris@76: // If no topic and board are given, they are not linked to a topic. Chris@76: $eventOptions['board'] = isset($eventOptions['board']) ? (int) $eventOptions['board'] : 0; Chris@76: $eventOptions['topic'] = isset($eventOptions['topic']) ? (int) $eventOptions['topic'] : 0; Chris@76: Chris@76: // Insert the event! Chris@76: $smcFunc['db_insert']('', Chris@76: '{db_prefix}calendar', Chris@76: array( Chris@76: 'id_board' => 'int', 'id_topic' => 'int', 'title' => 'string-60', 'id_member' => 'int', Chris@76: 'start_date' => 'date', 'end_date' => 'date', Chris@76: ), Chris@76: array( Chris@76: $eventOptions['board'], $eventOptions['topic'], $eventOptions['title'], $eventOptions['member'], Chris@76: $eventOptions['start_date'], $eventOptions['end_date'], Chris@76: ), Chris@76: array('id_event') Chris@76: ); Chris@76: Chris@76: // Store the just inserted id_event for future reference. Chris@76: $eventOptions['id'] = $smcFunc['db_insert_id']('{db_prefix}calendar', 'id_event'); Chris@76: Chris@76: // Update the settings to show something calendarish was updated. Chris@76: updateSettings(array( Chris@76: 'calendar_updated' => time(), Chris@76: )); Chris@76: } Chris@76: Chris@76: function modifyEvent($event_id, &$eventOptions) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // Properly sanitize the title. Chris@76: $eventOptions['title'] = $smcFunc['htmlspecialchars']($eventOptions['title'], ENT_QUOTES); Chris@76: Chris@76: // Scan the start date for validity and get its components. Chris@76: if (($num_results = sscanf($eventOptions['start_date'], '%d-%d-%d', $year, $month, $day)) !== 3) Chris@76: trigger_error('modifyEvent(): invalid start date format given', E_USER_ERROR); Chris@76: Chris@76: // Default span to 0 days. Chris@76: $eventOptions['span'] = isset($eventOptions['span']) ? (int) $eventOptions['span'] : 0; Chris@76: Chris@76: // Set the end date to the start date + span (if the end date wasn't already given). Chris@76: if (!isset($eventOptions['end_date'])) Chris@76: $eventOptions['end_date'] = strftime('%Y-%m-%d', mktime(0, 0, 0, $month, $day, $year) + $eventOptions['span'] * 86400); Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}calendar Chris@76: SET Chris@76: start_date = {date:start_date}, Chris@76: end_date = {date:end_date}, Chris@76: title = SUBSTRING({string:title}, 1, 60), Chris@76: id_board = {int:id_board}, Chris@76: id_topic = {int:id_topic} Chris@76: WHERE id_event = {int:id_event}', Chris@76: array( Chris@76: 'start_date' => $eventOptions['start_date'], Chris@76: 'end_date' => $eventOptions['end_date'], Chris@76: 'title' => $eventOptions['title'], Chris@76: 'id_board' => isset($eventOptions['board']) ? (int) $eventOptions['board'] : 0, Chris@76: 'id_topic' => isset($eventOptions['topic']) ? (int) $eventOptions['topic'] : 0, Chris@76: 'id_event' => $event_id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: updateSettings(array( Chris@76: 'calendar_updated' => time(), Chris@76: )); Chris@76: } Chris@76: Chris@76: function removeEvent($event_id) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}calendar Chris@76: WHERE id_event = {int:id_event}', Chris@76: array( Chris@76: 'id_event' => $event_id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: updateSettings(array( Chris@76: 'calendar_updated' => time(), Chris@76: )); Chris@76: } Chris@76: Chris@76: function getEventProperties($event_id) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT Chris@76: c.id_event, c.id_board, c.id_topic, MONTH(c.start_date) AS month, Chris@76: DAYOFMONTH(c.start_date) AS day, YEAR(c.start_date) AS year, Chris@76: (TO_DAYS(c.end_date) - TO_DAYS(c.start_date)) AS span, c.id_member, c.title, Chris@76: t.id_first_msg, t.id_member_started Chris@76: FROM {db_prefix}calendar AS c Chris@76: LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = c.id_topic) Chris@76: WHERE c.id_event = {int:id_event}', Chris@76: array( Chris@76: 'id_event' => $event_id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // If nothing returned, we are in poo, poo. Chris@76: if ($smcFunc['db_num_rows']($request) === 0) Chris@76: return false; Chris@76: Chris@76: $row = $smcFunc['db_fetch_assoc']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: $return_value = array( Chris@76: 'boards' => array(), Chris@76: 'board' => $row['id_board'], Chris@76: 'new' => 0, Chris@76: 'eventid' => $event_id, Chris@76: 'year' => $row['year'], Chris@76: 'month' => $row['month'], Chris@76: 'day' => $row['day'], Chris@76: 'title' => $row['title'], Chris@76: 'span' => 1 + $row['span'], Chris@76: 'member' => $row['id_member'], Chris@76: 'topic' => array( Chris@76: 'id' => $row['id_topic'], Chris@76: 'member_started' => $row['id_member_started'], Chris@76: 'first_msg' => $row['id_first_msg'], Chris@76: ), Chris@76: ); Chris@76: Chris@76: $return_value['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $return_value['month'] == 12 ? 1 : $return_value['month'] + 1, 0, $return_value['month'] == 12 ? $return_value['year'] + 1 : $return_value['year'])); Chris@76: Chris@76: return $return_value; Chris@76: } Chris@76: Chris@76: function list_getHolidays($start, $items_per_page, $sort) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title Chris@76: FROM {db_prefix}calendar_holidays Chris@76: ORDER BY {raw:sort} Chris@76: LIMIT ' . $start . ', ' . $items_per_page, Chris@76: array( Chris@76: 'sort' => $sort, Chris@76: ) Chris@76: ); Chris@76: $holidays = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $holidays[] = $row; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: return $holidays; Chris@76: } Chris@76: Chris@76: function list_getNumHolidays() Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT COUNT(*) Chris@76: FROM {db_prefix}calendar_holidays', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: list($num_items) = $smcFunc['db_fetch_row']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: return $num_items; Chris@76: } Chris@76: Chris@76: function removeHolidays($holiday_ids) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}calendar_holidays Chris@76: WHERE id_holiday IN ({array_int:id_holiday})', Chris@76: array( Chris@76: 'id_holiday' => $holiday_ids, Chris@76: ) Chris@76: ); Chris@76: Chris@76: updateSettings(array( Chris@76: 'calendar_updated' => time(), Chris@76: )); Chris@76: } Chris@76: Chris@76: ?>