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

calendar.php in Calendar 1.3.3, at calendar.php

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