2
0

Subs-Calendar.php 41 KB

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