PluginProbe
Calendar / 1.3.4
Calendar v1.3.4
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.3.4, at calendar.php

3,203 lines 124.5 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.3.4
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 global $wpdb;
34 define('WP_CALENDAR_TABLE', $wpdb->prefix . 'calendar');
35 define('WP_CALENDAR_CONFIG_TABLE', $wpdb->prefix . 'calendar_config');
36 define('WP_CALENDAR_CATEGORIES_TABLE', $wpdb->prefix . 'calendar_categories');
37
38 // Check ensure calendar is installed and install it if not - required for
39 // the successful operation of most functions called from this point on
40 check_calendar();
41
42 // Create a master category for Calendar and its sub-pages
43 add_action('admin_menu', 'calendar_menu');
44
45 // Enable the ability for the calendar to be loaded from pages
46 add_filter('the_content','calendar_insert');
47 add_filter('the_content','minical_insert');
48
49 // Enable the ability for the lists to be loaded from pages
50 add_filter('the_content','upcoming_insert');
51 add_filter('the_content','todays_insert');
52
53 // Add the function that puts style information in the header
54 add_action('wp_head', 'calendar_wp_head');
55
56 // Add the function that deals with deleted users
57 add_action('delete_user', 'deal_with_deleted_user');
58
59 // Add the widgets if we are using version 2.8
60 add_action('widgets_init', 'widget_init_calendar_today');
61 add_action('widgets_init', 'widget_init_calendar_upcoming');
62 add_action('widgets_init', 'widget_init_events_calendar');
63
64 // Add the short code
65 add_shortcode( 'calendar', 'calendar_shortcode_insert' );
66 add_filter('widget_text', 'do_shortcode');
67
68 // Function to deal with events posted by a user when that user is deleted
69 function deal_with_deleted_user($id)
70 {
71 global $wpdb;
72
73 // Do the query
74 $wpdb->get_results($wpdb->prepare("UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0)." WHERE event_author=%d",$id));
75 }
76
77 // Function to display a warning on the admin panel if the calendar plugin is mising setup
78 add_action( 'admin_notices', 'calendar_setup_incomplete_warning' );
79 function calendar_setup_incomplete_warning() {
80 global $wpdb;
81 $incomplete_check = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='show_attribution_link'");
82 if (empty($incomplete_check) && !(isset($_GET['page']) && $_GET['page'] == 'calendar-config')) {
83 $args = array( 'page' => 'calendar-config');
84 $url = add_query_arg( $args, admin_url( 'admin.php' ) );
85 ?>
86 <div class="update-nag"><p><strong><?php _e('Warning','calendar'); ?>:</strong> <?php _e("Calendar setup incomplete. Go to the <a href=\"$url\">calendar plugin settings</a> to complete setup.",'calendar'); ?></p></div>
87 <?php
88 }
89 }
90
91 // Function to provide time with WordPress offset, localy replaces time()
92 function ctwo()
93 {
94 return (time()+(3600*(get_option('gmt_offset'))));
95 }
96
97 // Function to add the calendar style into the header
98 function calendar_wp_head()
99 {
100 global $wpdb;
101
102 $style = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
103 if ($style != '')
104 {
105 echo '<style type="text/css">
106 ';
107 echo stripslashes($style).'
108 ';
109 echo '</style>
110 ';
111 }
112 }
113
114 // Function to deal with adding the calendar menus
115 function calendar_menu()
116 {
117 global $wpdb;
118
119 // Set admin as the only one who can use Calendar for security
120 $allowed_group = 'manage_options';
121
122 // Use the database to *potentially* override the above if allowed
123 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
124 if (!empty($configs))
125 {
126 foreach ($configs as $config)
127 {
128 $allowed_group = $config->config_value;
129 }
130 }
131
132 // Add the admin panel pages for Calendar. Use permissions pulled from above
133 if (function_exists('add_menu_page'))
134 {
135 add_menu_page(__('Calendar','calendar'), __('Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
136 }
137 if (function_exists('add_submenu_page'))
138 {
139 $calendar_manage_menu = add_submenu_page('calendar', __('Manage Calendar','calendar'), __('Manage Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
140 add_action( "admin_head-".$calendar_manage_menu, 'calendar_add_javascript' );
141 // Note only admin can change calendar options
142 add_submenu_page('calendar', __('Manage Categories','calendar'), __('Manage Categories','calendar'), 'manage_options', 'calendar-categories', 'manage_categories');
143 add_submenu_page('calendar', __('Calendar Config','calendar'), __('Calendar Options','calendar'), 'manage_options', 'calendar-config', 'edit_calendar_config');
144 }
145 }
146
147 // Function to add the javascript to the admin header
148 function calendar_add_javascript()
149 {
150 echo '<script type="text/javascript" src="';
151 bloginfo('wpurl');
152 echo '/wp-content/plugins/calendar/javascript.js"></script>
153 <style type="text/css">
154 .bcal-container{
155 background-color: #fff;
156 border-radius: 4px;
157 -moz-border-radius: 4px;
158 -webkit-border-radius: 4px;
159 float: left;
160 padding: 5px;
161 border: solid 1px #ccc;
162 box-shadow: 0 0 3px #C0C0C0;
163 }
164 .bcal-table{
165 border-collapse: separate;
166 border-spacing: 0;
167 border: solid 1px #A2A6AF;
168 font-family: Tahoma, sans-serif;
169 font-size: 11px;
170 font-weight: normal;
171 float: left;
172 margin: 1px;
173 padding: 0;
174 width: 200px;
175 }
176 .bcal-table,
177 .bcal-table th,
178 .bcal-table td {
179 box-sizing: border-box;
180 -moz-box-sizing: border-box;
181 -webkit-box-sizing: border-box;
182 }
183 .bcal-table thead tr{
184 background-color: #CECED2;
185 border: 1px solid #A2A6AF;
186 background-image: linear-gradient(bottom, rgb(206,206,210) 37%, rgb(241,240,242) 69%);
187 background-image: -o-linear-gradient(bottom, rgb(206,206,210) 37%, rgb(241,240,242) 69%);
188 background-image: -moz-linear-gradient(bottom, rgb(206,206,210) 37%, rgb(241,240,242) 69%);
189 background-image: -webkit-linear-gradient(bottom, rgb(206,206,210) 37%, rgb(241,240,242) 69%);
190 background-image: -ms-linear-gradient(bottom, rgb(206,206,210) 37%, rgb(241,240,242) 69%);
191 background-image: -webkit-gradient(
192 linear,
193 left bottom,
194 left top,
195 color-stop(0.37, rgb(206,206,210)),
196 color-stop(0.69, rgb(241,240,242))
197 );
198 }
199 .bcal-table thead tr:first-child{
200 line-height: 23px;
201 }
202 .bcal-table thead th{
203 font-weight: bold;
204 color: #404D5D;
205 height: 20px;
206 text-align: center;
207 }
208 .bcal-table tbody td{
209 color: #3B4959;
210 cursor: pointer;
211 font-weight: normal;
212 height: 25px;
213 padding: 0 0 1px 1px;
214 width: 25px;
215 text-align: center;
216 }
217 .bcal-table tbody tr:first-child td{
218 border-top: solid 1px #fff;
219 }
220 .bcal-table tbody td:first-child{
221 border-left: none;
222 padding: 0 0 1px;
223 }
224 .bcal-table .bcal-wday,
225 .bcal-table .bcal-wnum{
226 font-weight: normal;
227 border-right: 1px solid #A2A6AF;
228 border-top: 1px solid #A2A6AF;
229 border-bottom: 1px solid #A2A6AF;
230 padding: 1px 1px 2px 2px;
231 }
232 .bcal-table .bcal-wnum{
233 border-right: 1px solid #A2A6AF;
234 border-bottom: 1px solid #A2A6AF;
235 color: #CC6600;
236 background-image: linear-gradient(bottom, rgb(255,225,159) 23%, rgb(255,250,234) 62%);
237 background-image: -o-linear-gradient(bottom, rgb(255,225,159) 23%, rgb(255,250,234) 62%);
238 background-image: -moz-linear-gradient(bottom, rgb(255,225,159) 23%, rgb(255,250,234) 62%);
239 background-image: -webkit-linear-gradient(bottom, rgb(255,225,159) 23%, rgb(255,250,234) 62%);
240 background-image: -ms-linear-gradient(bottom, rgb(255,225,159) 23%, rgb(255,250,234) 62%);
241 background-image: -webkit-gradient(
242 linear,
243 left bottom,
244 left top,
245 color-stop(0.23, rgb(255,225,159)),
246 color-stop(0.62, rgb(255,250,234))
247 );
248 }
249 .bcal-table .bcal-wday:last-child{
250 border-right: none;
251 }
252 .bcal-table .bcal-empty{
253 background-color: #F6F6F7;
254 border-right: solid 1px #fff;
255 border-bottom: solid 1px #fff;
256 color: #999;
257 cursor: default;
258 }
259 .bcal-table tr:last-child .bcal-empty,
260 .bcal-table tr:last-child .bcal-week,
261 .bcal-table tr:last-child .bcal-date{
262 border-bottom: none;
263 }
264 .bcal-table .bcal-date{
265 border-right: solid 1px #fff;
266 border-bottom: solid 1px #fff;
267 background-color: #ECECEE;
268 }
269 .bcal-table .bcal-past{
270 background-color: #e6e6e6;
271 color: #999;
272 cursor: default;
273 }
274 .bcal-table .bcal-today{
275 background-color: #D6D6D1;
276 color: #333;
277 }
278 .bcal-table .bcal-week{
279 background-color: #FEF3DA;
280 border-right: solid 1px #fff;
281 border-bottom: solid 1px #fff;
282 color: #CC6600;
283 cursor: default;
284 }
285 .bcal-table .bcal-selected{
286 background-color: #BDBDBD;
287 color: #F2F2F2;
288 }
289 .bcal-table .bcal-over,
290 .bcal-table .bcal-date:hover{
291 background-color: #B2B2A9;
292 color: #fff;
293 }
294 .bcal-table .bcal-date:last-child,
295 .bcal-table .bcal-empty:last-child{
296 border-right: none;
297 }
298 .bcal-table .bcal-past:hover{
299 background-color: #e0e0e0;
300 color: #666;
301 }
302 .bcal-table .bcal-month,
303 .bcal-table .bcal-navi,
304 .bcal-table .bcal-wnum,
305 .bcal-table .bcal-wday,
306 .bcal-table .bcal-week{
307 cursor: text;
308 }
309 </style>
310 ';
311 }
312
313 // Function to deal with loading the calendar into pages
314 function calendar_shortcode_insert($atts) {
315 $a = shortcode_atts( array(
316 'categories' => '',
317 'type' => ''
318 ), $atts );
319 if ($a['categories'] == '') {
320 if ($a['type'] == 'mini') {
321 return minical();
322 } else {
323 return calendar();
324 }
325 } else {
326 if ($a['type'] == 'mini') {
327 return minical();
328 } else {
329 return calendar( $a['categories'] );
330 }
331 }
332 }
333 function calendar_insert($content)
334 {
335 if (preg_match('/\{CALENDAR*.+\}/',$content))
336 {
337 $cat_list = preg_split('/\{CALENDAR\;/',$content);
338 if (sizeof($cat_list) > 1) {
339 $cat_list = preg_split('/\}/',$cat_list[1]);
340 $cat_list = $cat_list[0];
341 $cal_output = calendar($cat_list);
342 } else {
343 $cal_output = calendar();
344 }
345 $content = preg_replace('/\{CALENDAR*.+\}/',$cal_output,$content);
346 }
347 return $content;
348 }
349
350 // Function to show a mini calendar in pages
351 function minical_insert($content)
352 {
353 if (preg_match('/\{MINICAL*.+\}/',$content))
354 {
355 $cat_list= preg_split('/\{MINICAL\;/',$content);
356 if (sizeof($cat_list) > 1) {
357 $cat_list = preg_split('/\}/',$cat_list[1]);
358 $cat_list= $cat_list[0];
359 $cal_output = minical($cat_list);
360 } else {
361 $cal_output = minical();
362 }
363 $content = preg_replace('/\{MINICAL*.+\}/',$cal_output,$content);
364 }
365 return $content;
366 }
367
368 // Functions to allow the widgets to be inserted into posts and pages
369 function upcoming_insert($content)
370 {
371 if (preg_match('/\{UPCOMING_EVENTS*.+\}/',$content))
372 {
373 $cat_list= preg_split('/\{UPCOMING_EVENTS\;/',$content);
374 if (sizeof($cat_list) > 1) {
375 $cat_list = preg_split('/\}/',$cat_list[1]);
376 $cat_list= $cat_list[0];
377 $cal_output = '<span class="page-upcoming-events">'.upcoming_events($cat_list).'</span>';
378 } else {
379 $cal_output = '<span class="page-upcoming-events">'.upcoming_events().'</span>';
380 }
381 $content = preg_replace('/\{UPCOMING_EVENTS*.+\}/',$cal_output,$content);
382 }
383 return $content;
384 }
385 function todays_insert($content)
386 {
387 if (preg_match('/\{TODAYS_EVENTS*.+\}/',$content))
388 {
389 $cat_list= preg_split('/\{TODAYS_EVENTS\;/',$content);
390 if (sizeof($cat_list) > 1) {
391 $cat_list = preg_split('/\}/',$cat_list[1]);
392 $cat_list= $cat_list[0];
393 $cal_output = '<span class="page-todays-events">'.todays_events($cat_list).'</span>';
394 } else {
395 $cal_output = '<span class="page-todays-events">'.todays_events().'</span>';
396 }
397 $content = preg_replace('/\{TODAYS_EVENTS*.+\}/',$cal_output,$content);
398 }
399 return $content;
400 }
401
402 // Function to check what version of Calendar is installed and install if needed
403 function check_calendar()
404 {
405 // Checks to make sure Calendar is installed, if not it adds the default
406 // database tables and populates them with test data. If it is, then the
407 // version is checked through various means and if it is not up to date
408 // then it is upgraded.
409
410 // Lets see if this is first run and create us a table if it is!
411 global $wpdb, $initial_style;
412
413 // All this style info will go into the database on a new install
414 // This looks nice in the TwentyTen theme
415 $initial_style = " .calnk a:hover {
416 background-position:0 0;
417 text-decoration:none;
418 color:#000000;
419 border-bottom:1px dotted #000000;
420 }
421 .calnk a:visited {
422 text-decoration:none;
423 color:#000000;
424 border-bottom:1px dotted #000000;
425 }
426 .calnk a {
427 text-decoration:none;
428 color:#000000;
429 border-bottom:1px dotted #000000;
430 }
431 .calnk a span {
432 display:none;
433 }
434 .calnk a:hover span {
435 color:#333333;
436 background:#F6F79B;
437 display:block;
438 position:absolute;
439 margin-top:1px;
440 padding:5px;
441 width:150px;
442 z-index:100;
443 line-height:1.2em;
444 }
445 .calendar-table {
446 border:0 !important;
447 width:100% !important;
448 border-collapse:separate !important;
449 border-spacing:2px !important;
450 }
451 .calendar-heading {
452 height:25px;
453 text-align:center;
454 background-color:#E4EBE3;
455 }
456 .calendar-next {
457 width:20%;
458 text-align:center;
459 border:none;
460 }
461 .calendar-prev {
462 width:20%;
463 text-align:center;
464 border:none;
465 }
466 .calendar-month {
467 width:60%;
468 text-align:center;
469 font-weight:bold;
470 border:none;
471 }
472 .normal-day-heading {
473 text-align:center;
474 width:25px;
475 height:25px;
476 font-size:0.8em;
477 border:1px solid #DFE6DE;
478 background-color:#EBF2EA;
479 }
480 .weekend-heading {
481 text-align:center;
482 width:25px;
483 height:25px;
484 font-size:0.8em;
485 border:1px solid #DFE6DE;
486 background-color:#EBF2EA;
487 color:#FF0000;
488 }
489 .day-with-date {
490 vertical-align:text-top;
491 text-align:left;
492 width:60px;
493 height:60px;
494 border:1px solid #DFE6DE;
495 }
496 .no-events {
497
498 }
499 .day-without-date {
500 width:60px;
501 height:60px;
502 border:1px solid #E9F0E8;
503 }
504 span.weekend {
505 color:#FF0000;
506 }
507 .current-day {
508 vertical-align:text-top;
509 text-align:left;
510 width:60px;
511 height:60px;
512 border:1px solid #BFBFBF;
513 background-color:#E4EBE3;
514 }
515 span.event {
516 font-size:0.75em;
517 }
518 .kjo-link {
519 font-size:0.75em;
520 text-align:center;
521 }
522 .calendar-date-switcher {
523 height:25px;
524 text-align:center;
525 border:1px solid #D6DED5;
526 background-color:#E4EBE3;
527 }
528 .calendar-date-switcher form {
529 margin:2px;
530 }
531 .calendar-date-switcher input {
532 border:1px #D6DED5 solid;
533 margin:0;
534 }
535 .calendar-date-switcher input[type=submit] {
536 padding:3px 10px;
537 }
538 .calendar-date-switcher select {
539 border:1px #D6DED5 solid;
540 margin:0;
541 }
542 .calnk a:hover span span.event-title {
543 padding:0;
544 text-align:center;
545 font-weight:bold;
546 font-size:1.2em;
547 margin-left:0px;
548 }
549 .calnk a:hover span span.event-title-break {
550 width:96%;
551 text-align:center;
552 height:1px;
553 margin-top:5px;
554 margin-right:2%;
555 padding:0;
556 background-color:#000000;
557 margin-left:0px;
558 }
559 .calnk a:hover span span.event-content-break {
560 width:96%;
561 text-align:center;
562 height:1px;
563 margin-top:5px;
564 margin-right:2%;
565 padding:0;
566 background-color:#000000;
567 margin-left:0px;
568 }
569 .page-upcoming-events {
570 font-size:80%;
571 }
572 .page-todays-events {
573 font-size:80%;
574 }
575 .calendar-table table,tbody,tr,td {
576 margin:0 !important;
577 padding:0 !important;
578 }
579 table.calendar-table {
580 margin-bottom:5px !important;
581 }
582 .cat-key {
583 width:100%;
584 margin-top:30px;
585 padding:5px;
586 border:0 !important;
587 }
588 .cal-separate {
589 border:0 !important;
590 margin-top:10px;
591 }
592 table.cat-key {
593 margin-top:5px !important;
594 border:1px solid #DFE6DE !important;
595 border-collapse:separate !important;
596 border-spacing:4px !important;
597 margin-left:2px !important;
598 width:99.5% !important;
599 margin-bottom:5px !important;
600 }
601 .cat-key td {
602 border:0 !important;
603 }";
604
605
606 // Assume this is not a new install until we prove otherwise
607 $new_install = false;
608 $vone_point_one_upgrade = false;
609 $vone_point_two_beta_upgrade = false;
610
611 $wp_calendar_exists = false;
612 $wp_calendar_config_exists = false;
613 $wp_calendar_config_version_number_exists = false;
614
615 // Determine the calendar version
616 $tables = $wpdb->get_results("show tables");
617 foreach ( $tables as $table )
618 {
619 foreach ( $table as $value )
620 {
621 if ( $value == WP_CALENDAR_TABLE )
622 {
623 $wp_calendar_exists = true;
624 }
625 if ( $value == WP_CALENDAR_CONFIG_TABLE )
626 {
627 $wp_calendar_config_exists = true;
628
629 // We now try and find the calendar version number
630 // This will be a lot easier than finding other stuff
631 // in the future.
632 $version_number = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_version'");
633 if ($version_number == "1.2")
634 {
635 $wp_calendar_config_version_number_exists = true;
636 }
637 }
638 }
639 }
640
641 if ($wp_calendar_exists == false && $wp_calendar_config_exists == false)
642 {
643 $new_install = true;
644 }
645 else if ($wp_calendar_exists == true && $wp_calendar_config_exists == false)
646 {
647 $vone_point_one_upgrade = true;
648 }
649 else if ($wp_calendar_exists == true && $wp_calendar_config_exists == true && $wp_calendar_config_version_number_exists == false)
650 {
651 $vone_point_two_beta_upgrade = true;
652 }
653
654 // Now we've determined what the current install is or isn't
655 // we perform operations according to the findings
656 if ( $new_install == true )
657 {
658 $sql = "CREATE TABLE " . WP_CALENDAR_TABLE . " (
659 event_id INT(11) NOT NULL AUTO_INCREMENT ,
660 event_begin DATE NOT NULL ,
661 event_end DATE NOT NULL ,
662 event_title VARCHAR(30) NOT NULL ,
663 event_desc TEXT NOT NULL ,
664 event_time TIME ,
665 event_recur CHAR(1) ,
666 event_repeats INT(3) ,
667 event_author BIGINT(20) UNSIGNED ,
668 event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1 ,
669 event_link TEXT ,
670 PRIMARY KEY (event_id)
671 )";
672 $wpdb->get_results($sql);
673 $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
674 config_item VARCHAR(30) NOT NULL ,
675 config_value TEXT NOT NULL ,
676 PRIMARY KEY (config_item)
677 )";
678 $wpdb->get_results($sql);
679 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
680 $wpdb->get_results($sql);
681 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
682 $wpdb->get_results($sql);
683 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
684 $wpdb->get_results($sql);
685 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
686 $wpdb->get_results($sql);
687 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
688 $wpdb->get_results($sql);
689 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
690 $wpdb->get_results($sql);
691 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
692 $wpdb->get_results($sql);
693 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
694 $wpdb->get_results($sql);
695 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
696 $wpdb->get_results($sql);
697 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
698 category_id INT(11) NOT NULL AUTO_INCREMENT,
699 category_name VARCHAR(30) NOT NULL ,
700 category_colour VARCHAR(30) NOT NULL ,
701 PRIMARY KEY (category_id)
702 )";
703 $wpdb->get_results($sql);
704 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
705 $wpdb->get_results($sql);
706 }
707 else if ($vone_point_one_upgrade == true)
708 {
709 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_author BIGINT(20) UNSIGNED";
710 $wpdb->get_results($sql);
711 $sql = "UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0);
712 $wpdb->get_results($sql);
713 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." MODIFY event_desc TEXT NOT NULL";
714 $wpdb->get_results($sql);
715 $sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
716 config_item VARCHAR(30) NOT NULL ,
717 config_value TEXT NOT NULL ,
718 PRIMARY KEY (config_item)
719 )";
720 $wpdb->get_results($sql);
721 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
722 $wpdb->get_results($sql);
723 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
724 $wpdb->get_results($sql);
725 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
726 $wpdb->get_results($sql);
727 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
728 $wpdb->get_results($sql);
729 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
730 $wpdb->get_results($sql);
731 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
732 $wpdb->get_results($sql);
733 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
734 $wpdb->get_results($sql);
735 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
736 $wpdb->get_results($sql);
737 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
738 $wpdb->get_results($sql);
739 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
740 $wpdb->get_results($sql);
741 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT";
742 $wpdb->get_results($sql);
743 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
744 category_id INT(11) NOT NULL AUTO_INCREMENT,
745 category_name VARCHAR(30) NOT NULL ,
746 category_colour VARCHAR(30) NOT NULL ,
747 PRIMARY KEY (category_id)
748 )";
749 $wpdb->get_results($sql);
750 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
751 $wpdb->get_results($sql);
752 }
753 else if ($vone_point_two_beta_upgrade == true)
754 {
755 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
756 $wpdb->get_results($sql);
757 $sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
758 $wpdb->get_results($sql);
759 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
760 $wpdb->get_results($sql);
761 $sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT ";
762 $wpdb->get_results($sql);
763 $sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
764 category_id INT(11) NOT NULL AUTO_INCREMENT,
765 category_name VARCHAR(30) NOT NULL ,
766 category_colour VARCHAR(30) NOT NULL ,
767 PRIMARY KEY (category_id)
768 )";
769 $wpdb->get_results($sql);
770 $sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
771 $wpdb->get_results($sql);
772 $sql = "UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value='".$initial_style."' WHERE config_item='calendar_style'";
773 $wpdb->get_results($sql);
774 }
775 }
776
777 // Used on the manage events admin page to display a list of events
778 function wp_events_display_list(){
779
780 global $wpdb;
781
782 $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " ORDER BY event_begin DESC");
783
784 if ( !empty($events) )
785 {
786 ?>
787 <table class="widefat page fixed" width="100%" cellpadding="3" cellspacing="3">
788 <thead>
789 <tr>
790 <th class="manage-column" scope="col"><?php _e('ID','calendar') ?></th>
791 <th class="manage-column" scope="col"><?php _e('Title','calendar') ?></th>
792 <th class="manage-column" scope="col"><?php _e('Start Date','calendar') ?></th>
793 <th class="manage-column" scope="col"><?php _e('End Date','calendar') ?></th>
794 <th class="manage-column" scope="col"><?php _e('Time','calendar') ?></th>
795 <th class="manage-column" scope="col"><?php _e('Recurs','calendar') ?></th>
796 <th class="manage-column" scope="col"><?php _e('Repeats','calendar') ?></th>
797 <th class="manage-column" scope="col"><?php _e('Author','calendar') ?></th>
798 <th class="manage-column" scope="col"><?php _e('Category','calendar') ?></th>
799 <th class="manage-column" scope="col"><?php _e('Edit','calendar') ?></th>
800 <th class="manage-column" scope="col"><?php _e('Delete','calendar') ?></th>
801 </tr>
802 </thead>
803 <?php
804 $class = '';
805 foreach ( $events as $event )
806 {
807 $class = ($class == 'alternate') ? '' : 'alternate';
808 ?>
809 <tr class="<?php echo $class; ?>">
810 <th scope="row"><?php echo stripslashes($event->event_id); ?></th>
811 <td><?php echo stripslashes($event->event_title); ?></td>
812 <td><?php echo stripslashes($event->event_begin); ?></td>
813 <td><?php echo stripslashes($event->event_end); ?></td>
814 <td><?php if ($event->event_time == '00:00:00') { echo __('N/A','calendar'); } else { echo stripslashes($event->event_time); } ?></td>
815 <td>
816 <?php
817 // Interpret the DB values into something human readable
818 if ($event->event_recur == 'S') { echo __('Never','calendar'); }
819 else if ($event->event_recur == 'W') { echo __('Weekly','calendar'); }
820 else if ($event->event_recur == 'M') { echo __('Monthly (date)','calendar'); }
821 else if ($event->event_recur == 'U') { echo __('Monthly (day)','calendar'); }
822 else if ($event->event_recur == 'Y') { echo __('Yearly','calendar'); }
823 ?>
824 </td>
825 <td>
826 <?php
827 // Interpret the DB values into something human readable
828 if ($event->event_recur == 'S') { echo __('N/A','calendar'); }
829 else if ($event->event_repeats == 0) { echo __('Forever','calendar'); }
830 else if ($event->event_repeats > 0) { echo stripslashes($event->event_repeats).' '.__('Times','calendar'); }
831 ?>
832 </td>
833 <td><?php $e = get_userdata($event->event_author); echo $e->display_name; ?></td>
834 <?php
835 $sql = $wpdb->prepare("SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=%d",$event->event_category);
836 $this_cat = $wpdb->get_row($sql);
837 ?>
838 <td style="background-color:<?php echo stripslashes($this_cat->category_colour);?>;"><?php echo stripslashes($this_cat->category_name); ?></td>
839 <?php unset($this_cat); ?>
840 <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>
841 <td><a href="
842 <?php echo wp_nonce_url(bloginfo('wpurl').'/wp-admin/admin.php?page=calendar&amp;action=delete&amp;event_id='.stripslashes($event->event_id),'calendar-delete_'.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>
843 </tr>
844 <?php
845 }
846 ?>
847 </table>
848 <?php
849 }
850 else
851 {
852 ?>
853 <p><?php _e("There are no events in the database!",'calendar') ?></p>
854 <?php
855 }
856 }
857
858
859 // The event edit form for the manage events admin page
860 function wp_events_edit_form($mode='add', $event_id=false)
861 {
862 global $wpdb,$users_entries;
863 $data = false;
864
865 if ( $event_id !== false )
866 {
867 if ( intval($event_id) != $event_id )
868 {
869 echo "<div class=\"error\"><p>".__('Bad Monkey! No banana!','calendar')."</p></div>";
870 return;
871 }
872 else
873 {
874 $data = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_id='%d' LIMIT 1",$event_id));
875 if ( empty($data) )
876 {
877 echo "<div class=\"error\"><p>".__("An event with that ID couldn't be found",'calendar')."</p></div>";
878 return;
879 }
880 $data = $data[0];
881 }
882 // Recover users entries if they exist; in other words if editing an event went wrong
883 if (!empty($users_entries))
884 {
885 $data = $users_entries;
886 }
887 }
888 // Deal with possibility that form was submitted but not saved due to error - recover user's entries here
889 else
890 {
891 $data = $users_entries;
892 }
893
894 ?>
895 <div id="pop_up_cal" style="position:absolute;margin-left:150px;visibility:hidden;background-color:white;layer-background-color:white;z-index:1;"></div>
896 <form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar">
897 <input type="hidden" name="action" value="<?php echo $mode; ?>">
898 <input type="hidden" name="event_id" value="<?php echo stripslashes($event_id); ?>">
899 <?php
900 if (stripslashes($event_id) != "") {
901 $nonce_string = 'calendar-'.$mode.'_'.stripslashes($event_id);
902 } else {
903 $nonce_string = 'calendar-'.$mode;
904 }
905 wp_nonce_field($nonce_string);
906 ?>
907
908 <div id="linkadvanceddiv" class="postbox">
909 <div style="float: left; width: 98%; clear: both;" class="inside">
910 <table cellpadding="5" cellspacing="5">
911 <tr>
912 <td><legend><?php _e('Event Title','calendar'); ?></legend></td>
913 <td><input type="text" name="event_title" class="input" size="40" maxlength="30"
914 value="<?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_title)); ?>" /></td>
915 </tr>
916 <tr>
917 <td style="vertical-align:top;"><legend><?php _e('Event Description','calendar'); ?></legend></td>
918 <td><textarea name="event_desc" class="input" rows="5" cols="50"><?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_desc)); ?></textarea></td>
919 </tr>
920 <tr>
921 <td><legend><?php _e('Event Category','calendar'); ?></legend></td>
922 <td> <select name="event_category">
923 <?php
924 // Grab all the categories and list them
925 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE;
926 $cats = $wpdb->get_results($sql);
927 foreach($cats as $cat)
928 {
929 echo '<option value="'.stripslashes($cat->category_id).'"';
930 if (!empty($data))
931 {
932 if ($data->event_category == $cat->category_id)
933 {
934 echo 'selected="selected"';
935 }
936 }
937 echo '>'.stripslashes($cat->category_name).'</option>
938 ';
939 }
940 ?>
941 </select>
942 </td>
943 </tr>
944 <tr>
945 <td><legend><?php _e('Event Link (Optional)','calendar'); ?></legend></td>
946 <td><input type="text" name="event_link" class="input" size="40" value="<?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_link)); ?>" /></td>
947 </tr>
948 <tr>
949 <td><legend><?php _e('Start Date','calendar'); ?></legend></td>
950 <td>
951 <input type="text" name="event_begin" id="event_begin" class="input" size="12"
952 value="<?php
953 if ( !empty($data) )
954 {
955 echo htmlspecialchars(stripslashes($data->event_begin));
956 }
957 else
958 {
959 echo date("Y-m-d",ctwo());
960 }
961 ?>" />
962 <script type="text/javascript">
963 var cal_1 = new Calendar({
964 element: 'event_begin',
965 startDay: <?php echo get_option('start_of_week'); ?>,
966 onSelect: function unifydates(element) {
967 document.forms['quoteform'].event_end.value = document.forms['quoteform'].event_begin.value;
968 }
969 });
970 </script>
971 </td>
972 </tr>
973 <tr>
974 <td><legend><?php _e('End Date','calendar'); ?></legend></td>
975 <td>
976 <input type="text" name="event_end" id="event_end" class="input" size="12"
977 value="<?php
978 if ( !empty($data) )
979 {
980 echo htmlspecialchars(stripslashes($data->event_end));
981 }
982 else
983 {
984 echo date("Y-m-d",ctwo());
985 }
986 ?>" />
987 <script type="text/javascript">
988 var cal_2 = new Calendar({
989 element: 'event_end',
990 startDay: <?php echo get_option('start_of_week'); ?>,
991 minDate: new Date(parseInt(document.forms['quoteform'].event_begin.value.split('-')[0]),parseInt(document.forms['quoteform'].event_begin.value.split('-')[1]-1),parseInt(document.forms['quoteform'].event_begin.value.split('-')[2]))
992 });
993 </script>
994 </td>
995 </tr>
996 <tr>
997 <td><legend><?php _e('Time (hh:mm)','calendar'); ?></legend></td>
998 <td> <input type="text" name="event_time" class="input" size=12
999 value="<?php
1000 if ( !empty($data) )
1001 {
1002 if ($data->event_time == "00:00:00")
1003 {
1004 echo '';
1005 }
1006 else
1007 {
1008 echo date("H:i",strtotime(htmlspecialchars(stripslashes($data->event_time))));
1009 }
1010 }
1011 else
1012 {
1013 echo date("H:i",ctwo());
1014 }
1015 ?>" /> <?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'); ?>
1016 </td>
1017 </tr>
1018 <tr>
1019 <td><legend><?php _e('Recurring Events','calendar'); ?></legend></td>
1020 <td> <?php
1021 if (isset($data)) {
1022 if ($data->event_repeats != NULL)
1023 {
1024 $repeats = $data->event_repeats;
1025 }
1026 else
1027 {
1028 $repeats = 0;
1029 }
1030 }
1031 else
1032 {
1033 $repeats = 0;
1034 }
1035
1036 $selected_s = '';
1037 $selected_w = '';
1038 $selected_m = '';
1039 $selected_y = '';
1040 $selected_u = '';
1041 if (isset($data)) {
1042 if ($data->event_recur == "S")
1043 {
1044 $selected_s = 'selected="selected"';
1045 }
1046 else if ($data->event_recur == "W")
1047 {
1048 $selected_w = 'selected="selected"';
1049 }
1050 else if ($data->event_recur == "M")
1051 {
1052 $selected_m = 'selected="selected"';
1053 }
1054 else if ($data->event_recur == "Y")
1055 {
1056 $selected_y = 'selected="selected"';
1057 }
1058 else if ($data->event_recur == "U")
1059 {
1060 $selected_u = 'selected="selected"';
1061 }
1062 }
1063 ?>
1064 <?php _e('Repeats for','calendar'); ?>
1065 <input type="text" name="event_repeats" class="input" size="1" value="<?php echo $repeats; ?>" />
1066 <select name="event_recur" class="input">
1067 <option class="input" <?php echo $selected_s; ?> value="S"><?php _e('None') ?></option>
1068 <option class="input" <?php echo $selected_w; ?> value="W"><?php _e('Weeks') ?></option>
1069 <option class="input" <?php echo $selected_m; ?> value="M"><?php _e('Months (date)') ?></option>
1070 <option class="input" <?php echo $selected_u; ?> value="U"><?php _e('Months (day)') ?></option>
1071 <option class="input" <?php echo $selected_y; ?> value="Y"><?php _e('Years') ?></option>
1072 </select><br />
1073 <?php _e('Entering 0 means forever. Where the recurrance interval is left at none, the event will not reoccur.','calendar'); ?>
1074 </td>
1075 </tr>
1076 </table>
1077 </div>
1078 <div style="clear:both; height:1px;">&nbsp;</div>
1079 </div>
1080 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1081 </form>
1082 <?php
1083 }
1084
1085 // The actual function called to render the manage events page and
1086 // to deal with posts
1087 function edit_calendar()
1088 {
1089 global $current_user, $wpdb, $users_entries;
1090 ?>
1091 <style type="text/css">
1092 <!--
1093 .error {
1094 background: lightcoral;
1095 border: 1px solid #e64f69;
1096 margin: 1em 5% 10px;
1097 padding: 0 1em 0 1em;
1098 }
1099
1100 .center {
1101 text-align: center;
1102 }
1103 .right { text-align: right;
1104 }
1105 .left {
1106 text-align: left;
1107 }
1108 .top {
1109 vertical-align: top;
1110 }
1111 .bold {
1112 font-weight: bold;
1113 }
1114 .private {
1115 color: #e64f69;
1116 }
1117 //-->
1118 </style>
1119
1120 <?php
1121
1122 // First some quick cleaning up
1123 $edit = $create = $save = $delete = false;
1124
1125 // Make sure we are collecting the variables we need to select years and months
1126 $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
1127 $event_id = !empty($_REQUEST['event_id']) ? $_REQUEST['event_id'] : '';
1128
1129 // Deal with adding an event to the database
1130 if ( $action == 'add' )
1131 {
1132 $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
1133 $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
1134 $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
1135 $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
1136 $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
1137 $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
1138 $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
1139 $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
1140 $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
1141
1142 if (wp_verify_nonce($_POST['_wpnonce'],'calendar-add') == false) {
1143 ?>
1144 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try adding the event again",'calendar'); ?></p></div>
1145 <?php
1146 } else {
1147
1148 // Perform some validation on the submitted dates - this checks for valid years and months
1149 $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
1150 $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
1151 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)))
1152 {
1153 // We know we have a valid year and month and valid integers for days so now we do a final check on the date
1154 $begin_split = explode('-',$begin);
1155 $begin_y = $begin_split[0];
1156 $begin_m = $begin_split[1];
1157 $begin_d = $begin_split[2];
1158 $end_split = explode('-',$end);
1159 $end_y = $end_split[0];
1160 $end_m = $end_split[1];
1161 $end_d = $end_split[2];
1162 if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
1163 {
1164 // 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
1165 if (strtotime($end) >= strtotime($begin))
1166 {
1167 $start_date_ok = 1;
1168 $end_date_ok = 1;
1169 }
1170 else
1171 {
1172 ?>
1173 <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>
1174 <?php
1175 }
1176 }
1177 else
1178 {
1179 ?>
1180 <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>
1181 <?php
1182 }
1183 }
1184 else
1185 {
1186 ?>
1187 <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>
1188 <?php
1189 }
1190 // We check for a valid time, or an empty one
1191 $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
1192 $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
1193 if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
1194 {
1195 $time_ok = 1;
1196 if ($time == '')
1197 {
1198 $time_to_use = '00:00:00';
1199 }
1200 else if ($time == '00:00')
1201 {
1202 $time_to_use = '00:00:01';
1203 }
1204 else
1205 {
1206 $time_to_use = $time;
1207 }
1208 }
1209 else
1210 {
1211 ?>
1212 <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>
1213 <?php
1214 }
1215 // We check to make sure the URL is alright
1216 if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
1217 {
1218 $url_ok = 1;
1219 }
1220 else
1221 {
1222 ?>
1223 <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>
1224 <?php
1225 }
1226 // The title must be at least one character in length and no more than 30
1227 if (preg_match('/^.{1,30}$/',$title))
1228 {
1229 $title_ok =1;
1230 }
1231 else
1232 {
1233 ?>
1234 <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>
1235 <?php
1236 }
1237 // We run some checks on recurrance
1238 $repeats = (int)$repeats;
1239 if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y' || $recur == 'U')))
1240 {
1241 $recurring_ok = 1;
1242 }
1243 else
1244 {
1245 ?>
1246 <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>
1247 <?php
1248 }
1249 if (isset($start_date_ok) && isset($end_date_ok) && isset($time_ok) && isset($url_ok) && isset($title_ok) && isset($recurring_ok))
1250 {
1251 $sql = $wpdb->prepare("INSERT INTO " . WP_CALENDAR_TABLE . " SET event_title='%s', event_desc='%s', event_begin='%s', event_end='%s', event_time='%s', event_recur='%s', event_repeats='%s', event_author=%d, event_category=%d, event_link='%s'",$title,$desc,$begin,$end,$time_to_use,$recur,$repeats,$current_user->ID,$category,$linky);
1252
1253 $wpdb->get_results($sql);
1254
1255 $sql = $wpdb->prepare("SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='%s' AND event_desc='%s' AND event_begin='%s' AND event_end='%s' AND event_recur='%s' AND event_repeats='%s' LIMIT 1",$title,$desc,$begin,$end,$recur,$repeats);
1256 $result = $wpdb->get_results($sql);
1257
1258 if ( empty($result) || empty($result[0]->event_id) )
1259 {
1260 ?>
1261 <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>
1262 <?php
1263 }
1264 else
1265 {
1266 ?>
1267 <div class="updated"><p><?php _e('Event added. It will now show in your calendar.','calendar'); ?></p></div>
1268 <?php
1269 }
1270 }
1271 else
1272 {
1273 // The form is going to be rejected due to field validation issues, so we preserve the users entries here
1274 $users_entries->event_title = $title;
1275 $users_entries->event_desc = $desc;
1276 $users_entries->event_begin = $begin;
1277 $users_entries->event_end = $end;
1278 $users_entries->event_time = $time;
1279 $users_entries->event_recur = $recur;
1280 $users_entries->event_repeats = $repeats;
1281 $users_entries->event_category = $category;
1282 $users_entries->event_link = $linky;
1283 }
1284 }
1285 }
1286 // Permit saving of events that have been edited
1287 elseif ( $action == 'edit_save' )
1288 {
1289 $title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
1290 $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
1291 $begin = !empty($_REQUEST['event_begin']) ? $_REQUEST['event_begin'] : '';
1292 $end = !empty($_REQUEST['event_end']) ? $_REQUEST['event_end'] : '';
1293 $time = !empty($_REQUEST['event_time']) ? $_REQUEST['event_time'] : '';
1294 $recur = !empty($_REQUEST['event_recur']) ? $_REQUEST['event_recur'] : '';
1295 $repeats = !empty($_REQUEST['event_repeats']) ? $_REQUEST['event_repeats'] : '';
1296 $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
1297 $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
1298
1299 if ( empty($event_id) )
1300 {
1301 ?>
1302 <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>
1303 <?php
1304 }
1305 elseif (wp_verify_nonce($_POST['_wpnonce'],'calendar-edit_save_'.$event_id) == false) {
1306 ?>
1307 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try editing the event again",'calendar'); ?></p></div>
1308 <?php
1309 }
1310 else
1311 {
1312 // Perform some validation on the submitted dates - this checks for valid years and months
1313 $date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
1314 $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
1315 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)))
1316 {
1317 // We know we have a valid year and month and valid integers for days so now we do a final check on the date
1318 $begin_split = explode('-',$begin);
1319 $begin_y = $begin_split[0];
1320 $begin_m = $begin_split[1];
1321 $begin_d = $begin_split[2];
1322 $end_split = explode('-',$end);
1323 $end_y = $end_split[0];
1324 $end_m = $end_split[1];
1325 $end_d = $end_split[2];
1326 if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y))
1327 {
1328 // 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
1329 if (strtotime($end) >= strtotime($begin))
1330 {
1331 $start_date_ok = 1;
1332 $end_date_ok = 1;
1333 }
1334 else
1335 {
1336 ?>
1337 <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>
1338 <?php
1339 }
1340 }
1341 else
1342 {
1343 ?>
1344 <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>
1345 <?php
1346 }
1347 }
1348 else
1349 {
1350 ?>
1351 <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>
1352 <?php
1353 }
1354 // We check for a valid time, or an empty one
1355 $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
1356 $time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
1357 if (preg_match($time_format_one,$time) || preg_match($time_format_two,$time) || $time == '')
1358 {
1359 $time_ok = 1;
1360 if ($time == '')
1361 {
1362 $time_to_use = '00:00:00';
1363 }
1364 else if ($time == '00:00')
1365 {
1366 $time_to_use = '00:00:01';
1367 }
1368 else
1369 {
1370 $time_to_use = $time;
1371 }
1372 }
1373 else
1374 {
1375 ?>
1376 <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>
1377 <?php
1378 }
1379 // We check to make sure the URL is alright
1380 if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == '')
1381 {
1382 $url_ok = 1;
1383 }
1384 else
1385 {
1386 ?>
1387 <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>
1388 <?php
1389 }
1390 // The title must be at least one character in length and no more than 30
1391 if (preg_match('/^.{1,30}$/',$title))
1392 {
1393 $title_ok =1;
1394 }
1395 else
1396 {
1397 ?>
1398 <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>
1399 <?php
1400 }
1401 // We run some checks on recurrance
1402 $repeats = (int)$repeats;
1403 if (($repeats == 0 && $recur == 'S') || (($repeats >= 0) && ($recur == 'W' || $recur == 'M' || $recur == 'Y' || $recur == 'U')))
1404 {
1405 $recurring_ok = 1;
1406 }
1407 else
1408 {
1409 ?>
1410 <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>
1411 <?php
1412 }
1413 if (isset($start_date_ok) && isset($end_date_ok) && isset($time_ok) && isset($url_ok) && isset($title_ok) && isset($recurring_ok))
1414 {
1415 $sql = $wpdb->prepare("UPDATE " . WP_CALENDAR_TABLE . " SET event_title='%s', event_desc='%s', event_begin='%s', event_end='%s', event_time='%s', event_recur='%s', event_repeats='%s', event_author=%d, event_category=%d, event_link='%s' WHERE event_id='%s'",$title,$desc,$begin,$end,$time_to_use,$recur,$repeats,$current_user->ID,$category,$linky,$event_id);
1416 $wpdb->get_results($sql);
1417 $sql = $wpdb->prepare("SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_title='%s' AND event_desc='%s' AND event_begin='%s' AND event_end='%s' AND event_recur='%s' AND event_repeats='%s' LIMIT 1",$title,$desc,$begin,$end,$recur,$repeats);
1418 $result = $wpdb->get_results($sql);
1419
1420 if ( empty($result) || empty($result[0]->event_id) )
1421 {
1422 ?>
1423 <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>
1424 <?php
1425 }
1426 else
1427 {
1428 ?>
1429 <div class="updated"><p><?php _e('Event updated successfully','calendar'); ?></p></div>
1430 <?php
1431 }
1432 }
1433 else
1434 {
1435 // The form is going to be rejected due to field validation issues, so we preserve the users entries here
1436 $users_entries->event_title = $title;
1437 $users_entries->event_desc = $desc;
1438 $users_entries->event_begin = $begin;
1439 $users_entries->event_end = $end;
1440 $users_entries->event_time = $time;
1441 $users_entries->event_recur = $recur;
1442 $users_entries->event_repeats = $repeats;
1443 $users_entries->event_category = $category;
1444 $users_entries->event_link = $linky;
1445 $error_with_saving = 1;
1446 }
1447 }
1448 }
1449 // Deal with deleting an event from the database
1450 elseif ( $action == 'delete' )
1451 {
1452 if ( empty($event_id) )
1453 {
1454 ?>
1455 <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>
1456 <?php
1457 }
1458 elseif (wp_verify_nonce($_GET['_wpnonce'],'calendar-delete_'.$event_id) == false) {
1459 ?>
1460 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try deleting the event again",'calendar'); ?></p></div>
1461 <?php
1462 }
1463 else
1464 {
1465 $sql = $wpdb->prepare("DELETE FROM " . WP_CALENDAR_TABLE . " WHERE event_id='%s'",$event_id);
1466 $wpdb->get_results($sql);
1467
1468 $sql = $wpdb->prepare("SELECT event_id FROM " . WP_CALENDAR_TABLE . " WHERE event_id='%s'",$event_id);
1469 $result = $wpdb->get_results($sql);
1470
1471 if ( empty($result) || empty($result[0]->event_id) )
1472 {
1473 ?>
1474 <div class="updated"><p><?php _e('Event deleted successfully','calendar'); ?></p></div>
1475 <?php
1476 }
1477 else
1478 {
1479 ?>
1480 <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>
1481 <?php
1482
1483 }
1484 }
1485 }
1486
1487 // Now follows a little bit of code that pulls in the main
1488 // components of this page; the edit form and the list of events
1489 ?>
1490
1491 <div class="wrap">
1492 <?php
1493 if ( $action == 'edit' || ($action == 'edit_save' && isset($error_with_saving)))
1494 {
1495 ?>
1496 <h2><?php _e('Edit Event','calendar'); ?></h2>
1497 <?php
1498 if ( empty($event_id) )
1499 {
1500 echo "<div class=\"error\"><p>".__("You must provide an event id in order to edit it",'calendar')."</p></div>";
1501 }
1502 else
1503 {
1504 wp_events_edit_form('edit_save', $event_id);
1505 }
1506 }
1507 else
1508 {
1509 ?>
1510 <h2><?php _e('Add Event','calendar'); ?></h2>
1511 <?php wp_events_edit_form(); ?>
1512
1513 <h2><?php _e('Manage Events','calendar'); ?></h2>
1514 <?php
1515 wp_events_display_list();
1516 }
1517 ?>
1518 </div>
1519
1520 <?php
1521
1522 }
1523
1524 // Display the admin configuration page
1525 function edit_calendar_config()
1526 {
1527 global $wpdb, $initial_style;
1528
1529 if (isset($_POST['permissions']) && isset($_POST['style']) && wp_verify_nonce($_POST['_wpnonce'],'calendar-config') == false) {
1530 ?>
1531 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try editing the config again",'calendar'); ?></p></div>
1532 <?php
1533 }
1534 elseif (isset($_POST['permissions']) && isset($_POST['style']))
1535 {
1536 if ($_POST['permissions'] == 'subscriber') { $new_perms = 'read'; }
1537 else if ($_POST['permissions'] == 'contributor') { $new_perms = 'edit_posts'; }
1538 else if ($_POST['permissions'] == 'author') { $new_perms = 'publish_posts'; }
1539 else if ($_POST['permissions'] == 'editor') { $new_perms = 'moderate_comments'; }
1540 else if ($_POST['permissions'] == 'admin') { $new_perms = 'manage_options'; }
1541 else { $new_perms = 'manage_options'; }
1542
1543 $calendar_style = $_POST['style']; // Escape done in query below
1544 $display_upcoming_days = $_POST['display_upcoming_days']; // Escape done in query below
1545
1546 if ($_POST['display_author'] == 'on')
1547 {
1548 $disp_author = 'true';
1549 }
1550 else
1551 {
1552 $disp_author = 'false';
1553 }
1554
1555 if ($_POST['display_jump'] == 'on')
1556 {
1557 $disp_jump = 'true';
1558 }
1559 else
1560 {
1561 $disp_jump = 'false';
1562 }
1563
1564 if ($_POST['display_todays'] == 'on')
1565 {
1566 $disp_todays = 'true';
1567 }
1568 else
1569 {
1570 $disp_todays = 'false';
1571 }
1572
1573 if ($_POST['display_upcoming'] == 'on')
1574 {
1575 $disp_upcoming = 'true';
1576 }
1577 else
1578 {
1579 $disp_upcoming = 'false';
1580 }
1581
1582 if ($_POST['enable_categories'] == 'on')
1583 {
1584 $enable_categories = 'true';
1585 }
1586 else
1587 {
1588 $enable_categories = 'false';
1589 }
1590
1591 if ($_POST['show_attribution_link'] == 'on') {
1592 $show_attribution_link = 'true';
1593 } else {
1594 $show_attribution_link = 'false';
1595 }
1596
1597 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='can_manage_events'",$new_perms));
1598 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='calendar_style'",$calendar_style));
1599 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='display_author'",$disp_author));
1600 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='display_jump'",$disp_jump));
1601 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='display_todays'",$disp_todays));
1602 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='display_upcoming'",$disp_upcoming));
1603 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%d' WHERE config_item='display_upcoming_days'",$display_upcoming_days));
1604 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='enable_categories'",$enable_categories));
1605 $attribution_present = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='show_attribution_link'");
1606 if (empty($attribution_present)) {
1607 $wpdb->get_results("INSERT INTO " . WP_CALENDAR_CONFIG_TABLE . " SET config_item='show_attribution_link', config_value='false'");
1608 }
1609 $wpdb->get_results($wpdb->prepare("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '%s' WHERE config_item='show_attribution_link'",$show_attribution_link));
1610
1611 // Check to see if we are replacing the original style
1612 if (isset($_POST['reset_styles'])) {
1613 if ($_POST['reset_styles'] == 'on')
1614 {
1615 $wpdb->get_results("UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value = '".$initial_style."' WHERE config_item='calendar_style'");
1616 }
1617 }
1618
1619 echo "<div class=\"updated\"><p><strong>".__('Settings saved','calendar').".</strong></p></div>";
1620 }
1621
1622 // Pull the values out of the database that we need for the form
1623 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
1624 if (!empty($configs))
1625 {
1626 foreach ($configs as $config)
1627 {
1628 $allowed_group = stripslashes($config->config_value);
1629 }
1630 }
1631
1632 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
1633 if (!empty($configs))
1634 {
1635 foreach ($configs as $config)
1636 {
1637 $calendar_style = stripslashes($config->config_value);
1638 }
1639 }
1640 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_author'");
1641 $yes_disp_author = '';
1642 $no_disp_author = '';
1643 if (!empty($configs))
1644 {
1645 foreach ($configs as $config)
1646 {
1647 if ($config->config_value == 'true')
1648 {
1649 $yes_disp_author = 'selected="selected"';
1650 }
1651 else
1652 {
1653 $no_disp_author = 'selected="selected"';
1654 }
1655 }
1656 }
1657 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_jump'");
1658 $yes_disp_jump = '';
1659 $no_disp_jump = '';
1660 if (!empty($configs))
1661 {
1662 foreach ($configs as $config)
1663 {
1664 if ($config->config_value == 'true')
1665 {
1666 $yes_disp_jump = 'selected="selected"';
1667 }
1668 else
1669 {
1670 $no_disp_jump = 'selected="selected"';
1671 }
1672 }
1673 }
1674 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_todays'");
1675 $yes_disp_todays = '';
1676 $no_disp_todays = '';
1677 if (!empty($configs))
1678 {
1679 foreach ($configs as $config)
1680 {
1681 if ($config->config_value == 'true')
1682 {
1683 $yes_disp_todays = 'selected="selected"';
1684 }
1685 else
1686 {
1687 $no_disp_todays = 'selected="selected"';
1688 }
1689 }
1690 }
1691 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_upcoming'");
1692 $yes_disp_upcoming = '';
1693 $no_disp_upcoming = '';
1694 if (!empty($configs))
1695 {
1696 foreach ($configs as $config)
1697 {
1698 if ($config->config_value == 'true')
1699 {
1700 $yes_disp_upcoming = 'selected="selected"';
1701 }
1702 else
1703 {
1704 $no_disp_upcoming = 'selected="selected"';
1705 }
1706 }
1707 }
1708 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='display_upcoming_days'");
1709 if (!empty($configs))
1710 {
1711 foreach ($configs as $config)
1712 {
1713 $upcoming_days = stripslashes($config->config_value);
1714 }
1715 }
1716 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='enable_categories'");
1717 $yes_enable_categories = '';
1718 $no_enable_categories = '';
1719 if (!empty($configs))
1720 {
1721 foreach ($configs as $config)
1722 {
1723 if ($config->config_value == 'true')
1724 {
1725 $yes_enable_categories = 'selected="selected"';
1726 }
1727 else
1728 {
1729 $no_enable_categories = 'selected="selected"';
1730 }
1731 }
1732 }
1733 $configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='show_attribution_link'");
1734 $yes_show_attribution_link = '';
1735 $no_show_attribution_link = '';
1736 if (!empty($configs))
1737 {
1738 foreach ($configs as $config)
1739 {
1740 if ($config->config_value == 'true')
1741 {
1742 $yes_show_attribution_link = 'selected="selected"';
1743 }
1744 else
1745 {
1746 $no_show_attribution_link = 'selected="selected"';
1747 }
1748 }
1749 }
1750 $subscriber_selected = '';
1751 $contributor_selected = '';
1752 $author_selected = '';
1753 $editor_selected = '';
1754 $admin_selected = '';
1755 if ($allowed_group == 'read') { $subscriber_selected='selected="selected"';}
1756 else if ($allowed_group == 'edit_posts') { $contributor_selected='selected="selected"';}
1757 else if ($allowed_group == 'publish_posts') { $author_selected='selected="selected"';}
1758 else if ($allowed_group == 'moderate_comments') { $editor_selected='selected="selected"';}
1759 else if ($allowed_group == 'manage_options') { $admin_selected='selected="selected"';}
1760
1761 // Now we render the form
1762 ?>
1763 <style type="text/css">
1764 <!--
1765 .error {
1766 background: lightcoral;
1767 border: 1px solid #e64f69;
1768 margin: 1em 5% 10px;
1769 padding: 0 1em 0 1em;
1770 }
1771
1772 .center {
1773 text-align: center;
1774 }
1775 .right {
1776 text-align: right;
1777 }
1778 .left {
1779 text-align: left;
1780 }
1781 .top {
1782 vertical-align: top;
1783 }
1784 .bold {
1785 font-weight: bold;
1786 }
1787 .private {
1788 color: #e64f69;
1789 }
1790 //-->
1791 </style>
1792
1793 <div class="wrap">
1794 <h2><?php _e('Calendar Options','calendar'); ?></h2>
1795 <form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-config">
1796 <?php wp_nonce_field('calendar-config'); ?>
1797 <div id="linkadvanceddiv" class="postbox">
1798 <div style="float: left; width: 98%; clear: both;" class="inside">
1799 <table cellpadding="5" cellspacing="5">
1800 <tr>
1801 <td><legend><?php _e('Choose the lowest user group that may manage events','calendar'); ?></legend></td>
1802 <td> <select name="permissions">
1803 <option value="subscriber"<?php echo $subscriber_selected ?>><?php _e('Subscriber','calendar')?></option>
1804 <option value="contributor" <?php echo $contributor_selected ?>><?php _e('Contributor','calendar')?></option>
1805 <option value="author" <?php echo $author_selected ?>><?php _e('Author','calendar')?></option>
1806 <option value="editor" <?php echo $editor_selected ?>><?php _e('Editor','calendar')?></option>
1807 <option value="admin" <?php echo $admin_selected ?>><?php _e('Administrator','calendar')?></option>
1808 </select>
1809 </td>
1810 </tr>
1811 <tr>
1812 <td><legend><?php _e('Do you want to display the author name on events?','calendar'); ?></legend></td>
1813 <td> <select name="display_author">
1814 <option value="on" <?php echo $yes_disp_author ?>><?php _e('Yes','calendar') ?></option>
1815 <option value="off" <?php echo $no_disp_author ?>><?php _e('No','calendar') ?></option>
1816 </select>
1817 </td>
1818 </tr>
1819 <tr>
1820 <td><legend><?php _e('Display a jumpbox for changing month and year quickly?','calendar'); ?></legend></td>
1821 <td> <select name="display_jump">
1822 <option value="on" <?php echo $yes_disp_jump ?>><?php _e('Yes','calendar') ?></option>
1823 <option value="off" <?php echo $no_disp_jump ?>><?php _e('No','calendar') ?></option>
1824 </select>
1825 </td>
1826 </tr>
1827 <tr>
1828 <td><legend><?php _e('Display todays events?','calendar'); ?></legend></td>
1829 <td> <select name="display_todays">
1830 <option value="on" <?php echo $yes_disp_todays ?>><?php _e('Yes','calendar') ?></option>
1831 <option value="off" <?php echo $no_disp_todays ?>><?php _e('No','calendar') ?></option>
1832 </select>
1833 </td>
1834 </tr>
1835 <tr>
1836 <td><legend><?php _e('Display upcoming events?','calendar'); ?></legend></td>
1837 <td> <select name="display_upcoming">
1838 <option value="on" <?php echo $yes_disp_upcoming ?>><?php _e('Yes','calendar') ?></option>
1839 <option value="off" <?php echo $no_disp_upcoming ?>><?php _e('No','calendar') ?></option>
1840 </select>
1841 <?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'); ?>
1842 </td>
1843 </tr>
1844 <tr>
1845 <td><legend><?php _e('Enable event categories?','calendar'); ?></legend></td>
1846 <td> <select name="enable_categories">
1847 <option value="on" <?php echo $yes_enable_categories ?>><?php _e('Yes','calendar') ?></option>
1848 <option value="off" <?php echo $no_enable_categories ?>><?php _e('No','calendar') ?></option>
1849 </select>
1850 </td>
1851 </tr>
1852 <tr>
1853 <td><legend><?php _e('Enable attribution link?','calendar'); ?></legend></td>
1854 <td> <select name="show_attribution_link">
1855 <?php if ($yes_show_attribution_link == '' && $yes_show_attribution_link == '') { ?>
1856 <option value="on" selected="selected"></option>
1857 <?php } ?>
1858 <option value="on" <?php echo $yes_show_attribution_link ?>><?php _e('Yes','calendar') ?></option>
1859 <option value="off" <?php echo $no_show_attribution_link ?>><?php _e('No','calendar') ?></option>
1860 </select>
1861 </td>
1862 </tr>
1863 <tr>
1864 <td style="vertical-align:top;"><legend><?php _e('Configure the stylesheet for Calendar','calendar'); ?></legend></td>
1865 <td><textarea name="style" rows="10" cols="60" tabindex="2"><?php echo $calendar_style; ?></textarea><br />
1866 <input type="checkbox" name="reset_styles" /> <?php _e('Tick this box if you wish to reset the Calendar style to default','calendar'); ?></td>
1867 </tr>
1868 </table>
1869 </div>
1870 <div style="clear:both; height:1px;">&nbsp;</div>
1871 </div>
1872 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1873 </form>
1874 </div>
1875 <?php
1876
1877
1878 }
1879
1880 // Function to handle the management of categories
1881 function manage_categories()
1882 {
1883 global $wpdb;
1884
1885 ?>
1886 <style type="text/css">
1887 <!--
1888 .error {
1889 background: lightcoral;
1890 border: 1px solid #e64f69;
1891 margin: 1em 5% 10px;
1892 padding: 0 1em 0 1em;
1893 }
1894
1895 .center {
1896 text-align: center;
1897 }
1898 .right {
1899 text-align: right;
1900 }
1901 .left {
1902 text-align: left;
1903 }
1904 .top {
1905 vertical-align: top;
1906 }
1907 .bold {
1908 font-weight: bold;
1909 }
1910 .private {
1911 color: #e64f69;
1912 }
1913 //-->
1914
1915 </style>
1916 <?php
1917 // We do some checking to see what we're doing
1918 if (isset($_POST['mode']) && $_POST['mode'] == 'add')
1919 {
1920 if (wp_verify_nonce($_POST['_wpnonce'],'calendar-category_add') == false) {
1921 ?>
1922 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try adding the category again",'calendar'); ?></p></div>
1923 <?php
1924 } else {
1925 // Proceed with the save
1926 $sql = $wpdb->prepare("INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_name='%s', category_colour='%s'",$_POST['category_name'],$_POST['category_colour']);
1927 $wpdb->get_results($sql);
1928 echo "<div class=\"updated\"><p><strong>".__('Category added successfully','calendar')."</strong></p></div>";
1929 }
1930 }
1931 else if (isset($_GET['mode']) && isset($_GET['category_id']) && $_GET['mode'] == 'delete')
1932 {
1933 if (wp_verify_nonce($_GET['_wpnonce'],'calendar-category_delete_'.$_GET['category_id']) == false) {
1934 ?>
1935 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try deleting the category again",'calendar'); ?></p></div>
1936 <?php
1937 } else {
1938 $sql = $wpdb->prepare("DELETE FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=%d",$_GET['category_id']);
1939 $wpdb->get_results($sql);
1940 $sql = $wpdb->prepare("UPDATE " . WP_CALENDAR_TABLE . " SET event_category=1 WHERE event_category=%d",$_GET['category_id']);
1941 $wpdb->get_results($sql);
1942 echo "<div class=\"updated\"><p><strong>".__('Category deleted successfully','calendar')."</strong></p></div>";
1943 }
1944 }
1945 else if (isset($_GET['mode']) && isset($_GET['category_id']) && $_GET['mode'] == 'edit' && !isset($_POST['mode']))
1946 {
1947 $sql = $wpdb->prepare("SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=%d",$_GET['category_id']);
1948 $cur_cat = $wpdb->get_row($sql);
1949 ?>
1950 <div class="wrap">
1951 <h2><?php _e('Edit Category','calendar'); ?></h2>
1952 <form name="catform" id="catform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-categories">
1953 <input type="hidden" name="mode" value="edit" />
1954 <input type="hidden" name="category_id" value="<?php echo stripslashes($cur_cat->category_id) ?>" />
1955 <?php wp_nonce_field('calendar-category_edit_'.stripslashes($cur_cat->category_id)); ?>
1956 <div id="linkadvanceddiv" class="postbox">
1957 <div style="float: left; width: 98%; clear: both;" class="inside">
1958 <table cellpadding="5" cellspacing="5">
1959 <tr>
1960 <td><legend><?php _e('Category Name','calendar'); ?>:</legend></td>
1961 <td><input type="text" name="category_name" class="input" size="30" maxlength="30" value="<?php echo stripslashes($cur_cat->category_name) ?>" /></td>
1962 </tr>
1963 <tr>
1964 <td><legend><?php _e('Category Colour (Hex format)','calendar'); ?>:</legend></td>
1965 <td><input type="text" name="category_colour" class="input" size="10" maxlength="7" value="<?php echo stripslashes($cur_cat->category_colour) ?>" /></td>
1966 </tr>
1967 </table>
1968 </div>
1969 <div style="clear:both; height:1px;">&nbsp;</div>
1970 </div>
1971 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
1972 </form>
1973 </div>
1974 <?php
1975 }
1976 else if (isset($_POST['mode']) && isset($_POST['category_id']) && isset($_POST['category_name']) && isset($_POST['category_colour']) && $_POST['mode'] == 'edit')
1977 {
1978 if (wp_verify_nonce($_POST['_wpnonce'],'calendar-category_edit_'.$_POST['category_id']) == false) {
1979 ?>
1980 <div class="error"><p><strong><?php _e('Error','calendar'); ?>:</strong> <?php _e("Security check failure, try editing the category again",'calendar'); ?></p></div>
1981 <?php
1982 } else {
1983 // Proceed with the save
1984 $sql = $wpdb->prepare("UPDATE " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_name='%s', category_colour='%s' WHERE category_id=%d",$_POST['category_name'],$_POST['category_colour'],$_POST['category_id']);
1985 $wpdb->get_results($sql);
1986 echo "<div class=\"updated\"><p><strong>".__('Category edited successfully','calendar')."</strong></p></div>";
1987 }
1988 }
1989
1990 $get_mode = 0;
1991 $post_mode = 0;
1992 if (isset($_GET['mode'])) {
1993 if ($_GET['mode'] == 'edit') {
1994 $get_mode = 1;
1995 }
1996 }
1997 if (isset($_POST['mode'])) {
1998 if ($_POST['mode'] == 'edit') {
1999 $post_mode = 1;
2000 }
2001 }
2002 if ($get_mode != 1 || $post_mode == 1)
2003 {
2004 ?>
2005
2006 <div class="wrap">
2007 <h2><?php _e('Add Category','calendar'); ?></h2>
2008 <form name="catform" id="catform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=calendar-categories">
2009 <input type="hidden" name="mode" value="add" />
2010 <input type="hidden" name="category_id" value="">
2011 <?php wp_nonce_field('calendar-category_add'); ?>
2012 <div id="linkadvanceddiv" class="postbox">
2013 <div style="float: left; width: 98%; clear: both;" class="inside">
2014 <table cellspacing="5" cellpadding="5">
2015 <tr>
2016 <td><legend><?php _e('Category Name','calendar'); ?>:</legend></td>
2017 <td><input type="text" name="category_name" class="input" size="30" maxlength="30" value="" /></td>
2018 </tr>
2019 <tr>
2020 <td><legend><?php _e('Category Colour (Hex format)','calendar'); ?>:</legend></td>
2021 <td><input type="text" name="category_colour" class="input" size="10" maxlength="7" value="" /></td>
2022 </tr>
2023 </table>
2024 </div>
2025 <div style="clear:both; height:1px;">&nbsp;</div>
2026 </div>
2027 <input type="submit" name="save" class="button bold" value="<?php _e('Save','calendar'); ?> &raquo;" />
2028 </form>
2029 <h2><?php _e('Manage Categories','calendar'); ?></h2>
2030 <?php
2031
2032 // We pull the categories from the database
2033 $categories = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " ORDER BY category_id ASC");
2034
2035 if ( !empty($categories) )
2036 {
2037 ?>
2038 <table class="widefat page fixed" width="50%" cellpadding="3" cellspacing="3">
2039 <thead>
2040 <tr>
2041 <th class="manage-column" scope="col"><?php _e('ID','calendar') ?></th>
2042 <th class="manage-column" scope="col"><?php _e('Category Name','calendar') ?></th>
2043 <th class="manage-column" scope="col"><?php _e('Category Colour','calendar') ?></th>
2044 <th class="manage-column" scope="col"><?php _e('Edit','calendar') ?></th>
2045 <th class="manage-column" scope="col"><?php _e('Delete','calendar') ?></th>
2046 </tr>
2047 </thead>
2048 <?php
2049 $class = '';
2050 foreach ( $categories as $category )
2051 {
2052 $class = ($class == 'alternate') ? '' : 'alternate';
2053 ?>
2054 <tr class="<?php echo $class; ?>">
2055 <th scope="row"><?php echo stripslashes($category->category_id); ?></th>
2056 <td><?php echo stripslashes($category->category_name); ?></td>
2057 <td style="background-color:<?php echo stripslashes($category->category_colour); ?>;">&nbsp;</td>
2058 <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>
2059 <?php
2060 if ($category->category_id == 1)
2061 {
2062 echo '<td>'.__('N/A','calendar').'</td>';
2063 }
2064 else
2065 {
2066 ?>
2067 <td><a href="<?php echo wp_nonce_url(bloginfo('wpurl').'/wp-admin/admin.php?page=calendar-categories&amp;mode=delete&amp;category_id='.stripslashes($category->category_id), 'calendar-category_delete_'.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>
2068 <?php
2069 }
2070 ?>
2071 </tr>
2072 <?php
2073 }
2074 ?>
2075 </table>
2076 <?php
2077 }
2078 else
2079 {
2080 echo '<p>'.__('There are no categories in the database - something has gone wrong!','calendar').'</p>';
2081 }
2082
2083 ?>
2084 </div>
2085
2086 <?php
2087 }
2088 }
2089
2090 // Function to indicate the number of the day passed, eg. 1st or 2nd Sunday
2091 function np_of_day($date)
2092 {
2093 $instance = 0;
2094 $dom = date('j',strtotime($date));
2095 if (($dom-7) <= 0) { $instance = 1; }
2096 else if (($dom-7) > 0 && ($dom-7) <= 7) { $instance = 2; }
2097 else if (($dom-7) > 7 && ($dom-7) <= 14) { $instance = 3; }
2098 else if (($dom-7) > 14 && ($dom-7) <= 21) { $instance = 4; }
2099 else if (($dom-7) > 21 && ($dom-7) < 28) { $instance = 5; }
2100 return $instance;
2101 }
2102
2103 // Function to provide date of the nth day passed (eg. 2nd Sunday)
2104 function dt_of_sun($date,$instance,$day)
2105 {
2106 $plan = array();
2107 $plan['Mon'] = 1;
2108 $plan['Tue'] = 2;
2109 $plan['Wed'] = 3;
2110 $plan['Thu'] = 4;
2111 $plan['Fri'] = 5;
2112 $plan['Sat'] = 6;
2113 $plan['Sun'] = 7;
2114 $proper_date = date('Y-m-d',strtotime($date));
2115 $begin_month = substr($proper_date,0,8).'01';
2116 $offset = $plan[date('D',strtotime($begin_month))];
2117 $result_day = 0;
2118 $recon = 0;
2119 if (($day-($offset)) < 0) { $recon = 7; }
2120 if ($instance == 1) { $result_day = $day-($offset-1)+$recon; }
2121 else if ($instance == 2) { $result_day = $day-($offset-1)+$recon+7; }
2122 else if ($instance == 3) { $result_day = $day-($offset-1)+$recon+14; }
2123 else if ($instance == 4) { $result_day = $day-($offset-1)+$recon+21; }
2124 else if ($instance == 5) { $result_day = $day-($offset-1)+$recon+28; }
2125 return substr($proper_date,0,8).$result_day;
2126 }
2127
2128 // Function to return a prefix which will allow the correct
2129 // placement of arguments into the query string.
2130 function permalink_prefix()
2131 {
2132 // Get the permalink structure from WordPress
2133 if (is_home()) {
2134 $p_link = get_bloginfo('url');
2135 if ($p_link[strlen($p_link)-1] != '/') { $p_link = $p_link.'/'; }
2136 } else {
2137 $p_link = get_permalink();
2138 }
2139
2140 // Based on the structure, append the appropriate ending
2141 if (!(strstr($p_link,'?'))) { $link_part = $p_link.'?'; } else { $link_part = $p_link.'&'; }
2142
2143 return $link_part;
2144 }
2145
2146 // Configure the "Next" link in the calendar
2147 function next_link($cur_year,$cur_month,$minical = false)
2148 {
2149 $mod_rewrite_months = array(1=>'jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec');
2150 $next_year = $cur_year + 1;
2151
2152 if ($cur_month == 12)
2153 {
2154 if ($minical) { $rlink = ''; } else { $rlink = __('Next','calendar'); }
2155 return '<a href="' . permalink_prefix() . 'month=jan&amp;yr=' . $next_year . '">'.$rlink.' &raquo;</a>';
2156 }
2157 else
2158 {
2159 $next_month = $cur_month + 1;
2160 $month = $mod_rewrite_months[$next_month];
2161 if ($minical) { $rlink = ''; } else { $rlink = __('Next','calendar'); }
2162 return '<a href="' . permalink_prefix() . 'month='.$month.'&amp;yr=' . $cur_year . '">'.$rlink.' &raquo;</a>';
2163 }
2164 }
2165
2166 // Configure the "Previous" link in the calendar
2167 function prev_link($cur_year,$cur_month,$minical = false)
2168 {
2169 $mod_rewrite_months = array(1=>'jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec');
2170 $last_year = $cur_year - 1;
2171
2172 if ($cur_month == 1)
2173 {
2174 if ($minical) { $llink = ''; } else { $llink = __('Prev','calendar'); }
2175 return '<a href="' . permalink_prefix() . 'month=dec&amp;yr='. $last_year .'">&laquo; '.$llink.'</a>';
2176 }
2177 else
2178 {
2179 $next_month = $cur_month - 1;
2180 $month = $mod_rewrite_months[$next_month];
2181 if ($minical) { $llink = ''; } else { $llink = __('Prev','calendar'); }
2182 return '<a href="' . permalink_prefix() . 'month='.$month.'&amp;yr=' . $cur_year . '">&laquo; '.$llink.'</a>';
2183 }
2184 }
2185
2186 // Print upcoming events
2187 function upcoming_events($cat_list = '')
2188 {
2189 global $wpdb;
2190
2191 // Find out if we should be displaying upcoming events
2192 $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming'",0,0);
2193
2194 if ($display == 'true')
2195 {
2196 // Get number of days we should go into the future
2197 $future_days = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming_days'",0,0);
2198 $day_count = 1;
2199
2200 $output = '';
2201 while ($day_count < $future_days+1)
2202 {
2203 list($y,$m,$d) = explode("-",date("Y-m-d",mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))));
2204 $events = grab_events($y,$m,$d,'upcoming',$cat_list);
2205 usort($events, "time_cmp");
2206 if (count($events) != 0) {
2207 $output .= '<li>'.date_i18n(get_option('date_format'),mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))).'<ul>';
2208 }
2209 foreach($events as $event)
2210 {
2211 if ($event->event_time == '00:00:00') {
2212 $time_string = ' '.__('all day','calendar');
2213 }
2214 else {
2215 $time_string = ' '.__('at','calendar').' '.date(get_option('time_format'), strtotime(stripslashes($event->event_time)));
2216 }
2217 $output .= '<li>'.draw_event($event).$time_string.'</li>';
2218 }
2219 if (count($events) != 0) {
2220 $output .= '</ul></li>';
2221 }
2222 $day_count = $day_count+1;
2223 }
2224
2225 if ($output != '')
2226 {
2227 $visual = '<ul>';
2228 $visual .= $output;
2229 $visual .= '</ul>';
2230 return $visual;
2231 }
2232 }
2233 }
2234
2235 // Print todays events
2236 function todays_events($cat_list = '')
2237 {
2238 global $wpdb;
2239
2240 // Find out if we should be displaying todays events
2241 $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_todays'",0,0);
2242
2243 if ($display == 'true')
2244 {
2245 $output = '<ul>';
2246 $events = grab_events(date("Y",ctwo()),date("m",ctwo()),date("d",ctwo()),'todays',$cat_list);
2247 usort($events, "time_cmp");
2248 foreach($events as $event)
2249 {
2250 if ($event->event_time == '00:00:00') {
2251 $time_string = ' '.__('all day','calendar');
2252 }
2253 else {
2254 $time_string = ' '.__('at','calendar').' '.date(get_option('time_format'), strtotime(stripslashes($event->event_time)));
2255 }
2256 $output .= '<li>'.draw_event($event).$time_string.'</li>';
2257 }
2258 $output .= '</ul>';
2259 if (count($events) != 0)
2260 {
2261 return $output;
2262 }
2263 }
2264 }
2265
2266 // Function to compare time in event objects
2267 function time_cmp($a, $b)
2268 {
2269 if ($a->event_time == $b->event_time) {
2270 return 0;
2271 }
2272 return ($a->event_time < $b->event_time) ? -1 : 1;
2273 }
2274
2275 // Used to draw multiple events
2276 function draw_events($events)
2277 {
2278 // We need to sort arrays of objects by time
2279 usort($events, "time_cmp");
2280 $output = '';
2281 // Now process the events
2282 foreach($events as $event)
2283 {
2284 $output .= '* '.draw_event($event).'<br />';
2285 }
2286 return $output;
2287 }
2288
2289 // The widget to show the mini calendar
2290 function widget_init_events_calendar() {
2291 // Check for required functions
2292 if (!function_exists('wp_register_sidebar_widget'))
2293 return;
2294
2295 function widget_events_calendar($args) {
2296 extract($args);
2297 $the_title = stripslashes(get_option('events_calendar_widget_title'));
2298 $the_cats = stripslashes(get_option('events_calendar_widget_cats'));
2299 $widget_title = empty($the_title) ? __('Calendar','calendar') : $the_title;
2300 $the_events = minical($the_cats);
2301 if ($the_events != '') {
2302 echo $before_widget;
2303 echo $before_title . $widget_title . $after_title;
2304 echo '<br />'.$the_events;
2305 echo $after_widget;
2306 }
2307 }
2308
2309 function widget_events_calendar_control() {
2310 $widget_title = stripslashes(get_option('events_calendar_widget_title'));
2311 $widget_cats = stripslashes(get_option('events_calendar_widget_cats'));
2312 if (isset($_POST['events_calendar_widget_title']) || isset($_POST['events_calendar_widget_cats'])) {
2313 update_option('events_calendar_widget_title',strip_tags($_POST['events_calendar_widget_title']));
2314 update_option('events_calendar_widget_cats',strip_tags($_POST['events_calendar_widget_cats']));
2315 }
2316 ?>
2317 <p>
2318 <label for="events_calendar_widget_title"><?php _e('Title','calendar'); ?>:<br />
2319 <input class="widefat" type="text" id="events_calendar_widget_title" name="events_calendar_widget_title" value="<?php echo $widget_title; ?>"/></label>
2320 <label for="events_calendar_widget_cats"><?php _e('Comma separated category id list','calendar'); ?>:<br />
2321 <input class="widefat" type="text" id="events_calendar_widget_cats" name="events_calendar_widget_cats" value="<?php echo $widget_cats; ?>"/></label>
2322 </p>
2323 <?php
2324 }
2325
2326 wp_register_sidebar_widget('events_calendar',__('Calendar','calendar'),'widget_events_calendar',array('description'=>'A calendar of your events'));
2327 wp_register_widget_control('events_calendar','events_calendar','widget_events_calendar_control');
2328 }
2329
2330 // The widget to show todays events in the sidebar
2331 function widget_init_calendar_today() {
2332 // Check for required functions
2333 if (!function_exists('wp_register_sidebar_widget'))
2334 return;
2335
2336 function widget_calendar_today($args) {
2337 extract($args);
2338 $the_title = stripslashes(get_option('calendar_today_widget_title'));
2339 $the_cats = stripslashes(get_option('calendar_today_widget_cats'));
2340 $widget_title = empty($the_title) ? __('Today\'s Events','calendar') : $the_title;
2341 $the_events = todays_events($the_cats);
2342 if ($the_events != '') {
2343 echo $before_widget;
2344 echo $before_title . $widget_title . $after_title;
2345 echo $the_events;
2346 echo $after_widget;
2347 }
2348 }
2349
2350 function widget_calendar_today_control() {
2351 $widget_title = stripslashes(get_option('calendar_today_widget_title'));
2352 $widget_cats = stripslashes(get_option('calendar_today_widget_cats'));
2353 if (isset($_POST['calendar_today_widget_title']) || isset($_POST['calendar_today_widget_cats'])) {
2354 update_option('calendar_today_widget_title',strip_tags($_POST['calendar_today_widget_title']));
2355 update_option('calendar_today_widget_cats',strip_tags($_POST['calendar_today_widget_cats']));
2356 }
2357 ?>
2358 <p>
2359 <label for="calendar_today_widget_title"><?php _e('Title','calendar'); ?>:<br />
2360 <input class="widefat" type="text" id="calendar_today_widget_title" name="calendar_today_widget_title" value="<?php echo $widget_title; ?>"/></label>
2361 <label for="calendar_today_widget_cats"><?php _e('Comma separated category id list','calendar'); ?>:<br />
2362 <input class="widefat" type="text" id="calendar_today_widget_cats" name="calendar_today_widget_cats" value="<?php echo $widget_cats; ?>"/></label>
2363 </p>
2364 <?php
2365 }
2366
2367 wp_register_sidebar_widget('todays_events_calendar',__('Today\'s Events','calendar'),'widget_calendar_today',array('description'=>'A list of your events today'));
2368 wp_register_widget_control('todays_events_calendar','todays_events_calendar','widget_calendar_today_control');
2369 }
2370
2371 // The widget to show todays events in the sidebar
2372 function widget_init_calendar_upcoming() {
2373 // Check for required functions
2374 if (!function_exists('wp_register_sidebar_widget'))
2375 return;
2376
2377 function widget_calendar_upcoming($args) {
2378 extract($args);
2379 $the_title = stripslashes(get_option('calendar_upcoming_widget_title'));
2380 $the_cats = stripslashes(get_option('calendar_upcoming_widget_cats'));
2381 $widget_title = empty($the_title) ? __('Upcoming Events','calendar') : $the_title;
2382 $the_events = upcoming_events($the_cats);
2383 if ($the_events != '') {
2384 echo $before_widget;
2385 echo $before_title . $widget_title . $after_title;
2386 echo $the_events;
2387 echo $after_widget;
2388 }
2389 }
2390
2391 function widget_calendar_upcoming_control() {
2392 $widget_title = stripslashes(get_option('calendar_upcoming_widget_title'));
2393 $widget_cats = stripslashes(get_option('calendar_upcoming_widget_cats'));
2394 if (isset($_POST['calendar_upcoming_widget_title']) || isset($_POST['calendar_upcoming_widget_cats'])) {
2395 update_option('calendar_upcoming_widget_title',strip_tags($_POST['calendar_upcoming_widget_title']));
2396 update_option('calendar_upcoming_widget_cats',strip_tags($_POST['calendar_upcoming_widget_cats']));
2397 }
2398 ?>
2399 <p>
2400 <label for="calendar_upcoming_widget_title"><?php _e('Title','calendar'); ?>:<br />
2401 <input class="widefat" type="text" id="calendar_upcoming_widget_title" name="calendar_upcoming_widget_title" value="<?php echo $widget_title; ?>"/></label>
2402 <label for="calendar_upcoming_widget_cats"><?php _e('Comma separated category id list','calendar'); ?>:<br />
2403 <input class="widefat" type="text" id="calendar_upcoming_widget_cats" name="calendar_upcoming_widget_cats" value="<?php echo $widget_cats; ?>"/></label>
2404 </p>
2405 <?php
2406 }
2407
2408 wp_register_sidebar_widget('upcoming_events_calendar',__('Upcoming Events','calendar'),'widget_calendar_upcoming',array('description'=>'A list of your upcoming events'));
2409 wp_register_widget_control('upcoming_events_calendar','upcoming_events_calendar','widget_calendar_upcoming_control');
2410 }
2411
2412 // Used to draw an event to the screen
2413 function draw_event($event)
2414 {
2415 global $wpdb;
2416
2417 // Before we do anything we want to know if we
2418 // should display the author and/or show categories.
2419 // We check for this later
2420 $display_author = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_author'",0,0);
2421 $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
2422 $style = '';
2423 if ($show_cat == 'true')
2424 {
2425 $sql = $wpdb->prepare("SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=%d",$event->event_category);
2426 $cat_details = $wpdb->get_row($sql);
2427 $style = 'style="background-color:'.stripslashes($cat_details->category_colour).';"';
2428 }
2429
2430 $header_details = '<span class="event-title" '.$style.'>'.stripslashes($event->event_title).'</span><br />
2431 <span class="event-title-break"></span><br />';
2432 if ($event->event_time != "00:00:00")
2433 {
2434 $header_details .= '<strong>'.__('Time','calendar').':</strong> ' . date(get_option('time_format'), strtotime(stripslashes($event->event_time))) . '<br />';
2435 }
2436 if ($display_author == 'true')
2437 {
2438 $e = get_userdata(stripslashes($event->event_author));
2439 $header_details .= '<strong>'.__('Posted by', 'calendar').':</strong> '.$e->display_name.'<br />';
2440 }
2441 if ($display_author == 'true' || $event->event_time != "00:00:00")
2442 {
2443 $header_details .= '<span class="event-content-break"></span><br />';
2444 }
2445 if ($event->event_link != '') { $linky = stripslashes($event->event_link); }
2446 else { $linky = '#'; }
2447
2448 $details = '<span class="calnk"><a href="'.$linky.'" '.$style.'>' . stripslashes($event->event_title) . '<span '.$style.'>' . $header_details . '' . stripslashes($event->event_desc) . '</span></a></span>';
2449
2450 return $details;
2451 }
2452
2453 // Grab all events for the requested date from calendar
2454 function grab_events($y,$m,$d,$typing,$cat_list = '')
2455 {
2456 global $wpdb;
2457
2458 $arr_events = array();
2459
2460 // Get the date format right
2461 $date = $y . '-' . $m . '-' . $d;
2462
2463 // Format the category list
2464 if ($cat_list == '') { $cat_sql = ''; }
2465 else { $cat_sql = 'AND event_category in ('.$cat_list.')'; }
2466
2467 // The collated SQL code
2468 $sql = "SELECT a.*,'Normal' AS type FROM " . WP_CALENDAR_TABLE . " AS a WHERE a.event_begin <= '$date' AND a.event_end >= '$date' AND a.event_recur = 'S' ".$cat_sql."
2469 UNION ALL
2470 SELECT b.*,'Yearly' AS type FROM " . WP_CALENDAR_TABLE . " AS b WHERE b.event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM b.event_begin) AND b.event_repeats = 0 ".$cat_sql."
2471 UNION ALL
2472 SELECT c.*,'Yearly' AS type FROM " . WP_CALENDAR_TABLE . " AS c WHERE c.event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM c.event_begin) AND c.event_repeats != 0 AND (EXTRACT(YEAR FROM '$date')-EXTRACT(YEAR FROM c.event_begin)) <= c.event_repeats ".$cat_sql."
2473 UNION ALL
2474 SELECT d.*,'Monthly' AS type FROM " . WP_CALENDAR_TABLE . " AS d WHERE d.event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM d.event_begin) AND d.event_repeats = 0 ".$cat_sql."
2475 UNION ALL
2476 SELECT e.*,'Monthly' AS type FROM " . WP_CALENDAR_TABLE . " AS e WHERE e.event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM e.event_begin) AND e.event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM e.event_begin))) <= e.event_repeats ".$cat_sql."
2477 UNION ALL
2478 SELECT f.*,'MonthSun' AS type FROM " . WP_CALENDAR_TABLE . " AS f WHERE f.event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM f.event_begin) AND f.event_repeats = 0 ".$cat_sql."
2479 UNION ALL
2480 SELECT g.*,'MonthSun' AS type FROM " . WP_CALENDAR_TABLE . " AS g WHERE g.event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM g.event_begin) AND g.event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM g.event_begin))) <= g.event_repeats ".$cat_sql."
2481 UNION ALL
2482 SELECT h.*,'Weekly' AS type FROM " . WP_CALENDAR_TABLE . " AS h WHERE h.event_recur = 'W' AND '$date' >= h.event_begin AND h.event_repeats = 0 ".$cat_sql."
2483 UNION ALL
2484 SELECT i.*,'Weekly' AS type FROM " . WP_CALENDAR_TABLE . " AS i WHERE i.event_recur = 'W' AND '$date' >= i.event_begin AND i.event_repeats != 0 AND (i.event_repeats*7) >= (TO_DAYS('$date') - TO_DAYS(i.event_end)) ".$cat_sql."
2485 ORDER BY event_id";
2486
2487 // Run the collated code
2488 $events =$wpdb->get_results($sql);
2489 if (!empty($events))
2490 {
2491 foreach($events as $event)
2492 {
2493 if ($event->type == 'Normal')
2494 {
2495 array_push($arr_events, $event);
2496 }
2497 else if ($event->type == 'Yearly')
2498 {
2499 // This is going to get complex so lets setup what we would place in for
2500 // an event so we can drop it in with ease
2501
2502 // Technically we don't care about the years, but we need to find out if the
2503 // event spans the turn of a year so we can deal with it appropriately.
2504 $year_begin = date('Y',strtotime($event->event_begin));
2505 $year_end = date('Y',strtotime($event->event_end));
2506
2507 if ($year_begin == $year_end)
2508 {
2509 if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) &&
2510 date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
2511 {
2512 array_push($arr_events, $event);
2513 }
2514 }
2515 else if ($year_begin < $year_end)
2516 {
2517 if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) ||
2518 date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
2519 {
2520 array_push($arr_events, $event);
2521 }
2522 }
2523 }
2524 else if ($event->type == 'Monthly')
2525 {
2526 // This is going to get complex so lets setup what we would place in for
2527 // an event so we can drop it in with ease
2528
2529 // Technically we don't care about the years or months, but we need to find out if the
2530 // event spans the turn of a year or month so we can deal with it appropriately.
2531 $month_begin = date('m',strtotime($event->event_begin));
2532 $month_end = date('m',strtotime($event->event_end));
2533
2534 if (($month_begin == $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2535 {
2536 if (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) &&
2537 date('d',strtotime($event->event_end)) >= date('d',strtotime($date)))
2538 {
2539 array_push($arr_events, $event);
2540 }
2541 }
2542 else if (($month_begin < $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2543 {
2544 if ( ($event->event_begin <= date('Y-m-d',strtotime($date))) && (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) ||
2545 date('d',strtotime($event->event_end)) >= date('d',strtotime($date))) )
2546 {
2547 array_push($arr_events, $event);
2548 }
2549 }
2550 }
2551 else if ($event->type == 'MonthSun')
2552 {
2553 // This used to be complex but writing the dt_of_sun() function helped loads!
2554
2555 // Technically we don't care about the years or months, but we need to find out if the
2556 // event spans the turn of a year or month so we can deal with it appropriately.
2557 $month_begin = date('m',strtotime($event->event_begin));
2558 $month_end = date('m',strtotime($event->event_end));
2559
2560 // Setup some variables and get some values
2561 $dow = date('w',strtotime($event->event_begin));
2562 if ($dow == 0) { $dow = 7; }
2563 $start_ent_this = dt_of_sun($date,np_of_day($event->event_begin),$dow);
2564 $start_ent_prev = dt_of_sun(date('Y-m-d',strtotime($date.'-1 month')),np_of_day($event->event_begin),$dow);
2565 $len_ent = strtotime($event->event_end)-strtotime($event->event_begin);
2566
2567 // The grunt work
2568 if (($month_begin == $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2569 {
2570 // The checks
2571 if (strtotime($event->event_begin) <= strtotime($date) && strtotime($event->event_end) >= strtotime($date)) // Handle the first occurance
2572 {
2573 array_push($arr_events, $event);
2574 }
2575 else if (strtotime($start_ent_this) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_this)+$len_ent) // Now remaining items
2576 {
2577 array_push($arr_events, $event);
2578 }
2579 }
2580 else if (($month_begin < $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
2581 {
2582 // The checks
2583 if (strtotime($event->event_begin) <= strtotime($date) && strtotime($event->event_end) >= strtotime($date)) // Handle the first occurance
2584 {
2585 array_push($arr_events, $event);
2586 }
2587 else if (strtotime($start_ent_prev) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_prev)+$len_ent) // Remaining items from prev month
2588 {
2589 array_push($arr_events, $event);
2590 }
2591 else if (strtotime($start_ent_this) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_this)+$len_ent) // Remaining items starting this month
2592 {
2593 array_push($arr_events, $event);
2594 }
2595 }
2596 }
2597 else if ($event->type == 'Weekly')
2598 {
2599 // This is going to get complex so lets setup what we would place in for
2600 // an event so we can drop it in with ease
2601
2602 // Now we are going to check to see what day the original event
2603 // fell on and see if the current date is both after it and on
2604 // the correct day. If it is, display the event!
2605 $day_start_event = date('D',strtotime($event->event_begin));
2606 $day_end_event = date('D',strtotime($event->event_end));
2607 $current_day = date('D',strtotime($date));
2608
2609 $plan = array();
2610 $plan['Mon'] = 1;
2611 $plan['Tue'] = 2;
2612 $plan['Wed'] = 3;
2613 $plan['Thu'] = 4;
2614 $plan['Fri'] = 5;
2615 $plan['Sat'] = 6;
2616 $plan['Sun'] = 7;
2617
2618 if ($plan[$day_start_event] > $plan[$day_end_event])
2619 {
2620 if (($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
2621 {
2622 array_push($arr_events, $event);
2623 }
2624 }
2625 else if (($plan[$day_start_event] < $plan[$day_end_event]) || ($plan[$day_start_event]== $plan[$day_end_event]))
2626 {
2627 if (($plan[$day_start_event] <= $plan[$current_day]) && ($plan[$current_day] <= $plan[$day_end_event]))
2628 {
2629 array_push($arr_events, $event);
2630 }
2631 }
2632 }
2633 }
2634 }
2635
2636 return $arr_events;
2637 }
2638
2639 // Setup comparison functions for building the calendar later
2640 function calendar_month_comparison($month)
2641 {
2642 $current_month = strtolower(date("M", ctwo()));
2643 if (isset($_GET['yr']) && isset($_GET['month']))
2644 {
2645 if ($month == $_GET['month'])
2646 {
2647 return ' selected="selected"';
2648 }
2649 }
2650 elseif ($month == $current_month)
2651 {
2652 return ' selected="selected"';
2653 }
2654 }
2655 function calendar_year_comparison($year)
2656 {
2657 $current_year = strtolower(date("Y", ctwo()));
2658 if (isset($_GET['yr']) && isset($_GET['month']))
2659 {
2660 if ($year == $_GET['yr'])
2661 {
2662 return ' selected="selected"';
2663 }
2664 }
2665 else if ($year == $current_year)
2666 {
2667 return ' selected="selected"';
2668 }
2669 }
2670
2671 // Actually do the printing of the calendar
2672 // Compared to searching for and displaying events
2673 // this bit is really rather easy!
2674 function calendar($cat_list = '')
2675 {
2676 global $wpdb;
2677
2678 // Deal with the week not starting on a monday
2679 if (get_option('start_of_week') == 0)
2680 {
2681 $name_days = array(1=>__('Sunday','calendar'),__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'));
2682 }
2683 // Choose Monday if anything other than Sunday is set
2684 else
2685 {
2686 $name_days = array(1=>__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'),__('Sunday','calendar'));
2687 }
2688
2689 // Carry on with the script
2690 $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'));
2691
2692 // If we don't pass arguments we want a calendar that is relevant to today
2693 if (empty($_GET['month']) || empty($_GET['yr']))
2694 {
2695 $c_year = date("Y",ctwo());
2696 $c_month = date("m",ctwo());
2697 $c_day = date("d",ctwo());
2698 }
2699
2700 // Years get funny if we exceed 3000, so we use this check
2701 if (isset($_GET['yr']))
2702 {
2703 if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0 && (int)$_GET['yr'] != 0)
2704 {
2705 // This is just plain nasty and all because of permalinks
2706 // which are no longer used, this will be cleaned up soon
2707 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')
2708 {
2709
2710 // Again nasty code to map permalinks into something
2711 // databases can understand. This will be cleaned up
2712 $c_year = $wpdb->prepare("%d",$_GET['yr']);
2713 if ($_GET['month'] == 'jan') { $t_month = 1; }
2714 else if ($_GET['month'] == 'feb') { $t_month = 2; }
2715 else if ($_GET['month'] == 'mar') { $t_month = 3; }
2716 else if ($_GET['month'] == 'apr') { $t_month = 4; }
2717 else if ($_GET['month'] == 'may') { $t_month = 5; }
2718 else if ($_GET['month'] == 'jun') { $t_month = 6; }
2719 else if ($_GET['month'] == 'jul') { $t_month = 7; }
2720 else if ($_GET['month'] == 'aug') { $t_month = 8; }
2721 else if ($_GET['month'] == 'sept') { $t_month = 9; }
2722 else if ($_GET['month'] == 'oct') { $t_month = 10; }
2723 else if ($_GET['month'] == 'nov') { $t_month = 11; }
2724 else if ($_GET['month'] == 'dec') { $t_month = 12; }
2725 $c_month = $t_month;
2726 $c_day = date("d",ctwo());
2727 }
2728 // No valid month causes the calendar to default to today
2729 else
2730 {
2731 $c_year = date("Y",ctwo());
2732 $c_month = date("m",ctwo());
2733 $c_day = date("d",ctwo());
2734 }
2735 }
2736 }
2737 // No valid year causes the calendar to default to today
2738 else
2739 {
2740 $c_year = date("Y",ctwo());
2741 $c_month = date("m",ctwo());
2742 $c_day = date("d",ctwo());
2743 }
2744
2745 // Fix the days of the week if week start is not on a monday
2746 if (get_option('start_of_week') == 0)
2747 {
2748 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
2749 $first_weekday = ($first_weekday==0?1:$first_weekday+1);
2750 }
2751 // Otherwise assume the week starts on a Monday. Anything other
2752 // than Sunday or Monday is just plain odd
2753 else
2754 {
2755 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
2756 $first_weekday = ($first_weekday==0?7:$first_weekday);
2757 }
2758
2759 $days_in_month = date("t", mktime (0,0,0,$c_month,1,$c_year));
2760
2761 // Start the table and add the header and naviagtion
2762 $calendar_body = '';
2763 $calendar_body .= '
2764 <table cellspacing="1" cellpadding="0" class="calendar-table">
2765 ';
2766
2767 // We want to know if we should display the date switcher
2768 $date_switcher = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_jump'",0,0);
2769
2770 if ($date_switcher == 'true')
2771 {
2772 $calendar_body .= '<tr>
2773 <td colspan="7" class="calendar-date-switcher">
2774 <form method="get" action="'.htmlspecialchars($_SERVER['REQUEST_URI']).'">
2775 ';
2776 $qsa = array();
2777 parse_str($_SERVER['QUERY_STRING'],$qsa);
2778 foreach ($qsa as $name => $argument)
2779 {
2780 if ($name != 'month' && $name != 'yr')
2781 {
2782 $calendar_body .= '<input type="hidden" name="'.strip_tags($name).'" value="'.strip_tags($argument).'" />
2783 ';
2784 }
2785 }
2786
2787 // We build the months in the switcher
2788 $calendar_body .= '
2789 '.__('Month','calendar').': <select name="month" style="width:100px;">
2790 <option value="jan"'.calendar_month_comparison('jan').'>'.__('January','calendar').'</option>
2791 <option value="feb"'.calendar_month_comparison('feb').'>'.__('February','calendar').'</option>
2792 <option value="mar"'.calendar_month_comparison('mar').'>'.__('March','calendar').'</option>
2793 <option value="apr"'.calendar_month_comparison('apr').'>'.__('April','calendar').'</option>
2794 <option value="may"'.calendar_month_comparison('may').'>'.__('May','calendar').'</option>
2795 <option value="jun"'.calendar_month_comparison('jun').'>'.__('June','calendar').'</option>
2796 <option value="jul"'.calendar_month_comparison('jul').'>'.__('July','calendar').'</option>
2797 <option value="aug"'.calendar_month_comparison('aug').'>'.__('August','calendar').'</option>
2798 <option value="sept"'.calendar_month_comparison('sept').'>'.__('September','calendar').'</option>
2799 <option value="oct"'.calendar_month_comparison('oct').'>'.__('October','calendar').'</option>
2800 <option value="nov"'.calendar_month_comparison('nov').'>'.__('November','calendar').'</option>
2801 <option value="dec"'.calendar_month_comparison('dec').'>'.__('December','calendar').'</option>
2802 </select>
2803 '.__('Year','calendar').': <select name="yr" style="width:60px;">
2804 ';
2805
2806 // The year builder is string mania. If you can make sense of this, you know your PHP!
2807
2808 $past = 30;
2809 $future = 30;
2810 $fut = 1;
2811 $f = '';
2812 $p = '';
2813 while ($past > 0)
2814 {
2815 $p .= ' <option value="';
2816 $p .= date("Y",ctwo())-$past;
2817 $p .= '"'.calendar_year_comparison(date("Y",ctwo())-$past).'>';
2818 $p .= date("Y",ctwo())-$past.'</option>
2819 ';
2820 $past = $past - 1;
2821 }
2822 while ($fut < $future)
2823 {
2824 $f .= ' <option value="';
2825 $f .= date("Y",ctwo())+$fut;
2826 $f .= '"'.calendar_year_comparison(date("Y",ctwo())+$fut).'>';
2827 $f .= date("Y",ctwo())+$fut.'</option>
2828 ';
2829 $fut = $fut + 1;
2830 }
2831 $calendar_body .= $p;
2832 $calendar_body .= ' <option value="'.date("Y",ctwo()).'"'.calendar_year_comparison(date("Y",ctwo())).'>'.date("Y",ctwo()).'</option>
2833 ';
2834 $calendar_body .= $f;
2835 $calendar_body .= '</select>
2836 <input type="submit" value="'.__('Go','calendar').'" />
2837 </form>
2838 </td>
2839 </tr>
2840 ';
2841 }
2842
2843 // The header of the calendar table and the links. Note calls to link functions
2844 $calendar_body .= '<tr>
2845 <td colspan="7" class="calendar-heading">
2846 <table border="0" cellpadding="0" cellspacing="0" width="100%">
2847 <tr>
2848 <td class="calendar-prev">' . prev_link($c_year,$c_month) . '</td>
2849 <td class="calendar-month">'.$name_months[(int)$c_month].' '.$c_year.'</td>
2850 <td class="calendar-next">' . next_link($c_year,$c_month) . '</td>
2851 </tr>
2852 </table>
2853 </td>
2854 </tr>
2855 ';
2856
2857 // Print the headings of the days of the week
2858 $calendar_body .= '<tr>
2859 ';
2860 for ($i=1; $i<=7; $i++)
2861 {
2862 // Colours need to be different if the starting day of the week is different
2863 if (get_option('start_of_week') == 0)
2864 {
2865 $calendar_body .= ' <td class="'.($i<7&&$i>1?'normal-day-heading':'weekend-heading').'">'.$name_days[$i].'</td>
2866 ';
2867 }
2868 else
2869 {
2870 $calendar_body .= ' <td class="'.($i<6?'normal-day-heading':'weekend-heading').'">'.$name_days[$i].'</td>
2871 ';
2872 }
2873 }
2874 $calendar_body .= '</tr>
2875 ';
2876 $go = FALSE;
2877 for ($i=1; $i<=$days_in_month;)
2878 {
2879 $calendar_body .= '<tr>
2880 ';
2881 for ($ii=1; $ii<=7; $ii++)
2882 {
2883 if ($ii==$first_weekday && $i==1)
2884 {
2885 $go = TRUE;
2886 }
2887 elseif ($i > $days_in_month )
2888 {
2889 $go = FALSE;
2890 }
2891 if ($go)
2892 {
2893 // Colours again, this time for the day numbers
2894 if (get_option('start_of_week') == 0)
2895 {
2896 // This bit of code is for styles believe it or not.
2897 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
2898 $no_events_class = '';
2899 if (!count($grabbed_events))
2900 {
2901 $no_events_class = ' no-events';
2902 }
2903 $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>
2904 ';
2905 }
2906 else
2907 {
2908 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
2909 $no_events_class = '';
2910 if (!count($grabbed_events))
2911 {
2912 $no_events_class = ' no-events';
2913 }
2914 $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>
2915 ';
2916 }
2917 }
2918 else
2919 {
2920 $calendar_body .= ' <td class="day-without-date">&nbsp;</td>
2921 ';
2922 }
2923 }
2924 $calendar_body .= '</tr>
2925 ';
2926 }
2927 $calendar_body .= '</table>
2928 ';
2929 $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
2930
2931 if ($show_cat == 'true')
2932 {
2933 $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " ORDER BY category_name ASC";
2934 $cat_details = $wpdb->get_results($sql);
2935 $calendar_body .= '<table class="cat-key">
2936 <tr><td colspan="2" class="cat-key-cell"><strong>'.__('Category Key','calendar').'</strong></td></tr>
2937 ';
2938 foreach($cat_details as $cat_detail)
2939 {
2940 $calendar_body .= '<tr><td style="background-color:'.$cat_detail->category_colour.'; width:20px; height:20px;" class="cat-key-cell"></td>
2941 <td class="cat-key-cell">&nbsp;'.$cat_detail->category_name.'</td></tr>';
2942 }
2943 $calendar_body .= '</table>
2944 ';
2945 }
2946
2947 // A little link to yours truly
2948 $link_approved = 'false';
2949 $link_approved_results = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='show_attribution_link'");
2950 foreach ($link_approved_results as $link_approved_item)
2951 {
2952 if ($link_approved_item->config_value == 'true')
2953 {
2954 $link_approved = 'true';
2955 }
2956 }
2957 if ($link_approved == 'true') {
2958 $linkback_url = '<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>
2959 ';
2960 } else {
2961 $linkback_url = '';
2962 }
2963 $calendar_body .= $linkback_url;
2964
2965 // Phew! After that bit of string building, spit it all out.
2966 // The actual printing is done by the calling function.
2967 return $calendar_body;
2968 }
2969
2970 // Used to create a hover will all a day's events in for minical
2971 function minical_draw_events($events,$day_of_week = '')
2972 {
2973 // We need to sort arrays of objects by time
2974 usort($events, "time_cmp");
2975 // Only show anything if there are events
2976 $output = '';
2977 if (count($events)) {
2978 // Setup the wrapper
2979 $output = '<span class="calnk"><a href="#" style="background-color:#F6F79B;">'.$day_of_week.'<span>';
2980 // Now process the events
2981 foreach($events as $event)
2982 {
2983 if ($event->event_time == '00:00:00') { $the_time = 'all day'; } else { $the_time = 'at '.date(get_option('time_format'), strtotime(stripslashes($event->event_time))); }
2984 $output .= '* <strong>'.$event->event_title.'</strong> '.$the_time.'<br />';
2985 }
2986 // The tail
2987 $output .= '</span></a></span>';
2988 } else {
2989 $output .= $day_of_week;
2990 }
2991 return $output;
2992 }
2993
2994 function minical($cat_list = '') {
2995
2996 global $wpdb;
2997
2998 // Deal with the week not starting on a monday
2999 if (get_option('start_of_week') == 0)
3000 {
3001 $name_days = array(1=>__('Su','calendar'),__('Mo','calendar'),__('Tu','calendar'),__('We','calendar'),__('Th','calendar'),__('Fr','calendar'),__('Sa','calendar'));
3002 }
3003 // Choose Monday if anything other than Sunday is set
3004 else
3005 {
3006 $name_days = array(1=>__('Mo','calendar'),__('Tu','calendar'),__('We','calendar'),__('Th','calendar'),__('Fr','calendar'),__('Sa','calendar'),__('Su','calendar'));
3007 }
3008
3009 // Carry on with the script
3010 $name_months = array(1=>__('January','calendar'),__('February','calendar'),__('March','calendar'),__('April','calendar'),__('May','calendar'),__('June','calendar'),__('July','\
3011 calendar'),__('August','calendar'),__('September','calendar'),__('October','calendar'),__('November','calendar'),__('December','calendar'));
3012
3013 // If we don't pass arguments we want a calendar that is relevant to today
3014 if (empty($_GET['month']) || empty($_GET['yr']))
3015 {
3016 $c_year = date("Y",ctwo());
3017 $c_month = date("m",ctwo());
3018 $c_day = date("d",ctwo());
3019 }
3020
3021 // Years get funny if we exceed 3000, so we use this check
3022 if (isset($_GET['yr']))
3023 {
3024 if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0 && (int)$_GET['yr'] != 0)
3025 {
3026 // This is just plain nasty and all because of permalinks
3027 // which are no longer used, this will be cleaned up soon
3028 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')
3029 {
3030
3031 // Again nasty code to map permalinks into something
3032 // databases can understand. This will be cleaned up
3033 $c_year = $wpdb->prepare("%s",$_GET['yr']);
3034 if ($_GET['month'] == 'jan') { $t_month = 1; }
3035 else if ($_GET['month'] == 'feb') { $t_month = 2; }
3036 else if ($_GET['month'] == 'mar') { $t_month = 3; }
3037 else if ($_GET['month'] == 'apr') { $t_month = 4; }
3038 else if ($_GET['month'] == 'may') { $t_month = 5; }
3039 else if ($_GET['month'] == 'jun') { $t_month = 6; }
3040 else if ($_GET['month'] == 'jul') { $t_month = 7; }
3041 else if ($_GET['month'] == 'aug') { $t_month = 8; }
3042 else if ($_GET['month'] == 'sept') { $t_month = 9; }
3043 else if ($_GET['month'] == 'oct') { $t_month = 10; }
3044 else if ($_GET['month'] == 'nov') { $t_month = 11; }
3045 else if ($_GET['month'] == 'dec') { $t_month = 12; }
3046 $c_month = $t_month;
3047 $c_day = date("d",ctwo());
3048 }
3049 // No valid month causes the calendar to default to today
3050 else
3051 {
3052 $c_year = date("Y",ctwo());
3053 $c_month = date("m",ctwo());
3054 $c_day = date("d",ctwo());
3055 }
3056 }
3057 }
3058 // No valid year causes the calendar to default to today
3059 else
3060 {
3061 $c_year = date("Y",ctwo());
3062 $c_month = date("m",ctwo());
3063 $c_day = date("d",ctwo());
3064 }
3065
3066 // Fix the days of the week if week start is not on a monday
3067 if (get_option('start_of_week') == 0)
3068 {
3069 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
3070 $first_weekday = ($first_weekday==0?1:$first_weekday+1);
3071 }
3072 // Otherwise assume the week starts on a Monday. Anything other
3073 // than Sunday or Monday is just plain odd
3074 else
3075 {
3076 $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
3077 $first_weekday = ($first_weekday==0?7:$first_weekday);
3078 }
3079
3080 $days_in_month = date("t", mktime (0,0,0,$c_month,1,$c_year));
3081
3082 // Start the table and add the header and naviagtion
3083 $calendar_body = '';
3084 $calendar_body .= '<div style="width:200px;"><table cellspacing="1" cellpadding="0" class="calendar-table">
3085 ';
3086
3087
3088 // The header of the calendar table and the links. Note calls to link functions
3089 $calendar_body .= '<tr>
3090 <td colspan="7" class="calendar-heading" style="height:0;">
3091 <table border="0" cellpadding="0" cellspacing="0" width="100%">
3092 <tr>
3093 <td class="calendar-prev">' . prev_link($c_year,$c_month,true) . '</td>
3094 <td class="calendar-month">'.$name_months[(int)$c_month].' '.$c_year.'</td>
3095 <td class="calendar-next">' . next_link($c_year,$c_month,true) . '</td>
3096 </tr>
3097 </table>
3098 </td>
3099 </tr>
3100 ';
3101
3102 // Print the headings of the days of the week
3103 $calendar_body .= '<tr>
3104 ';
3105 for ($i=1; $i<=7; $i++)
3106 {
3107 // Colours need to be different if the starting day of the week is different
3108 if (get_option('start_of_week') == 0)
3109 {
3110 $calendar_body .= ' <td class="'.($i<7&&$i>1?'normal-day-heading':'weekend-heading').'" style="height:0;">'.$name_days[$i].'</td>
3111 ';
3112 }
3113 else
3114 {
3115 $calendar_body .= ' <td class="'.($i<6?'normal-day-heading':'weekend-heading').'" style="height:0;">'.$name_days[$i].'</td>
3116 ';
3117 }
3118 }
3119 $calendar_body .= '</tr>
3120 ';
3121 $go = FALSE;
3122 for ($i=1; $i<=$days_in_month;)
3123 {
3124 $calendar_body .= '<tr>
3125 ';
3126 for ($ii=1; $ii<=7; $ii++)
3127 {
3128 if ($ii==$first_weekday && $i==1)
3129 {
3130 $go = TRUE;
3131 }
3132 elseif ($i > $days_in_month )
3133 {
3134 $go = FALSE;
3135 }
3136 if ($go)
3137 {
3138 // Colours again, this time for the day numbers
3139 if (get_option('start_of_week') == 0)
3140 {
3141 // This bit of code is for styles believe it or not.
3142 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
3143 $no_events_class = '';
3144 if (!count($grabbed_events))
3145 {
3146 $no_events_class = ' no-events';
3147 }
3148 $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.'" style="height:0;"><span '.($ii<7&&$ii>1?'':'class="weekend"').'>'.minical_draw_events($grabbed_events,$i++).'</span></td>
3149 ';
3150 }
3151 else
3152 {
3153 $grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
3154 $no_events_class = '';
3155 if (!count($grabbed_events))
3156 {
3157 $no_events_class = ' no-events';
3158 }
3159 $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.'" style="height:0;"><span '.($ii<6?'':'class="weekend"').'>'.minical_draw_events($grabbed_events,$i++).'</span></td>
3160 ';
3161 }
3162 }
3163 else
3164 {
3165 $calendar_body .= ' <td class="day-without-date" style="height:0;">&nbsp;</td>
3166 ';
3167 }
3168 }
3169 $calendar_body .= '</tr>
3170 ';
3171 }
3172 $calendar_body .= '</table>
3173 ';
3174
3175 // A little link to yours truly
3176 $link_approved = 'false';
3177 $link_approved_results = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='show_attribution_link'");
3178 foreach ($link_approved_results as $link_approved_item)
3179 {
3180 if ($link_approved_item->config_value == 'true')
3181 {
3182 $link_approved = 'true';
3183 }
3184 }
3185 if ($link_approved == 'true') {
3186 $linkback_url = '<div class="kjo-link" style="visibility:visible !important;display:block !important;"><p>'.__('Calendar by ', 'calendar').'<a href="http://www.kieranoshea.com">Kieran O\'Shea</a></p></div>
3187 ';
3188 } else {
3189 $linkback_url = '';
3190 }
3191 $calendar_body .= $linkback_url;
3192
3193 // Closing div
3194 $calendar_body .= '</div>
3195 ';
3196 // Phew! After that bit of string building, spit it all out.
3197 // The actual printing is done by the calling function.
3198 return $calendar_body;
3199
3200 }
3201
3202 ?>
3203