ManageCalendar.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 2012 Simple Machines Forum 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('Hacking attempt...');
  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, $scripturl, $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. $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. ),
  128. 'data' => array(
  129. 'sprintf' => array(
  130. 'format' => '<input type="checkbox" name="holiday[%1$d]" class="input_check" />',
  131. 'params' => array(
  132. 'id_holiday' => false,
  133. ),
  134. ),
  135. 'style' => 'text-align: center',
  136. ),
  137. ),
  138. ),
  139. 'form' => array(
  140. 'href' => $scripturl . '?action=admin;area=managecalendar;sa=holidays',
  141. 'token' => 'admin-mc',
  142. ),
  143. 'additional_rows' => array(
  144. array(
  145. 'position' => 'below_table_data',
  146. 'value' => '
  147. <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. 'style' => 'text-align: right;',
  150. ),
  151. ),
  152. );
  153. require_once($sourcedir . '/Subs-List.php');
  154. createList($listOptions);
  155. //loadTemplate('ManageCalendar');
  156. $context['page_title'] = $txt['manage_holidays'];
  157. // Since the list is the only thing to show, use the default list template.
  158. $context['default_list'] = 'holiday_list';
  159. $context['sub_template'] = 'show_list';
  160. }
  161. /**
  162. * This function is used for adding/editing a specific holiday
  163. */
  164. function EditHoliday()
  165. {
  166. global $txt, $context, $scripturl, $smcFunc;
  167. loadTemplate('ManageCalendar');
  168. $context['is_new'] = !isset($_REQUEST['holiday']);
  169. $context['page_title'] = $context['is_new'] ? $txt['holidays_add'] : $txt['holidays_edit'];
  170. $context['sub_template'] = 'edit_holiday';
  171. // Cast this for safety...
  172. if (isset($_REQUEST['holiday']))
  173. $_REQUEST['holiday'] = (int) $_REQUEST['holiday'];
  174. // Submitting?
  175. if (isset($_POST[$context['session_var']]) && (isset($_REQUEST['delete']) || $_REQUEST['title'] != ''))
  176. {
  177. checkSession();
  178. // Not too long good sir?
  179. $_REQUEST['title'] = $smcFunc['substr']($_REQUEST['title'], 0, 60);
  180. $_REQUEST['holiday'] = isset($_REQUEST['holiday']) ? (int) $_REQUEST['holiday'] : 0;
  181. if (isset($_REQUEST['delete']))
  182. $smcFunc['db_query']('', '
  183. DELETE FROM {db_prefix}calendar_holidays
  184. WHERE id_holiday = {int:selected_holiday}',
  185. array(
  186. 'selected_holiday' => $_REQUEST['holiday'],
  187. )
  188. );
  189. else
  190. {
  191. $date = strftime($_REQUEST['year'] <= 4 ? '0004-%m-%d' : '%Y-%m-%d', mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']));
  192. if (isset($_REQUEST['edit']))
  193. $smcFunc['db_query']('', '
  194. UPDATE {db_prefix}calendar_holidays
  195. SET event_date = {date:holiday_date}, title = {string:holiday_title}
  196. WHERE id_holiday = {int:selected_holiday}',
  197. array(
  198. 'holiday_date' => $date,
  199. 'selected_holiday' => $_REQUEST['holiday'],
  200. 'holiday_title' => $_REQUEST['title'],
  201. )
  202. );
  203. else
  204. $smcFunc['db_insert']('',
  205. '{db_prefix}calendar_holidays',
  206. array(
  207. 'event_date' => 'date', 'title' => 'string-60',
  208. ),
  209. array(
  210. $date, $_REQUEST['title'],
  211. ),
  212. array('id_holiday')
  213. );
  214. }
  215. updateSettings(array(
  216. 'calendar_updated' => time(),
  217. ));
  218. redirectexit('action=admin;area=managecalendar;sa=holidays');
  219. }
  220. // Default states...
  221. if ($context['is_new'])
  222. $context['holiday'] = array(
  223. 'id' => 0,
  224. 'day' => date('d'),
  225. 'month' => date('m'),
  226. 'year' => '0000',
  227. 'title' => ''
  228. );
  229. // If it's not new load the data.
  230. else
  231. {
  232. $request = $smcFunc['db_query']('', '
  233. SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
  234. FROM {db_prefix}calendar_holidays
  235. WHERE id_holiday = {int:selected_holiday}
  236. LIMIT 1',
  237. array(
  238. 'selected_holiday' => $_REQUEST['holiday'],
  239. )
  240. );
  241. while ($row = $smcFunc['db_fetch_assoc']($request))
  242. $context['holiday'] = array(
  243. 'id' => $row['id_holiday'],
  244. 'day' => $row['day'],
  245. 'month' => $row['month'],
  246. 'year' => $row['year'] <= 4 ? 0 : $row['year'],
  247. 'title' => $row['title']
  248. );
  249. $smcFunc['db_free_result']($request);
  250. }
  251. // Last day for the drop down?
  252. $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']));
  253. }
  254. /**
  255. * Show and allow to modify calendar settings. Obviously.
  256. *
  257. * @param bool $return_config = false
  258. */
  259. function ModifyCalendarSettings($return_config = false)
  260. {
  261. global $modSettings, $context, $settings, $txt, $boarddir, $sourcedir, $scripturl, $smcFunc;
  262. // Load the boards list.
  263. $boards = array('');
  264. $request = $smcFunc['db_query']('order_by_board_order', '
  265. SELECT b.id_board, b.name AS board_name, c.name AS cat_name
  266. FROM {db_prefix}boards AS b
  267. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)',
  268. array(
  269. )
  270. );
  271. while ($row = $smcFunc['db_fetch_assoc']($request))
  272. $boards[$row['id_board']] = $row['cat_name'] . ' - ' . $row['board_name'];
  273. $smcFunc['db_free_result']($request);
  274. // Look, all the calendar settings - of which there are many!
  275. $config_vars = array(
  276. // All the permissions:
  277. array('permissions', 'calendar_view', 'help' => 'cal_enabled'),
  278. array('permissions', 'calendar_post'),
  279. array('permissions', 'calendar_edit_own'),
  280. array('permissions', 'calendar_edit_any'),
  281. '',
  282. // How many days to show on board index, and where to display events etc?
  283. array('int', 'cal_days_for_index', 6, 'postinput' => $txt['days_word']),
  284. 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'])),
  285. 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'])),
  286. 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'])),
  287. array('check', 'cal_export'),
  288. '',
  289. // Linking events etc...
  290. array('select', 'cal_defaultboard', $boards),
  291. array('check', 'cal_daysaslink'),
  292. array('check', 'cal_allow_unlinked'),
  293. array('check', 'cal_showInTopic'),
  294. '',
  295. // Dates of calendar...
  296. array('int', 'cal_minyear'),
  297. array('int', 'cal_maxyear'),
  298. '',
  299. // Calendar spanning...
  300. array('check', 'cal_allowspan'),
  301. array('int', 'cal_maxspan', 6, 'postinput' => $txt['days_word']),
  302. );
  303. call_integration_hook('integrate_modify_calendar_settings', array(&$config_vars));
  304. if ($return_config)
  305. return $config_vars;
  306. // Get the settings template fired up.
  307. require_once($sourcedir . '/ManageServer.php');
  308. // Some important context stuff
  309. $context['page_title'] = $txt['calendar_settings'];
  310. $context['sub_template'] = 'show_settings';
  311. // Get the final touches in place.
  312. $context['post_url'] = $scripturl . '?action=admin;area=managecalendar;save;sa=settings';
  313. $context['settings_title'] = $txt['calendar_settings'];
  314. // Saving the settings?
  315. if (isset($_GET['save']))
  316. {
  317. checkSession();
  318. call_integration_hook('integrate_save_calendar_settings');
  319. saveDBSettings($config_vars);
  320. // Update the stats in case.
  321. updateSettings(array(
  322. 'calendar_updated' => time(),
  323. ));
  324. redirectexit('action=admin;area=managecalendar;sa=settings');
  325. }
  326. // We need this for the in-line permissions
  327. createToken('admin-mp');
  328. // Prepare the settings...
  329. prepareDBSettingContext($config_vars);
  330. }
  331. ?>