annotate forum/Sources/Subs-Calendar.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
rev   line source
Chris@76 1 <?php
Chris@76 2
Chris@76 3 /**
Chris@76 4 * Simple Machines Forum (SMF)
Chris@76 5 *
Chris@76 6 * @package SMF
Chris@76 7 * @author Simple Machines http://www.simplemachines.org
Chris@76 8 * @copyright 2011 Simple Machines
Chris@76 9 * @license http://www.simplemachines.org/about/smf/license.php BSD
Chris@76 10 *
Chris@76 11 * @version 2.0
Chris@76 12 */
Chris@76 13
Chris@76 14 if (!defined('SMF'))
Chris@76 15 die('Hacking attempt...');
Chris@76 16
Chris@76 17 /* This file contains several functions for retrieving and manipulating
Chris@76 18 calendar events, birthdays and holidays.
Chris@76 19
Chris@76 20 array getBirthdayRange(string earliest_date, string latest_date)
Chris@76 21 - finds all the birthdays in the specified range of days.
Chris@76 22 - earliest_date and latest_date are inclusive, and should both be in
Chris@76 23 the YYYY-MM-DD format.
Chris@76 24 - works with birthdays set for no year, or any other year, and
Chris@76 25 respects month and year boundaries.
Chris@76 26 - returns an array of days, each of which an array of birthday
Chris@76 27 information for the context.
Chris@76 28
Chris@76 29 array getEventRange(string earliest_date, string latest_date,
Chris@76 30 bool use_permissions = true)
Chris@76 31 - finds all the posted calendar events within a date range.
Chris@76 32 - both the earliest_date and latest_date should be in the standard
Chris@76 33 YYYY-MM-DD format.
Chris@76 34 - censors the posted event titles.
Chris@76 35 - uses the current user's permissions if use_permissions is true,
Chris@76 36 otherwise it does nothing "permission specific".
Chris@76 37 - returns an array of contextual information if use_permissions is
Chris@76 38 true, and an array of the data needed to build that otherwise.
Chris@76 39
Chris@76 40 array getHolidayRange(string earliest_date, string latest_date)
Chris@76 41 - finds all the applicable holidays for the specified date range.
Chris@76 42 - earliest_date and latest_date should be YYYY-MM-DD.
Chris@76 43 - returns an array of days, which are all arrays of holiday names.
Chris@76 44
Chris@76 45 void canLinkEvent()
Chris@76 46 - checks if the current user can link the current topic to the
Chris@76 47 calendar, permissions et al.
Chris@76 48 - this requires the calendar_post permission, a forum moderator, or a
Chris@76 49 topic starter.
Chris@76 50 - expects the $topic and $board variables to be set.
Chris@76 51 - if the user doesn't have proper permissions, an error will be shown.
Chris@76 52
Chris@76 53 array getTodayInfo()
Chris@76 54 - returns an array with the current date, day, month, and year.
Chris@76 55 - takes the users time offset into account.
Chris@76 56
Chris@76 57 array getCalendarGrid(int month, int year, array calendarOptions)
Chris@76 58 - returns an array containing all the information needed to show a
Chris@76 59 calendar grid for the given month.
Chris@76 60 - also provides information (link, month, year) about the previous and
Chris@76 61 next month.
Chris@76 62
Chris@76 63 array getCalendarWeek(int month, int year, int day, array calendarOptions)
Chris@76 64 - as for getCalendarGrid but provides information relating to the week
Chris@76 65 within which the passed date sits.
Chris@76 66
Chris@76 67 array cache_getOffsetIndependentEvents(int days_to_index)
Chris@76 68 - cache callback function used to retrieve the birthdays, holidays, and
Chris@76 69 events between now and now + days_to_index.
Chris@76 70 - widens the search range by an extra 24 hours to support time offset
Chris@76 71 shifts.
Chris@76 72 - used by the cache_getRecentEvents function to get the information
Chris@76 73 needed to calculate the events taking the users time offset into
Chris@76 74 account.
Chris@76 75
Chris@76 76 array cache_getRecentEvents(array eventOptions)
Chris@76 77 - cache callback function used to retrieve the upcoming birthdays,
Chris@76 78 holidays, and events within the given period, taking into account
Chris@76 79 the users time offset.
Chris@76 80 - used by the board index and SSI to show the upcoming events.
Chris@76 81
Chris@76 82 void validateEventPost()
Chris@76 83 - checks if the calendar post was valid.
Chris@76 84
Chris@76 85 int getEventPoster(int event_id)
Chris@76 86 - gets the member_id of an event identified by event_id.
Chris@76 87 - returns false if the event was not found.
Chris@76 88
Chris@76 89 void insertEvent(array eventOptions)
Chris@76 90 - inserts the passed event information into the calendar table.
Chris@76 91 - allows to either set a time span (in days) or an end_date.
Chris@76 92 - does not check any permissions of any sort.
Chris@76 93
Chris@76 94 void modifyEvent(int event_id, array eventOptions)
Chris@76 95 - modifies an event.
Chris@76 96 - allows to either set a time span (in days) or an end_date.
Chris@76 97 - does not check any permissions of any sort.
Chris@76 98
Chris@76 99 void removeEvent(int event_id)
Chris@76 100 - removes an event.
Chris@76 101 - does no permission checks.
Chris@76 102 */
Chris@76 103
Chris@76 104 // Get all birthdays within the given time range.
Chris@76 105 function getBirthdayRange($low_date, $high_date)
Chris@76 106 {
Chris@76 107 global $scripturl, $modSettings, $smcFunc;
Chris@76 108
Chris@76 109 // We need to search for any birthday in this range, and whatever year that birthday is on.
Chris@76 110 $year_low = (int) substr($low_date, 0, 4);
Chris@76 111 $year_high = (int) substr($high_date, 0, 4);
Chris@76 112
Chris@76 113 // Collect all of the birthdays for this month. I know, it's a painful query.
Chris@76 114 $result = $smcFunc['db_query']('birthday_array', '
Chris@76 115 SELECT id_member, real_name, YEAR(birthdate) AS birth_year, birthdate
Chris@76 116 FROM {db_prefix}members
Chris@76 117 WHERE YEAR(birthdate) != {string:year_one}
Chris@76 118 AND MONTH(birthdate) != {int:no_month}
Chris@76 119 AND DAYOFMONTH(birthdate) != {int:no_day}
Chris@76 120 AND YEAR(birthdate) <= {int:max_year}
Chris@76 121 AND (
Chris@76 122 DATE_FORMAT(birthdate, {string:year_low}) BETWEEN {date:low_date} AND {date:high_date}' . ($year_low == $year_high ? '' : '
Chris@76 123 OR DATE_FORMAT(birthdate, {string:year_high}) BETWEEN {date:low_date} AND {date:high_date}') . '
Chris@76 124 )
Chris@76 125 AND is_activated = {int:is_activated}',
Chris@76 126 array(
Chris@76 127 'is_activated' => 1,
Chris@76 128 'no_month' => 0,
Chris@76 129 'no_day' => 0,
Chris@76 130 'year_one' => '0001',
Chris@76 131 'year_low' => $year_low . '-%m-%d',
Chris@76 132 'year_high' => $year_high . '-%m-%d',
Chris@76 133 'low_date' => $low_date,
Chris@76 134 'high_date' => $high_date,
Chris@76 135 'max_year' => $year_high,
Chris@76 136 )
Chris@76 137 );
Chris@76 138 $bday = array();
Chris@76 139 while ($row = $smcFunc['db_fetch_assoc']($result))
Chris@76 140 {
Chris@76 141 if ($year_low != $year_high)
Chris@76 142 $age_year = substr($row['birthdate'], 5) < substr($high_date, 5) ? $year_high : $year_low;
Chris@76 143 else
Chris@76 144 $age_year = $year_low;
Chris@76 145
Chris@76 146 $bday[$age_year . substr($row['birthdate'], 4)][] = array(
Chris@76 147 'id' => $row['id_member'],
Chris@76 148 'name' => $row['real_name'],
Chris@76 149 'age' => $row['birth_year'] > 4 && $row['birth_year'] <= $age_year ? $age_year - $row['birth_year'] : null,
Chris@76 150 'is_last' => false
Chris@76 151 );
Chris@76 152 }
Chris@76 153 $smcFunc['db_free_result']($result);
Chris@76 154
Chris@76 155 // Set is_last, so the themes know when to stop placing separators.
Chris@76 156 foreach ($bday as $mday => $array)
Chris@76 157 $bday[$mday][count($array) - 1]['is_last'] = true;
Chris@76 158
Chris@76 159 return $bday;
Chris@76 160 }
Chris@76 161
Chris@76 162 // Get all events within the given time range.
Chris@76 163 function getEventRange($low_date, $high_date, $use_permissions = true)
Chris@76 164 {
Chris@76 165 global $scripturl, $modSettings, $user_info, $smcFunc, $context;
Chris@76 166
Chris@76 167 $low_date_time = sscanf($low_date, '%04d-%02d-%02d');
Chris@76 168 $low_date_time = mktime(0, 0, 0, $low_date_time[1], $low_date_time[2], $low_date_time[0]);
Chris@76 169 $high_date_time = sscanf($high_date, '%04d-%02d-%02d');
Chris@76 170 $high_date_time = mktime(0, 0, 0, $high_date_time[1], $high_date_time[2], $high_date_time[0]);
Chris@76 171
Chris@76 172 // Find all the calendar info...
Chris@76 173 $result = $smcFunc['db_query']('', '
Chris@76 174 SELECT
Chris@76 175 cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
Chris@76 176 cal.id_board, b.member_groups, t.id_first_msg, t.approved, b.id_board
Chris@76 177 FROM {db_prefix}calendar AS cal
Chris@76 178 LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
Chris@76 179 LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
Chris@76 180 WHERE cal.start_date <= {date:high_date}
Chris@76 181 AND cal.end_date >= {date:low_date}' . ($use_permissions ? '
Chris@76 182 AND (cal.id_board = {int:no_board_link} OR {query_wanna_see_board})' : ''),
Chris@76 183 array(
Chris@76 184 'high_date' => $high_date,
Chris@76 185 'low_date' => $low_date,
Chris@76 186 'no_board_link' => 0,
Chris@76 187 )
Chris@76 188 );
Chris@76 189 $events = array();
Chris@76 190 while ($row = $smcFunc['db_fetch_assoc']($result))
Chris@76 191 {
Chris@76 192 // If the attached topic is not approved then for the moment pretend it doesn't exist
Chris@76 193 //!!! This should be fixed to show them all and then sort by approval state later?
Chris@76 194 if (!empty($row['id_first_msg']) && $modSettings['postmod_active'] && !$row['approved'])
Chris@76 195 continue;
Chris@76 196
Chris@76 197 // Force a censor of the title - as often these are used by others.
Chris@76 198 censorText($row['title'], $use_permissions ? false : true);
Chris@76 199
Chris@76 200 $start_date = sscanf($row['start_date'], '%04d-%02d-%02d');
Chris@76 201 $start_date = max(mktime(0, 0, 0, $start_date[1], $start_date[2], $start_date[0]), $low_date_time);
Chris@76 202 $end_date = sscanf($row['end_date'], '%04d-%02d-%02d');
Chris@76 203 $end_date = min(mktime(0, 0, 0, $end_date[1], $end_date[2], $end_date[0]), $high_date_time);
Chris@76 204
Chris@76 205 $lastDate = '';
Chris@76 206 for ($date = $start_date; $date <= $end_date; $date += 86400)
Chris@76 207 {
Chris@76 208 // Attempt to avoid DST problems.
Chris@76 209 //!!! Resolve this properly at some point.
Chris@76 210 if (strftime('%Y-%m-%d', $date) == $lastDate)
Chris@76 211 $date += 3601;
Chris@76 212 $lastDate = strftime('%Y-%m-%d', $date);
Chris@76 213
Chris@76 214 // If we're using permissions (calendar pages?) then just ouput normal contextual style information.
Chris@76 215 if ($use_permissions)
Chris@76 216 $events[strftime('%Y-%m-%d', $date)][] = array(
Chris@76 217 'id' => $row['id_event'],
Chris@76 218 'title' => $row['title'],
Chris@76 219 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
Chris@76 220 '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 221 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
Chris@76 222 'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
Chris@76 223 'start_date' => $row['start_date'],
Chris@76 224 'end_date' => $row['end_date'],
Chris@76 225 'is_last' => false,
Chris@76 226 'id_board' => $row['id_board'],
Chris@76 227 );
Chris@76 228 // Otherwise, this is going to be cached and the VIEWER'S permissions should apply... just put together some info.
Chris@76 229 else
Chris@76 230 $events[strftime('%Y-%m-%d', $date)][] = array(
Chris@76 231 'id' => $row['id_event'],
Chris@76 232 'title' => $row['title'],
Chris@76 233 'topic' => $row['id_topic'],
Chris@76 234 'msg' => $row['id_first_msg'],
Chris@76 235 'poster' => $row['id_member'],
Chris@76 236 'start_date' => $row['start_date'],
Chris@76 237 'end_date' => $row['end_date'],
Chris@76 238 'is_last' => false,
Chris@76 239 'allowed_groups' => explode(',', $row['member_groups']),
Chris@76 240 'id_board' => $row['id_board'],
Chris@76 241 'href' => $row['id_topic'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
Chris@76 242 'link' => $row['id_topic'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
Chris@76 243 'can_edit' => false,
Chris@76 244 );
Chris@76 245 }
Chris@76 246 }
Chris@76 247 $smcFunc['db_free_result']($result);
Chris@76 248
Chris@76 249 // If we're doing normal contextual data, go through and make things clear to the templates ;).
Chris@76 250 if ($use_permissions)
Chris@76 251 {
Chris@76 252 foreach ($events as $mday => $array)
Chris@76 253 $events[$mday][count($array) - 1]['is_last'] = true;
Chris@76 254 }
Chris@76 255
Chris@76 256 return $events;
Chris@76 257 }
Chris@76 258
Chris@76 259 // Get all holidays within the given time range.
Chris@76 260 function getHolidayRange($low_date, $high_date)
Chris@76 261 {
Chris@76 262 global $smcFunc;
Chris@76 263
Chris@76 264 // Get the lowest and highest dates for "all years".
Chris@76 265 if (substr($low_date, 0, 4) != substr($high_date, 0, 4))
Chris@76 266 $allyear_part = 'event_date BETWEEN {date:all_year_low} AND {date:all_year_dec}
Chris@76 267 OR event_date BETWEEN {date:all_year_jan} AND {date:all_year_high}';
Chris@76 268 else
Chris@76 269 $allyear_part = 'event_date BETWEEN {date:all_year_low} AND {date:all_year_high}';
Chris@76 270
Chris@76 271 // Find some holidays... ;).
Chris@76 272 $result = $smcFunc['db_query']('', '
Chris@76 273 SELECT event_date, YEAR(event_date) AS year, title
Chris@76 274 FROM {db_prefix}calendar_holidays
Chris@76 275 WHERE event_date BETWEEN {date:low_date} AND {date:high_date}
Chris@76 276 OR ' . $allyear_part,
Chris@76 277 array(
Chris@76 278 'low_date' => $low_date,
Chris@76 279 'high_date' => $high_date,
Chris@76 280 'all_year_low' => '0004' . substr($low_date, 4),
Chris@76 281 'all_year_high' => '0004' . substr($high_date, 4),
Chris@76 282 'all_year_jan' => '0004-01-01',
Chris@76 283 'all_year_dec' => '0004-12-31',
Chris@76 284 )
Chris@76 285 );
Chris@76 286 $holidays = array();
Chris@76 287 while ($row = $smcFunc['db_fetch_assoc']($result))
Chris@76 288 {
Chris@76 289 if (substr($low_date, 0, 4) != substr($high_date, 0, 4))
Chris@76 290 $event_year = substr($row['event_date'], 5) < substr($high_date, 5) ? substr($high_date, 0, 4) : substr($low_date, 0, 4);
Chris@76 291 else
Chris@76 292 $event_year = substr($low_date, 0, 4);
Chris@76 293
Chris@76 294 $holidays[$event_year . substr($row['event_date'], 4)][] = $row['title'];
Chris@76 295 }
Chris@76 296 $smcFunc['db_free_result']($result);
Chris@76 297
Chris@76 298 return $holidays;
Chris@76 299 }
Chris@76 300
Chris@76 301 // Does permission checks to see if an event can be linked to a board/topic.
Chris@76 302 function canLinkEvent()
Chris@76 303 {
Chris@76 304 global $user_info, $topic, $board, $smcFunc;
Chris@76 305
Chris@76 306 // If you can't post, you can't link.
Chris@76 307 isAllowedTo('calendar_post');
Chris@76 308
Chris@76 309 // No board? No topic?!?
Chris@76 310 if (empty($board))
Chris@76 311 fatal_lang_error('missing_board_id', false);
Chris@76 312 if (empty($topic))
Chris@76 313 fatal_lang_error('missing_topic_id', false);
Chris@76 314
Chris@76 315 // Administrator, Moderator, or owner. Period.
Chris@76 316 if (!allowedTo('admin_forum') && !allowedTo('moderate_board'))
Chris@76 317 {
Chris@76 318 // Not admin or a moderator of this board. You better be the owner - or else.
Chris@76 319 $result = $smcFunc['db_query']('', '
Chris@76 320 SELECT id_member_started
Chris@76 321 FROM {db_prefix}topics
Chris@76 322 WHERE id_topic = {int:current_topic}
Chris@76 323 LIMIT 1',
Chris@76 324 array(
Chris@76 325 'current_topic' => $topic,
Chris@76 326 )
Chris@76 327 );
Chris@76 328 if ($row = $smcFunc['db_fetch_assoc']($result))
Chris@76 329 {
Chris@76 330 // Not the owner of the topic.
Chris@76 331 if ($row['id_member_started'] != $user_info['id'])
Chris@76 332 fatal_lang_error('not_your_topic', 'user');
Chris@76 333 }
Chris@76 334 // Topic/Board doesn't exist.....
Chris@76 335 else
Chris@76 336 fatal_lang_error('calendar_no_topic', 'general');
Chris@76 337 $smcFunc['db_free_result']($result);
Chris@76 338 }
Chris@76 339 }
Chris@76 340
Chris@76 341 // Returns date information about 'today' relative to the users time offset.
Chris@76 342 function getTodayInfo()
Chris@76 343 {
Chris@76 344 return array(
Chris@76 345 'day' => (int) strftime('%d', forum_time()),
Chris@76 346 'month' => (int) strftime('%m', forum_time()),
Chris@76 347 'year' => (int) strftime('%Y', forum_time()),
Chris@76 348 'date' => strftime('%Y-%m-%d', forum_time()),
Chris@76 349 );
Chris@76 350 }
Chris@76 351
Chris@76 352 // Returns the information needed to show a calendar grid for the given month.
Chris@76 353 function getCalendarGrid($month, $year, $calendarOptions)
Chris@76 354 {
Chris@76 355 global $scripturl, $modSettings;
Chris@76 356
Chris@76 357 // Eventually this is what we'll be returning.
Chris@76 358 $calendarGrid = array(
Chris@76 359 'week_days' => array(),
Chris@76 360 'weeks' => array(),
Chris@76 361 'short_day_titles' => !empty($calendarOptions['short_day_titles']),
Chris@76 362 'current_month' => $month,
Chris@76 363 'current_year' => $year,
Chris@76 364 'show_next_prev' => !empty($calendarOptions['show_next_prev']),
Chris@76 365 'show_week_links' => !empty($calendarOptions['show_week_links']),
Chris@76 366 'previous_calendar' => array(
Chris@76 367 'year' => $month == 1 ? $year - 1 : $year,
Chris@76 368 'month' => $month == 1 ? 12 : $month - 1,
Chris@76 369 'disabled' => $modSettings['cal_minyear'] > ($month == 1 ? $year - 1 : $year),
Chris@76 370 ),
Chris@76 371 'next_calendar' => array(
Chris@76 372 'year' => $month == 12 ? $year + 1 : $year,
Chris@76 373 'month' => $month == 12 ? 1 : $month + 1,
Chris@76 374 'disabled' => $modSettings['cal_maxyear'] < ($month == 12 ? $year + 1 : $year),
Chris@76 375 ),
Chris@76 376 //!!! Better tweaks?
Chris@76 377 'size' => isset($calendarOptions['size']) ? $calendarOptions['size'] : 'large',
Chris@76 378 );
Chris@76 379
Chris@76 380 // Get todays date.
Chris@76 381 $today = getTodayInfo();
Chris@76 382
Chris@76 383 // Get information about this month.
Chris@76 384 $month_info = array(
Chris@76 385 'first_day' => array(
Chris@76 386 'day_of_week' => (int) strftime('%w', mktime(0, 0, 0, $month, 1, $year)),
Chris@76 387 'week_num' => (int) strftime('%U', mktime(0, 0, 0, $month, 1, $year)),
Chris@76 388 'date' => strftime('%Y-%m-%d', mktime(0, 0, 0, $month, 1, $year)),
Chris@76 389 ),
Chris@76 390 'last_day' => array(
Chris@76 391 'day_of_month' => (int) strftime('%d', mktime(0, 0, 0, $month == 12 ? 1 : $month + 1, 0, $month == 12 ? $year + 1 : $year)),
Chris@76 392 'date' => strftime('%Y-%m-%d', mktime(0, 0, 0, $month == 12 ? 1 : $month + 1, 0, $month == 12 ? $year + 1 : $year)),
Chris@76 393 ),
Chris@76 394 'first_day_of_year' => (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year)),
Chris@76 395 'first_day_of_next_year' => (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year + 1)),
Chris@76 396 );
Chris@76 397
Chris@76 398 // The number of days the first row is shifted to the right for the starting day.
Chris@76 399 $nShift = $month_info['first_day']['day_of_week'];
Chris@76 400
Chris@76 401 $calendarOptions['start_day'] = empty($calendarOptions['start_day']) ? 0 : (int) $calendarOptions['start_day'];
Chris@76 402
Chris@76 403 // Starting any day other than Sunday means a shift...
Chris@76 404 if (!empty($calendarOptions['start_day']))
Chris@76 405 {
Chris@76 406 $nShift -= $calendarOptions['start_day'];
Chris@76 407 if ($nShift < 0)
Chris@76 408 $nShift = 7 + $nShift;
Chris@76 409 }
Chris@76 410
Chris@76 411 // Number of rows required to fit the month.
Chris@76 412 $nRows = floor(($month_info['last_day']['day_of_month'] + $nShift) / 7);
Chris@76 413 if (($month_info['last_day']['day_of_month'] + $nShift) % 7)
Chris@76 414 $nRows++;
Chris@76 415
Chris@76 416 // Fetch the arrays for birthdays, posted events, and holidays.
Chris@76 417 $bday = $calendarOptions['show_birthdays'] ? getBirthdayRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array();
Chris@76 418 $events = $calendarOptions['show_events'] ? getEventRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array();
Chris@76 419 $holidays = $calendarOptions['show_holidays'] ? getHolidayRange($month_info['first_day']['date'], $month_info['last_day']['date']) : array();
Chris@76 420
Chris@76 421 // Days of the week taking into consideration that they may want it to start on any day.
Chris@76 422 $count = $calendarOptions['start_day'];
Chris@76 423 for ($i = 0; $i < 7; $i++)
Chris@76 424 {
Chris@76 425 $calendarGrid['week_days'][] = $count;
Chris@76 426 $count++;
Chris@76 427 if ($count == 7)
Chris@76 428 $count = 0;
Chris@76 429 }
Chris@76 430
Chris@76 431 // An adjustment value to apply to all calculated week numbers.
Chris@76 432 if (!empty($calendarOptions['show_week_num']))
Chris@76 433 {
Chris@76 434 // If the first day of the year is a Sunday, then there is no
Chris@76 435 // adjustment to be made. However, if the first day of the year is not
Chris@76 436 // a Sunday, then there is a partial week at the start of the year
Chris@76 437 // that needs to be accounted for.
Chris@76 438 if ($calendarOptions['start_day'] === 0)
Chris@76 439 $nWeekAdjust = $month_info['first_day_of_year'] === 0 ? 0 : 1;
Chris@76 440 // If we are viewing the weeks, with a starting date other than Sunday,
Chris@76 441 // then things get complicated! Basically, as PHP is calculating the
Chris@76 442 // weeks with a Sunday starting date, we need to take this into account
Chris@76 443 // and offset the whole year dependant on whether the first day in the
Chris@76 444 // year is above or below our starting date. Note that we offset by
Chris@76 445 // two, as some of this will get undone quite quickly by the statement
Chris@76 446 // below.
Chris@76 447 else
Chris@76 448 $nWeekAdjust = $calendarOptions['start_day'] > $month_info['first_day_of_year'] && $month_info['first_day_of_year'] !== 0 ? 2 : 1;
Chris@76 449
Chris@76 450 // If our week starts on a day greater than the day the month starts
Chris@76 451 // on, then our week numbers will be one too high. So we need to
Chris@76 452 // reduce it by one - all these thoughts of offsets makes my head
Chris@76 453 // hurt...
Chris@76 454 if ($month_info['first_day']['day_of_week'] < $calendarOptions['start_day'] || $month_info['first_day_of_year'] > 4)
Chris@76 455 $nWeekAdjust--;
Chris@76 456 }
Chris@76 457 else
Chris@76 458 $nWeekAdjust = 0;
Chris@76 459
Chris@76 460 // Iterate through each week.
Chris@76 461 $calendarGrid['weeks'] = array();
Chris@76 462 for ($nRow = 0; $nRow < $nRows; $nRow++)
Chris@76 463 {
Chris@76 464 // Start off the week - and don't let it go above 52, since that's the number of weeks in a year.
Chris@76 465 $calendarGrid['weeks'][$nRow] = array(
Chris@76 466 'days' => array(),
Chris@76 467 'number' => $month_info['first_day']['week_num'] + $nRow + $nWeekAdjust
Chris@76 468 );
Chris@76 469 // Handle the dreaded "week 53", it can happen, but only once in a blue moon ;)
Chris@76 470 if ($calendarGrid['weeks'][$nRow]['number'] == 53 && $nShift != 4 && $month_info['first_day_of_next_year'] < 4)
Chris@76 471 $calendarGrid['weeks'][$nRow]['number'] = 1;
Chris@76 472
Chris@76 473 // And figure out all the days.
Chris@76 474 for ($nCol = 0; $nCol < 7; $nCol++)
Chris@76 475 {
Chris@76 476 $nDay = ($nRow * 7) + $nCol - $nShift + 1;
Chris@76 477
Chris@76 478 if ($nDay < 1 || $nDay > $month_info['last_day']['day_of_month'])
Chris@76 479 $nDay = 0;
Chris@76 480
Chris@76 481 $date = sprintf('%04d-%02d-%02d', $year, $month, $nDay);
Chris@76 482
Chris@76 483 $calendarGrid['weeks'][$nRow]['days'][$nCol] = array(
Chris@76 484 'day' => $nDay,
Chris@76 485 'date' => $date,
Chris@76 486 'is_today' => $date == $today['date'],
Chris@76 487 'is_first_day' => !empty($calendarOptions['show_week_num']) && (($month_info['first_day']['day_of_week'] + $nDay - 1) % 7 == $calendarOptions['start_day']),
Chris@76 488 'holidays' => !empty($holidays[$date]) ? $holidays[$date] : array(),
Chris@76 489 'events' => !empty($events[$date]) ? $events[$date] : array(),
Chris@76 490 'birthdays' => !empty($bday[$date]) ? $bday[$date] : array()
Chris@76 491 );
Chris@76 492 }
Chris@76 493 }
Chris@76 494
Chris@76 495 // Set the previous and the next month's links.
Chris@76 496 $calendarGrid['previous_calendar']['href'] = $scripturl . '?action=calendar;year=' . $calendarGrid['previous_calendar']['year'] . ';month=' . $calendarGrid['previous_calendar']['month'];
Chris@76 497 $calendarGrid['next_calendar']['href'] = $scripturl . '?action=calendar;year=' . $calendarGrid['next_calendar']['year'] . ';month=' . $calendarGrid['next_calendar']['month'];
Chris@76 498
Chris@76 499 return $calendarGrid;
Chris@76 500 }
Chris@76 501
Chris@76 502 // Returns the information needed to show a calendar for the given week.
Chris@76 503 function getCalendarWeek($month, $year, $day, $calendarOptions)
Chris@76 504 {
Chris@76 505 global $scripturl, $modSettings;
Chris@76 506
Chris@76 507 // Get todays date.
Chris@76 508 $today = getTodayInfo();
Chris@76 509
Chris@76 510 // What is the actual "start date" for the passed day.
Chris@76 511 $calendarOptions['start_day'] = empty($calendarOptions['start_day']) ? 0 : (int) $calendarOptions['start_day'];
Chris@76 512 $day_of_week = (int) strftime('%w', mktime(0, 0, 0, $month, $day, $year));
Chris@76 513 if ($day_of_week != $calendarOptions['start_day'])
Chris@76 514 {
Chris@76 515 // Here we offset accordingly to get things to the real start of a week.
Chris@76 516 $date_diff = $day_of_week - $calendarOptions['start_day'];
Chris@76 517 if ($date_diff < 0)
Chris@76 518 $date_diff += 7;
Chris@76 519 $new_timestamp = mktime(0, 0, 0, $month, $day, $year) - $date_diff * 86400;
Chris@76 520 $day = (int) strftime('%d', $new_timestamp);
Chris@76 521 $month = (int) strftime('%m', $new_timestamp);
Chris@76 522 $year = (int) strftime('%Y', $new_timestamp);
Chris@76 523 }
Chris@76 524
Chris@76 525 // Now start filling in the calendar grid.
Chris@76 526 $calendarGrid = array(
Chris@76 527 'show_next_prev' => !empty($calendarOptions['show_next_prev']),
Chris@76 528 // Previous week is easy - just step back one day.
Chris@76 529 'previous_week' => array(
Chris@76 530 'year' => $day == 1 ? ($month == 1 ? $year - 1 : $year) : $year,
Chris@76 531 'month' => $day == 1 ? ($month == 1 ? 12 : $month - 1) : $month,
Chris@76 532 'day' => $day == 1 ? 28 : $day - 1,
Chris@76 533 'disabled' => $day < 7 && $modSettings['cal_minyear'] > ($month == 1 ? $year - 1 : $year),
Chris@76 534 ),
Chris@76 535 'next_week' => array(
Chris@76 536 'disabled' => $day > 25 && $modSettings['cal_maxyear'] < ($month == 12 ? $year + 1 : $year),
Chris@76 537 ),
Chris@76 538 );
Chris@76 539
Chris@76 540 // The next week calculation requires a bit more work.
Chris@76 541 $curTimestamp = mktime(0, 0, 0, $month, $day, $year);
Chris@76 542 $nextWeekTimestamp = $curTimestamp + 604800;
Chris@76 543 $calendarGrid['next_week']['day'] = (int) strftime('%d', $nextWeekTimestamp);
Chris@76 544 $calendarGrid['next_week']['month'] = (int) strftime('%m', $nextWeekTimestamp);
Chris@76 545 $calendarGrid['next_week']['year'] = (int) strftime('%Y', $nextWeekTimestamp);
Chris@76 546
Chris@76 547 // Fetch the arrays for birthdays, posted events, and holidays.
Chris@76 548 $startDate = strftime('%Y-%m-%d', $curTimestamp);
Chris@76 549 $endDate = strftime('%Y-%m-%d', $nextWeekTimestamp);
Chris@76 550 $bday = $calendarOptions['show_birthdays'] ? getBirthdayRange($startDate, $endDate) : array();
Chris@76 551 $events = $calendarOptions['show_events'] ? getEventRange($startDate, $endDate) : array();
Chris@76 552 $holidays = $calendarOptions['show_holidays'] ? getHolidayRange($startDate, $endDate) : array();
Chris@76 553
Chris@76 554 // An adjustment value to apply to all calculated week numbers.
Chris@76 555 if (!empty($calendarOptions['show_week_num']))
Chris@76 556 {
Chris@76 557 $first_day_of_year = (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year));
Chris@76 558 $first_day_of_next_year = (int) strftime('%w', mktime(0, 0, 0, 1, 1, $year + 1));
Chris@76 559 $last_day_of_last_year = (int) strftime('%w', mktime(0, 0, 0, 12, 31, $year - 1));
Chris@76 560
Chris@76 561 // All this is as getCalendarGrid.
Chris@76 562 if ($calendarOptions['start_day'] === 0)
Chris@76 563 $nWeekAdjust = $first_day_of_year === 0 && $first_day_of_year > 3 ? 0 : 1;
Chris@76 564 else
Chris@76 565 $nWeekAdjust = $calendarOptions['start_day'] > $first_day_of_year && $first_day_of_year !== 0 ? 2 : 1;
Chris@76 566
Chris@76 567 $calendarGrid['week_number'] = (int) strftime('%U', mktime(0, 0, 0, $month, $day, $year)) + $nWeekAdjust;
Chris@76 568
Chris@76 569 // If this crosses a year boundry and includes january it should be week one.
Chris@76 570 if ((int) strftime('%Y', $curTimestamp + 518400) != $year && $calendarGrid['week_number'] > 53 && $first_day_of_next_year < 5)
Chris@76 571 $calendarGrid['week_number'] = 1;
Chris@76 572 }
Chris@76 573
Chris@76 574 // This holds all the main data - there is at least one month!
Chris@76 575 $calendarGrid['months'] = array();
Chris@76 576 $lastDay = 99;
Chris@76 577 $curDay = $day;
Chris@76 578 $curDayOfWeek = $calendarOptions['start_day'];
Chris@76 579 for ($i = 0; $i < 7; $i++)
Chris@76 580 {
Chris@76 581 // Have we gone into a new month (Always happens first cycle too)
Chris@76 582 if ($lastDay > $curDay)
Chris@76 583 {
Chris@76 584 $curMonth = $lastDay == 99 ? $month : ($month == 12 ? 1 : $month + 1);
Chris@76 585 $curYear = $lastDay == 99 ? $year : ($curMonth == 1 && $month == 12 ? $year + 1 : $year);
Chris@76 586 $calendarGrid['months'][$curMonth] = array(
Chris@76 587 'current_month' => $curMonth,
Chris@76 588 'current_year' => $curYear,
Chris@76 589 'days' => array(),
Chris@76 590 );
Chris@76 591 }
Chris@76 592
Chris@76 593 // Add todays information to the pile!
Chris@76 594 $date = sprintf('%04d-%02d-%02d', $curYear, $curMonth, $curDay);
Chris@76 595
Chris@76 596 $calendarGrid['months'][$curMonth]['days'][$curDay] = array(
Chris@76 597 'day' => $curDay,
Chris@76 598 'day_of_week' => $curDayOfWeek,
Chris@76 599 'date' => $date,
Chris@76 600 'is_today' => $date == $today['date'],
Chris@76 601 'holidays' => !empty($holidays[$date]) ? $holidays[$date] : array(),
Chris@76 602 'events' => !empty($events[$date]) ? $events[$date] : array(),
Chris@76 603 'birthdays' => !empty($bday[$date]) ? $bday[$date] : array()
Chris@76 604 );
Chris@76 605
Chris@76 606 // Make the last day what the current day is and work out what the next day is.
Chris@76 607 $lastDay = $curDay;
Chris@76 608 $curTimestamp += 86400;
Chris@76 609 $curDay = (int) strftime('%d', $curTimestamp);
Chris@76 610
Chris@76 611 // Also increment the current day of the week.
Chris@76 612 $curDayOfWeek = $curDayOfWeek >= 6 ? 0 : ++$curDayOfWeek;
Chris@76 613 }
Chris@76 614
Chris@76 615 // Set the previous and the next week's links.
Chris@76 616 $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 617 $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 618
Chris@76 619 return $calendarGrid;
Chris@76 620 }
Chris@76 621
Chris@76 622 // Retrieve all events for the given days, independently of the users offset.
Chris@76 623 function cache_getOffsetIndependentEvents($days_to_index)
Chris@76 624 {
Chris@76 625 global $sourcedir;
Chris@76 626
Chris@76 627 $low_date = strftime('%Y-%m-%d', forum_time(false) - 24 * 3600);
Chris@76 628 $high_date = strftime('%Y-%m-%d', forum_time(false) + $days_to_index * 24 * 3600);
Chris@76 629
Chris@76 630 return array(
Chris@76 631 'data' => array(
Chris@76 632 'holidays' => getHolidayRange($low_date, $high_date),
Chris@76 633 'birthdays' => getBirthdayRange($low_date, $high_date),
Chris@76 634 'events' => getEventRange($low_date, $high_date, false),
Chris@76 635 ),
Chris@76 636 '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 637 'expires' => time() + 3600,
Chris@76 638 );
Chris@76 639 }
Chris@76 640
Chris@76 641 // Called from the BoardIndex to display the current day's events on the board index.
Chris@76 642 function cache_getRecentEvents($eventOptions)
Chris@76 643 {
Chris@76 644 global $modSettings, $user_info, $scripturl;
Chris@76 645
Chris@76 646 // With the 'static' cached data we can calculate the user-specific data.
Chris@76 647 $cached_data = cache_quick_get('calendar_index', 'Subs-Calendar.php', 'cache_getOffsetIndependentEvents', array($eventOptions['num_days_shown']));
Chris@76 648
Chris@76 649 // Get the information about today (from user perspective).
Chris@76 650 $today = getTodayInfo();
Chris@76 651
Chris@76 652 $return_data = array(
Chris@76 653 'calendar_holidays' => array(),
Chris@76 654 'calendar_birthdays' => array(),
Chris@76 655 'calendar_events' => array(),
Chris@76 656 );
Chris@76 657
Chris@76 658 // Set the event span to be shown in seconds.
Chris@76 659 $days_for_index = $eventOptions['num_days_shown'] * 86400;
Chris@76 660
Chris@76 661 // Get the current member time/date.
Chris@76 662 $now = forum_time();
Chris@76 663
Chris@76 664 // Holidays between now and now + days.
Chris@76 665 for ($i = $now; $i < $now + $days_for_index; $i += 86400)
Chris@76 666 {
Chris@76 667 if (isset($cached_data['holidays'][strftime('%Y-%m-%d', $i)]))
Chris@76 668 $return_data['calendar_holidays'] = array_merge($return_data['calendar_holidays'], $cached_data['holidays'][strftime('%Y-%m-%d', $i)]);
Chris@76 669 }
Chris@76 670
Chris@76 671 // Happy Birthday, guys and gals!
Chris@76 672 for ($i = $now; $i < $now + $days_for_index; $i += 86400)
Chris@76 673 {
Chris@76 674 $loop_date = strftime('%Y-%m-%d', $i);
Chris@76 675 if (isset($cached_data['birthdays'][$loop_date]))
Chris@76 676 {
Chris@76 677 foreach ($cached_data['birthdays'][$loop_date] as $index => $dummy)
Chris@76 678 $cached_data['birthdays'][strftime('%Y-%m-%d', $i)][$index]['is_today'] = $loop_date === $today['date'];
Chris@76 679 $return_data['calendar_birthdays'] = array_merge($return_data['calendar_birthdays'], $cached_data['birthdays'][$loop_date]);
Chris@76 680 }
Chris@76 681 }
Chris@76 682
Chris@76 683 $duplicates = array();
Chris@76 684 for ($i = $now; $i < $now + $days_for_index; $i += 86400)
Chris@76 685 {
Chris@76 686 // Determine the date of the current loop step.
Chris@76 687 $loop_date = strftime('%Y-%m-%d', $i);
Chris@76 688
Chris@76 689 // No events today? Check the next day.
Chris@76 690 if (empty($cached_data['events'][$loop_date]))
Chris@76 691 continue;
Chris@76 692
Chris@76 693 // Loop through all events to add a few last-minute values.
Chris@76 694 foreach ($cached_data['events'][$loop_date] as $ev => $event)
Chris@76 695 {
Chris@76 696 // Create a shortcut variable for easier access.
Chris@76 697 $this_event = &$cached_data['events'][$loop_date][$ev];
Chris@76 698
Chris@76 699 // Skip duplicates.
Chris@76 700 if (isset($duplicates[$this_event['topic'] . $this_event['title']]))
Chris@76 701 {
Chris@76 702 unset($cached_data['events'][$loop_date][$ev]);
Chris@76 703 continue;
Chris@76 704 }
Chris@76 705 else
Chris@76 706 $duplicates[$this_event['topic'] . $this_event['title']] = true;
Chris@76 707
Chris@76 708 // Might be set to true afterwards, depending on the permissions.
Chris@76 709 $this_event['can_edit'] = false;
Chris@76 710 $this_event['is_today'] = $loop_date === $today['date'];
Chris@76 711 $this_event['date'] = $loop_date;
Chris@76 712 }
Chris@76 713
Chris@76 714 if (!empty($cached_data['events'][$loop_date]))
Chris@76 715 $return_data['calendar_events'] = array_merge($return_data['calendar_events'], $cached_data['events'][$loop_date]);
Chris@76 716 }
Chris@76 717
Chris@76 718 // Mark the last item so that a list separator can be used in the template.
Chris@76 719 for ($i = 0, $n = count($return_data['calendar_birthdays']); $i < $n; $i++)
Chris@76 720 $return_data['calendar_birthdays'][$i]['is_last'] = !isset($return_data['calendar_birthdays'][$i + 1]);
Chris@76 721 for ($i = 0, $n = count($return_data['calendar_events']); $i < $n; $i++)
Chris@76 722 $return_data['calendar_events'][$i]['is_last'] = !isset($return_data['calendar_events'][$i + 1]);
Chris@76 723
Chris@76 724 return array(
Chris@76 725 'data' => $return_data,
Chris@76 726 'expires' => time() + 3600,
Chris@76 727 '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 728 'post_retri_eval' => '
Chris@76 729 global $context, $scripturl, $user_info;
Chris@76 730
Chris@76 731 foreach ($cache_block[\'data\'][\'calendar_events\'] as $k => $event)
Chris@76 732 {
Chris@76 733 // Remove events that the user may not see or wants to ignore.
Chris@76 734 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 735 unset($cache_block[\'data\'][\'calendar_events\'][$k]);
Chris@76 736 else
Chris@76 737 {
Chris@76 738 // Whether the event can be edited depends on the permissions.
Chris@76 739 $cache_block[\'data\'][\'calendar_events\'][$k][\'can_edit\'] = allowedTo(\'calendar_edit_any\') || ($event[\'poster\'] == $user_info[\'id\'] && allowedTo(\'calendar_edit_own\'));
Chris@76 740
Chris@76 741 // The added session code makes this URL not cachable.
Chris@76 742 $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 743 }
Chris@76 744 }
Chris@76 745
Chris@76 746 if (empty($params[0][\'include_holidays\']))
Chris@76 747 $cache_block[\'data\'][\'calendar_holidays\'] = array();
Chris@76 748 if (empty($params[0][\'include_birthdays\']))
Chris@76 749 $cache_block[\'data\'][\'calendar_birthdays\'] = array();
Chris@76 750 if (empty($params[0][\'include_events\']))
Chris@76 751 $cache_block[\'data\'][\'calendar_events\'] = array();
Chris@76 752
Chris@76 753 $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 754 );
Chris@76 755 }
Chris@76 756
Chris@76 757 // Makes sure the calendar post is valid.
Chris@76 758 function validateEventPost()
Chris@76 759 {
Chris@76 760 global $modSettings, $txt, $sourcedir, $smcFunc;
Chris@76 761
Chris@76 762 if (!isset($_POST['deleteevent']))
Chris@76 763 {
Chris@76 764 // No month? No year?
Chris@76 765 if (!isset($_POST['month']))
Chris@76 766 fatal_lang_error('event_month_missing', false);
Chris@76 767 if (!isset($_POST['year']))
Chris@76 768 fatal_lang_error('event_year_missing', false);
Chris@76 769
Chris@76 770 // Check the month and year...
Chris@76 771 if ($_POST['month'] < 1 || $_POST['month'] > 12)
Chris@76 772 fatal_lang_error('invalid_month', false);
Chris@76 773 if ($_POST['year'] < $modSettings['cal_minyear'] || $_POST['year'] > $modSettings['cal_maxyear'])
Chris@76 774 fatal_lang_error('invalid_year', false);
Chris@76 775 }
Chris@76 776
Chris@76 777 // Make sure they're allowed to post...
Chris@76 778 isAllowedTo('calendar_post');
Chris@76 779
Chris@76 780 if (isset($_POST['span']))
Chris@76 781 {
Chris@76 782 // Make sure it's turned on and not some fool trying to trick it.
Chris@76 783 if (empty($modSettings['cal_allowspan']))
Chris@76 784 fatal_lang_error('no_span', false);
Chris@76 785 if ($_POST['span'] < 1 || $_POST['span'] > $modSettings['cal_maxspan'])
Chris@76 786 fatal_lang_error('invalid_days_numb', false);
Chris@76 787 }
Chris@76 788
Chris@76 789 // There is no need to validate the following values if we are just deleting the event.
Chris@76 790 if (!isset($_POST['deleteevent']))
Chris@76 791 {
Chris@76 792 // No day?
Chris@76 793 if (!isset($_POST['day']))
Chris@76 794 fatal_lang_error('event_day_missing', false);
Chris@76 795 if (!isset($_POST['evtitle']) && !isset($_POST['subject']))
Chris@76 796 fatal_lang_error('event_title_missing', false);
Chris@76 797 elseif (!isset($_POST['evtitle']))
Chris@76 798 $_POST['evtitle'] = $_POST['subject'];
Chris@76 799
Chris@76 800 // Bad day?
Chris@76 801 if (!checkdate($_POST['month'], $_POST['day'], $_POST['year']))
Chris@76 802 fatal_lang_error('invalid_date', false);
Chris@76 803
Chris@76 804 // No title?
Chris@76 805 if ($smcFunc['htmltrim']($_POST['evtitle']) === '')
Chris@76 806 fatal_lang_error('no_event_title', false);
Chris@76 807 if ($smcFunc['strlen']($_POST['evtitle']) > 30)
Chris@76 808 $_POST['evtitle'] = $smcFunc['substr']($_POST['evtitle'], 0, 30);
Chris@76 809 $_POST['evtitle'] = str_replace(';', '', $_POST['evtitle']);
Chris@76 810 }
Chris@76 811 }
Chris@76 812
Chris@76 813 // Get the event's poster.
Chris@76 814 function getEventPoster($event_id)
Chris@76 815 {
Chris@76 816 global $smcFunc;
Chris@76 817
Chris@76 818 // A simple database query, how hard can that be?
Chris@76 819 $request = $smcFunc['db_query']('', '
Chris@76 820 SELECT id_member
Chris@76 821 FROM {db_prefix}calendar
Chris@76 822 WHERE id_event = {int:id_event}
Chris@76 823 LIMIT 1',
Chris@76 824 array(
Chris@76 825 'id_event' => $event_id,
Chris@76 826 )
Chris@76 827 );
Chris@76 828
Chris@76 829 // No results, return false.
Chris@76 830 if ($smcFunc['db_num_rows'] === 0)
Chris@76 831 return false;
Chris@76 832
Chris@76 833 // Grab the results and return.
Chris@76 834 list ($poster) = $smcFunc['db_fetch_row']($request);
Chris@76 835 $smcFunc['db_free_result']($request);
Chris@76 836 return $poster;
Chris@76 837 }
Chris@76 838
Chris@76 839 // Consolidating the various INSERT statements into this function.
Chris@76 840 function insertEvent(&$eventOptions)
Chris@76 841 {
Chris@76 842 global $modSettings, $smcFunc;
Chris@76 843
Chris@76 844 // Add special chars to the title.
Chris@76 845 $eventOptions['title'] = $smcFunc['htmlspecialchars']($eventOptions['title'], ENT_QUOTES);
Chris@76 846
Chris@76 847 // Add some sanity checking to the span.
Chris@76 848 $eventOptions['span'] = isset($eventOptions['span']) && $eventOptions['span'] > 0 ? (int) $eventOptions['span'] : 0;
Chris@76 849
Chris@76 850 // Make sure the start date is in ISO order.
Chris@76 851 if (($num_results = sscanf($eventOptions['start_date'], '%d-%d-%d', $year, $month, $day)) !== 3)
Chris@76 852 trigger_error('modifyEvent(): invalid start date format given', E_USER_ERROR);
Chris@76 853
Chris@76 854 // Set the end date (if not yet given)
Chris@76 855 if (!isset($eventOptions['end_date']))
Chris@76 856 $eventOptions['end_date'] = strftime('%Y-%m-%d', mktime(0, 0, 0, $month, $day, $year) + $eventOptions['span'] * 86400);
Chris@76 857
Chris@76 858 // If no topic and board are given, they are not linked to a topic.
Chris@76 859 $eventOptions['board'] = isset($eventOptions['board']) ? (int) $eventOptions['board'] : 0;
Chris@76 860 $eventOptions['topic'] = isset($eventOptions['topic']) ? (int) $eventOptions['topic'] : 0;
Chris@76 861
Chris@76 862 // Insert the event!
Chris@76 863 $smcFunc['db_insert']('',
Chris@76 864 '{db_prefix}calendar',
Chris@76 865 array(
Chris@76 866 'id_board' => 'int', 'id_topic' => 'int', 'title' => 'string-60', 'id_member' => 'int',
Chris@76 867 'start_date' => 'date', 'end_date' => 'date',
Chris@76 868 ),
Chris@76 869 array(
Chris@76 870 $eventOptions['board'], $eventOptions['topic'], $eventOptions['title'], $eventOptions['member'],
Chris@76 871 $eventOptions['start_date'], $eventOptions['end_date'],
Chris@76 872 ),
Chris@76 873 array('id_event')
Chris@76 874 );
Chris@76 875
Chris@76 876 // Store the just inserted id_event for future reference.
Chris@76 877 $eventOptions['id'] = $smcFunc['db_insert_id']('{db_prefix}calendar', 'id_event');
Chris@76 878
Chris@76 879 // Update the settings to show something calendarish was updated.
Chris@76 880 updateSettings(array(
Chris@76 881 'calendar_updated' => time(),
Chris@76 882 ));
Chris@76 883 }
Chris@76 884
Chris@76 885 function modifyEvent($event_id, &$eventOptions)
Chris@76 886 {
Chris@76 887 global $smcFunc;
Chris@76 888
Chris@76 889 // Properly sanitize the title.
Chris@76 890 $eventOptions['title'] = $smcFunc['htmlspecialchars']($eventOptions['title'], ENT_QUOTES);
Chris@76 891
Chris@76 892 // Scan the start date for validity and get its components.
Chris@76 893 if (($num_results = sscanf($eventOptions['start_date'], '%d-%d-%d', $year, $month, $day)) !== 3)
Chris@76 894 trigger_error('modifyEvent(): invalid start date format given', E_USER_ERROR);
Chris@76 895
Chris@76 896 // Default span to 0 days.
Chris@76 897 $eventOptions['span'] = isset($eventOptions['span']) ? (int) $eventOptions['span'] : 0;
Chris@76 898
Chris@76 899 // Set the end date to the start date + span (if the end date wasn't already given).
Chris@76 900 if (!isset($eventOptions['end_date']))
Chris@76 901 $eventOptions['end_date'] = strftime('%Y-%m-%d', mktime(0, 0, 0, $month, $day, $year) + $eventOptions['span'] * 86400);
Chris@76 902
Chris@76 903 $smcFunc['db_query']('', '
Chris@76 904 UPDATE {db_prefix}calendar
Chris@76 905 SET
Chris@76 906 start_date = {date:start_date},
Chris@76 907 end_date = {date:end_date},
Chris@76 908 title = SUBSTRING({string:title}, 1, 60),
Chris@76 909 id_board = {int:id_board},
Chris@76 910 id_topic = {int:id_topic}
Chris@76 911 WHERE id_event = {int:id_event}',
Chris@76 912 array(
Chris@76 913 'start_date' => $eventOptions['start_date'],
Chris@76 914 'end_date' => $eventOptions['end_date'],
Chris@76 915 'title' => $eventOptions['title'],
Chris@76 916 'id_board' => isset($eventOptions['board']) ? (int) $eventOptions['board'] : 0,
Chris@76 917 'id_topic' => isset($eventOptions['topic']) ? (int) $eventOptions['topic'] : 0,
Chris@76 918 'id_event' => $event_id,
Chris@76 919 )
Chris@76 920 );
Chris@76 921
Chris@76 922 updateSettings(array(
Chris@76 923 'calendar_updated' => time(),
Chris@76 924 ));
Chris@76 925 }
Chris@76 926
Chris@76 927 function removeEvent($event_id)
Chris@76 928 {
Chris@76 929 global $smcFunc;
Chris@76 930
Chris@76 931 $smcFunc['db_query']('', '
Chris@76 932 DELETE FROM {db_prefix}calendar
Chris@76 933 WHERE id_event = {int:id_event}',
Chris@76 934 array(
Chris@76 935 'id_event' => $event_id,
Chris@76 936 )
Chris@76 937 );
Chris@76 938
Chris@76 939 updateSettings(array(
Chris@76 940 'calendar_updated' => time(),
Chris@76 941 ));
Chris@76 942 }
Chris@76 943
Chris@76 944 function getEventProperties($event_id)
Chris@76 945 {
Chris@76 946 global $smcFunc;
Chris@76 947
Chris@76 948 $request = $smcFunc['db_query']('', '
Chris@76 949 SELECT
Chris@76 950 c.id_event, c.id_board, c.id_topic, MONTH(c.start_date) AS month,
Chris@76 951 DAYOFMONTH(c.start_date) AS day, YEAR(c.start_date) AS year,
Chris@76 952 (TO_DAYS(c.end_date) - TO_DAYS(c.start_date)) AS span, c.id_member, c.title,
Chris@76 953 t.id_first_msg, t.id_member_started
Chris@76 954 FROM {db_prefix}calendar AS c
Chris@76 955 LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = c.id_topic)
Chris@76 956 WHERE c.id_event = {int:id_event}',
Chris@76 957 array(
Chris@76 958 'id_event' => $event_id,
Chris@76 959 )
Chris@76 960 );
Chris@76 961
Chris@76 962 // If nothing returned, we are in poo, poo.
Chris@76 963 if ($smcFunc['db_num_rows']($request) === 0)
Chris@76 964 return false;
Chris@76 965
Chris@76 966 $row = $smcFunc['db_fetch_assoc']($request);
Chris@76 967 $smcFunc['db_free_result']($request);
Chris@76 968
Chris@76 969 $return_value = array(
Chris@76 970 'boards' => array(),
Chris@76 971 'board' => $row['id_board'],
Chris@76 972 'new' => 0,
Chris@76 973 'eventid' => $event_id,
Chris@76 974 'year' => $row['year'],
Chris@76 975 'month' => $row['month'],
Chris@76 976 'day' => $row['day'],
Chris@76 977 'title' => $row['title'],
Chris@76 978 'span' => 1 + $row['span'],
Chris@76 979 'member' => $row['id_member'],
Chris@76 980 'topic' => array(
Chris@76 981 'id' => $row['id_topic'],
Chris@76 982 'member_started' => $row['id_member_started'],
Chris@76 983 'first_msg' => $row['id_first_msg'],
Chris@76 984 ),
Chris@76 985 );
Chris@76 986
Chris@76 987 $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 988
Chris@76 989 return $return_value;
Chris@76 990 }
Chris@76 991
Chris@76 992 function list_getHolidays($start, $items_per_page, $sort)
Chris@76 993 {
Chris@76 994 global $smcFunc;
Chris@76 995
Chris@76 996 $request = $smcFunc['db_query']('', '
Chris@76 997 SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
Chris@76 998 FROM {db_prefix}calendar_holidays
Chris@76 999 ORDER BY {raw:sort}
Chris@76 1000 LIMIT ' . $start . ', ' . $items_per_page,
Chris@76 1001 array(
Chris@76 1002 'sort' => $sort,
Chris@76 1003 )
Chris@76 1004 );
Chris@76 1005 $holidays = array();
Chris@76 1006 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1007 $holidays[] = $row;
Chris@76 1008 $smcFunc['db_free_result']($request);
Chris@76 1009
Chris@76 1010 return $holidays;
Chris@76 1011 }
Chris@76 1012
Chris@76 1013 function list_getNumHolidays()
Chris@76 1014 {
Chris@76 1015 global $smcFunc;
Chris@76 1016
Chris@76 1017 $request = $smcFunc['db_query']('', '
Chris@76 1018 SELECT COUNT(*)
Chris@76 1019 FROM {db_prefix}calendar_holidays',
Chris@76 1020 array(
Chris@76 1021 )
Chris@76 1022 );
Chris@76 1023 list($num_items) = $smcFunc['db_fetch_row']($request);
Chris@76 1024 $smcFunc['db_free_result']($request);
Chris@76 1025
Chris@76 1026 return $num_items;
Chris@76 1027 }
Chris@76 1028
Chris@76 1029 function removeHolidays($holiday_ids)
Chris@76 1030 {
Chris@76 1031 global $smcFunc;
Chris@76 1032
Chris@76 1033 $smcFunc['db_query']('', '
Chris@76 1034 DELETE FROM {db_prefix}calendar_holidays
Chris@76 1035 WHERE id_holiday IN ({array_int:id_holiday})',
Chris@76 1036 array(
Chris@76 1037 'id_holiday' => $holiday_ids,
Chris@76 1038 )
Chris@76 1039 );
Chris@76 1040
Chris@76 1041 updateSettings(array(
Chris@76 1042 'calendar_updated' => time(),
Chris@76 1043 ));
Chris@76 1044 }
Chris@76 1045
Chris@76 1046 ?>