Subs-Calendar.php 39 KB

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