Subs-Calendar.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. $event_columns = array(
  765. 'id_board' => 'int', 'id_topic' => 'int', 'title' => 'string-60', 'id_member' => 'int',
  766. 'start_date' => 'date', 'end_date' => 'date',
  767. );
  768. $event_parameters = array(
  769. $eventOptions['board'], $eventOptions['topic'], $eventOptions['title'], $eventOptions['member'],
  770. $eventOptions['start_date'], $eventOptions['end_date'],
  771. );
  772. call_integration_hook('integrate_create_event', array(&$eventOptions, &$event_columns, &$event_parameters));
  773. // Insert the event!
  774. $smcFunc['db_insert']('',
  775. '{db_prefix}calendar',
  776. $event_columns,
  777. $event_parameters,
  778. array('id_event')
  779. );
  780. // Store the just inserted id_event for future reference.
  781. $eventOptions['id'] = $smcFunc['db_insert_id']('{db_prefix}calendar', 'id_event');
  782. // Update the settings to show something calendarish was updated.
  783. updateSettings(array(
  784. 'calendar_updated' => time(),
  785. ));
  786. }
  787. /**
  788. * @param int $event_id
  789. * @param array &$eventOptions
  790. */
  791. function modifyEvent($event_id, &$eventOptions)
  792. {
  793. global $smcFunc;
  794. // Properly sanitize the title.
  795. $eventOptions['title'] = $smcFunc['htmlspecialchars']($eventOptions['title'], ENT_QUOTES);
  796. // Scan the start date for validity and get its components.
  797. if (($num_results = sscanf($eventOptions['start_date'], '%d-%d-%d', $year, $month, $day)) !== 3)
  798. trigger_error('modifyEvent(): invalid start date format given', E_USER_ERROR);
  799. // Default span to 0 days.
  800. $eventOptions['span'] = isset($eventOptions['span']) ? (int) $eventOptions['span'] : 0;
  801. // Set the end date to the start date + span (if the end date wasn't already given).
  802. if (!isset($eventOptions['end_date']))
  803. $eventOptions['end_date'] = strftime('%Y-%m-%d', mktime(0, 0, 0, $month, $day, $year) + $eventOptions['span'] * 86400);
  804. $event_columns = array(
  805. 'start_date' => '{date:start_date}',
  806. 'end_date' => '{date:end_date}',
  807. 'title' => 'SUBSTRING({string:title}, 1, 60)',
  808. 'id_board' => '{int:id_board}',
  809. 'id_topic' => '{int:id_topic}'
  810. );
  811. $event_parameters = array(
  812. 'start_date' => $eventOptions['start_date'],
  813. 'end_date' => $eventOptions['end_date'],
  814. 'title' => $eventOptions['title'],
  815. 'id_board' => isset($eventOptions['board']) ? (int) $eventOptions['board'] : 0,
  816. 'id_topic' => isset($eventOptions['topic']) ? (int) $eventOptions['topic'] : 0,
  817. );
  818. // This is to prevent hooks to modify the id of the event
  819. $real_event_id = $event_id;
  820. call_integration_hook('integrate_modify_event', array($event_id, &$eventOptions, &$event_columns, &$event_parameters));
  821. $smcFunc['db_query']('', '
  822. UPDATE {db_prefix}calendar
  823. SET
  824. ' . implode(', ', $event_columns) . '
  825. WHERE id_event = {int:id_event}',
  826. array_merge(
  827. $event_parameters,
  828. array(
  829. 'id_event' => $real_event_id
  830. )
  831. )
  832. );
  833. updateSettings(array(
  834. 'calendar_updated' => time(),
  835. ));
  836. }
  837. /**
  838. * Remove an event
  839. * @param int $event_id
  840. */
  841. function removeEvent($event_id)
  842. {
  843. global $smcFunc;
  844. $smcFunc['db_query']('', '
  845. DELETE FROM {db_prefix}calendar
  846. WHERE id_event = {int:id_event}',
  847. array(
  848. 'id_event' => $event_id,
  849. )
  850. );
  851. call_integration_hook('integrate_remove_event', array($event_id));
  852. updateSettings(array(
  853. 'calendar_updated' => time(),
  854. ));
  855. }
  856. /**
  857. * @param int $event_id
  858. * @return array
  859. */
  860. function getEventProperties($event_id)
  861. {
  862. global $smcFunc;
  863. $request = $smcFunc['db_query']('', '
  864. SELECT
  865. c.id_event, c.id_board, c.id_topic, MONTH(c.start_date) AS month,
  866. DAYOFMONTH(c.start_date) AS day, YEAR(c.start_date) AS year,
  867. (TO_DAYS(c.end_date) - TO_DAYS(c.start_date)) AS span, c.id_member, c.title,
  868. t.id_first_msg, t.id_member_started,
  869. mb.real_name, m.modified_time
  870. FROM {db_prefix}calendar AS c
  871. LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = c.id_topic)
  872. LEFT JOIN {db_prefix}members AS mb ON (mb.id_member = t.id_member_started)
  873. LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  874. WHERE c.id_event = {int:id_event}',
  875. array(
  876. 'id_event' => $event_id,
  877. )
  878. );
  879. // If nothing returned, we are in poo, poo.
  880. if ($smcFunc['db_num_rows']($request) === 0)
  881. return false;
  882. $row = $smcFunc['db_fetch_assoc']($request);
  883. $smcFunc['db_free_result']($request);
  884. $return_value = array(
  885. 'boards' => array(),
  886. 'board' => $row['id_board'],
  887. 'new' => 0,
  888. 'eventid' => $event_id,
  889. 'year' => $row['year'],
  890. 'month' => $row['month'],
  891. 'day' => $row['day'],
  892. 'title' => $row['title'],
  893. 'span' => 1 + $row['span'],
  894. 'member' => $row['id_member'],
  895. 'realname' => $row['real_name'],
  896. 'sequence' => $row['modified_time'],
  897. 'topic' => array(
  898. 'id' => $row['id_topic'],
  899. 'member_started' => $row['id_member_started'],
  900. 'first_msg' => $row['id_first_msg'],
  901. ),
  902. );
  903. $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']));
  904. return $return_value;
  905. }
  906. /**
  907. *
  908. * @param int $start
  909. * @param int $items_per_page
  910. * @param string $sort
  911. * @return array
  912. */
  913. function list_getHolidays($start, $items_per_page, $sort)
  914. {
  915. global $smcFunc;
  916. $request = $smcFunc['db_query']('', '
  917. SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
  918. FROM {db_prefix}calendar_holidays
  919. ORDER BY {raw:sort}
  920. LIMIT ' . $start . ', ' . $items_per_page,
  921. array(
  922. 'sort' => $sort,
  923. )
  924. );
  925. $holidays = array();
  926. while ($row = $smcFunc['db_fetch_assoc']($request))
  927. $holidays[] = $row;
  928. $smcFunc['db_free_result']($request);
  929. return $holidays;
  930. }
  931. /**
  932. * @return int
  933. */
  934. function list_getNumHolidays()
  935. {
  936. global $smcFunc;
  937. $request = $smcFunc['db_query']('', '
  938. SELECT COUNT(*)
  939. FROM {db_prefix}calendar_holidays',
  940. array(
  941. )
  942. );
  943. list($num_items) = $smcFunc['db_fetch_row']($request);
  944. $smcFunc['db_free_result']($request);
  945. return (int) $num_items;
  946. }
  947. /**
  948. * @param array $holiday_ids An array of
  949. */
  950. function removeHolidays($holiday_ids)
  951. {
  952. global $smcFunc;
  953. $smcFunc['db_query']('', '
  954. DELETE FROM {db_prefix}calendar_holidays
  955. WHERE id_holiday IN ({array_int:id_holiday})',
  956. array(
  957. 'id_holiday' => $holiday_ids,
  958. )
  959. );
  960. updateSettings(array(
  961. 'calendar_updated' => time(),
  962. ));
  963. }