ManageCalendar.php 12 KB

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