PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/sources/subs/Calendar.subs.php

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