2
0

Subs-Calendar.php 40 KB

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