2
0

ManageCalendar.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * This file allows you to manage the calendar.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * The main controlling function doesn't have much to do... yet.
  18. * Just check permissions and delegate to the rest.
  19. *
  20. * @uses ManageCalendar language file.
  21. */
  22. function ManageCalendar()
  23. {
  24. global $context, $txt, $modSettings;
  25. isAllowedTo('admin_forum');
  26. // Everything's gonna need this.
  27. loadLanguage('ManageCalendar');
  28. // Default text.
  29. $context['explain_text'] = $txt['calendar_desc'];
  30. // Little short on the ground of functions here... but things can and maybe will change...
  31. if (!empty($modSettings['cal_enabled']))
  32. {
  33. $subActions = array(
  34. 'editholiday' => 'EditHoliday',
  35. 'holidays' => 'ModifyHolidays',
  36. 'settings' => 'ModifyCalendarSettings'
  37. );
  38. $default = 'holidays';
  39. }
  40. else
  41. {
  42. $subActions = array(
  43. 'settings' => 'ModifyCalendarSettings'
  44. );
  45. $default = 'settings';
  46. }
  47. call_integration_hook('integrate_manage_calendar', array(&$subActions));
  48. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : $default;
  49. // Set up the two tabs here...
  50. $context[$context['admin_menu_name']]['tab_data'] = array(
  51. 'title' => $txt['manage_calendar'],
  52. 'help' => 'calendar',
  53. 'description' => $txt['calendar_settings_desc'],
  54. );
  55. if (!empty($modSettings['cal_enabled']))
  56. $context[$context['admin_menu_name']]['tab_data']['tabs'] = array(
  57. 'holidays' => array(
  58. 'description' => $txt['manage_holidays_desc'],
  59. ),
  60. 'settings' => array(
  61. 'description' => $txt['calendar_settings_desc'],
  62. ),
  63. );
  64. $subActions[$_REQUEST['sa']]();
  65. }
  66. /**
  67. * The function that handles adding, and deleting holiday data
  68. */
  69. function ModifyHolidays()
  70. {
  71. global $sourcedir, $scripturl, $txt, $context;
  72. // Submitting something...
  73. if (isset($_REQUEST['delete']) && !empty($_REQUEST['holiday']))
  74. {
  75. checkSession();
  76. validateToken('admin-mc');
  77. foreach ($_REQUEST['holiday'] as $id => $value)
  78. $_REQUEST['holiday'][$id] = (int) $id;
  79. // Now the IDs are "safe" do the delete...
  80. require_once($sourcedir . '/Subs-Calendar.php');
  81. removeHolidays($_REQUEST['holiday']);
  82. }
  83. createToken('admin-mc');
  84. $listOptions = array(
  85. 'id' => 'holiday_list',
  86. 'title' => $txt['current_holidays'],
  87. 'items_per_page' => 20,
  88. 'base_href' => $scripturl . '?action=admin;area=managecalendar;sa=holidays',
  89. 'default_sort_col' => 'name',
  90. 'get_items' => array(
  91. 'file' => $sourcedir . '/Subs-Calendar.php',
  92. 'function' => 'list_getHolidays',
  93. ),
  94. 'get_count' => array(
  95. 'file' => $sourcedir . '/Subs-Calendar.php',
  96. 'function' => 'list_getNumHolidays',
  97. ),
  98. 'no_items_label' => $txt['holidays_no_entries'],
  99. 'columns' => array(
  100. 'name' => array(
  101. 'header' => array(
  102. 'value' => $txt['holidays_title'],
  103. ),
  104. 'data' => array(
  105. 'sprintf' => array(
  106. 'format' => '<a href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday;holiday=%1$d">%2$s</a>',
  107. 'params' => array(
  108. 'id_holiday' => false,
  109. 'title' => false,
  110. ),
  111. ),
  112. ),
  113. 'sort' => array(
  114. 'default' => 'title ASC, event_date ASC',
  115. 'reverse' => 'title DESC, event_date ASC',
  116. )
  117. ),
  118. 'date' => array(
  119. 'header' => array(
  120. 'value' => $txt['date'],
  121. ),
  122. 'data' => array(
  123. 'function' => create_function('$rowData', '
  124. global $txt;
  125. // Recurring every year or just a single year?
  126. $year = $rowData[\'year\'] == \'0004\' ? sprintf(\'(%1$s)\', $txt[\'every_year\']) : $rowData[\'year\'];
  127. // Construct the date.
  128. return sprintf(\'%1$d %2$s %3$s\', $rowData[\'day\'], $txt[\'months\'][(int) $rowData[\'month\']], $year);
  129. '),
  130. ),
  131. 'sort' => array(
  132. 'default' => 'event_date',
  133. 'reverse' => 'event_date DESC',
  134. ),
  135. ),
  136. 'check' => array(
  137. 'header' => array(
  138. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check">',
  139. 'class' => 'centercol',
  140. ),
  141. 'data' => array(
  142. 'sprintf' => array(
  143. 'format' => '<input type="checkbox" name="holiday[%1$d]" class="input_check">',
  144. 'params' => array(
  145. 'id_holiday' => false,
  146. ),
  147. ),
  148. 'class' => 'centercol',
  149. ),
  150. ),
  151. ),
  152. 'form' => array(
  153. 'href' => $scripturl . '?action=admin;area=managecalendar;sa=holidays',
  154. 'token' => 'admin-mc',
  155. ),
  156. 'additional_rows' => array(
  157. array(
  158. 'position' => 'below_table_data',
  159. 'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button_submit">
  160. <a class="button_link" href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday" style="margin: 0 1em">' . $txt['holidays_add'] . '</a>',
  161. ),
  162. ),
  163. );
  164. require_once($sourcedir . '/Subs-List.php');
  165. createList($listOptions);
  166. //loadTemplate('ManageCalendar');
  167. $context['page_title'] = $txt['manage_holidays'];
  168. // Since the list is the only thing to show, use the default list template.
  169. $context['default_list'] = 'holiday_list';
  170. $context['sub_template'] = 'show_list';
  171. }
  172. /**
  173. * This function is used for adding/editing a specific holiday
  174. */
  175. function EditHoliday()
  176. {
  177. global $txt, $context, $smcFunc;
  178. loadTemplate('ManageCalendar');
  179. $context['is_new'] = !isset($_REQUEST['holiday']);
  180. $context['page_title'] = $context['is_new'] ? $txt['holidays_add'] : $txt['holidays_edit'];
  181. $context['sub_template'] = 'edit_holiday';
  182. // Cast this for safety...
  183. if (isset($_REQUEST['holiday']))
  184. $_REQUEST['holiday'] = (int) $_REQUEST['holiday'];
  185. // Submitting?
  186. if (isset($_POST[$context['session_var']]) && (isset($_REQUEST['delete']) || $_REQUEST['title'] != ''))
  187. {
  188. checkSession();
  189. // Not too long good sir?
  190. $_REQUEST['title'] = $smcFunc['substr']($_REQUEST['title'], 0, 60);
  191. $_REQUEST['holiday'] = isset($_REQUEST['holiday']) ? (int) $_REQUEST['holiday'] : 0;
  192. if (isset($_REQUEST['delete']))
  193. $smcFunc['db_query']('', '
  194. DELETE FROM {db_prefix}calendar_holidays
  195. WHERE id_holiday = {int:selected_holiday}',
  196. array(
  197. 'selected_holiday' => $_REQUEST['holiday'],
  198. )
  199. );
  200. else
  201. {
  202. $date = strftime($_REQUEST['year'] <= 4 ? '0004-%m-%d' : '%Y-%m-%d', mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']));
  203. if (isset($_REQUEST['edit']))
  204. $smcFunc['db_query']('', '
  205. UPDATE {db_prefix}calendar_holidays
  206. SET event_date = {date:holiday_date}, title = {string:holiday_title}
  207. WHERE id_holiday = {int:selected_holiday}',
  208. array(
  209. 'holiday_date' => $date,
  210. 'selected_holiday' => $_REQUEST['holiday'],
  211. 'holiday_title' => $_REQUEST['title'],
  212. )
  213. );
  214. else
  215. $smcFunc['db_insert']('',
  216. '{db_prefix}calendar_holidays',
  217. array(
  218. 'event_date' => 'date', 'title' => 'string-60',
  219. ),
  220. array(
  221. $date, $_REQUEST['title'],
  222. ),
  223. array('id_holiday')
  224. );
  225. }
  226. updateSettings(array(
  227. 'calendar_updated' => time(),
  228. ));
  229. redirectexit('action=admin;area=managecalendar;sa=holidays');
  230. }
  231. // Default states...
  232. if ($context['is_new'])
  233. $context['holiday'] = array(
  234. 'id' => 0,
  235. 'day' => date('d'),
  236. 'month' => date('m'),
  237. 'year' => '0000',
  238. 'title' => ''
  239. );
  240. // If it's not new load the data.
  241. else
  242. {
  243. $request = $smcFunc['db_query']('', '
  244. SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
  245. FROM {db_prefix}calendar_holidays
  246. WHERE id_holiday = {int:selected_holiday}
  247. LIMIT 1',
  248. array(
  249. 'selected_holiday' => $_REQUEST['holiday'],
  250. )
  251. );
  252. while ($row = $smcFunc['db_fetch_assoc']($request))
  253. $context['holiday'] = array(
  254. 'id' => $row['id_holiday'],
  255. 'day' => $row['day'],
  256. 'month' => $row['month'],
  257. 'year' => $row['year'] <= 4 ? 0 : $row['year'],
  258. 'title' => $row['title']
  259. );
  260. $smcFunc['db_free_result']($request);
  261. }
  262. // Last day for the drop down?
  263. $context['holiday']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['holiday']['month'] == 12 ? 1 : $context['holiday']['month'] + 1, 0, $context['holiday']['month'] == 12 ? $context['holiday']['year'] + 1 : $context['holiday']['year']));
  264. }
  265. /**
  266. * Show and allow to modify calendar settings. Obviously.
  267. *
  268. * @param bool $return_config = false
  269. */
  270. function ModifyCalendarSettings($return_config = false)
  271. {
  272. global $context, $txt, $sourcedir, $scripturl, $smcFunc, $modSettings;
  273. // Load the boards list.
  274. $boards = array('');
  275. $request = $smcFunc['db_query']('order_by_board_order', '
  276. SELECT b.id_board, b.name AS board_name, c.name AS cat_name
  277. FROM {db_prefix}boards AS b
  278. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)',
  279. array(
  280. )
  281. );
  282. while ($row = $smcFunc['db_fetch_assoc']($request))
  283. $boards[$row['id_board']] = $row['cat_name'] . ' - ' . $row['board_name'];
  284. $smcFunc['db_free_result']($request);
  285. // Look, all the calendar settings - of which there are many!
  286. if (!empty($modSettings['cal_enabled']))
  287. $config_vars = array(
  288. array('check', 'cal_enabled'),
  289. '',
  290. // All the permissions:
  291. array('permissions', 'calendar_view', 'help' => 'cal_enabled'),
  292. array('permissions', 'calendar_post'),
  293. array('permissions', 'calendar_edit_own'),
  294. array('permissions', 'calendar_edit_any'),
  295. '',
  296. // How many days to show on board index, and where to display events etc?
  297. array('int', 'cal_days_for_index', 6, 'postinput' => $txt['days_word']),
  298. array('select', 'cal_showholidays', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
  299. array('select', 'cal_showbdays', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
  300. array('select', 'cal_showevents', array(0 => $txt['setting_cal_show_never'], 1 => $txt['setting_cal_show_cal'], 3 => $txt['setting_cal_show_index'], 2 => $txt['setting_cal_show_all'])),
  301. array('check', 'cal_export'),
  302. '',
  303. // Linking events etc...
  304. array('select', 'cal_defaultboard', $boards),
  305. array('check', 'cal_daysaslink'),
  306. array('check', 'cal_allow_unlinked'),
  307. array('check', 'cal_showInTopic'),
  308. '',
  309. // Dates of calendar...
  310. array('int', 'cal_minyear'),
  311. array('int', 'cal_maxyear'),
  312. '',
  313. // Calendar spanning...
  314. array('check', 'cal_allowspan'),
  315. array('int', 'cal_maxspan', 6, 'postinput' => $txt['days_word']),
  316. '',
  317. // A comment is like a dog marking its territory. ;)
  318. array('select', 'cal_highlight_events', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
  319. array('select', 'cal_highlight_holidays', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
  320. array('select', 'cal_highlight_birthdays', array(0 => $txt['setting_cal_highlight_none'], 1 => $txt['setting_cal_highlight_mini'], 2 => $txt['setting_cal_highlight_main'], 3 => $txt['setting_cal_highlight_both'])),
  321. '',
  322. // Miscellaneous layout settings...
  323. array('check', 'cal_disable_prev_next'),
  324. array('select', 'cal_display_type', array(0 => $txt['setting_cal_display_comfortable'], 1 => $txt['setting_cal_display_compact'])),
  325. array('select', 'cal_week_links', array(0 => $txt['setting_cal_week_links_none'], 1 => $txt['setting_cal_week_links_mini'], 2 => $txt['setting_cal_week_links_main'], 3 => $txt['setting_cal_week_links_both'])),
  326. array('check', 'cal_prev_next_links'),
  327. array('check', 'cal_short_days'),
  328. array('check', 'cal_short_months'),
  329. );
  330. else
  331. $config_vars = array(
  332. array('check', 'cal_enabled'),
  333. );
  334. call_integration_hook('integrate_modify_calendar_settings', array(&$config_vars));
  335. if ($return_config)
  336. return $config_vars;
  337. // Get the settings template fired up.
  338. require_once($sourcedir . '/ManageServer.php');
  339. // Some important context stuff
  340. $context['page_title'] = $txt['calendar_settings'];
  341. $context['sub_template'] = 'show_settings';
  342. // Get the final touches in place.
  343. $context['post_url'] = $scripturl . '?action=admin;area=managecalendar;save;sa=settings';
  344. $context['settings_title'] = $txt['calendar_settings'];
  345. // Saving the settings?
  346. if (isset($_GET['save']))
  347. {
  348. checkSession();
  349. call_integration_hook('integrate_save_calendar_settings');
  350. saveDBSettings($config_vars);
  351. // Update the stats in case.
  352. updateSettings(array(
  353. 'calendar_updated' => time(),
  354. ));
  355. $_SESSION['adm-save'] = true;
  356. redirectexit('action=admin;area=managecalendar;sa=settings');
  357. }
  358. // We need this for the inline permissions
  359. createToken('admin-mp');
  360. // Prepare the settings...
  361. prepareDBSettingContext($config_vars);
  362. }
  363. ?>