Subs-Calendar.php 41 KB

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