PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/calendar/calendar.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1268 lines | 1102 code | 70 blank | 96 comment | 212 complexity | 97eab6853c6f6da6caa8372e2f84d7bf MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Calendar
  4. Plugin URI: http://www.kieranoshea.com
  5. Description: This plugin allows you to display a calendar of all your events and appointments as a page on your site.
  6. Author: Kieran O'Shea
  7. Author URI: http://www.kieranoshea.com
  8. Version: 1.2.2
  9. */
  10. /* Copyright 2008 Kieran O'Shea (email : kieran@kieranoshea.com)
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. // Enable internationalisation
  24. $plugin_dir = basename(dirname(__FILE__));
  25. load_plugin_textdomain( 'calendar','wp-content/plugins/'.$plugin_dir, $plugin_dir);
  26. // Define the tables used in Calendar
  27. define('WP_CALENDAR_TABLE', $table_prefix . 'calendar');
  28. define('WP_CALENDAR_CONFIG_TABLE', $table_prefix . 'calendar_config');
  29. define('WP_CALENDAR_CATEGORIES_TABLE', $table_prefix . 'calendar_categories');
  30. // Check ensure calendar is installed and install it if not - required for
  31. // the successful operation of most functions called from this point on
  32. check_calendar();
  33. // Create a master category for Calendar and its sub-pages
  34. add_action('admin_menu', 'calendar_menu');
  35. // Enable the ability for the calendar to be loaded from pages
  36. add_filter('the_content','calendar_insert');
  37. // Enable the ability for the lists to be loaded from pages
  38. add_filter('the_content','upcoming_insert');
  39. add_filter('the_content','todays_insert');
  40. // Add the function that puts style information in the header
  41. add_action('wp_head', 'calendar_wp_head');
  42. // Add the function that deals with deleted users
  43. add_action('delete_user', 'deal_with_deleted_user');
  44. // Add the widgets if we are using version 2.8
  45. add_action('widgets_init', 'widget_init_calendar_today');
  46. add_action('widgets_init', 'widget_init_calendar_upcoming');
  47. // Before we get on with the functions, we need to define the initial style used for Calendar
  48. // Function to deal with events posted by a user when that user is deleted
  49. function deal_with_deleted_user($id)
  50. {
  51. global $wpdb;
  52. // Do the query
  53. $wpdb->get_results("UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0)." WHERE event_author=".$id);
  54. }
  55. // Function to provide time with WordPress offset, localy replaces time()
  56. function ctwo()
  57. {
  58. return (time()+(3600*(get_option('gmt_offset'))));
  59. }
  60. // Function to add the calendar style into the header
  61. function calendar_wp_head()
  62. {
  63. global $wpdb;
  64. $styles = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
  65. if (!empty($styles))
  66. {
  67. foreach ($styles as $style)
  68. {
  69. echo '<style type="text/css">
  70. ';
  71. echo $style->config_value.'
  72. ';
  73. echo '</style>
  74. ';
  75. }
  76. }
  77. }
  78. // Function to deal with adding the calendar menus
  79. function calendar_menu()
  80. {
  81. global $wpdb;
  82. // Set admin as the only one who can use Calendar for security
  83. $allowed_group = 'manage_options';
  84. // Use the database to *potentially* override the above if allowed
  85. $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
  86. if (!empty($configs))
  87. {
  88. foreach ($configs as $config)
  89. {
  90. $allowed_group = $config->config_value;
  91. }
  92. }
  93. // Add the admin panel pages for Calendar. Use permissions pulled from above
  94. if (function_exists('add_menu_page'))
  95. {
  96. add_menu_page(__('Calendar','calendar'), __('Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
  97. }
  98. if (function_exists('add_submenu_page'))
  99. {
  100. add_submenu_page('calendar', __('Manage Calendar','calendar'), __('Manage Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
  101. add_action( "admin_head", 'calendar_add_javascript' );
  102. // Note only admin can change calendar options
  103. add_submenu_page('calendar', __('Manage Categories','calendar'), __('Manage Categories','calendar'), 'manage_options', 'calendar-categories', 'manage_categories');
  104. add_submenu_page('calendar', __('Calendar Config','calendar'), __('Calendar Options','calendar'), 'manage_options', 'calendar-config', 'edit_calendar_config');
  105. }
  106. }
  107. // Function to add the javascript to the admin header
  108. function calendar_add_javascript()
  109. {
  110. echo '<script type="text/javascript" src="';
  111. bloginfo('wpurl');
  112. echo '/wp-content/plugins/calendar/javascript.js"></script>
  113. <script type="text/javascript">document.write(getCalendarStyles());</script>
  114. ';
  115. }
  116. // Function to deal with loading the calendar into pages
  117. function calendar_insert($content)
  118. {
  119. if (preg_match('{CALENDAR}',$content))
  120. {
  121. $cal_output = calendar();
  122. $content = str_replace('{CALENDAR}',$cal_output,$content);
  123. }
  124. return $content;
  125. }
  126. // Functions to allow the widgets to be inserted into posts and pages
  127. function upcoming_insert($content)
  128. {
  129. if (preg_match('{UPCOMING_EVENTS}',$content))
  130. {
  131. $cal_output = '<span class="page-upcoming-events">'.upcoming_events().'</span>';
  132. $content = str_replace('{UPCOMING_EVENTS}',$cal_output,$content);
  133. }
  134. return $content;
  135. }
  136. function todays_insert($content)
  137. {
  138. if (preg_match('{TODAYS_EVENTS}',$content))
  139. {
  140. $cal_output = '<span class="page-todays-events">'.todays_events().'</span>';
  141. $content = str_replace('{TODAYS_EVENTS}',$cal_output,$content);
  142. }
  143. return $content;
  144. }
  145. // Function to check what version of Calendar is installed and install if needed
  146. function check_calendar()
  147. {
  148. // Checks to make sure Calendar is installed, if not it adds the default
  149. // database tables and populates them with test data. If it is, then the
  150. // version is checked through various means and if it is not up to date
  151. // then it is upgraded.
  152. // Lets see if this is first run and create us a table if it is!
  153. global $wpdb, $initial_style;
  154. // All this style info will go into the database on a new install
  155. // This looks nice in the Kubrick theme
  156. $initial_style = " .calnk a:hover {
  157. background-position:0 0;
  158. text-decoration:none;
  159. color:#000000;
  160. border-bottom:1px dotted #000000;
  161. }
  162. .calnk a:visited {
  163. text-decoration:none;
  164. color:#000000;
  165. border-bottom:1px dotted #000000;
  166. }
  167. .calnk a {
  168. text-decoration:none;
  169. color:#000000;
  170. border-bottom:1px dotted #000000;
  171. }
  172. .calnk a span {
  173. display:none;
  174. }
  175. .calnk a:hover span {
  176. color:#333333;
  177. background:#F6F79B;
  178. display:block;
  179. position:absolute;
  180. margin-top:1px;
  181. padding:5px;
  182. width:150px;
  183. z-index:100;
  184. line-height:1.2em;
  185. }
  186. .calendar-table {
  187. border:none;
  188. width:100%;
  189. }
  190. .calendar-heading {
  191. height:25px;
  192. text-align:center;
  193. border:1px solid #D6DED5;
  194. background-color:#E4EBE3;
  195. }
  196. .calendar-next {
  197. width:25%;
  198. text-align:center;
  199. }
  200. .calendar-prev {
  201. width:25%;
  202. text-align:center;
  203. }
  204. .calendar-month {
  205. width:50%;
  206. text-align:center;
  207. font-weight:bold;
  208. }
  209. .normal-day-heading {
  210. text-align:center;
  211. width:25px;
  212. height:25px;
  213. font-size:0.8em;
  214. border:1px solid #DFE6DE;
  215. background-color:#EBF2EA;
  216. }
  217. .weekend-heading {
  218. text-align:center;
  219. width:25px;
  220. height:25px;
  221. font-size:0.8em;
  222. border:1px solid #DFE6DE;
  223. background-color:#EBF2EA;
  224. color:#FF0000;
  225. }
  226. .day-with-date {
  227. vertical-align:text-top;
  228. text-align:left;
  229. width:60px;
  230. height:60px;
  231. border:1px solid #DFE6DE;
  232. }
  233. .no-events {
  234. }
  235. .day-without-date {
  236. width:60px;
  237. height:60px;
  238. border:1px solid #E9F0E8;
  239. }
  240. span.weekend {
  241. color:#FF0000;
  242. }
  243. .current-day {
  244. vertical-align:text-top;
  245. text-align:left;
  246. width:60px;
  247. height:60px;
  248. border:1px solid #BFBFBF;
  249. background-color:#E4EBE3;
  250. }
  251. span.event {
  252. font-size:0.75em;
  253. }
  254. .kjo-link {
  255. font-size:0.75em;
  256. text-align:center;
  257. }
  258. .calendar-date-switcher {
  259. height:25px;
  260. text-align:center;
  261. border:1px solid #D6DED5;
  262. background-color:#E4EBE3;
  263. }
  264. .calendar-date-switcher form {
  265. margin:0;
  266. padding:0;
  267. }
  268. .calendar-date-switcher input {
  269. border:1px #D6DED5 solid;
  270. }
  271. .calendar-date-switcher select {
  272. border:1px #D6DED5 solid;
  273. }
  274. .cat-key {
  275. width:100%;
  276. margin-top:10px;
  277. padding:5px;
  278. border:1px solid #D6DED5;
  279. }
  280. .calnk a:hover span span.event-title {
  281. padding:0;
  282. text-align:center;
  283. font-weight:bold;
  284. font-size:1.2em;
  285. }
  286. .calnk a:hover span span.event-title-break {
  287. width:96%;
  288. text-align:center;
  289. height:1px;
  290. margin-top:5px;
  291. margin-right:2%;
  292. padding:0;
  293. background-color:#000000;
  294. }
  295. .calnk a:hover span span.event-content-break {
  296. width:96%;
  297. text-align:center;
  298. height:1px;
  299. margin-top:5px;
  300. margin-right:2%;
  301. padding:0;
  302. background-color:#000000;
  303. }
  304. .page-upcoming-events {
  305. font-size:80%;
  306. }
  307. .page-todays-events {
  308. font-size:80%;
  309. }";
  310. // Assume this is not a new install until we prove otherwise
  311. $new_install = false;
  312. $vone_point_one_upgrade = false;
  313. $vone_point_two_beta_upgrade = false;
  314. $wp_calendar_exists = false;
  315. $wp_calendar_config_exists = false;
  316. $wp_calendar_config_version_number_exists = false;
  317. // Determine the calendar version
  318. $tables = $wpdb->get_results("show tables");
  319. foreach ( $tables as $table )
  320. {
  321. foreach ( $table as $value )
  322. {
  323. if ( $value == WP_CALENDAR_TABLE )
  324. {
  325. $wp_calendar_exists = true;
  326. }
  327. if ( $value == WP_CALENDAR_CONFIG_TABLE )
  328. {
  329. $wp_calendar_config_exists = true;
  330. // We now try and find the calendar version number
  331. // This will be a lot easier than finding other stuff
  332. // in the future.
  333. $version_number = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_version'");
  334. if ($version_number == "1.2")
  335. {
  336. $wp_calendar_config_version_number_exists = true;
  337. }
  338. }
  339. }
  340. }
  341. if ($wp_calendar_exists == false && $wp_calendar_config_exists == false)
  342. {
  343. $new_install = true;
  344. }
  345. else if ($wp_calendar_exists == true && $wp_calendar_config_exists == false)
  346. {
  347. $vone_point_one_upgrade = true;
  348. }
  349. else if ($wp_calendar_exists == true && $wp_calendar_config_exists == true && $wp_calendar_config_version_number_exists == false)
  350. {
  351. $vone_point_two_beta_upgrade = true;
  352. }
  353. // Now we've determined what the current install is or isn't
  354. // we perform operations according to the findings
  355. if ( $new_install == true )
  356. {
  357. $sql = "CREATE TABLE " . WP_CALENDAR_TABLE . " (
  358. event_id INT(11) NOT NULL AUTO_INCREMENT ,
  359. event_begin DATE NOT NULL ,
  360. event_end DATE NOT NULL ,
  361. event_title VARCHAR(30) NOT NULL ,
  362. event_desc TEXT NOT NULL ,
  363. event_time TIME ,
  364. event_recur CHAR(1) ,
  365. event_repeats INT(3) ,
  366. event_author BIGINT(20) UNSIGNED,
  367. PRIMARY KEY (event_id)
  368. )";
  369. $wpdb->get_results($sql);
  370. $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
  371. config_item VARCHAR(30) NOT NULL ,
  372. config_value TEXT NOT NULL ,
  373. PRIMARY KEY (config_item)
  374. )";
  375. $wpdb->get_results($sql);
  376. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
  377. $wpdb->get_results($sql);
  378. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
  379. $wpdb->get_results($sql);
  380. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
  381. $wpdb->get_results($sql);
  382. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
  383. $wpdb->get_results($sql);
  384. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
  385. $wpdb->get_results($sql);
  386. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
  387. $wpdb->get_results($sql);
  388. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
  389. $wpdb->get_results($sql);
  390. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
  391. $wpdb->get_results($sql);
  392. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
  393. $wpdb->get_results($sql);
  394. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
  395. $wpdb->get_results($sql);
  396. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT DEFAULT ''";
  397. $wpdb->get_results($sql);
  398. $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
  399. category_id INT(11) NOT NULL AUTO_INCREMENT,
  400. category_name VARCHAR(30) NOT NULL ,
  401. category_colour VARCHAR(30) NOT NULL ,
  402. PRIMARY KEY (category_id)
  403. )";
  404. $wpdb->get_results($sql);
  405. $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
  406. $wpdb->get_results($sql);
  407. }
  408. else if ($vone_point_one_upgrade == true)
  409. {
  410. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_author BIGINT(20) UNSIGNED";
  411. $wpdb->get_results($sql);
  412. $sql = "UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0);
  413. $wpdb->get_results($sql);
  414. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." MODIFY event_desc TEXT NOT NULL";
  415. $wpdb->get_results($sql);
  416. $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
  417. config_item VARCHAR(30) NOT NULL ,
  418. config_value TEXT NOT NULL ,
  419. PRIMARY KEY (config_item)
  420. )";
  421. $wpdb->get_results($sql);
  422. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
  423. $wpdb->get_results($sql);
  424. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
  425. $wpdb->get_results($sql);
  426. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
  427. $wpdb->get_results($sql);
  428. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
  429. $wpdb->get_results($sql);
  430. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
  431. $wpdb->get_results($sql);
  432. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
  433. $wpdb->get_results($sql);
  434. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
  435. $wpdb->get_results($sql);
  436. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
  437. $wpdb->get_results($sql);
  438. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
  439. $wpdb->get_results($sql);
  440. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
  441. $wpdb->get_results($sql);
  442. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT DEFAULT ''";
  443. $wpdb->get_results($sql);
  444. $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
  445. category_id INT(11) NOT NULL AUTO_INCREMENT,
  446. category_name VARCHAR(30) NOT NULL ,
  447. category_colour VARCHAR(30) NOT NULL ,
  448. PRIMARY KEY (category_id)
  449. )";
  450. $wpdb->get_results($sql);
  451. $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
  452. $wpdb->get_results($sql);
  453. }
  454. else if ($vone_point_two_beta_upgrade == true)
  455. {
  456. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
  457. $wpdb->get_results($sql);
  458. $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
  459. $wpdb->get_results($sql);
  460. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
  461. $wpdb->get_results($sql);
  462. $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT DEFAULT ''";
  463. $wpdb->get_results($sql);
  464. $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
  465. category_id INT(11) NOT NULL AUTO_INCREMENT,
  466. category_name VARCHAR(30) NOT NULL ,
  467. category_colour VARCHAR(30) NOT NULL ,
  468. PRIMARY KEY (category_id)
  469. )";
  470. $wpdb->get_results($sql);
  471. $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
  472. $wpdb->get_results($sql);
  473. $sql = "UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value='".$initial_style."' WHERE config_item='calendar_style'";
  474. $wpdb->get_results($sql);
  475. }
  476. }
  477. // Used on the manage events admin page to display a list of events
  478. function wp_events_display_list()
  479. {
  480. global $wpdb;
  481. $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " ORDER BY event_begin DESC");
  482. if ( !empty($events) )
  483. {
  484. ?>
  485. <table class="widefat page fixed" width="100%" cellpadding="3" cellspacing="3">
  486. <thead>
  487. <tr>
  488. <th class="manage-column" scope="col"><?php _e('ID','calendar') ?></th>
  489. <th class="manage-column" scope="col"><?php _e('Title','calendar') ?></th>
  490. <th class="manage-column" scope="col"><?php _e('Start Date','calendar') ?></th>
  491. <th class="manage-column" scope="col"><?php _e('End Date','calendar') ?></th>
  492. <th class="manage-column" scope="col"><?php _e('Time','calendar') ?></th>
  493. <th class="manage-column" scope="col"><?php _e('Recurs','calendar') ?></th>
  494. <th class="manage-column" scope="col"><?php _e('Repeats','calendar') ?></th>
  495. <th class="manage-column" scope="col"><?php _e('Author','calendar') ?></th>
  496. <th class="manage-column" scope="col"><?php _e('Category','calendar') ?></th>
  497. <th class="manage-column" scope="col"><?php _e('Edit','calendar') ?></th>
  498. <th class="manage-column" scope="col"><?php _e('Delete','calendar') ?></th>
  499. </tr>
  500. </thead>
  501. <?php
  502. $class = '';
  503. foreach ( $events as $event )
  504. {
  505. $class = ($class == 'alternate') ? '' : 'alternate';
  506. ?>
  507. <tr class="<?php echo $class; ?>">
  508. <th scope="row"><?php echo $event->event_id; ?></th>
  509. <td><?php echo $event->event_title; ?></td>
  510. <td><?php echo $event->event_begin; ?></td>
  511. <td><?php echo $event->event_end; ?></td>
  512. <td><?php if ($event->event_time == '00:00:00') { echo __('N/A','calendar'); } else { echo $event->event_time; } ?></td>
  513. <td>
  514. <?php
  515. // Interpret the DB values into something human readable
  516. if ($event->event_recur == 'S') { echo __('Never','calendar'); }
  517. else if ($event->event_recur == 'W') { echo __('Weekly','calendar'); }
  518. else if ($event->event_recur == 'M') { echo __('Monthly','calendar'); }
  519. else if ($event->event_recur == 'Y') { echo __('Yearly','calendar'); }
  520. ?>
  521. </td>
  522. <td>
  523. <?php
  524. // Interpret the DB values into something human readable
  525. if ($event->event_recur == 'S') { echo __('N/A','calendar'); }
  526. else if ($event->event_repeats == 0) { echo __('Forever','calendar'); }
  527. else if ($event->event_repeats > 0) { echo $event->event_repeats.' '.__('Times','calendar'); }
  528. ?>
  529. </td>
  530. <td><?php $e = get_userdata($event->event_author); echo $e->display_name; ?></td>
  531. <?php
  532. $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".$event->event_category;
  533. $this_cat = $wpdb->get_row($sql);
  534. ?>
  535. <td style="background-color:<?php echo $this_cat->category_colour;?>;"><?php echo $this_cat->category_name; ?></td>
  536. <?php unset($this_cat); ?>
  537. <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar&amp;action=edit&amp;event_id=<?php echo $event->event_id;?>" class='edit'><?php echo __('Edit','calendar'); ?></a></td>
  538. <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar&amp;action=delete&amp;event_id=<?php echo $event->event_id;?>" class="delete" onclick="return confirm('<?php _e('Are you sure you want to delete this event?','calendar'); ?>')"><?php echo __('Delete','calendar'); ?></a></td>
  539. </tr>
  540. <?php
  541. }
  542. ?>
  543. </table>
  544. <?php
  545. }
  546. else
  547. {
  548. ?>
  549. <p><?php _e("There are no events in the database!",'calendar') ?></p>
  550. <?php
  551. }
  552. }
  553. // The event edit form for the manage events admin page
  554. function wp_events_edit_form($mode='add', $event_id=false)
  555. {
  556. global $wpdb,$users_entries;
  557. $data = false;
  558. if ( $event_id !== false )
  559. {
  560. if ( intval($event_id) != $event_id )
  561. {
  562. echo "<div class=\"error\"><p>".__('Bad Monkey! No banana!','calendar')."</p></div>";
  563. return;
  564. }
  565. else
  566. {
  567. $data = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "' LIMIT 1");
  568. if ( empty($data) )
  569. {
  570. echo "<div class=\"error\"><p>".__("An event with that ID couldn't be found",'calendar')."</p></div>";
  571. return;
  572. }
  573. $data = $data[0];
  574. }
  575. // Recover users entries if they exist; in other words if editing an event went wrong
  576. if (!empty($users_entries))
  577. {
  578. $data = $users_entries;
  579. }
  580. }
  581. // Deal with possibility that form was submitted but not saved due to error - recover user's entries here
  582. else
  583. {
  584. $data = $users_entries;
  585. }
  586. ?>
  587. <div id="pop_up_cal" style="position:absolute;margin-left:150px;visibility:hidden;background-color:white;layer-background-color:white;z-index:1;"></div>
  588. <form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar">
  589. <input type="hidden" name="action" value="<?php echo $mode; ?>">
  590. <input type="hidden" name="event_id" value="<?php echo $event_id; ?>">
  591. <div id="linkadvanceddiv" class="postbox">
  592. <div style="float: left; width: 98%; clear: both;" class="inside">
  593. <table cellpadding="5" cellspacing="5">
  594. <tr>
  595. <td><legend><?php _e('Event Title','calendar'); ?></legend></td>
  596. <td><input type="text" name="event_title" class="input" size="40" maxlength="30"
  597. value="<?php if ( !empty($data) ) echo htmlspecialchars($data->event_title); ?>" /></td>
  598. </tr>
  599. <tr>
  600. <td style="vertical-align:top;"><legend><?php _e('Event Description','calendar'); ?></legend></td>
  601. <td><textarea name="event_desc" class="input" rows="5" cols="50"><?php if ( !empty($data) ) echo htmlspecialchars($data->event_desc); ?></textarea></td>
  602. </tr>
  603. <tr>
  604. <td><legend><?php _e('Event Category','calendar'); ?></legend></td>
  605. <td> <select name="event_category">
  606. <?php
  607. // Grab all the categories and list them
  608. $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE;
  609. $cats = $wpdb->get_results($sql);
  610. foreach($cats as $cat)
  611. {
  612. echo '<option value="'.$cat->category_id.'"';
  613. if (!empty($data))
  614. {
  615. if ($data->event_category == $cat->category_id)
  616. {
  617. echo 'selected="selected"';
  618. }
  619. }
  620. echo '>'.$cat->category_name.'</option>
  621. ';
  622. }
  623. ?>
  624. </select>
  625. </td>
  626. </tr>
  627. <tr>
  628. <td><legend><?php _e('Event Link (Optional)','calendar'); ?></legend></td>
  629. <td><input type="text" name="event_link" class="input" size="40" value="<?php if ( !empty($data) ) echo htmlspecialchars($data->event_link); ?>" /></td>
  630. </tr>
  631. <tr>
  632. <td><legend><?php _e('Start Date','calendar'); ?></legend></td>
  633. <td> <script type="text/javascript">
  634. var cal_begin = new CalendarPopup('pop_up_cal');
  635. cal_begin.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
  636. function unifydates() {
  637. document.forms['quoteform'].event_end.value = document.forms['quoteform'].event_begin.value;
  638. }
  639. </script>
  640. <input type="text" name="event_begin" class="input" size="12"
  641. value="<?php
  642. if ( !empty($data) )
  643. {
  644. echo htmlspecialchars($data->event_begin);
  645. }
  646. else
  647. {
  648. echo date("Y-m-d",ctwo());
  649. }
  650. ?>" /> <a href="#" onClick="cal_begin.select(document.forms['quoteform'].event_begin,'event_begin_anchor','yyyy-MM-dd'); return false;" name="event_begin_anchor" id="event_begin_anchor"><?php _e('Select Date','calendar'); ?></a>
  651. </td>
  652. </tr>
  653. <tr>
  654. <td><legend><?php _e('End Date','calendar'); ?></legend></td>
  655. <td> <script type="text/javascript">
  656. function check_and_print() {
  657. unifydates();
  658. var cal_end = new CalendarPopup('pop_up_cal');
  659. cal_end.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
  660. var newDate = new Date();
  661. newDate.setFullYear(document.forms['quoteform'].event_begin.value.split('-')[0],document.forms['quoteform'].event_begin.value.split('-')[1]-1,document.forms['quoteform'].event_begin.value.split('-')[2]);
  662. newDate.setDate(newDate.getDate()-1);
  663. cal_end.addDisabledDates(null, formatDate(newDate, "yyyy-MM-dd"));
  664. cal_end.select(document.forms['quoteform'].event_end,'event_end_anchor','yyyy-MM-dd');
  665. }
  666. </script>
  667. <input type="text" name="event_end" class="input" size="12"
  668. value="<?php
  669. if ( !empty($data) )
  670. {
  671. echo htmlspecialchars($data->event_end);
  672. }
  673. else
  674. {
  675. echo date("Y-m-d",ctwo());
  676. }
  677. ?>" /> <a href="#" onClick="check_and_print(); return false;" name="event_end_anchor" id="event_end_anchor"><?php _e('Select Date','calendar'); ?></a>
  678. </td>
  679. </tr>
  680. <tr>
  681. <td><legend><?php _e('Time (hh:mm)','calendar'); ?></legend></td>
  682. <td> <input type="text" name="event_time" class="input" size=12
  683. value="<?php
  684. if ( !empty($data) )
  685. {
  686. if ($data->event_time == "00:00:00")
  687. {
  688. echo '';
  689. }
  690. else
  691. {
  692. echo date("H:i",strtotime(htmlspecialchars($data->event_time)));
  693. }
  694. }
  695. else
  696. {
  697. echo date("H:i",ctwo());
  698. }
  699. ?>" /> <?php _e('Optional, set blank if not required.','calendar'); ?> <?php _e('Current time difference from GMT is ','calendar'); echo get_option('gmt_offset'); _e(' hour(s)','calendar'); ?>
  700. </td>
  701. </tr>
  702. <tr>
  703. <td><legend><?php _e('Recurring Events','calendar'); ?></legend></td>
  704. <td> <?php
  705. if ($data->event_repeats != NULL)
  706. {
  707. $repeats = $data->event_repeats;
  708. }
  709. else
  710. {
  711. $repeats = 0;
  712. }
  713. if ($data->event_recur == "S")
  714. {
  715. $selected_s = 'selected="selected"';
  716. }
  717. else if ($data->event_recur == "W")
  718. {
  719. $selected_w = 'selected="selected"';
  720. }
  721. else if ($data->event_recur == "M")
  722. {
  723. $selected_m = 'selected="selected"';
  724. }
  725. else if ($data->event_recur == "Y")
  726. {
  727. $selected_y = 'selected="selected"';
  728. }
  729. ?>
  730. <?php _e('Repeats for','calendar'); ?>
  731. <input type="text" name="event_repeats" class="input" size="1" value="<?php echo $repeats; ?>" />
  732. <select name="event_recur" class="input">
  733. <option class="input" <?php echo $selected_s; ?> value="S">None</option>
  734. <option class="input" <?php echo $selected_w; ?> value="W">Weeks</option>
  735. <option class="input" <?php echo $selected_m; ?> value="M">Months</option>
  736. <option class="input" <?php echo $selected_y; ?> value="Y">Years</option>
  737. </select><br />
  738. <?php _e('Entering 0 means forever. Where the recurrance interval is left at none, the event will not reoccur.','calendar'); ?>
  739. </td>
  740. </tr>
  741. </table>
  742. </div>
  743. <div style="clear:both; height:1px;">&nbsp;</div>
  744. </div>
  745. <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
  746. </form>
  747. <?php
  748. }
  749. // The actual function called to render the manage events page and
  750. // to deal with posts
  751. function edit_calendar()
  752. {
  753. global $current_user, $wpdb, $users_entries;
  754. ?>
  755. <style type="text/css">
  756. <!--
  757. .error {
  758. background: lightcoral;
  759. border: 1px solid #e64f69;
  760. margin: 1em 5% 10px;
  761. padding: 0 1em 0 1em;
  762. }
  763. .center {
  764. text-align: center;
  765. }
  766. .right { text-align: right;
  767. }
  768. .left {
  769. text-align: left;
  770. }
  771. .top {
  772. vertical-align: top;
  773. }
  774. .bold {
  775. font-weight: bold;
  776. }
  777. .private {
  778. color: #e64f69;
  779. }
  780. //-->
  781. </style>
  782. <?php
  783. // First some quick cleaning up
  784. $edit = $create = $save = $delete = false;
  785. // Make sure we are collecting the variables we need to select years and months
  786. $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
  787. $event_id = !empty($_REQUEST['event_id']) ? $_REQUEST['event_id'] : '';
  788. // Deal with adding an event to the database
  789. if ( $action == 'add' )
  790. {
  791. $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
  792. $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
  793. $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
  794. $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
  795. $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
  796. $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
  797. $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
  798. $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
  799. $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
  800. // Deal with the fools who have left magic quotes turned on
  801. if ( ini_get('magic_quotes_gpc') )
  802. {
  803. $title = stripslashes($title);
  804. $desc = stripslashes($desc);
  805. $begin = stripslashes($begin);
  806. $end = stripslashes($end);
  807. $time = stripslashes($time);
  808. $recur = stripslashes($recur);
  809. $repeats = stripslashes($repeats);
  810. $category = stripslashes($category);
  811. $linky = stripslashes($linky);
  812. }
  813. // Perform some validation on the submitted dates - this checks for valid years and months
  814. $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
  815. $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
  816. if ((preg_match($date_format_one,$begin) || preg_match($date_format_two,$begin)) && (preg_match($date_format_one,$end) || preg_match($date_format_two,$end)))
  817. {
  818. // We know we have a valid year and month and valid integers for days so now we do a final check on the date
  819. $begin_split = split('-',$begin);
  820. $begin_y = $begin_split[0];
  821. $begin_m = $begin_split[1];
  822. $begin_d = $begin_split[2];
  823. $end_split = split('-',$end);
  824. $end_y = $end_split[0];
  825. $end_m = $end_split[1];
  826. $end_d = $end_split[2];
  827. if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
  828. {
  829. // Ok, now we know we have valid dates, we want to make sure that they are either equal or that the end date is later than the start date
  830. if (strtotime($end) >= strtotime($begin))
  831. {
  832. $start_date_ok = 1;
  833. $end_date_ok = 1;
  834. }
  835. else
  836. {
  837. ?>
  838. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Your event end date must be either after or the same as your event begin date','calendar'); ?></p></div>
  839. <?php
  840. }
  841. }
  842. else
  843. {
  844. ?>
  845. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Your date formatting is correct but one or more of your dates is invalid. Check for number of days in month and leap year related errors.','calendar'); ?></p></div>
  846. <?php
  847. }
  848. }
  849. else
  850. {
  851. ?>
  852. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Both start and end dates must be entered and be in the format YYYY-MM-DD','calendar'); ?></p></div>
  853. <?php
  854. }
  855. // We check for a valid time, or an empty one
  856. $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
  857. $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
  858. if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
  859. {
  860. $time_ok = 1;
  861. if ($time == '')
  862. {
  863. $time_to_use = '00:00:00';
  864. }
  865. else if ($time == '00:00')
  866. {
  867. $time_to_use = '00:00:01';
  868. }
  869. else
  870. {
  871. $time_to_use = $time;
  872. }
  873. }
  874. else
  875. {
  876. ?>
  877. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The time field must either be blank or be entered in the format hh:mm','calendar'); ?></p></div>
  878. <?php
  879. }
  880. // We check to make sure the URL is alright
  881. if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
  882. {
  883. $url_ok = 1;
  884. }
  885. else
  886. {
  887. ?>
  888. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The URL entered must either be prefixed with http:// or be completely blank','calendar'); ?></p></div>
  889. <?php
  890. }
  891. // The title must be at least one character in length and no more than 30
  892. if (preg_match('/^.{1,30}$/',$title))
  893. {
  894. $title_ok =1;
  895. }
  896. else
  897. {
  898. ?>
  899. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The event title must be between 1 and 30 characters in length','calendar'); ?></p></div>
  900. <?php
  901. }
  902. // We run some checks on recurrance
  903. $repeats = (int)$repeats;
  904. if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y')))
  905. {
  906. $recurring_ok = 1;
  907. }
  908. else
  909. {
  910. ?>
  911. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The repetition value must be 0 unless a type of recurrance is selected in which case the repetition value must be 0 or higher','calendar'); ?></p></div>
  912. <?php
  913. }
  914. if ($start_date_ok == 1 && $end_date_ok == 1 && $time_ok == 1 && $url_ok == 1 && $title_ok == 1 && $recurring_ok == 1)
  915. {
  916. $sql = "INSERT INTO " . WP_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title)
  917. . "', event_desc='" . mysql_escape_string($desc) . "', event_begin='" . mysql_escape_string($begin)
  918. . "', event_end='" . mysql_escape_string($end) . "', event_time='" . mysql_escape_string($time_to_use) . "', event_recur='" . mysql_escape_string($recur) . "', event_repeats='" . mysql_escape_string($repeats) . "', event_author=".$current_user->ID.", event_category=".mysql_escape_string($category).", event_link='".mysql_escape_string($linky)."'";
  919. $wpdb->get_results($sql);
  920. $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'"
  921. . " AND event_desc='" . mysql_escape_string($desc) . "' AND event_begin='" . mysql_escape_string($begin) . "' AND event_end='" . mysql_escape_string($end) . "' AND event_recur='" . mysql_escape_string($recur) . "' AND event_repeats='" . mysql_escape_string($repeats) . "' LIMIT 1";
  922. $result = $wpdb->get_results($sql);
  923. if ( empty($result) || empty($result[0]->event_id) )
  924. {
  925. ?>
  926. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('An event with the details you submitted could not be found in the database. This may indicate a problem with your database or the way in which it is configured.','calendar'); ?></p></div>
  927. <?php
  928. }
  929. else
  930. {
  931. ?>
  932. <div class="updated"><p><?php _e('Event added. It will now show in your calendar.','calendar'); ?></p></div>
  933. <?php
  934. }
  935. }
  936. else
  937. {
  938. // The form is going to be rejected due to field validation issues, so we preserve the users entries here
  939. $users_entries->event_title = $title;
  940. $users_entries->event_desc = $desc;
  941. $users_entries->event_begin = $begin;
  942. $users_entries->event_end = $end;
  943. $users_entries->event_time = $time;
  944. $users_entries->event_recur = $recur;
  945. $users_entries->event_repeats = $repeats;
  946. $users_entries->event_category = $category;
  947. $users_entries->event_link = $linky;
  948. }
  949. }
  950. // Permit saving of events that have been edited
  951. elseif ( $action == 'edit_save' )
  952. {
  953. $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
  954. $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
  955. $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
  956. $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
  957. $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
  958. $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
  959. $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
  960. $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
  961. $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
  962. // Deal with the fools who have left magic quotes turned on
  963. if ( ini_get('magic_quotes_gpc') )
  964. {
  965. $title = stripslashes($title);
  966. $desc = stripslashes($desc);
  967. $begin = stripslashes($begin);
  968. $end = stripslashes($end);
  969. $time = stripslashes($time);
  970. $recur = stripslashes($recur);
  971. $repeats = stripslashes($repeats);
  972. $category = stripslashes($category);
  973. $linky = stripslashes($linky);
  974. }
  975. if ( empty($event_id) )
  976. {
  977. ?>
  978. <div class="error"><p><strong><?php _e('Failure','calendar'); ?>:</strong> <?php _e("You can't update an event if you haven't submitted an event id",'calendar'); ?></p></div>
  979. <?php
  980. }
  981. else
  982. {
  983. // Perform some validation on the submitted dates - this checks for valid years and months
  984. $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
  985. $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
  986. if ((preg_match($date_format_one,$begin) || preg_match($date_format_two,$begin)) && (preg_match($date_format_one,$end) || preg_match($date_format_two,$end)))
  987. {
  988. // We know we have a valid year and month and valid integers for days so now we do a final check on the date
  989. $begin_split = split('-',$begin);
  990. $begin_y = $begin_split[0];
  991. $begin_m = $begin_split[1];
  992. $begin_d = $begin_split[2];
  993. $end_split = split('-',$end);
  994. $end_y = $end_split[0];
  995. $end_m = $end_split[1];
  996. $end_d = $end_split[2];
  997. if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
  998. {
  999. // Ok, now we know we have valid dates, we want to make sure that they are either equal or that the end date is later than the start date
  1000. if (strtotime($end) >= strtotime($begin))
  1001. {
  1002. $start_date_ok = 1;
  1003. $end_date_ok = 1;
  1004. }
  1005. else
  1006. {
  1007. ?>
  1008. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Your event end date must be either after or the same as your event begin date','calendar'); ?></p></div>
  1009. <?php
  1010. }
  1011. }
  1012. else
  1013. {
  1014. ?>
  1015. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Your date formatting is correct but one or more of your dates is invalid. Check for number of days in month and leap year related errors.','calendar'); ?></p></div>
  1016. <?php
  1017. }
  1018. }
  1019. else
  1020. {
  1021. ?>
  1022. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Both start and end dates must be entered and be in the format YYYY-MM-DD','calendar'); ?></p></div>
  1023. <?php
  1024. }
  1025. // We check for a valid time, or an empty one
  1026. $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
  1027. $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
  1028. if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
  1029. {
  1030. $time_ok = 1;
  1031. if ($time == '')
  1032. {
  1033. $time_to_use = '00:00:00';
  1034. }
  1035. else if ($time == '00:00')
  1036. {
  1037. $time_to_use = '00:00:01';
  1038. }
  1039. else
  1040. {
  1041. $time_to_use = $time;
  1042. }
  1043. }
  1044. else
  1045. {
  1046. ?>
  1047. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The time field must either be blank or be entered in the format hh:mm','calendar'); ?></p></div>
  1048. <?php
  1049. }
  1050. // We check to make sure the URL is alright
  1051. if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
  1052. {
  1053. $url_ok = 1;
  1054. }
  1055. else
  1056. {
  1057. ?>
  1058. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The URL entered must either be prefixed with http:// or be completely blank','calendar'); ?></p></div>
  1059. <?php
  1060. }
  1061. // The title must be at least one character in length and no more than 30
  1062. if (preg_match('/^.{1,30}$/',$title))
  1063. {
  1064. $title_ok =1;
  1065. }
  1066. else
  1067. {
  1068. ?>
  1069. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The event title must be between 1 and 30 characters in length','calendar'); ?></p></div>
  1070. <?php
  1071. }
  1072. // We run some checks on recurrance
  1073. $repeats = (int)$repeats;
  1074. if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y')))
  1075. {
  1076. $recurring_ok = 1;
  1077. }
  1078. else
  1079. {
  1080. ?>
  1081. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('The repetition value must be 0 unless a type of recurrance is selected in which case the repetition value must be 0 or higher','calendar'); ?></p></div>
  1082. <?php
  1083. }
  1084. if ($start_date_ok == 1 && $end_date_ok == 1 && $time_ok == 1 && $url_ok == 1 && $title_ok == 1 && $recurring_ok == 1)
  1085. {
  1086. $sql = "UPDATE " . WP_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title)
  1087. . "', event_desc='" . mysql_escape_string($desc) . "', event_begin='" . mysql_escape_string($begin)
  1088. . "', event_end='" . mysql_escape_string($end) . "', event_time='" . mysql_escape_string($time_to_use) . "', event_recur='" . mysql_escape_string($recur) . "', event_repeats='" . mysql_escape_string($repeats) . "', event_author=".$current_user->ID . ", event_category=".mysql_escape_string($category).", event_link='".mysql_escape_string($linky)."' WHERE event_id='" . mysql_escape_string($event_id) . "'";
  1089. $wpdb->get_results($sql);
  1090. $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'"
  1091. . " AND event_desc='" . mysql_escape_string($desc) . "' AND event_begin='" . mysql_escape_string($begin) . "' AND event_end='" . mysql_escape_string($end) . "' AND event_recur='" . mysql_escape_string($recur) . "' AND event_repeats='" . mysql_escape_string($repeats) . "' LIMIT 1";
  1092. $result = $wpdb->get_results($sql);
  1093. if ( empty($result) || empty($result[0]->event_id) )
  1094. {
  1095. ?>
  1096. <div class="error"><p><strong><?php _e('Failure','calendar'); ?>:</strong> <?php _e('The database failed to return data to indicate the event has been updated sucessfully. This may indicate a problem with your database or the way in which it is configured.','calendar'); ?></p></div>
  1097. <?php
  1098. }
  1099. else
  1100. {
  1101. ?>
  1102. <div class="updated"><p><?php _e('Event updated successfully','calendar'); ?></p></div>
  1103. <?php
  1104. }
  1105. }
  1106. else
  1107. {
  1108. // The form is going to be rejected due to field validation issues, so we preserve the users entries here
  1109. $users_entries->event_title = $title;
  1110. $users_entries->event_desc = $desc;
  1111. $users_entries->event_begin = $begin;
  1112. $users_entries->event_end = $end;
  1113. $users_entries->event_time = $time;
  1114. $users_entries->event_recur = $recur;
  1115. $users_entries->event_repeats = $repeats;
  1116. $users_entries->event_category = $category;
  1117. $users_entries->event_link = $linky;
  1118. $error_with_saving = 1;
  1119. }
  1120. }
  1121. }
  1122. // Deal with deleting an event from the database
  1123. elseif ( $action == 'delete' )
  1124. {
  1125. if ( empty($event_id) )
  1126. {
  1127. ?>
  1128. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("You can't delete an event if you haven't submitted an event id",'calendar'); ?></p></div>
  1129. <?php
  1130. }
  1131. else
  1132. {
  1133. $sql = "DELETE FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
  1134. $wpdb->get_results($sql);
  1135. $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
  1136. $result = $wpdb->get_results($sql);
  1137. if ( empty($result) || empty($result[0]->event_id) )
  1138. {
  1139. ?>
  1140. <div class="updated"><p><?php _e('Event deleted successfully','calendar'); ?></p></div>
  1141. <?php
  1142. }
  1143. else
  1144. {
  1145. ?>
  1146. <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e('Despite issuing a request to delete, the event still remains in the database. Please investigate.','calendar'); ?></p></div>
  1147. <?php
  1148. }
  1149. }
  1150. }
  1151. // Now follows a little bit of code that pulls in the main
  1152. // components of this page; the edit form and the list of events
  1153. ?>
  1154. <div class="wrap">
  1155. <?php
  1156. if ( $action == 'edit' || ($action == 'edit_save' && $error_with_saving == 1))
  1157. {
  1158. ?>
  1159. <h2><?php _e('Edit Event','calendar'); ?></h2>
  1160. <?php
  1161. if ( empty($event_id) )
  1162. {
  1163. echo "<div class=\"error\"><p>".__("You must provide an event id in order to edit it",'calendar')."</p></div>";
  1164. }
  1165. else
  1166. {
  1167. wp_events_edit_form('edit_save', $event_id);
  1168. }
  1169. }
  1170. else
  1171. {
  1172. ?>
  1173. <h2><?php _e('Add Event','calendar'); ?></h2>
  1174. <?php wp_events_edit_form(); ?>
  1175. <h2><?php _e('Manage Events','calendar'); ?></h2>
  1176. <?php
  1177. wp_events_display_list();
  1178. }
  1179. ?>
  1180. </div>
  1181. <?php
  1182. }
  1183. // Display the admin configuration page
  1184. function edit_calendar_config()
  1185. {
  1186. global $wpdb, $initial_style;
  1187. if (isset($_POST['permissions']) && isset($_POST['style']))
  1188. {
  1189. if ($_POST['permissions'] == 'subscriber') { $new_perms = 'read'; }
  1190. else if ($_POST['permissions'] == 'contributor') { $new_perms = 'edit_posts'; }
  1191. else if ($_POST['permissions'] == 'author') { $new_perms = 'publish_posts'; }
  1192. else if ($_POST['permissions'] == 'editor') { $new_perms = 'moderate_comments'; }
  1193. else if ($_POST['permissions'] == 'admin') { $new_perms = 'manage_options'; }
  1194. else { $new_perms = 'manage_options'; }
  1195. $calendar_style = mysql_escape_string($_POST