ManageCalendar.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 2011 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('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. 'class' => 'windowbg',
  119. ),
  120. 'sort' => array(
  121. 'default' => 'event_date',
  122. 'reverse' => 'event_date DESC',
  123. ),
  124. ),
  125. 'check' => array(
  126. 'header' => array(
  127. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  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. 'style' => 'text-align: center',
  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' => '
  148. <a href="' . $scripturl . '?action=admin;area=managecalendar;sa=editholiday" style="margin: 0 1em">[' . $txt['holidays_add'] . ']</a>
  149. <input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button_submit" />',
  150. 'style' => 'text-align: right;',
  151. ),
  152. ),
  153. );
  154. require_once($sourcedir . '/Subs-List.php');
  155. createList($listOptions);
  156. //loadTemplate('ManageCalendar');
  157. $context['page_title'] = $txt['manage_holidays'];
  158. // Since the list is the only thing to show, use the default list template.
  159. $context['default_list'] = 'holiday_list';
  160. $context['sub_template'] = 'show_list';
  161. }
  162. /**
  163. * This function is used for adding/editing a specific holiday
  164. */
  165. function EditHoliday()
  166. {
  167. global $txt, $context, $scripturl, $smcFunc;
  168. loadTemplate('ManageCalendar');
  169. $context['is_new'] = !isset($_REQUEST['holiday']);
  170. $context['page_title'] = $context['is_new'] ? $txt['holidays_add'] : $txt['holidays_edit'];
  171. $context['sub_template'] = 'edit_holiday';
  172. // Cast this for safety...
  173. if (isset($_REQUEST['holiday']))
  174. $_REQUEST['holiday'] = (int) $_REQUEST['holiday'];
  175. // Submitting?
  176. if (isset($_POST[$context['session_var']]) && (isset($_REQUEST['delete']) || $_REQUEST['title'] != ''))
  177. {
  178. checkSession();
  179. // Not too long good sir?
  180. $_REQUEST['title'] = $smcFunc['substr']($_REQUEST['title'], 0, 60);
  181. $_REQUEST['holiday'] = isset($_REQUEST['holiday']) ? (int) $_REQUEST['holiday'] : 0;
  182. if (isset($_REQUEST['delete']))
  183. $smcFunc['db_query']('', '
  184. DELETE FROM {db_prefix}calendar_holidays
  185. WHERE id_holiday = {int:selected_holiday}',
  186. array(
  187. 'selected_holiday' => $_REQUEST['holiday'],
  188. )
  189. );
  190. else
  191. {
  192. $date = strftime($_REQUEST['year'] <= 4 ? '0004-%m-%d' : '%Y-%m-%d', mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']));
  193. if (isset($_REQUEST['edit']))
  194. $smcFunc['db_query']('', '
  195. UPDATE {db_prefix}calendar_holidays
  196. SET event_date = {date:holiday_date}, title = {string:holiday_title}
  197. WHERE id_holiday = {int:selected_holiday}',
  198. array(
  199. 'holiday_date' => $date,
  200. 'selected_holiday' => $_REQUEST['holiday'],
  201. 'holiday_title' => $_REQUEST['title'],
  202. )
  203. );
  204. else
  205. $smcFunc['db_insert']('',
  206. '{db_prefix}calendar_holidays',
  207. array(
  208. 'event_date' => 'date', 'title' => 'string-60',
  209. ),
  210. array(
  211. $date, $_REQUEST['title'],
  212. ),
  213. array('id_holiday')
  214. );
  215. }
  216. updateSettings(array(
  217. 'calendar_updated' => time(),
  218. ));
  219. redirectexit('action=admin;area=managecalendar;sa=holidays');
  220. }
  221. // Default states...
  222. if ($context['is_new'])
  223. $context['holiday'] = array(
  224. 'id' => 0,
  225. 'day' => date('d'),
  226. 'month' => date('m'),
  227. 'year' => '0000',
  228. 'title' => ''
  229. );
  230. // If it's not new load the data.
  231. else
  232. {
  233. $request = $smcFunc['db_query']('', '
  234. SELECT id_holiday, YEAR(event_date) AS year, MONTH(event_date) AS month, DAYOFMONTH(event_date) AS day, title
  235. FROM {db_prefix}calendar_holidays
  236. WHERE id_holiday = {int:selected_holiday}
  237. LIMIT 1',
  238. array(
  239. 'selected_holiday' => $_REQUEST['holiday'],
  240. )
  241. );
  242. while ($row = $smcFunc['db_fetch_assoc']($request))
  243. $context['holiday'] = array(
  244. 'id' => $row['id_holiday'],
  245. 'day' => $row['day'],
  246. 'month' => $row['month'],
  247. 'year' => $row['year'] <= 4 ? 0 : $row['year'],
  248. 'title' => $row['title']
  249. );
  250. $smcFunc['db_free_result']($request);
  251. }
  252. // Last day for the drop down?
  253. $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']));
  254. }
  255. /**
  256. * Show and allow to modify calendar settings. Obviously.
  257. *
  258. * @param bool $return_config = false
  259. */
  260. function ModifyCalendarSettings($return_config = false)
  261. {
  262. global $modSettings, $context, $settings, $txt, $boarddir, $sourcedir, $scripturl, $smcFunc;
  263. // Load the boards list.
  264. $boards = array('');
  265. $request = $smcFunc['db_query']('order_by_board_order', '
  266. SELECT b.id_board, b.name AS board_name, c.name AS cat_name
  267. FROM {db_prefix}boards AS b
  268. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)',
  269. array(
  270. )
  271. );
  272. while ($row = $smcFunc['db_fetch_assoc']($request))
  273. $boards[$row['id_board']] = $row['cat_name'] . ' - ' . $row['board_name'];
  274. $smcFunc['db_free_result']($request);
  275. // Look, all the calendar settings - of which there are many!
  276. $config_vars = array(
  277. // All the permissions:
  278. array('permissions', 'calendar_view', 'help' => 'cal_enabled'),
  279. array('permissions', 'calendar_post'),
  280. array('permissions', 'calendar_edit_own'),
  281. array('permissions', 'calendar_edit_any'),
  282. '',
  283. // How many days to show on board index, and where to display events etc?
  284. array('int', 'cal_days_for_index'),
  285. 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'])),
  286. 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'])),
  287. 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'])),
  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'),
  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. // Prepare the settings...
  327. prepareDBSettingContext($config_vars);
  328. }
  329. ?>