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

calendar.php in Calendar 1.3, at calendar.php

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