Subs-Calendar.php 39 KB

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