PluginProbe
Calendar / 1.2.3
Calendar v1.2.3
trunk 1.0 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 All 28 releases
calendar / calendar.php

calendar.php in Calendar 1.2.3, at calendar.php

2,534 lines 100.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.3
9 */
10
11 /* Copyright 2008 Kieran O'Shea (email : kieran@kieranoshea.com)
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 // Enable internationalisation
29 $plugin_dir = basename(dirname(__FILE__));
30 load_plugin_textdomain( 'calendar','wp-content/plugins/'.$plugin_dir, $plugin_dir);
31
32 // Define the tables used in Calendar
33 define('WP_CALENDAR_TABLE', $table_prefix . 'calendar');
34 define('WP_CALENDAR_CONFIG_TABLE', $table_prefix . 'calendar_config');
35 define('WP_CALENDAR_CATEGORIES_TABLE', $table_prefix . 'calendar_categories');
36
37 // Check ensure calendar is installed and install it if not - required for
38 // the successful operation of most functions called from this point on
39 check_calendar();
40
41 // Create a master category for Calendar and its sub-pages
42 add_action('admin_menu', 'calendar_menu');
43
44 // Enable the ability for the calendar to be loaded from pages
45 add_filter('the_content','calendar_insert');
46
47 // Enable the ability for the lists to be loaded from pages
48 add_filter('the_content','upcoming_insert');
49 add_filter('the_content','todays_insert');
50
51 // Add the function that puts style information in the header
52 add_action('wp_head', 'calendar_wp_head');
53
54 // Add the function that deals with deleted users
55 add_action('delete_user', 'deal_with_deleted_user');
56
57 // Add the widgets if we are using version 2.8
58 add_action('widgets_init', 'widget_init_calendar_today');
59 add_action('widgets_init', 'widget_init_calendar_upcoming');
60
61 // Before we get on with the functions, we need to define the initial style used for Calendar
62
63 // Function to deal with events posted by a user when that user is deleted
64 function deal_with_deleted_user($id)
65 {
66 global $wpdb;
67
68 // Do the query
69 $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=".mysql_escape_string($id));
70 }
71
72 // Function to provide time with WordPress offset, localy replaces time()
73 function ctwo()
74 {
75 return (time()+(3600*(get_option('gmt_offset'))));
76 }
77
78 // Function to add the calendar style into the header
79 function calendar_wp_head()
80 {
81 global $wpdb;
82
83 $style = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
84 if ($style != '')
85 {
86 echo '<style type="text/css">
87 ';
88 echo stripslashes($style).'
89 ';
90 echo '</style>
91 ';
92 }
93 }
94
95 // Function to deal with adding the calendar menus
96 function calendar_menu()
97 {
98 global $wpdb;
99
100 // Set admin as the only one who can use Calendar for security
101 $allowed_group = 'manage_options';
102
103 // Use the database to *potentially* override the above if allowed
104 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
105 if (!empty($configs))
106 {
107 foreach ($configs as $config)
108 {
109 $allowed_group = $config->config_value;
110 }
111 }
112
113 // Add the admin panel pages for Calendar. Use permissions pulled from above
114 if (function_exists('add_menu_page'))
115 {
116 add_menu_page(__('Calendar','calendar'), __('Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
117 }
118 if (function_exists('add_submenu_page'))
119 {
120 add_submenu_page('calendar', __('Manage Calendar','calendar'), __('Manage Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
121 add_action( "admin_head", 'calendar_add_javascript' );
122 // Note only admin can change calendar options
123 add_submenu_page('calendar', __('Manage Categories','calendar'), __('Manage Categories','calendar'), 'manage_options', 'calendar-categories', 'manage_categories');
124 add_submenu_page('calendar', __('Calendar Config','calendar'), __('Calendar Options','calendar'), 'manage_options', 'calendar-config', 'edit_calendar_config');
125 }
126 }
127
128 // Function to add the javascript to the admin header
129 function calendar_add_javascript()
130 {
131 echo '<script type="text/javascript" src="';
132 bloginfo('wpurl');
133 echo '/wp-content/plugins/calendar/javascript.js"></script>
134 <script type="text/javascript">document.write(getCalendarStyles());</script>
135 ';
136 }
137
138 // Function to deal with loading the calendar into pages
139 function calendar_insert($content)
140 {
141 if (preg_match('{CALENDAR}',$content))
142 {
143 $cal_output = calendar();
144 $content = str_replace('{CALENDAR}',$cal_output,$content);
145 }
146 return $content;
147 }
148
149 // Functions to allow the widgets to be inserted into posts and pages
150 function upcoming_insert($content)
151 {
152 if (preg_match('{UPCOMING_EVENTS}',$content))
153 {
154 $cal_output = '<span class="page-upcoming-events">'.upcoming_events().'</span>';
155 $content = str_replace('{UPCOMING_EVENTS}',$cal_output,$content);
156 }
157 return $content;
158 }
159 function todays_insert($content)
160 {
161 if (preg_match('{TODAYS_EVENTS}',$content))
162 {
163 $cal_output = '<span class="page-todays-events">'.todays_events().'</span>';
164 $content = str_replace('{TODAYS_EVENTS}',$cal_output,$content);
165 }
166 return $content;
167 }
168
169 // Function to check what version of Calendar is installed and install if needed
170 function check_calendar()
171 {
172 // Checks to make sure Calendar is installed, if not it adds the default
173 // database tables and populates them with test data. If it is, then the
174 // version is checked through various means and if it is not up to date
175 // then it is upgraded.
176
177 // Lets see if this is first run and create us a table if it is!
178 global $wpdb, $initial_style;
179
180 // All this style info will go into the database on a new install
181 // This looks nice in the Kubrick theme
182 $initial_style = " .calnk a:hover {
183 background-position:0 0;
184 text-decoration:none;
185 color:#000000;
186 border-bottom:1px dotted #000000;
187 }
188 .calnk a:visited {
189 text-decoration:none;
190 color:#000000;
191 border-bottom:1px dotted #000000;
192 }
193 .calnk a {
194 text-decoration:none;
195 color:#000000;
196 border-bottom:1px dotted #000000;
197 }
198 .calnk a span {
199 display:none;
200 }
201 .calnk a:hover span {
202 color:#333333;
203 background:#F6F79B;
204 display:block;
205 position:absolute;
206 margin-top:1px;
207 padding:5px;
208 width:150px;
209 z-index:100;
210 line-height:1.2em;
211 }
212 .calendar-table {
213 border:none;
214 width:100%;
215 }
216 .calendar-heading {
217 height:25px;
218 text-align:center;
219 border:1px solid #D6DED5;
220 background-color:#E4EBE3;
221 }
222 .calendar-next {
223 width:25%;
224 text-align:center;
225 }
226 .calendar-prev {
227 width:25%;
228 text-align:center;
229 }
230 .calendar-month {
231 width:50%;
232 text-align:center;
233 font-weight:bold;
234 }
235 .normal-day-heading {
236 text-align:center;
237 width:25px;
238 height:25px;
239 font-size:0.8em;
240 border:1px solid #DFE6DE;
241 background-color:#EBF2EA;
242 }
243 .weekend-heading {
244 text-align:center;
245 width:25px;
246 height:25px;
247 font-size:0.8em;
248 border:1px solid #DFE6DE;
249 background-color:#EBF2EA;
250 color:#FF0000;
251 }
252 .day-with-date {
253 vertical-align:text-top;
254 text-align:left;
255 width:60px;
256 height:60px;
257 border:1px solid #DFE6DE;
258 }
259 .no-events {
260
261 }
262 .day-without-date {
263 width:60px;
264 height:60px;
265 border:1px solid #E9F0E8;
266 }
267 span.weekend {
268 color:#FF0000;
269 }
270 .current-day {
271 vertical-align:text-top;
272 text-align:left;
273 width:60px;
274 height:60px;
275 border:1px solid #BFBFBF;
276 background-color:#E4EBE3;
277 }
278 span.event {
279 font-size:0.75em;
280 }
281 .kjo-link {
282 font-size:0.75em;
283 text-align:center;
284 }
285 .calendar-date-switcher {
286 height:25px;
287 text-align:center;
288 border:1px solid #D6DED5;
289 background-color:#E4EBE3;
290 }
291 .calendar-date-switcher form {
292 margin:0;
293 padding:0;
294 }
295 .calendar-date-switcher input {
296 border:1px #D6DED5 solid;
297 }
298 .calendar-date-switcher select {
299 border:1px #D6DED5 solid;
300 }
301 .cat-key {
302 width:100%;
303 margin-top:10px;
304 padding:5px;
305 border:1px solid #D6DED5;
306 }
307 .calnk a:hover span span.event-title {
308 padding:0;
309 text-align:center;
310 font-weight:bold;
311 font-size:1.2em;
312 }
313 .calnk a:hover span span.event-title-break {
314 width:96%;
315 text-align:center;
316 height:1px;
317 margin-top:5px;
318 margin-right:2%;
319 padding:0;
320 background-color:#000000;
321 }
322 .calnk a:hover span span.event-content-break {
323 width:96%;
324 text-align:center;
325 height:1px;
326 margin-top:5px;
327 margin-right:2%;
328 padding:0;
329 background-color:#000000;
330 }
331 .page-upcoming-events {
332 font-size:80%;
333 }
334 .page-todays-events {
335 font-size:80%;
336 }";
337
338
339 // Assume this is not a new install until we prove otherwise
340 $new_install = false;
341 $vone_point_one_upgrade = false;
342 $vone_point_two_beta_upgrade = false;
343
344 $wp_calendar_exists = false;
345 $wp_calendar_config_exists = false;
346 $wp_calendar_config_version_number_exists = false;
347
348 // Determine the calendar version
349 $tables = $wpdb->get_results("show tables");
350 foreach ( $tables as $table )
351 {
352 foreach ( $table as $value )
353 {
354 if ( $value == WP_CALENDAR_TABLE )
355 {
356 $wp_calendar_exists = true;
357 }
358 if ( $value == WP_CALENDAR_CONFIG_TABLE )
359 {
360 $wp_calendar_config_exists = true;
361
362 // We now try and find the calendar version number
363 // This will be a lot easier than finding other stuff
364 // in the future.
365 $version_number = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_version'");
366 if ($version_number == "1.2")
367 {
368 $wp_calendar_config_version_number_exists = true;
369 }
370 }
371 }
372 }
373
374 if ($wp_calendar_exists == false && $wp_calendar_config_exists == false)
375 {
376 $new_install = true;
377 }
378 else if ($wp_calendar_exists == true && $wp_calendar_config_exists == false)
379 {
380 $vone_point_one_upgrade = true;
381 }
382 else if ($wp_calendar_exists == true && $wp_calendar_config_exists == true && $wp_calendar_config_version_number_exists == false)
383 {
384 $vone_point_two_beta_upgrade = true;
385 }
386
387 // Now we've determined what the current install is or isn't
388 // we perform operations according to the findings
389 if ( $new_install == true )
390 {
391 $sql = "CREATE TABLE " . WP_CALENDAR_TABLE . " (
392 event_id INT(11) NOT NULL AUTO_INCREMENT ,
393 event_begin DATE NOT NULL ,
394 event_end DATE NOT NULL ,
395 event_title VARCHAR(30) NOT NULL ,
396 event_desc TEXT NOT NULL ,
397 event_time TIME ,
398 event_recur CHAR(1) ,
399 event_repeats INT(3) ,
400 event_author BIGINT(20) UNSIGNED ,
401 event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1 ,
402 event_link TEXT DEFAULT '' ,
403 PRIMARY KEY (event_id)
404 )";
405 $wpdb->get_results($sql);
406 $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
407 config_item VARCHAR(30) NOT NULL ,
408 config_value TEXT NOT NULL ,
409 PRIMARY KEY (config_item)
410 )";
411 $wpdb->get_results($sql);
412 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
413 $wpdb->get_results($sql);
414 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
415 $wpdb->get_results($sql);
416 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
417 $wpdb->get_results($sql);
418 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
419 $wpdb->get_results($sql);
420 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
421 $wpdb->get_results($sql);
422 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
423 $wpdb->get_results($sql);
424 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
425 $wpdb->get_results($sql);
426 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
427 $wpdb->get_results($sql);
428 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
429 $wpdb->get_results($sql);
430 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
431 category_id INT(11) NOT NULL AUTO_INCREMENT,
432 category_name VARCHAR(30) NOT NULL ,
433 category_colour VARCHAR(30) NOT NULL ,
434 PRIMARY KEY (category_id)
435 )";
436 $wpdb->get_results($sql);
437 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
438 $wpdb->get_results($sql);
439 }
440 else if ($vone_point_one_upgrade == true)
441 {
442 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_author BIGINT(20) UNSIGNED";
443 $wpdb->get_results($sql);
444 $sql = "UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0);
445 $wpdb->get_results($sql);
446 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." MODIFY event_desc TEXT NOT NULL";
447 $wpdb->get_results($sql);
448 $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
449 config_item VARCHAR(30) NOT NULL ,
450 config_value TEXT NOT NULL ,
451 PRIMARY KEY (config_item)
452 )";
453 $wpdb->get_results($sql);
454 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
455 $wpdb->get_results($sql);
456 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
457 $wpdb->get_results($sql);
458 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
459 $wpdb->get_results($sql);
460 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
461 $wpdb->get_results($sql);
462 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
463 $wpdb->get_results($sql);
464 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
465 $wpdb->get_results($sql);
466 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
467 $wpdb->get_results($sql);
468 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
469 $wpdb->get_results($sql);
470 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
471 $wpdb->get_results($sql);
472 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
473 $wpdb->get_results($sql);
474 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT DEFAULT ''";
475 $wpdb->get_results($sql);
476 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
477 category_id INT(11) NOT NULL AUTO_INCREMENT,
478 category_name VARCHAR(30) NOT NULL ,
479 category_colour VARCHAR(30) NOT NULL ,
480 PRIMARY KEY (category_id)
481 )";
482 $wpdb->get_results($sql);
483 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
484 $wpdb->get_results($sql);
485 }
486 else if ($vone_point_two_beta_upgrade == true)
487 {
488 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
489 $wpdb->get_results($sql);
490 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
491 $wpdb->get_results($sql);
492 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
493 $wpdb->get_results($sql);
494 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT DEFAULT ''";
495 $wpdb->get_results($sql);
496 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
497 category_id INT(11) NOT NULL AUTO_INCREMENT,
498 category_name VARCHAR(30) NOT NULL ,
499 category_colour VARCHAR(30) NOT NULL ,
500 PRIMARY KEY (category_id)
501 )";
502 $wpdb->get_results($sql);
503 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
504 $wpdb->get_results($sql);
505 $sql = "UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value='".$initial_style."' WHERE config_item='calendar_style'";
506 $wpdb->get_results($sql);
507 }
508 }
509
510 // Used on the manage events admin page to display a list of events
511 function wp_events_display_list()
512 {
513 global $wpdb;
514
515 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " ORDER BY event_begin DESC");
516
517 if ( !empty($events) )
518 {
519 ?>
520 <table class="widefat page fixed" width="100%" cellpadding="3" cellspacing="3">
521 <thead>
522 <tr>
523 <th class="manage-column" scope="col"><?php _e('ID','calendar') ?></th>
524 <th class="manage-column" scope="col"><?php _e('Title','calendar') ?></th>
525 <th class="manage-column" scope="col"><?php _e('Start Date','calendar') ?></th>
526 <th class="manage-column" scope="col"><?php _e('End Date','calendar') ?></th>
527 <th class="manage-column" scope="col"><?php _e('Time','calendar') ?></th>
528 <th class="manage-column" scope="col"><?php _e('Recurs','calendar') ?></th>
529 <th class="manage-column" scope="col"><?php _e('Repeats','calendar') ?></th>
530 <th class="manage-column" scope="col"><?php _e('Author','calendar') ?></th>
531 <th class="manage-column" scope="col"><?php _e('Category','calendar') ?></th>
532 <th class="manage-column" scope="col"><?php _e('Edit','calendar') ?></th>
533 <th class="manage-column" scope="col"><?php _e('Delete','calendar') ?></th>
534 </tr>
535 </thead>
536 <?php
537 $class = '';
538 foreach ( $events as $event )
539 {
540 $class = ($class == 'alternate') ? '' : 'alternate';
541 ?>
542 <tr class="<?php echo $class; ?>">
543 <th scope="row"><?php echo stripslashes($event->event_id); ?></th>
544 <td><?php echo stripslashes($event->event_title); ?></td>
545 <td><?php echo stripslashes($event->event_begin); ?></td>
546 <td><?php echo stripslashes($event->event_end); ?></td>
547 <td><?php if ($event->event_time == '00:00:00') { echo __('N/A','calendar'); } else { echo stripslashes($event->event_time); } ?></td>
548 <td>
549 <?php
550 // Interpret the DB values into something human readable
551 if ($event->event_recur == 'S') { echo __('Never','calendar'); }
552 else if ($event->event_recur == 'W') { echo __('Weekly','calendar'); }
553 else if ($event->event_recur == 'M') { echo __('Monthly (date)','calendar'); }
554 else if ($event->event_recur == 'U') { echo __('Monthly (day)','calendar'); }
555 else if ($event->event_recur == 'Y') { echo __('Yearly','calendar'); }
556 ?>
557 </td>
558 <td>
559 <?php
560 // Interpret the DB values into something human readable
561 if ($event->event_recur == 'S') { echo __('N/A','calendar'); }
562 else if ($event->event_repeats == 0) { echo __('Forever','calendar'); }
563 else if ($event->event_repeats > 0) { echo stripslashes($event->event_repeats).' '.__('Times','calendar'); }
564 ?>
565 </td>
566 <td><?php $e = get_userdata($event->event_author); echo $e->display_name; ?></td>
567 <?php
568 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($event->event_category);
569 $this_cat = $wpdb->get_row($sql);
570 ?>
571 <td style="background-color:<?php echo stripslashes($this_cat->category_colour);?>;"><?php echo stripslashes($this_cat->category_name); ?></td>
572 <?php unset($this_cat); ?>
573 <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar&amp;action=edit&amp;event_id=<?php echo stripslashes($event->event_id);?>" class='edit'><?php echo __('Edit','calendar'); ?></a></td>
574 <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar&amp;action=delete&amp;event_id=<?php echo stripslashes($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>
575 </tr>
576 <?php
577 }
578 ?>
579 </table>
580 <?php
581 }
582 else
583 {
584 ?>
585 <p><?php _e("There are no events in the database!",'calendar') ?></p>
586 <?php
587 }
588 }
589
590
591 // The event edit form for the manage events admin page
592 function wp_events_edit_form($mode='add', $event_id=false)
593 {
594 global $wpdb,$users_entries;
595 $data = false;
596
597 if ( $event_id !== false )
598 {
599 if ( intval($event_id) != $event_id )
600 {
601 echo "<div class=\"error\"><p>".__('Bad Monkey! No banana!','calendar')."</p></div>";
602 return;
603 }
604 else
605 {
606 $data = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "' LIMIT 1");
607 if ( empty($data) )
608 {
609 echo "<div class=\"error\"><p>".__("An event with that ID couldn't be found",'calendar')."</p></div>";
610 return;
611 }
612 $data = $data[0];
613 }
614 // Recover users entries if they exist; in other words if editing an event went wrong
615 if (!empty($users_entries))
616 {
617 $data = $users_entries;
618 }
619 }
620 // Deal with possibility that form was submitted but not saved due to error - recover user's entries here
621 else
622 {
623 $data = $users_entries;
624 }
625
626 ?>
627 <div id="pop_up_cal" style="position:absolute;margin-left:150px;visibility:hidden;background-color:white;layer-background-color:white;z-index:1;"></div>
628 <form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar">
629 <input type="hidden" name="action" value="<?php echo $mode; ?>">
630 <input type="hidden" name="event_id" value="<?php echo stripslashes($event_id); ?>">
631
632 <div id="linkadvanceddiv" class="postbox">
633 <div style="float: left; width: 98%; clear: both;" class="inside">
634 <table cellpadding="5" cellspacing="5">
635 <tr>
636 <td><legend><?php _e('Event Title','calendar'); ?></legend></td>
637 <td><input type="text" name="event_title" class="input" size="40" maxlength="30"
638 value="<?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_title)); ?>" /></td>
639 </tr>
640 <tr>
641 <td style="vertical-align:top;"><legend><?php _e('Event Description','calendar'); ?></legend></td>
642 <td><textarea name="event_desc" class="input" rows="5" cols="50"><?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_desc)); ?></textarea></td>
643 </tr>
644 <tr>
645 <td><legend><?php _e('Event Category','calendar'); ?></legend></td>
646 <td> <select name="event_category">
647 <?php
648 // Grab all the categories and list them
649 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE;
650 $cats = $wpdb->get_results($sql);
651 foreach($cats as $cat)
652 {
653 echo '<option value="'.stripslashes($cat->category_id).'"';
654 if (!empty($data))
655 {
656 if ($data->event_category == $cat->category_id)
657 {
658 echo 'selected="selected"';
659 }
660 }
661 echo '>'.stripslashes($cat->category_name).'</option>
662 ';
663 }
664 ?>
665 </select>
666 </td>
667 </tr>
668 <tr>
669 <td><legend><?php _e('Event Link (Optional)','calendar'); ?></legend></td>
670 <td><input type="text" name="event_link" class="input" size="40" value="<?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_link)); ?>" /></td>
671 </tr>
672 <tr>
673 <td><legend><?php _e('Start Date','calendar'); ?></legend></td>
674 <td> <script type="text/javascript">
675 var cal_begin = new CalendarPopup('pop_up_cal');
676 cal_begin.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
677 function unifydates() {
678 document.forms['quoteform'].event_end.value = document.forms['quoteform'].event_begin.value;
679 }
680 </script>
681 <input type="text" name="event_begin" class="input" size="12"
682 value="<?php
683 if ( !empty($data) )
684 {
685 echo htmlspecialchars(stripslashes($data->event_begin));
686 }
687 else
688 {
689 echo date("Y-m-d",ctwo());
690 }
691 ?>" /> <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>
692 </td>
693 </tr>
694 <tr>
695 <td><legend><?php _e('End Date','calendar'); ?></legend></td>
696 <td> <script type="text/javascript">
697 function check_and_print() {
698 unifydates();
699 var cal_end = new CalendarPopup('pop_up_cal');
700 cal_end.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
701 var newDate = new Date();
702 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]);
703 newDate.setDate(newDate.getDate()-1);
704 cal_end.addDisabledDates(null, formatDate(newDate, "yyyy-MM-dd"));
705 cal_end.select(document.forms['quoteform'].event_end,'event_end_anchor','yyyy-MM-dd');
706 }
707 </script>
708 <input type="text" name="event_end" class="input" size="12"
709 value="<?php
710 if ( !empty($data) )
711 {
712 echo htmlspecialchars(stripslashes($data->event_end));
713 }
714 else
715 {
716 echo date("Y-m-d",ctwo());
717 }
718 ?>" /> <a href="#" onClick="check_and_print(); return false;" name="event_end_anchor" id="event_end_anchor"><?php _e('Select Date','calendar'); ?></a>
719 </td>
720 </tr>
721 <tr>
722 <td><legend><?php _e('Time (hh:mm)','calendar'); ?></legend></td>
723 <td> <input type="text" name="event_time" class="input" size=12
724 value="<?php
725 if ( !empty($data) )
726 {
727 if ($data->event_time == "00:00:00")
728 {
729 echo '';
730 }
731 else
732 {
733 echo date("H:i",strtotime(htmlspecialchars(stripslashes($data->event_time))));
734 }
735 }
736 else
737 {
738 echo date("H:i",ctwo());
739 }
740 ?>" /> <?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'); ?>
741 </td>
742 </tr>
743 <tr>
744 <td><legend><?php _e('Recurring Events','calendar'); ?></legend></td>
745 <td> <?php
746 if ($data->event_repeats != NULL)
747 {
748 $repeats = $data->event_repeats;
749 }
750 else
751 {
752 $repeats = 0;
753 }
754
755 if ($data->event_recur == "S")
756 {
757 $selected_s = 'selected="selected"';
758 }
759 else if ($data->event_recur == "W")
760 {
761 $selected_w = 'selected="selected"';
762 }
763 else if ($data->event_recur == "M")
764 {
765 $selected_m = 'selected="selected"';
766 }
767 else if ($data->event_recur == "Y")
768 {
769 $selected_y = 'selected="selected"';
770 }
771 else if ($data->event_recur == "U")
772 {
773 $selected_u = 'selected="selected"';
774 }
775 ?>
776 <?php _e('Repeats for','calendar'); ?>
777 <input type="text" name="event_repeats" class="input" size="1" value="<?php echo $repeats; ?>" />
778 <select name="event_recur" class="input">
779 <option class="input" <?php echo $selected_s; ?> value="S"><?php _e('None') ?></option>
780 <option class="input" <?php echo $selected_w; ?> value="W"><?php _e('Weeks') ?></option>
781 <option class="input" <?php echo $selected_m; ?> value="M"><?php _e('Months (date)') ?></option>
782 <option class="input" <?php echo $selected_u; ?> value="U"><?php _e('Months (day)') ?></option>
783 <option class="input" <?php echo $selected_y; ?> value="Y"><?php _e('Years') ?></option>
784 </select><br />
785 <?php _e('Entering 0 means forever. Where the recurrance interval is left at none, the event will not reoccur.','calendar'); ?>
786 </td>
787 </tr>
788 </table>
789 </div>
790 <div style="clear:both; height:1px;">&nbsp;</div>
791 </div>
792 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
793 </form>
794 <?php
795 }
796
797 // The actual function called to render the manage events page and
798 // to deal with posts
799 function edit_calendar()
800 {
801 global $current_user, $wpdb, $users_entries;
802 ?>
803 <style type="text/css">
804 <!--
805 .error {
806 background: lightcoral;
807 border: 1px solid #e64f69;
808 margin: 1em 5% 10px;
809 padding: 0 1em 0 1em;
810 }
811
812 .center {
813 text-align: center;
814 }
815 .right { text-align: right;
816 }
817 .left {
818 text-align: left;
819 }
820 .top {
821 vertical-align: top;
822 }
823 .bold {
824 font-weight: bold;
825 }
826 .private {
827 color: #e64f69;
828 }
829 //-->
830 </style>
831
832 <?php
833
834 // First some quick cleaning up
835 $edit = $create = $save = $delete = false;
836
837 // Make sure we are collecting the variables we need to select years and months
838 $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
839 $event_id = !empty($_REQUEST['event_id']) ? $_REQUEST['event_id'] : '';
840
841 // Deal with adding an event to the database
842 if ( $action == 'add' )
843 {
844 $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
845 $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
846 $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
847 $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
848 $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
849 $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
850 $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
851 $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
852 $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
853
854 // Perform some validation on the submitted dates - this checks for valid years and months
855 $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
856 $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
857 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)))
858 {
859 // We know we have a valid year and month and valid integers for days so now we do a final check on the date
860 $begin_split = split('-',$begin);
861 $begin_y = $begin_split[0];
862 $begin_m = $begin_split[1];
863 $begin_d = $begin_split[2];
864 $end_split = split('-',$end);
865 $end_y = $end_split[0];
866 $end_m = $end_split[1];
867 $end_d = $end_split[2];
868 if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
869 {
870 // 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
871 if (strtotime($end) >= strtotime($begin))
872 {
873 $start_date_ok = 1;
874 $end_date_ok = 1;
875 }
876 else
877 {
878 ?>
879 <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>
880 <?php
881 }
882 }
883 else
884 {
885 ?>
886 <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>
887 <?php
888 }
889 }
890 else
891 {
892 ?>
893 <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>
894 <?php
895 }
896 // We check for a valid time, or an empty one
897 $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
898 $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
899 if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
900 {
901 $time_ok = 1;
902 if ($time == '')
903 {
904 $time_to_use = '00:00:00';
905 }
906 else if ($time == '00:00')
907 {
908 $time_to_use = '00:00:01';
909 }
910 else
911 {
912 $time_to_use = $time;
913 }
914 }
915 else
916 {
917 ?>
918 <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>
919 <?php
920 }
921 // We check to make sure the URL is alright
922 if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
923 {
924 $url_ok = 1;
925 }
926 else
927 {
928 ?>
929 <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>
930 <?php
931 }
932 // The title must be at least one character in length and no more than 30
933 if (preg_match('/^.{1,30}$/',$title))
934 {
935 $title_ok =1;
936 }
937 else
938 {
939 ?>
940 <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>
941 <?php
942 }
943 // We run some checks on recurrance
944 $repeats = (int)$repeats;
945 if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y' || $recur == 'U')))
946 {
947 $recurring_ok = 1;
948 }
949 else
950 {
951 ?>
952 <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>
953 <?php
954 }
955 if ($start_date_ok == 1 && $end_date_ok == 1 && $time_ok == 1 && $url_ok == 1 && $title_ok == 1 && $recurring_ok == 1)
956 {
957 $sql = "INSERT INTO " . WP_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title)
958 . "', event_desc='" . mysql_escape_string($desc) . "', event_begin='" . mysql_escape_string($begin)
959 . "', 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)."'";
960
961 $wpdb->get_results($sql);
962
963 $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'"
964 . " 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";
965 $result = $wpdb->get_results($sql);
966
967 if ( empty($result) || empty($result[0]->event_id) )
968 {
969 ?>
970 <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>
971 <?php
972 }
973 else
974 {
975 ?>
976 <div class="updated"><p><?php _e('Event added. It will now show in your calendar.','calendar'); ?></p></div>
977 <?php
978 }
979 }
980 else
981 {
982 // The form is going to be rejected due to field validation issues, so we preserve the users entries here
983 $users_entries->event_title = $title;
984 $users_entries->event_desc = $desc;
985 $users_entries->event_begin = $begin;
986 $users_entries->event_end = $end;
987 $users_entries->event_time = $time;
988 $users_entries->event_recur = $recur;
989 $users_entries->event_repeats = $repeats;
990 $users_entries->event_category = $category;
991 $users_entries->event_link = $linky;
992 }
993 }
994 // Permit saving of events that have been edited
995 elseif ( $action == 'edit_save' )
996 {
997 $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
998 $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
999 $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
1000 $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
1001 $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
1002 $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
1003 $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
1004 $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
1005 $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
1006
1007 if ( empty($event_id) )
1008 {
1009 ?>
1010 <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>
1011 <?php
1012 }
1013 else
1014 {
1015 // Perform some validation on the submitted dates - this checks for valid years and months
1016 $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
1017 $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
1018 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)))
1019 {
1020 // We know we have a valid year and month and valid integers for days so now we do a final check on the date
1021 $begin_split = split('-',$begin);
1022 $begin_y = $begin_split[0];
1023 $begin_m = $begin_split[1];
1024 $begin_d = $begin_split[2];
1025 $end_split = split('-',$end);
1026 $end_y = $end_split[0];
1027 $end_m = $end_split[1];
1028 $end_d = $end_split[2];
1029 if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
1030 {
1031 // 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
1032 if (strtotime($end) >= strtotime($begin))
1033 {
1034 $start_date_ok = 1;
1035 $end_date_ok = 1;
1036 }
1037 else
1038 {
1039 ?>
1040 <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>
1041 <?php
1042 }
1043 }
1044 else
1045 {
1046 ?>
1047 <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>
1048 <?php
1049 }
1050 }
1051 else
1052 {
1053 ?>
1054 <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>
1055 <?php
1056 }
1057 // We check for a valid time, or an empty one
1058 $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
1059 $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
1060 if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
1061 {
1062 $time_ok = 1;
1063 if ($time == '')
1064 {
1065 $time_to_use = '00:00:00';
1066 }
1067 else if ($time == '00:00')
1068 {
1069 $time_to_use = '00:00:01';
1070 }
1071 else
1072 {
1073 $time_to_use = $time;
1074 }
1075 }
1076 else
1077 {
1078 ?>
1079 <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>
1080 <?php
1081 }
1082 // We check to make sure the URL is alright
1083 if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
1084 {
1085 $url_ok = 1;
1086 }
1087 else
1088 {
1089 ?>
1090 <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>
1091 <?php
1092 }
1093 // The title must be at least one character in length and no more than 30
1094 if (preg_match('/^.{1,30}$/',$title))
1095 {
1096 $title_ok =1;
1097 }
1098 else
1099 {
1100 ?>
1101 <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>
1102 <?php
1103 }
1104 // We run some checks on recurrance
1105 $repeats = (int)$repeats;
1106 if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y' || $recur == 'U')))
1107 {
1108 $recurring_ok = 1;
1109 }
1110 else
1111 {
1112 ?>
1113 <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>
1114 <?php
1115 }
1116 if ($start_date_ok == 1 && $end_date_ok == 1 && $time_ok == 1 && $url_ok == 1 && $title_ok == 1 && $recurring_ok == 1)
1117 {
1118 $sql = "UPDATE " . WP_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title)
1119 . "', event_desc='" . mysql_escape_string($desc) . "', event_begin='" . mysql_escape_string($begin)
1120 . "', 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) . "'";
1121
1122 $wpdb->get_results($sql);
1123
1124 $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'"
1125 . " 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";
1126 $result = $wpdb->get_results($sql);
1127
1128 if ( empty($result) || empty($result[0]->event_id) )
1129 {
1130 ?>
1131 <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>
1132 <?php
1133 }
1134 else
1135 {
1136 ?>
1137 <div class="updated"><p><?php _e('Event updated successfully','calendar'); ?></p></div>
1138 <?php
1139 }
1140 }
1141 else
1142 {
1143 // The form is going to be rejected due to field validation issues, so we preserve the users entries here
1144 $users_entries->event_title = $title;
1145 $users_entries->event_desc = $desc;
1146 $users_entries->event_begin = $begin;
1147 $users_entries->event_end = $end;
1148 $users_entries->event_time = $time;
1149 $users_entries->event_recur = $recur;
1150 $users_entries->event_repeats = $repeats;
1151 $users_entries->event_category = $category;
1152 $users_entries->event_link = $linky;
1153 $error_with_saving = 1;
1154 }
1155 }
1156 }
1157 // Deal with deleting an event from the database
1158 elseif ( $action == 'delete' )
1159 {
1160 if ( empty($event_id) )
1161 {
1162 ?>
1163 <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>
1164 <?php
1165 }
1166 else
1167 {
1168 $sql = "DELETE FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
1169 $wpdb->get_results($sql);
1170
1171 $sql = "SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
1172 $result = $wpdb->get_results($sql);
1173
1174 if ( empty($result) || empty($result[0]->event_id) )
1175 {
1176 ?>
1177 <div class="updated"><p><?php _e('Event deleted successfully','calendar'); ?></p></div>
1178 <?php
1179 }
1180 else
1181 {
1182 ?>
1183 <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>
1184 <?php
1185
1186 }
1187 }
1188 }
1189
1190 // Now follows a little bit of code that pulls in the main
1191 // components of this page; the edit form and the list of events
1192 ?>
1193
1194 <div class="wrap">
1195 <?php
1196 if ( $action == 'edit' || ($action == 'edit_save' && $error_with_saving == 1))
1197 {
1198 ?>
1199 <h2><?php _e('Edit Event','calendar'); ?></h2>
1200 <?php
1201 if ( empty($event_id) )
1202 {
1203 echo "<div class=\"error\"><p>".__("You must provide an event id in order to edit it",'calendar')."</p></div>";
1204 }
1205 else
1206 {
1207 wp_events_edit_form('edit_save', $event_id);
1208 }
1209 }
1210 else
1211 {
1212 ?>
1213 <h2><?php _e('Add Event','calendar'); ?></h2>
1214 <?php wp_events_edit_form(); ?>
1215
1216 <h2><?php _e('Manage Events','calendar'); ?></h2>
1217 <?php
1218 wp_events_display_list();
1219 }
1220 ?>
1221 </div>
1222
1223 <?php
1224
1225 }
1226
1227 // Display the admin configuration page
1228 function edit_calendar_config()
1229 {
1230 global $wpdb, $initial_style;
1231
1232 if (isset($_POST['permissions']) && isset($_POST['style']))
1233 {
1234 if ($_POST['permissions'] == 'subscriber') { $new_perms = 'read'; }
1235 else if ($_POST['permissions'] == 'contributor') { $new_perms = 'edit_posts'; }
1236 else if ($_POST['permissions'] == 'author') { $new_perms = 'publish_posts'; }
1237 else if ($_POST['permissions'] == 'editor') { $new_perms = 'moderate_comments'; }
1238 else if ($_POST['permissions'] == 'admin') { $new_perms = 'manage_options'; }
1239 else { $new_perms = 'manage_options'; }
1240
1241 $calendar_style = mysql_escape_string($_POST['style']);
1242 $display_upcoming_days = mysql_escape_string($_POST['display_upcoming_days']);
1243
1244 if (mysql_escape_string($_POST['display_author']) == 'on')
1245 {
1246 $disp_author = 'true';
1247 }
1248 else
1249 {
1250 $disp_author = 'false';
1251 }
1252
1253 if (mysql_escape_string($_POST['display_jump']) == 'on')
1254 {
1255 $disp_jump = 'true';
1256 }
1257 else
1258 {
1259 $disp_jump = 'false';
1260 }
1261
1262 if (mysql_escape_string($_POST['display_todays']) == 'on')
1263 {
1264 $disp_todays = 'true';
1265 }
1266 else
1267 {
1268 $disp_todays = 'false';
1269 }
1270
1271 if (mysql_escape_string($_POST['display_upcoming']) == 'on')
1272 {
1273 $disp_upcoming = 'true';
1274 }
1275 else
1276 {
1277 $disp_upcoming = 'false';
1278 }
1279
1280 if (mysql_escape_string($_POST['enable_categories']) == 'on')
1281 {
1282 $enable_categories = 'true';
1283 }
1284 else
1285 {
1286 $enable_categories = 'false';
1287 }
1288
1289 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$new_perms."' WHERE config_item='can_manage_events'");
1290 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$calendar_style."' WHERE config_item='calendar_style'");
1291 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$disp_author."' WHERE config_item='display_author'");
1292 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$disp_jump."' WHERE config_item='display_jump'");
1293 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$disp_todays."' WHERE config_item='display_todays'");
1294 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$disp_upcoming."' WHERE config_item='display_upcoming'");
1295 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$display_upcoming_days."' WHERE config_item='display_upcoming_days'");
1296 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$enable_categories."' WHERE config_item='enable_categories'");
1297
1298 // Check to see if we are replacing the original style
1299 if (mysql_escape_string($_POST['reset_styles']) == 'on')
1300 {
1301 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$initial_style."' WHERE config_item='calendar_style'");
1302 }
1303
1304 echo "<div class=\"updated\"><p><strong>".__('Settings saved','calendar').".</strong></p></div>";
1305 }
1306
1307 // Pull the values out of the database that we need for the form
1308 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
1309 if (!empty($configs))
1310 {
1311 foreach ($configs as $config)
1312 {
1313 $allowed_group = stripslashes($config->config_value);
1314 }
1315 }
1316
1317 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
1318 if (!empty($configs))
1319 {
1320 foreach ($configs as $config)
1321 {
1322 $calendar_style = stripslashes($config->config_value);
1323 }
1324 }
1325 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_author'");
1326 if (!empty($configs))
1327 {
1328 foreach ($configs as $config)
1329 {
1330 if ($config->config_value == 'true')
1331 {
1332 $yes_disp_author = 'selected="selected"';
1333 }
1334 else
1335 {
1336 $no_disp_author = 'selected="selected"';
1337 }
1338 }
1339 }
1340 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_jump'");
1341 if (!empty($configs))
1342 {
1343 foreach ($configs as $config)
1344 {
1345 if ($config->config_value == 'true')
1346 {
1347 $yes_disp_jump = 'selected="selected"';
1348 }
1349 else
1350 {
1351 $no_disp_jump = 'selected="selected"';
1352 }
1353 }
1354 }
1355 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_todays'");
1356 if (!empty($configs))
1357 {
1358 foreach ($configs as $config)
1359 {
1360 if ($config->config_value == 'true')
1361 {
1362 $yes_disp_todays = 'selected="selected"';
1363 }
1364 else
1365 {
1366 $no_disp_todays = 'selected="selected"';
1367 }
1368 }
1369 }
1370 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_upcoming'");
1371 if (!empty($configs))
1372 {
1373 foreach ($configs as $config)
1374 {
1375 if ($config->config_value == 'true')
1376 {
1377 $yes_disp_upcoming = 'selected="selected"';
1378 }
1379 else
1380 {
1381 $no_disp_upcoming = 'selected="selected"';
1382 }
1383 }
1384 }
1385 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_upcoming_days'");
1386 if (!empty($configs))
1387 {
1388 foreach ($configs as $config)
1389 {
1390 $upcoming_days = stripslashes($config->config_value);
1391 }
1392 }
1393 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='enable_categories'");
1394 if (!empty($configs))
1395 {
1396 foreach ($configs as $config)
1397 {
1398 if ($config->config_value == 'true')
1399 {
1400 $yes_enable_categories = 'selected="selected"';
1401 }
1402 else
1403 {
1404 $no_enable_categories = 'selected="selected"';
1405 }
1406 }
1407 }
1408 if ($allowed_group == 'read') { $subscriber_selected='selected="selected"';}
1409 else if ($allowed_group == 'edit_posts') { $contributor_selected='selected="selected"';}
1410 else if ($allowed_group == 'publish_posts') { $author_selected='selected="selected"';}
1411 else if ($allowed_group == 'moderate_comments') { $editor_selected='selected="selected"';}
1412 else if ($allowed_group == 'manage_options') { $admin_selected='selected="selected"';}
1413
1414 // Now we render the form
1415 ?>
1416 <style type="text/css">
1417 <!--
1418 .error {
1419 background: lightcoral;
1420 border: 1px solid #e64f69;
1421 margin: 1em 5% 10px;
1422 padding: 0 1em 0 1em;
1423 }
1424
1425 .center {
1426 text-align: center;
1427 }
1428 .right {
1429 text-align: right;
1430 }
1431 .left {
1432 text-align: left;
1433 }
1434 .top {
1435 vertical-align: top;
1436 }
1437 .bold {
1438 font-weight: bold;
1439 }
1440 .private {
1441 color: #e64f69;
1442 }
1443 //-->
1444 </style>
1445
1446 <div class="wrap">
1447 <h2><?php _e('Calendar Options','calendar'); ?></h2>
1448 <form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-config">
1449 <div id="linkadvanceddiv" class="postbox">
1450 <div style="float: left; width: 98%; clear: both;" class="inside">
1451 <table cellpadding="5" cellspacing="5">
1452 <tr>
1453 <td><legend><?php _e('Choose the lowest user group that may manage events','calendar'); ?></legend></td>
1454 <td> <select name="permissions">
1455 <option value="subscriber"<?php echo $subscriber_selected ?>><?php _e('Subscriber','calendar')?></option>
1456 <option value="contributor" <?php echo $contributor_selected ?>><?php _e('Contributor','calendar')?></option>
1457 <option value="author" <?php echo $author_selected ?>><?php _e('Author','calendar')?></option>
1458 <option value="editor" <?php echo $editor_selected ?>><?php _e('Editor','calendar')?></option>
1459 <option value="admin" <?php echo $admin_selected ?>><?php _e('Administrator','calendar')?></option>
1460 </select>
1461 </td>
1462 </tr>
1463 <tr>
1464 <td><legend><?php _e('Do you want to display the author name on events?','calendar'); ?></legend></td>
1465 <td> <select name="display_author">
1466 <option value="on" <?php echo $yes_disp_author ?>><?php _e('Yes','calendar') ?></option>
1467 <option value="off" <?php echo $no_disp_author ?>><?php _e('No','calendar') ?></option>
1468 </select>
1469 </td>
1470 </tr>
1471 <tr>
1472 <td><legend><?php _e('Display a jumpbox for changing month and year quickly?','calendar'); ?></legend></td>
1473 <td> <select name="display_jump">
1474 <option value="on" <?php echo $yes_disp_jump ?>><?php _e('Yes','calendar') ?></option>
1475 <option value="off" <?php echo $no_disp_jump ?>><?php _e('No','calendar') ?></option>
1476 </select>
1477 </td>
1478 </tr>
1479 <tr>
1480 <td><legend><?php _e('Display todays events?','calendar'); ?></legend></td>
1481 <td> <select name="display_todays">
1482 <option value="on" <?php echo $yes_disp_todays ?>><?php _e('Yes','calendar') ?></option>
1483 <option value="off" <?php echo $no_disp_todays ?>><?php _e('No','calendar') ?></option>
1484 </select>
1485 </td>
1486 </tr>
1487 <tr>
1488 <td><legend><?php _e('Display upcoming events?','calendar'); ?></legend></td>
1489 <td> <select name="display_upcoming">
1490 <option value="on" <?php echo $yes_disp_upcoming ?>><?php _e('Yes','calendar') ?></option>
1491 <option value="off" <?php echo $no_disp_upcoming ?>><?php _e('No','calendar') ?></option>
1492 </select>
1493 <?php _e('for','calendar'); ?> <input type="text" name="display_upcoming_days" value="<?php echo $upcoming_days ?>" size="1" maxlength="2" /> <?php _e('days into the future','calendar'); ?>
1494 </td>
1495 </tr>
1496 <tr>
1497 <td><legend><?php _e('Enable event categories?','calendar'); ?></legend></td>
1498 <td> <select name="enable_categories">
1499 <option value="on" <?php echo $yes_enable_categories ?>><?php _e('Yes','calendar') ?></option>
1500 <option value="off" <?php echo $no_enable_categories ?>><?php _e('No','calendar') ?></option>
1501 </select>
1502 </td>
1503 </tr>
1504 <tr>
1505 <td style="vertical-align:top;"><legend><?php _e('Configure the stylesheet for Calendar','calendar'); ?></legend></td>
1506 <td><textarea name="style" rows="10" cols="60" tabindex="2"><?php echo $calendar_style; ?></textarea><br />
1507 <input type="checkbox" name="reset_styles" /> <?php _e('Tick this box if you wish to reset the Calendar style to default','calendar'); ?></td>
1508 </tr>
1509 </table>
1510 </div>
1511 <div style="clear:both; height:1px;">&nbsp;</div>
1512 </div>
1513 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1514 </form>
1515 </div>
1516 <?php
1517
1518
1519 }
1520
1521 // Function to handle the management of categories
1522 function manage_categories()
1523 {
1524 global $wpdb;
1525
1526 ?>
1527 <style type="text/css">
1528 <!--
1529 .error {
1530 background: lightcoral;
1531 border: 1px solid #e64f69;
1532 margin: 1em 5% 10px;
1533 padding: 0 1em 0 1em;
1534 }
1535
1536 .center {
1537 text-align: center;
1538 }
1539 .right {
1540 text-align: right;
1541 }
1542 .left {
1543 text-align: left;
1544 }
1545 .top {
1546 vertical-align: top;
1547 }
1548 .bold {
1549 font-weight: bold;
1550 }
1551 .private {
1552 color: #e64f69;
1553 }
1554 //-->
1555
1556 </style>
1557 <?php
1558 // We do some checking to see what we're doing
1559 if (isset($_POST['mode']) && $_POST['mode'] == 'add')
1560 {
1561 // Proceed with the save
1562 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_name='".mysql_escape_string($_POST['category_name'])."', category_colour='".mysql_escape_string($_POST['category_colour'])."'";
1563 $wpdb->get_results($sql);
1564 echo "<div class=\"updated\"><p><strong>".__('Category added successfully','calendar')."</strong></p></div>";
1565 }
1566 else if (isset($_GET['mode']) && isset($_GET['category_id']) && $_GET['mode'] == 'delete')
1567 {
1568 $sql = "DELETE FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($_GET['category_id']);
1569 $wpdb->get_results($sql);
1570 $sql = "UPDATE " . WP_CALENDAR_TABLE . " SET event_category=1 WHERE event_category=".mysql_escape_string($_GET['category_id']);
1571 $wpdb->get_results($sql);
1572 echo "<div class=\"updated\"><p><strong>".__('Category deleted successfully','calendar')."</strong></p></div>";
1573 }
1574 else if (isset($_GET['mode']) && isset($_GET['category_id']) && $_GET['mode'] == 'edit' && !isset($_POST['mode']))
1575 {
1576 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($_GET['category_id']);
1577 $cur_cat = $wpdb->get_row($sql);
1578 ?>
1579 <div class="wrap">
1580 <h2><?php _e('Edit Category','calendar'); ?></h2>
1581 <form name="catform" id="catform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-categories">
1582 <input type="hidden" name="mode" value="edit" />
1583 <input type="hidden" name="category_id" value="<?php echo stripslashes($cur_cat->category_id) ?>" />
1584 <div id="linkadvanceddiv" class="postbox">
1585 <div style="float: left; width: 98%; clear: both;" class="inside">
1586 <table cellpadding="5" cellspacing="5">
1587 <tr>
1588 <td><legend><?php _e('Category Name','calendar'); ?>:</legend></td>
1589 <td><input type="text" name="category_name" class="input" size="30" maxlength="30" value="<?php echo stripslashes($cur_cat->category_name) ?>" /></td>
1590 </tr>
1591 <tr>
1592 <td><legend><?php _e('Category Colour (Hex format)','calendar'); ?>:</legend></td>
1593 <td><input type="text" name="category_colour" class="input" size="10" maxlength="7" value="<?php echo stripslashes($cur_cat->category_colour) ?>" /></td>
1594 </tr>
1595 </table>
1596 </div>
1597 <div style="clear:both; height:1px;">&nbsp;</div>
1598 </div>
1599 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1600 </form>
1601 </div>
1602 <?php
1603 }
1604 else if (isset($_POST['mode']) && isset($_POST['category_id']) && isset($_POST['category_name']) && isset($_POST['category_colour']) && $_POST['mode'] == 'edit')
1605 {
1606 // Proceed with the save
1607 $sql = "UPDATE " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_name='".mysql_escape_string($_POST['category_name'])."', category_colour='".mysql_escape_string($_POST['category_colour'])."' WHERE category_id=".mysql_escape_string($_POST['category_id']);
1608 $wpdb->get_results($sql);
1609 echo "<div class=\"updated\"><p><strong>".__('Category edited successfully','calendar')."</strong></p></div>";
1610 }
1611
1612 if ($_GET['mode'] != 'edit' || $_POST['mode'] == 'edit')
1613 {
1614 ?>
1615
1616 <div class="wrap">
1617 <h2><?php _e('Add Category','calendar'); ?></h2>
1618 <form name="catform" id="catform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-categories">
1619 <input type="hidden" name="mode" value="add" />
1620 <input type="hidden" name="category_id" value="">
1621 <div id="linkadvanceddiv" class="postbox">
1622 <div style="float: left; width: 98%; clear: both;" class="inside">
1623 <table cellspacing="5" cellpadding="5">
1624 <tr>
1625 <td><legend><?php _e('Category Name','calendar'); ?>:</legend></td>
1626 <td><input type="text" name="category_name" class="input" size="30" maxlength="30" value="" /></td>
1627 </tr>
1628 <tr>
1629 <td><legend><?php _e('Category Colour (Hex format)','calendar'); ?>:</legend></td>
1630 <td><input type="text" name="category_colour" class="input" size="10" maxlength="7" value="" /></td>
1631 </tr>
1632 </table>
1633 </div>
1634 <div style="clear:both; height:1px;">&nbsp;</div>
1635 </div>
1636 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1637 </form>
1638 <h2><?php _e('Manage Categories','calendar'); ?></h2>
1639 <?php
1640
1641 // We pull the categories from the database
1642 $categories = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " ORDER BY category_id ASC");
1643
1644 if ( !empty($categories) )
1645 {
1646 ?>
1647 <table class="widefat page fixed" width="50%" cellpadding="3" cellspacing="3">
1648 <thead>
1649 <tr>
1650 <th class="manage-column" scope="col"><?php _e('ID','calendar') ?></th>
1651 <th class="manage-column" scope="col"><?php _e('Category Name','calendar') ?></th>
1652 <th class="manage-column" scope="col"><?php _e('Category Colour','calendar') ?></th>
1653 <th class="manage-column" scope="col"><?php _e('Edit','calendar') ?></th>
1654 <th class="manage-column" scope="col"><?php _e('Delete','calendar') ?></th>
1655 </tr>
1656 </thead>
1657 <?php
1658 $class = '';
1659 foreach ( $categories as $category )
1660 {
1661 $class = ($class == 'alternate') ? '' : 'alternate';
1662 ?>
1663 <tr class="<?php echo $class; ?>">
1664 <th scope="row"><?php echo stripslashes($category->category_id); ?></th>
1665 <td><?php echo stripslashes($category->category_name); ?></td>
1666 <td style="background-color:<?php echo stripslashes($category->category_colour); ?>;">&nbsp;</td>
1667 <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar-categories&amp;mode=edit&amp;category_id=<?php echo stripslashes($category->category_id);?>" class='edit'><?php echo __('Edit','calendar'); ?></a></td>
1668 <?php
1669 if ($category->category_id == 1)
1670 {
1671 echo '<td>'.__('N/A','calendar').'</td>';
1672 }
1673 else
1674 {
1675 ?>
1676 <td><a href="<?php echo bloginfo('wpurl') ?>/wp-admin/admin.php?page=calendar-categories&amp;mode=delete&amp;category_id=<?php echo stripslashes($category->category_id);?>" class="delete" onclick="return confirm('<?php echo __('Are you sure you want to delete this category?','calendar'); ?>')"><?php echo __('Delete','calendar'); ?></a></td>
1677 <?php
1678 }
1679 ?>
1680 </tr>
1681 <?php
1682 }
1683 ?>
1684 </table>
1685 <?php
1686 }
1687 else
1688 {
1689 echo '<p>'.__('There are no categories in the database - something has gone wrong!','calendar').'</p>';
1690 }
1691
1692 ?>
1693 </div>
1694
1695 <?php
1696 }
1697 }
1698
1699 // Function to indicate the number of the day passed, eg. 1st or 2nd Sunday
1700 function np_of_day($date)
1701 {
1702 $instance = 0;
1703 $dom = date('j',strtotime($date));
1704 if (($dom-7) <= 0) { $instance = 1; }
1705 else if (($dom-7) > 0 && ($dom-7) <= 7) { $instance = 2; }
1706 else if (($dom-7) > 7 && ($dom-7) <= 14) { $instance = 3; }
1707 else if (($dom-7) > 14 && ($dom-7) <= 21) { $instance = 4; }
1708 else if (($dom-7) > 21 && ($dom-7) < 28) { $instance = 5; }
1709 return $instance;
1710 }
1711
1712 // Function to return a prefix which will allow the correct
1713 // placement of arguments into the query string.
1714 function permalink_prefix()
1715 {
1716 // Get the permalink structure from WordPress
1717 if (is_home()) {
1718 $p_link = get_bloginfo('url');
1719 if ($p_link[strlen($p_link)-1] != '/') { $p_link = $p_link.'/'; }
1720 } else {
1721 $p_link = get_permalink();
1722 }
1723
1724 // Based on the structure, append the appropriate ending
1725 if (!(strstr($p_link,'?'))) { $link_part = $p_link.'?'; } else { $link_part = $p_link.'&'; }
1726
1727 return $link_part;
1728 }
1729
1730 // Configure the "Next" link in the calendar
1731 function next_link($cur_year,$cur_month)
1732 {
1733 $mod_rewrite_months = array(1=>'jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec');
1734 $next_year = $cur_year + 1;
1735
1736 if ($cur_month == 12)
1737 {
1738 return '<a href="' . permalink_prefix() . 'month=jan&amp;yr=' . $next_year . '">'.__('Next','calendar').' &raquo;</a>';
1739 }
1740 else
1741 {
1742 $next_month = $cur_month + 1;
1743 $month = $mod_rewrite_months[$next_month];
1744 return '<a href="' . permalink_prefix() . 'month='.$month.'&amp;yr=' . $cur_year . '">'.__('Next','calendar').' &raquo;</a>';
1745 }
1746 }
1747
1748 // Configure the "Previous" link in the calendar
1749 function prev_link($cur_year,$cur_month)
1750 {
1751 $mod_rewrite_months = array(1=>'jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec');
1752 $last_year = $cur_year - 1;
1753
1754 if ($cur_month == 1)
1755 {
1756 return '<a href="' . permalink_prefix() . 'month=dec&amp;yr='. $last_year .'">&laquo; '.__('Prev','calendar').'</a>';
1757 }
1758 else
1759 {
1760 $next_month = $cur_month - 1;
1761 $month = $mod_rewrite_months[$next_month];
1762 return '<a href="' . permalink_prefix() . 'month='.$month.'&amp;yr=' . $cur_year . '">&laquo; '.__('Prev','calendar').'</a>';
1763 }
1764 }
1765
1766 // Print upcoming events
1767 function upcoming_events()
1768 {
1769 global $wpdb;
1770
1771 // Find out if we should be displaying upcoming events
1772 $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming'",0,0);
1773
1774 if ($display == 'true')
1775 {
1776 // Get number of days we should go into the future
1777 $future_days = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming_days'",0,0);
1778 $day_count = 1;
1779
1780 while ($day_count < $future_days+1)
1781 {
1782 list($y,$m,$d) = split("-",date("Y-m-d",mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))));
1783 $events = grab_events($y,$m,$d,'upcoming');
1784 usort($events, "time_cmp");
1785 if (count($events) != 0) {
1786 $output .= '<li>'.date_i18n(get_option('date_format'),mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))).'<ul>';
1787 }
1788 foreach($events as $event)
1789 {
1790 if ($event->event_time == '00:00:00') {
1791 $time_string = ' '.__('all day','calendar');
1792 }
1793 else {
1794 $time_string = ' '.__('at','calendar').' '.date(get_option('time_format'), strtotime(stripslashes($event->event_time)));
1795 }
1796 $output .= '<li>'.draw_event($event).$time_string.'</li>';
1797 }
1798 if (count($events) != 0) {
1799 $output .= '</ul></li>';
1800 }
1801 $day_count = $day_count+1;
1802 }
1803
1804 if ($output != '')
1805 {
1806 $visual = '<ul>';
1807 $visual .= $output;
1808 $visual .= '</ul>';
1809 return $visual;
1810 }
1811 }
1812 }
1813
1814 // Print todays events
1815 function todays_events()
1816 {
1817 global $wpdb;
1818
1819 // Find out if we should be displaying todays events
1820 $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_todays'",0,0);
1821
1822 if ($display == 'true')
1823 {
1824 $output = '<ul>';
1825 $events = grab_events(date("Y",ctwo()),date("m",ctwo()),date("d",ctwo()),'todays');
1826 usort($events, "time_cmp");
1827 foreach($events as $event)
1828 {
1829 if ($event->event_time == '00:00:00') {
1830 $time_string = ' '.__('all day','calendar');
1831 }
1832 else {
1833 $time_string = ' '.__('at','calendar').' '.date(get_option('time_format'), strtotime(stripslashes($event->event_time)));
1834 }
1835 $output .= '<li>'.draw_event($event).$time_string.'</li>';
1836 }
1837 $output .= '</ul>';
1838 if (count($events) != 0)
1839 {
1840 return $output;
1841 }
1842 }
1843 }
1844
1845 // Function to compare time in event objects
1846 function time_cmp($a, $b)
1847 {
1848 if ($a->event_time == $b->event_time) {
1849 return 0;
1850 }
1851 return ($a->event_time < $b->event_time) ? -1 : 1;
1852 }
1853
1854 // Used to draw multiple events
1855 function draw_events($events)
1856 {
1857 // We need to sort arrays of objects by time
1858 usort($events, "time_cmp");
1859
1860 // Now process the events
1861 foreach($events as $event)
1862 {
1863 $output .= '* '.draw_event($event).'<br />';
1864 }
1865 return $output;
1866 }
1867
1868 // The widget to show todays events in the sidebar
1869 function widget_init_calendar_today() {
1870 // Check for required functions
1871 if (!function_exists('register_sidebar_widget'))
1872 return;
1873
1874 function widget_calendar_today($args) {
1875 extract($args);
1876 $the_title = get_option('calendar_today_widget_title');
1877 $widget_title = empty($the_title) ? __('Today\'s Events','calendar') : $the_title;
1878 $the_events = todays_events();
1879 if ($the_events != '') {
1880 echo $before_widget;
1881 echo $before_title . $widget_title . $after_title;
1882 echo $the_events;
1883 echo $after_widget;
1884 }
1885 }
1886
1887 function widget_calendar_today_control() {
1888 $widget_title = get_option('calendar_today_widget_title');
1889 if (isset($_POST['calendar_today_widget_title'])) {
1890 update_option('calendar_today_widget_title',strip_tags($_POST['calendar_today_widget_title']));
1891 }
1892 ?>
1893 <p>
1894 <label for="calendar_today_widget_title"><?php _e('Title','calendar'); ?>:<br />
1895 <input class="widefat" type="text" id="calendar_today_widget_title" name="calendar_today_widget_title" value="<?php echo $widget_title; ?>"/></label>
1896 </p>
1897 <?php
1898 }
1899
1900 register_sidebar_widget(__('Today\'s Events','calendar'),'widget_calendar_today');
1901 register_widget_control(__('Today\'s Events','calendar'),'widget_calendar_today_control');
1902 }
1903
1904 // The widget to show todays events in the sidebar
1905 function widget_init_calendar_upcoming() {
1906 // Check for required functions
1907 if (!function_exists('register_sidebar_widget'))
1908 return;
1909
1910 function widget_calendar_upcoming($args) {
1911 extract($args);
1912 $the_title = get_option('calendar_upcoming_widget_title');
1913 $widget_title = empty($the_title) ? __('Upcoming Events','calendar') : $the_title;
1914 $the_events = upcoming_events();
1915 if ($the_events != '') {
1916 echo $before_widget;
1917 echo $before_title . $widget_title . $after_title;
1918 echo $the_events;
1919 echo $after_widget;
1920 }
1921 }
1922
1923 function widget_calendar_upcoming_control() {
1924 $widget_title = get_option('calendar_upcoming_widget_title');
1925 if (isset($_POST['calendar_upcoming_widget_title'])) {
1926 update_option('calendar_upcoming_widget_title',strip_tags($_POST['calendar_upcoming_widget_title']));
1927 }
1928 ?>
1929 <p>
1930 <label for="calendar_upcoming_widget_title"><?php _e('Title','calendar'); ?>:<br />
1931 <input class="widefat" type="text" id="calendar_upcoming_widget_title" name="calendar_upcoming_widget_title" value="<?php echo $widget_title; ?>"/></label>
1932 </p>
1933 <?php
1934 }
1935
1936 register_sidebar_widget(__('Upcoming Events','calendar'),'widget_calendar_upcoming');
1937 register_widget_control(__('Upcoming Events','calendar'),'widget_calendar_upcoming_control');
1938 }
1939
1940
1941 // Used to draw an event to the screen
1942 function draw_event($event)
1943 {
1944 global $wpdb;
1945
1946 // Before we do anything we want to know if we
1947 // should display the author and/or show categories.
1948 // We check for this later
1949 $display_author = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_author'",0,0);
1950 $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
1951
1952 if ($show_cat == 'true')
1953 {
1954 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($event->event_category);
1955 $cat_details = $wpdb->get_row($sql);
1956 $style = "background-color:".stripslashes($cat_details->category_colour).";";
1957 }
1958
1959 $header_details .= '<span class="event-title">'.stripslashes($event->event_title).'</span><br />
1960 <span class="event-title-break"></span><br />';
1961 if ($event->event_time != "00:00:00")
1962 {
1963 $header_details .= '<strong>'.__('Time','calendar').':</strong> ' . date(get_option('time_format'), strtotime(stripslashes($event->event_time))) . '<br />';
1964 }
1965 if ($display_author == 'true')
1966 {
1967 $e = get_userdata(stripslashes($event->event_author));
1968 $header_details .= '<strong>'.__('Posted by', 'calendar').':</strong> '.$e->display_name.'<br />';
1969 }
1970 if ($display_author == 'true' || $event->event_time != "00:00:00")
1971 {
1972 $header_details .= '<span class="event-content-break"></span><br />';
1973 }
1974 if ($event->event_link != '') { $linky = stripslashes($event->event_link); }
1975 else { $linky = '#'; }
1976
1977 $details = '<span class="calnk"><a href="'.$linky.'" style="'.$style.'">' . stripslashes($event->event_title) . '<span style="'.$style.'">' . $header_details . '' . stripslashes($event->event_desc) . '</span></a></span>';
1978
1979 return $details;
1980 }
1981
1982 // Grab all events for the requested date from calendar
1983 function grab_events($y,$m,$d,$typing)
1984 {
1985 global $wpdb,$tod_no,$cal_no;
1986
1987 $arr_events = array();
1988
1989 // Get the date format right
1990 $date = $y . '-' . $m . '-' . $d;
1991
1992 // Firstly we check for conventional events. These will form the first instance of a recurring event
1993 // or the only instance of a one-off event
1994 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_begin <= '$date' AND event_end >= '$date' AND event_recur = 'S' ORDER BY event_id");
1995 if (!empty($events))
1996 {
1997 foreach($events as $event)
1998 {
1999 array_push($arr_events, $event);
2000 }
2001 }
2002
2003 // Even if there were results for that query, we may still have events recurring
2004 // from the past on this day. We now methodically check the for these events
2005
2006 /*
2007 The yearly code - easy because the day and month will be the same, so we return all yearly
2008 events that match the date part. Out of these we show those with a repeat of 0, and fast-foward
2009 a number of years for those with a value more than 0. Those that land in the future are displayed.
2010 */
2011
2012
2013 // Deal with forever recurring year events unioned with those that have a limit
2014 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats = 0
2015 UNION ALL
2016 SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats != 0 AND (EXTRACT(YEAR FROM '$date')-EXTRACT(YEAR FROM event_begin)) <= event_repeats
2017 ORDER BY event_id");
2018
2019 if (!empty($events))
2020 {
2021 foreach($events as $event)
2022 {
2023 // This is going to get complex so lets setup what we would place in for
2024 // an event so we can drop it in with ease
2025
2026 // Technically we don't care about the years, but we need to find out if the
2027 // event spans the turn of a year so we can deal with it appropriately.
2028 $year_begin = date('Y',strtotime($event->event_begin));
2029 $year_end = date('Y',strtotime($event->event_end));
2030
2031 if ($year_begin == $year_end)
2032 {
2033 if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) &&
2034 date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
2035 {
2036 array_push($arr_events, $event);
2037 }
2038 }
2039 else if ($year_begin < $year_end)
2040 {
2041 if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) ||
2042 date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
2043 {
2044 array_push($arr_events, $event);
2045 }
2046 }
2047 }
2048 }
2049
2050
2051 /*
2052 The monthly code - just as easy because as long as the day of the month is correct, then we
2053 show the event
2054 */
2055
2056 // The monthly events that never stop recurring unioned with those that do
2057 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats = 0
2058 UNION ALL
2059 SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM event_begin))) <= event_repeats
2060 ORDER BY event_id");
2061 if (!empty($events))
2062 {
2063 foreach($events as $event)
2064 {
2065 // This is going to get complex so lets setup what we would place in for
2066 // an event so we can drop it in with ease
2067
2068 // Technically we don't care about the years or months, but we need to find out if the
2069 // event spans the turn of a year or month so we can deal with it appropriately.
2070 $month_begin = date('m',strtotime($event->event_begin));
2071 $month_end = date('m',strtotime($event->event_end));
2072
2073 if (($month_begin == $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2074 {
2075 if (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) &&
2076 date('d',strtotime($event->event_end)) >= date('d',strtotime($date)))
2077 {
2078 array_push($arr_events, $event);
2079 }
2080 }
2081 else if (($month_begin < $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2082 {
2083 if ( ($event->event_begin <= date('Y-m-d',strtotime($date))) && (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) ||
2084 date('d',strtotime($event->event_end)) >= date('d',strtotime($date))) )
2085 {
2086 array_push($arr_events, $event);
2087 }
2088 }
2089 }
2090 }
2091
2092
2093 /*
2094 The month of Sundays code - events that repeat on every nth instance of a day
2095 */
2096
2097 // The month of Sundays events that never stop recurring unioned with those that do
2098 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats = 0
2099 UNION ALL
2100 SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM event_begin))) <= event_repeats
2101 ORDER BY event_id");
2102 if (!empty($events))
2103 {
2104 foreach($events as $event) {
2105 // Technically we don't care about the years or months, but we need to find out if the
2106 // event spans the turn of a year or month so we can deal with it appropriately.
2107
2108 // In addition we need to know if the instance is the same
2109 $month_begin = date('m',strtotime($event->event_begin));
2110 $month_end = date('m',strtotime($event->event_end));
2111
2112 // We also deal with days here so we assign numeric to each day
2113 $day_start_event = date('D',strtotime($event->event_begin));
2114 $day_end_event = date('D',strtotime($event->event_end));
2115 $current_day = date('D',strtotime($date));
2116 $orig_diff = strtotime($event->event_end) - strtotime($event->event_begin);
2117 $cur_strto = strtotime($date);
2118 $plan = array();
2119 $plan['Mon'] = 1;
2120 $plan['Tue'] = 2;
2121 $plan['Wed'] = 3;
2122 $plan['Thu'] = 4;
2123 $plan['Fri'] = 5;
2124 $plan['Sat'] = 6;
2125 $plan['Sun'] = 7;
2126
2127 if (($month_begin == $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2128 {
2129 if (np_of_day($event->event_begin) == np_of_day($date) && $plan[$day_start_event] == $plan[$current_day])
2130 {
2131 if ($typing == 'calendar') { $cal_no[$event->event_id] = strtotime($date); }
2132 else if ($typing == 'upcoming') { $tod_no[$event->event_id] = strtotime($date); }
2133 else if ($typing == 'todays') { $tod_no[$event->event_id] = strtotime($date); }
2134 }
2135 if ($typing == 'calendar') { $week_no[$event->event_id] = $cal_no[$event->event_id]; }
2136 else if ($typing == 'upcoming') { $week_no[$event->event_id] = $tod_no[$event->event_id]; }
2137 else if ($typing == 'todays') { $week_no[$event->event_id] = $tod_no[$event->event_id]; }
2138
2139 if ((($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
2140 && ((np_of_day($event->event_begin) == np_of_day($date) && $plan[$day_start_event] == $plan[$current_day] && $cur_strto-$week_no[$event->event_id] <= $orig_diff )
2141 || (np_of_day($event->event_begin) == np_of_day($date) && ($plan[$day_start_event] < $plan[$current_day] || $plan[$current_day] <= $plan[$day_end_event]) && $cur_strto-$week_no[$event->event_id] <= $orig_diff)
2142 || (np_of_day($event->event_begin)+1 == np_of_day($date) && ($plan[$day_start_event] < $plan[$current_day] || $plan[$current_day] <= $plan[$day_end_event]) && $cur_strto-$week_no[$event->event_id] <= $orig_diff)))
2143 {
2144 array_push($arr_events, $event);
2145 }
2146 }
2147 else if (($month_begin < $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2148 {
2149 if ((($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
2150 && ((np_of_day($event->event_begin) == np_of_day($date) && $plan[$day_start_event] == $plan[$current_day] && $cur_strto-$week_no[$event->event_id] <= $orig_diff )
2151 || (np_of_day($event->event_begin) == np_of_day($date) && ($plan[$day_start_event] < $plan[$current_day] || $plan[$current_day] <= $plan[$day_end_event]) && $cur_strto-$week_no[$event->event_id] <= $orig_diff)
2152 || (np_of_day($event->event_begin)+1 == np_of_day($date) && ($plan[$day_start_event] < $plan[$current_day] || $plan[$current_day] <= $plan[$day_end_event]) && $cur_strto-$week_no[$event->event_id] <= $orig_diff)))
2153 {
2154 array_push($arr_events, $event);
2155 }
2156 }
2157 }
2158 }
2159
2160
2161 /*
2162 Weekly - well isn't this fun! We need to scan all weekly events, find what day they fell on
2163 and see if that matches the current day. If it does, we check to see if the repeats are 0.
2164 If they are, display the event, if not, we fast forward from the original day in week blocks
2165 until the number is exhausted. If the date we arrive at is in the future, display the event.
2166 */
2167
2168 // The weekly events that never stop recurring unioned with those that do
2169 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'W' AND '$date' >= event_begin AND event_repeats = 0
2170 UNION ALL
2171 SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'W' AND '$date' >= event_begin AND event_repeats != 0 AND (event_repeats*7) >= (TO_DAYS('$date') - TO_DAYS(event_end))
2172 ORDER BY event_id");
2173 if (!empty($events))
2174 {
2175 foreach($events as $event)
2176 {
2177 // This is going to get complex so lets setup what we would place in for
2178 // an event so we can drop it in with ease
2179
2180 // Now we are going to check to see what day the original event
2181 // fell on and see if the current date is both after it and on
2182 // the correct day. If it is, display the event!
2183 $day_start_event = date('D',strtotime($event->event_begin));
2184 $day_end_event = date('D',strtotime($event->event_end));
2185 $current_day = date('D',strtotime($date));
2186
2187 $plan = array();
2188 $plan['Mon'] = 1;
2189 $plan['Tue'] = 2;
2190 $plan['Wed'] = 3;
2191 $plan['Thu'] = 4;
2192 $plan['Fri'] = 5;
2193 $plan['Sat'] = 6;
2194 $plan['Sun'] = 7;
2195
2196 if ($plan[$day_start_event] > $plan[$day_end_event])
2197 {
2198 if (($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
2199 {
2200 array_push($arr_events, $event);
2201 }
2202 }
2203 else if (($plan[$day_start_event] < $plan[$day_end_event]) || ($plan[$day_start_event]== $plan[$day_end_event]))
2204 {
2205 if (($plan[$day_start_event] <= $plan[$current_day]) && ($plan[$current_day] <= $plan[$day_end_event]))
2206 {
2207 array_push($arr_events, $event);
2208 }
2209 }
2210
2211 }
2212 }
2213
2214 return $arr_events;
2215 }
2216
2217 // Setup comparison functions for building the calendar later
2218 function calendar_month_comparison($month)
2219 {
2220 $current_month = strtolower(date("M", ctwo()));
2221 if (isset($_GET['yr']) && isset($_GET['month']))
2222 {
2223 if ($month == $_GET['month'])
2224 {
2225 return ' selected="selected"';
2226 }
2227 }
2228 elseif ($month == $current_month)
2229 {
2230 return ' selected="selected"';
2231 }
2232 }
2233 function calendar_year_comparison($year)
2234 {
2235 $current_year = strtolower(date("Y", ctwo()));
2236 if (isset($_GET['yr']) && isset($_GET['month']))
2237 {
2238 if ($year == $_GET['yr'])
2239 {
2240 return ' selected="selected"';
2241 }
2242 }
2243 else if ($year == $current_year)
2244 {
2245 return ' selected="selected"';
2246 }
2247 }
2248
2249 // Actually do the printing of the calendar
2250 // Compared to searching for and displaying events
2251 // this bit is really rather easy!
2252 function calendar()
2253 {
2254 global $wpdb,$week_no;
2255
2256 // Clean up
2257 unset($week_no);
2258
2259 // Deal with the week not starting on a monday
2260 if (get_option('start_of_week') == 0)
2261 {
2262 $name_days = array(1=>__('Sunday','calendar'),__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'));
2263 }
2264 // Choose Monday if anything other than Sunday is set
2265 else
2266 {
2267 $name_days = array(1=>__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'),__('Sunday','calendar'));
2268 }
2269
2270 // Carry on with the script
2271 $name_months = array(1=>__('January','calendar'),__('February','calendar'),__('March','calendar'),__('April','calendar'),__('May','calendar'),__('June','calendar'),__('July','calendar'),__('August','calendar'),__('September','calendar'),__('October','calendar'),__('November','calendar'),__('December','calendar'));
2272
2273 // If we don't pass arguments we want a calendar that is relevant to today
2274 if (empty($_GET['month']) || empty($_GET['yr']))
2275 {
2276 $c_year = date("Y",ctwo());
2277 $c_month = date("m",ctwo());
2278 $c_day = date("d",ctwo());
2279 }
2280
2281 // Years get funny if we exceed 3000, so we use this check
2282 if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0 && (int)$_GET['yr'] != 0)
2283 {
2284 // This is just plain nasty and all because of permalinks
2285 // which are no longer used, this will be cleaned up soon
2286 if ($_GET['month'] == 'jan' || $_GET['month'] == 'feb' || $_GET['month'] == 'mar' || $_GET['month'] == 'apr' || $_GET['month'] == 'may' || $_GET['month'] == 'jun' || $_GET['month'] == 'jul' || $_GET['month'] == 'aug' || $_GET['month'] == 'sept' || $_GET['month'] == 'oct' || $_GET['month'] == 'nov' || $_GET['month'] == 'dec')
2287 {
2288
2289 // Again nasty code to map permalinks into something
2290 // databases can understand. This will be cleaned up
2291 $c_year = mysql_escape_string($_GET['yr']);
2292 if ($_GET['month'] == 'jan') { $t_month = 1; }
2293 else if ($_GET['month'] == 'feb') { $t_month = 2; }
2294 else if ($_GET['month'] == 'mar') { $t_month = 3; }
2295 else if ($_GET['month'] == 'apr') { $t_month = 4; }
2296 else if ($_GET['month'] == 'may') { $t_month = 5; }
2297 else if ($_GET['month'] == 'jun') { $t_month = 6; }
2298 else if ($_GET['month'] == 'jul') { $t_month = 7; }
2299 else if ($_GET['month'] == 'aug') { $t_month = 8; }
2300 else if ($_GET['month'] == 'sept') { $t_month = 9; }
2301 else if ($_GET['month'] == 'oct') { $t_month = 10; }
2302 else if ($_GET['month'] == 'nov') { $t_month = 11; }
2303 else if ($_GET['month'] == 'dec') { $t_month = 12; }
2304 $c_month = $t_month;
2305 $c_day = date("d",ctwo());
2306 }
2307 // No valid month causes the calendar to default to today
2308 else
2309 {
2310 $c_year = date("Y",ctwo());
2311 $c_month = date("m",ctwo());
2312 $c_day = date("d",ctwo());
2313 }
2314 }
2315 // No valid year causes the calendar to default to today
2316 else
2317 {
2318 $c_year = date("Y",ctwo());
2319 $c_month = date("m",ctwo());
2320 $c_day = date("d",ctwo());
2321 }
2322
2323 // Fix the days of the week if week start is not on a monday
2324 if (get_option('start_of_week') == 0)
2325 {
2326 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
2327 $first_weekday = ($first_weekday==0?1:$first_weekday+1);
2328 }
2329 // Otherwise assume the week starts on a Monday. Anything other
2330 // than Sunday or Monday is just plain odd
2331 else
2332 {
2333 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
2334 $first_weekday = ($first_weekday==0?7:$first_weekday);
2335 }
2336
2337 $days_in_month = date("t", mktime (0,0,0,$c_month,1,$c_year));
2338
2339 // Start the table and add the header and naviagtion
2340 $calendar_body .= '
2341 <table cellspacing="1" cellpadding="0" class="calendar-table">
2342 ';
2343
2344 // We want to know if we should display the date switcher
2345 $date_switcher = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_jump'",0,0);
2346
2347 if ($date_switcher == 'true')
2348 {
2349 $calendar_body .= '<tr>
2350 <td colspan="7" class="calendar-date-switcher">
2351 <form method="get" action="'.htmlspecialchars($_SERVER['REQUEST_URI']).'">
2352 ';
2353 $qsa = array();
2354 parse_str($_SERVER['QUERY_STRING'],$qsa);
2355 foreach ($qsa as $name => $argument)
2356 {
2357 if ($name != 'month' && $name != 'yr')
2358 {
2359 $calendar_body .= '<input type="hidden" name="'.strip_tags($name).'" value="'.strip_tags($argument).'" />
2360 ';
2361 }
2362 }
2363
2364 // We build the months in the switcher
2365 $calendar_body .= '
2366 '.__('Month','calendar').': <select name="month" style="width:100px;">
2367 <option value="jan"'.calendar_month_comparison('jan').'>'.__('January','calendar').'</option>
2368 <option value="feb"'.calendar_month_comparison('feb').'>'.__('February','calendar').'</option>
2369 <option value="mar"'.calendar_month_comparison('mar').'>'.__('March','calendar').'</option>
2370 <option value="apr"'.calendar_month_comparison('apr').'>'.__('April','calendar').'</option>
2371 <option value="may"'.calendar_month_comparison('may').'>'.__('May','calendar').'</option>
2372 <option value="jun"'.calendar_month_comparison('jun').'>'.__('June','calendar').'</option>
2373 <option value="jul"'.calendar_month_comparison('jul').'>'.__('July','calendar').'</option>
2374 <option value="aug"'.calendar_month_comparison('aug').'>'.__('August','calendar').'</option>
2375 <option value="sept"'.calendar_month_comparison('sept').'>'.__('September','calendar').'</option>
2376 <option value="oct"'.calendar_month_comparison('oct').'>'.__('October','calendar').'</option>
2377 <option value="nov"'.calendar_month_comparison('nov').'>'.__('November','calendar').'</option>
2378 <option value="dec"'.calendar_month_comparison('dec').'>'.__('December','calendar').'</option>
2379 </select>
2380 '.__('Year','calendar').': <select name="yr" style="width:60px;">
2381 ';
2382
2383 // The year builder is string mania. If you can make sense of this, you know your PHP!
2384
2385 $past = 30;
2386 $future = 30;
2387 $fut = 1;
2388 while ($past > 0)
2389 {
2390 $p .= ' <option value="';
2391 $p .= date("Y",ctwo())-$past;
2392 $p .= '"'.calendar_year_comparison(date("Y",ctwo())-$past).'>';
2393 $p .= date("Y",ctwo())-$past.'</option>
2394 ';
2395 $past = $past - 1;
2396 }
2397 while ($fut < $future)
2398 {
2399 $f .= ' <option value="';
2400 $f .= date("Y",ctwo())+$fut;
2401 $f .= '"'.calendar_year_comparison(date("Y",ctwo())+$fut).'>';
2402 $f .= date("Y",ctwo())+$fut.'</option>
2403 ';
2404 $fut = $fut + 1;
2405 }
2406 $calendar_body .= $p;
2407 $calendar_body .= ' <option value="'.date("Y",ctwo()).'"'.calendar_year_comparison(date("Y",ctwo())).'>'.date("Y",ctwo()).'</option>
2408 ';
2409 $calendar_body .= $f;
2410 $calendar_body .= '</select>
2411 <input type="submit" value="'.__('Go','calendar').'" />
2412 </form>
2413 </td>
2414 </tr>
2415 ';
2416 }
2417
2418 // The header of the calendar table and the links. Note calls to link functions
2419 $calendar_body .= '<tr>
2420 <td colspan="7" class="calendar-heading">
2421 <table border="0" cellpadding="0" cellspacing="0" width="100%">
2422 <tr>
2423 <td class="calendar-prev">' . prev_link($c_year,$c_month) . '</td>
2424 <td class="calendar-month">'.$name_months[(int)$c_month].' '.$c_year.'</td>
2425 <td class="calendar-next">' . next_link($c_year,$c_month) . '</td>
2426 </tr>
2427 </table>
2428 </td>
2429 </tr>
2430 ';
2431
2432 // Print the headings of the days of the week
2433 $calendar_body .= '<tr>
2434 ';
2435 for ($i=1; $i<=7; $i++)
2436 {
2437 // Colours need to be different if the starting day of the week is different
2438 if (get_option('start_of_week') == 0)
2439 {
2440 $calendar_body .= ' <td class="'.($i<7&&$i>1?'normal-day-heading':'weekend-heading').'">'.$name_days[$i].'</td>
2441 ';
2442 }
2443 else
2444 {
2445 $calendar_body .= ' <td class="'.($i<6?'normal-day-heading':'weekend-heading').'">'.$name_days[$i].'</td>
2446 ';
2447 }
2448 }
2449 $calendar_body .= '</tr>
2450 ';
2451
2452 for ($i=1; $i<=$days_in_month;)
2453 {
2454 $calendar_body .= '<tr>
2455 ';
2456 for ($ii=1; $ii<=7; $ii++)
2457 {
2458 if ($ii==$first_weekday && $i==1)
2459 {
2460 $go = TRUE;
2461 }
2462 elseif ($i > $days_in_month )
2463 {
2464 $go = FALSE;
2465 }
2466
2467 if ($go)
2468 {
2469 // Colours again, this time for the day numbers
2470 if (get_option('start_of_week') == 0)
2471 {
2472 // This bit of code is for styles believe it or not.
2473 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar');
2474 $no_events_class = '';
2475 if (!count($grabbed_events))
2476 {
2477 $no_events_class = ' no-events';
2478 }
2479 $calendar_body .= ' <td class="'.(date("Ymd", mktime (0,0,0,$c_month,$i,$c_year))==date("Ymd",ctwo())?'current-day':'day-with-date').$no_events_class.'"><span '.($ii<7&&$ii>1?'':'class="weekend"').'>'.$i++.'</span><span class="event"><br />' . draw_events($grabbed_events) . '</span></td>
2480 ';
2481 }
2482 else
2483 {
2484 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar');
2485 $no_events_class = '';
2486 if (!count($grabbed_events))
2487 {
2488 $no_events_class = ' no-events';
2489 }
2490 $calendar_body .= ' <td class="'.(date("Ymd", mktime (0,0,0,$c_month,$i,$c_year))==date("Ymd",ctwo())?'current-day':'day-with-date').$no_events_class.'"><span '.($ii<6?'':'class="weekend"').'>'.$i++.'</span><span class="event"><br />' . draw_events($grabbed_events) . '</span></td>
2491 ';
2492 }
2493 }
2494 else
2495 {
2496 $calendar_body .= ' <td class="day-without-date">&nbsp;</td>
2497 ';
2498 }
2499 }
2500 $calendar_body .= '</tr>
2501 ';
2502 }
2503 $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
2504
2505 if ($show_cat == 'true')
2506 {
2507 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " ORDER BY category_name ASC";
2508 $cat_details = $wpdb->get_results($sql);
2509 $calendar_body .= '<tr><td colspan="7">
2510 <table class="cat-key">
2511 <tr><td colspan="2"><strong>'.__('Category Key','calendar').'</strong></td></tr>
2512 ';
2513 foreach($cat_details as $cat_detail)
2514 {
2515 $calendar_body .= '<tr><td style="background-color:'.$cat_detail->category_colour.'; width:20px; height:20px;"></td><td>'.$cat_detail->category_name.'</td></tr>';
2516 }
2517 $calendar_body .= '</table>
2518 </td></tr>
2519 ';
2520 }
2521 $calendar_body .= '</table>
2522 ';
2523
2524 // A little link to yours truly. See the README if you wish to remove this
2525 $calendar_body .= '<div class="kjo-link" style="visibility:visible !important;display:block !important;"><p>'.__('Calendar developed and supported by ', 'calendar').'<a href="http://www.kieranoshea.com">Kieran O\'Shea</a></p></div>
2526 ';
2527
2528 // Phew! After that bit of string building, spit it all out.
2529 // The actual printing is done by the calling function.
2530 return $calendar_body;
2531 }
2532
2533 ?>
2534