Subs-Calendar.php 39 KB

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