PluginProbe
Calendar / 1.3.2
Calendar v1.3.2
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.2, at calendar.php

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