ManageCalendar.php 11 KB

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