Subs-Calendar.php 39 KB

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