Subs-Calendar.php 40 KB

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