Location: PHPKode > scripts > Live - Calendar > event.php
<?php
function live_calendar_event_form($mode='add_save', $event_id=false){
	global $wpdb,$users_entries;
	$data = false;
	
	if ( $event_id !== false ){
			$data = $wpdb->get_results("SELECT * FROM " . WP_LIVE_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "' LIMIT 1");
			$data = $data[0];
	}
  
  $default_city =  $wpdb->get_var($wpdb->prepare("SELECT config_value FROM " . WP_LIVE_CALENDAR_CONFIG_TABLE . " WHERE config_item='default_city'"));
  $default_link =  $wpdb->get_var($wpdb->prepare("SELECT config_value FROM " . WP_LIVE_CALENDAR_CONFIG_TABLE . " WHERE config_item='default_link'"));
?>
  <div class="wrap" >
	<?php screen_icon('edit'); ?>
	<h2><?php if($mode == 'edit_save') echo _e('Edit Events','live-calendar'); else echo _e('Add New Event','live-calendar'); ?></h2>
  <div id="pop_up_cal" style="position:absolute;margin-left:150px;visibility:hidden;background-color:white;layer-background-color:white;z-index:1;"></div>
	<form name="quoteform" id="quoteform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=live-calendar-events">
		<input type="hidden" name="action" value="<?php echo $mode; ?>">
		<input type="hidden" name="event_id" value="<?php echo stripslashes($event_id); ?>">
	
		<div id="linkadvanceddiv" class="postbox">
			<div style="float: left; width: 98%; clear: both;" class="inside">
        <table cellpadding="5" cellspacing="5">
        <tr>				
				<td><legend><?php _e('Event Title','live-calendar'); ?></legend></td>
				<td><input type="text" name="event_title" class="input" size="40" maxlength="30" value="<?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_title)); ?>" /></td>
        </tr>
        <tr>
				<td style="vertical-align:top;"><legend><?php _e('Event Description','live-calendar'); ?></legend></td>
				<td><textarea name="event_desc" class="input" rows="5" cols="50"><?php if ( !empty($data) ) echo htmlspecialchars(stripslashes($data->event_desc)); ?></textarea></td>
        </tr>
        <tr>
				<td><legend><?php _e('Event Category','live-calendar'); ?></legend></td>
				<td><select name="event_category">
					  <?php
							$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_CATEGORIES_TABLE;
	            $cats = $wpdb->get_results($sql);
              foreach($cats as $cat){
						     echo '<option value="'.stripslashes($cat->category_id).'"';
                 if (!empty($data)){
							 			if ($data->event_category == $cat->category_id){
							     			echo 'selected="selected"';
							   		}
						     } 
						  	 echo '>'.stripslashes($cat->category_name).'</option>';
						  }
             ?>
             </select>
        </td>
        </tr>
        <tr>
				<td><legend><?php _e('Event City','live-calendar'); ?></legend></td>
        <td><input type="text" name="event_city" class="input" size="40" value="<?php if ( !empty($data) ){ echo htmlspecialchars(stripslashes($data->event_city));} else {echo $default_city;} ?>" /></td>
        </tr>
        <tr>
				<td><legend><?php _e('Event Link (Optional)','live-calendar'); ?></legend></td>
        <td><input type="text" name="event_link" class="input" size="100" value="<?php if ( !empty($data) ){ echo htmlspecialchars(stripslashes($data->event_link));} else {echo $default_link;}  ?>" /></td>
        </tr>
        <tr>
				<td><legend><?php _e('Start Date','live-calendar'); ?></legend></td>
        <td>
        	<script type="text/javascript">
							var cal_begin = new CalendarPopup('pop_up_cal');
							cal_begin.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
							function unifydates() {
					  			document.forms['quoteform'].event_date_end.value = document.forms['quoteform'].event_date_begin.value;
							}
					</script>
					<input type="text" name="event_date_begin" class="input" size="12" value="<?php 
											if ( !empty($data) ) {
														echo htmlspecialchars(stripslashes($data->event_date_begin));
											}
											else{
														echo date("Y-m-d",ctwo());
											} 
									?>" /> 
									<a href="#" onClick="cal_begin.select(document.forms['quoteform'].event_date_begin,'event_date_begin_anchor','yyyy-MM-dd'); return false;" name="event_date_begin_anchor" id="event_date_begin_anchor"><?php _e('Select Date','live-calendar'); ?></a>
				</td>
        </tr>
        <tr>
				<td><legend><?php _e('End Date','live-calendar'); ?></legend></td>
        <td>
        	<script type="text/javascript">
								function check_and_print() {
										unifydates();
										var cal_end = new CalendarPopup('pop_up_cal');
                    cal_end.setWeekStartDay(<?php echo get_option('start_of_week'); ?>);
										var newDate = new Date();
										newDate.setFullYear(document.forms['quoteform'].event_date_begin.value.split('-')[0],document.forms['quoteform'].event_date_begin.value.split('-')[1]-1,document.forms['quoteform'].event_date_begin.value.split('-')[2]);
										newDate.setDate(newDate.getDate()-1);
                    cal_end.addDisabledDates(null, formatDate(newDate, "yyyy-MM-dd"));
                    cal_end.select(document.forms['quoteform'].event_date_end,'event_date_end_anchor','yyyy-MM-dd');
								}
           </script>
						<input type="text" name="event_date_end" class="input" size="12" value="<?php 
								if ( !empty($data) ) {
										echo htmlspecialchars(stripslashes($data->event_date_end));
								}
								else{
										echo date("Y-m-d",ctwo());
								}
						?>" />  
						<a href="#" onClick="check_and_print(); return false;" name="event_date_end_anchor" id="event_date_end_anchor"><?php _e('Select Date','live-calendar'); ?></a>
				</td>
        </tr>
        <tr>
				<td><legend><?php _e('Time (hh:mm)','live-calendar'); ?></legend></td>
				<td>
					<input type="text" name="event_time_begin" class="input" size=12 value="<?php 
							if ( !empty($data) ) {
									echo date("H:i",strtotime(htmlspecialchars(stripslashes($data->event_time_begin))));
							}else{
									echo date("H:i",ctwo());
							}
					?>" /> - 
					
					<input type="text" name="event_time_end" class="input" size=12 value="<?php 
							if ( !empty($data) ) {
											echo date("H:i",strtotime(htmlspecialchars(stripslashes($data->event_time_end))));
							}else{
									echo date("H:i",ctwo());
							}
					?>" />
				<?php _e('Optional, set blank if not required.','live-calendar'); ?> <?php _e('Current time difference from GMT is ','live-calendar'); echo get_option('gmt_offset'); _e(' hour(s)','live-calendar'); ?>.
				</td>
        </tr>
        </table>
			</div>
			<div style="clear:both; height:1px;">&nbsp;</div>
		</div>
    <input type="submit" name="save" class="button bold" value="<?php _e('Save','live-calendar'); ?> &raquo;" />
	</form>
<?php
}

function check_eventform(){
	global $wpdb;
	$title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
				$city = !empty($_REQUEST['event_city']) ? $_REQUEST['event_city'] : '';
				$desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
				$date_begin = !empty($_REQUEST['event_date_begin']) ? $_REQUEST['event_date_begin'] : '';
				$date_end = !empty($_REQUEST['event_date_end']) ? $_REQUEST['event_date_end'] : '';
				$time_begin = !empty($_REQUEST['event_time_begin']) ? $_REQUEST['event_time_begin'] : '';
				$time_end = !empty($_REQUEST['event_time_end']) ? $_REQUEST['event_time_end'] : '';
				$category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
        $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';
        
  			$event_title_width =  $wpdb->get_var($wpdb->prepare("SELECT config_value FROM " . WP_LIVE_CALENDAR_CONFIG_TABLE . " WHERE config_item='event_title_width'"));

				$date_format_one = '/^([0-9]{4})-([0][1-9])-([0-3][0-9])$/';
        $date_format_two = '/^([0-9]{4})-([1][0-2])-([0-3][0-9])$/';
				
				if ((preg_match($date_format_one,$date_begin) || preg_match($date_format_two,$date_begin)) && (preg_match($date_format_one,$date_end) || preg_match($date_format_two,$date_end))){
           
            $begin_split = split('-',$date_begin);
	    			$begin_y = $begin_split[0]; 
	    			$begin_m = $begin_split[1];
	    			$begin_d = $begin_split[2];
            $end_split = split('-',$date_end);
	    			$end_y = $end_split[0];
	    			$end_m = $end_split[1];
	    			$end_d = $end_split[2];
            if (checkdate($begin_m,$begin_d,$begin_y) && checkdate($end_m,$end_d,$end_y)){
	       				if (strtotime($date_end) >= strtotime($date_begin)){
		   							$start_date_ok = 1;
		   							$end_date_ok = 1;
		 						}
	       				else{
?>
		   								<div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('Your event end date must be either after or the same as your event begin date','live-calendar'); ?></p></div>
<?php
		 						}
	     			}else{
?>
      					<div class="error"><p><strong><?php _e('Error','live-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.','live-calendar'); ?></p></div>
<?php
	    			}
				}else{
?>
  					<div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('Both start and end dates must be entered and be in the format YYYY-MM-DD','live-calendar'); ?></p></div>
<?php
				}
        $time_format_one = '/^([0-1][0-9]):([0-5][0-9])$/';
				$time_format_two = '/^([2][0-3]):([0-5][0-9])$/';
				
        if ( ( ( preg_match($time_format_one,$time_begin) || preg_match($time_format_two,$time_begin) ) && ( preg_match($time_format_one,$time_end) || preg_match($time_format_two,$time_end) ) ) || $time == ''){
            $time_ok = 1;
        }else{
?>
            <div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('The time field must either be blank or be entered in the format hh:mm','live-calendar'); ?></p></div>
<?php
	  		}                                                     
				if (preg_match('/^(http)(s?)(:)\/\//',$linky) || $linky == ''){
	    				$url_ok = 1;
				}else{
?>
            <div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('The URL entered must either be prefixed with http:// or be completely blank','live-calendar'); ?></p></div>
<?php
	  		}
	  		if (preg_match('/^.{1,30}$/',$city)){
	    			$city_ok =1;
	  		}else{
?>
            <div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('The event city must not null','live-calendar'); ?></p></div>
<?php
				}
				if (preg_match('/^.{1,'.$event_title_width.'}$/',$title)){
	    			$title_ok =1;
	  		}else{
?>
            <div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('The event title is too long','live-calendar'); ?></p></div>
<?php
				}
				
				if ($start_date_ok == 1 && $end_date_ok == 1 && $time_ok == 1 && $url_ok == 1 && $title_ok == 1 && $city_ok == 1){
				  
				  	return true;
				
				}else{
						return false;
				}	
}
function live_calendar_event_edit(){
    global $wpdb;
		$edit = $create = $save = $delete = false;
		$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
		$event_id = !empty($_REQUEST['event_id']) ? $_REQUEST['event_id'] : '';
		$title = !empty($_REQUEST['event_title']) ? $_REQUEST['event_title'] : '';
		$city = !empty($_REQUEST['event_city']) ? $_REQUEST['event_city'] : '';
	  $desc = !empty($_REQUEST['event_desc']) ? $_REQUEST['event_desc'] : '';
	  $date_begin = !empty($_REQUEST['event_date_begin']) ? $_REQUEST['event_date_begin'] : '';
	  $date_end = !empty($_REQUEST['event_date_end']) ? $_REQUEST['event_date_end'] : '';
	  $time_begin = !empty($_REQUEST['event_time_begin']) ? $_REQUEST['event_time_begin'] : '';
	  $time_end = !empty($_REQUEST['event_time_end']) ? $_REQUEST['event_time_end'] : '';
	  $category = !empty($_REQUEST['event_category']) ? $_REQUEST['event_category'] : '';
    $linky = !empty($_REQUEST['event_link']) ? $_REQUEST['event_link'] : '';

		if ( $action == 'add_save' ){
				if(check_eventform()){
						if ($time_begin == '' || $time_end == ''){
								$time_begin = '00:00';
								$time_end = '00:00';
	      		}
	      			      		
	    			$sql = "INSERT INTO " . WP_LIVE_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title). "',event_city='" . mysql_escape_string($city). "',event_desc='" .mysql_escape_string($desc). "', event_date_begin='" . mysql_escape_string($date_begin). "', event_date_end='" . mysql_escape_string($date_end). "', event_time_begin='" . mysql_escape_string($time_begin) . "', event_time_end='" . mysql_escape_string($time_end) . "', event_category=".mysql_escape_string($category).", event_link='".mysql_escape_string($linky)."'";	     
	    			$wpdb->get_results($sql);	
	    			$sql = "SELECT event_id FROM " . WP_LIVE_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'". " AND event_desc='" . mysql_escape_string($desc) . "' AND event_date_begin='" . mysql_escape_string($date_begin) . "' AND event_date_end='" . mysql_escape_string($date_end) . "' LIMIT 1";
	    			$result = $wpdb->get_results($sql);
	
	    			if ( empty($result) || empty($result[0]->event_id) ){
?>
								<div class="error"><p><strong><?php _e('Error','live-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.','live-calendar'); ?></p></div>
<?php
						}else{
?>
								<div class="updated"><p><?php _e('Event added. It will now show in your calendar.','live-calendar'); ?></p></div>
<?php
 									live_calendar_event_form('edit_save', $result[0]->event_id);
	      		}
				}
		}elseif ( $action == 'edit_save' ){	
				if ( empty($event_id) ){
?>
						<div class="error"><p><strong><?php _e('Failure','live-calendar'); ?>:</strong> <?php _e("You can't update an event if you haven't submitted an event id",'live-calendar'); ?></p></div>
<?php		
				}else{
						if(check_eventform()){
			        	$sql = "UPDATE " . WP_LIVE_CALENDAR_TABLE . " SET event_title='" . mysql_escape_string($title)
		             . "', event_city='" . mysql_escape_string($city) . "', event_desc='" . mysql_escape_string($desc) . "', event_date_begin='" . mysql_escape_string($date_begin) 
                             . "', event_date_end='" . mysql_escape_string($date_end) . "', event_time_begin='" . mysql_escape_string($time_begin) . "', event_time_end='" . mysql_escape_string($time_end) . "', event_category=".mysql_escape_string($category).", event_link='".mysql_escape_string($linky)."' WHERE event_id='" . mysql_escape_string($event_id) . "'";
		             
			        	$wpdb->get_results($sql);
		          
			        	$sql = "SELECT event_id FROM " . WP_LIVE_CALENDAR_TABLE . " WHERE event_title='" . mysql_escape_string($title) . "'"
		             . " AND event_desc='" . mysql_escape_string($desc) . "' AND event_date_begin='" . mysql_escape_string($date_begin) . "' AND event_date_end='" . mysql_escape_string($date_end) . "' LIMIT 1";
			        	$result = $wpdb->get_results($sql);
		
								if ( empty($result)){
?>
									<div class="error"><p><strong><?php _e('Failure','live-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.','live-calendar'); ?></p></div>
<?php
								}else{
?>
									<div class="updated"><p><?php _e('Event updated successfully','live-calendar'); ?></p></div>
<?php               live_calendar_event_form('edit_save', $event_id);
								}
						}
				}
	  }elseif ( $action == 'delete' ){
				if ( empty($event_id) ){
?>
		<div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e("You can't delete an event if you haven't submitted an event id",'live-calendar'); ?></p></div>
<?php			
				}else{
		        $sql = "DELETE FROM " . WP_LIVE_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
		        $wpdb->get_results($sql);
		        
		        $sql = "SELECT event_id FROM " . WP_LIVE_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "'";
		        $result = $wpdb->get_results($sql);
		        
		        if (empty($result)){
 ?>
		        		<div class="updated"><p><?php _e('Event deleted successfully','live-calendar'); ?></p></div>
<?php
		        }else{
?>
								<div class="error"><p><strong><?php _e('Error','live-calendar'); ?>:</strong> <?php _e('Despite issuing a request to delete, the event still remains in the database. Please investigate.','live-calendar'); ?></p></div>
<?php

						}		
				}
		}			
}

function live_calendar_event_manage(){
	if(!empty($_REQUEST['action'])){
			if($_REQUEST['action'] == 'add'){
					live_calendar_event_form();
			}else if($_REQUEST['action'] == 'add_save'){
					live_calendar_event_edit();	
					live_calendar_event_list_form();
			}else if($_REQUEST['action'] == 'edit'){
					if(isset($_GET['event_id'])){
			 				live_calendar_event_form('edit_save', $_GET['event_id']);
			 		}			
			}else if($_REQUEST['action'] == 'edit_save'){
			    live_calendar_event_edit();
			    live_calendar_event_list_form();
			}else if($_REQUEST['action'] == 'delete'){
			    live_calendar_event_edit();
			    live_calendar_event_list_form();
			}
			/*
		if($_POST['action'] == 'edit_save'){
			live_calendar_event_edit();
		}else if($_GET['events'] == 'add'){
			live_calendar_event_form();
		}else{
			if($_GET['action'] == 'edit'){
					if(isset($_GET['event_id'])){
			 				live_calendar_event_form('edit_save', $_GET['event_id']);
			 		}
			}else{
					if($_GET['action'] == 'delete' ){
							live_calendar_event_edit();
		  		}else if($_POST['action'] == 'add_save' ){
							live_calendar_event_edit();
	    		}
	    }
	 	}*/
	}else{
	 live_calendar_event_list_form();
	}
}
function live_calendar_event_list_form(){
	global $wpdb;
?>
<div class="wrap">
<?php screen_icon('edit'); ?>
<h2><?php echo _e('Events','live-calendar'); ?> <a href="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=live-calendar-events&action=add" class="button add-new-h2"><?php echo _e('Add New','live-calendar'); ?></a> </h2>
<form name="searchform" id="searchform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=live-calendar-events">
<p class="search-box">
	<input type="text" id="event_search_input" name="event_search_input" value="<?php if(isset($_POST['search'])) echo $_POST['event_search_input']; ?>" />
	<input type="hidden" name="search" value="1">
	<input type="submit" value="<?php echo _e('Search','live-calendar'); ?>" class="button" />
</p>
</form>
<form name="filterform" id="filterform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=live-calendar-events">
<p><b>
<?php
_e('Category : ','live-calendar'); 
?></b>
<select name="f_event_category">
<?php
		$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_CATEGORIES_TABLE;
	  $f_cats = $wpdb->get_results($sql);
	  if(isset($_POST['filter'])){
	  		echo '<option value="all" ';
	  		if($_POST['f_event_category'] == 'all') echo ' select="selected" ';
	  		echo ' >'.__('All','live-calendar').'</option>';
	  }else{
	  		echo '<option value="all" select="selected" >'.__('All','live-calendar').'</option>';
	  }
    foreach($f_cats as $f_cat){
				echo '<option value="'.stripslashes($f_cat->category_id).'"';
				if ($f_cat->category_id == $_POST['f_event_category']){
						echo 'selected="selected"';
				}
				echo '>'.stripslashes($f_cat->category_name).'</option>';
		}
 ?>
</select> &nbsp;
<b><?php _e('Start Date : ','live-calendar'); ?></b>
<input type="text" name="f_event_date_begin" class="input" size="12" value="<?php 

	if(isset($_POST['filter'])){
		echo $_POST['f_event_date_begin'];		
	}else{
		echo date("Y-m-d",(ctwo()- 3600*24*365));
	}
?>" />  &nbsp;
<b><?php _e('End Date : ','live-calendar'); ?></b>
<input type="text" name="f_event_date_end" class="input" size="12" value="<?php 
	if(isset($_POST['filter'])){
		echo $_POST['f_event_date_end'];
	}else{
		echo date("Y-m-d",ctwo());
	}
?>" /> &nbsp;
<input type="hidden" name="filter" value="1">
<input type="submit" id="post-query-submit" value="<?php _e('Filter','live-calendar'); ?>" class="button-secondary" />
</p>
</form>
<?php
	$page_no = $_GET['page_no'];
	if($page_no == 0) $page_no=1;
	if(isset($_GET['f_event_date_begin']))  $filter['f_event_date_begin'] = $_GET['f_event_date_begin'];
	if(isset($_GET['f_event_date_end']))  $filter['f_event_date_end'] = $_GET['f_event_date_end'];
	if(isset($_GET['f_event_category']))  $filter['f_event_category'] = $_GET['f_event_category'];
	if(isset($_GET['event_search_input']))  $filter['event_search_input'] = $_GET['event_search_input'];
				
if(isset($_POST['search']) || isset($filter['event_search_input'])){
	if(!empty($_POST['event_search_input'])){
			$key = "event_title like \"%".mysql_escape_string($_POST['event_search_input'])."%\" OR event_desc like \"%".mysql_escape_string($_POST['event_search_input'])."%\" ";
			$filter['event_search_input'] = $_POST['event_search_input'];
	}else if(!empty($filter['event_search_input'])){
			$key = "event_title like \"%".mysql_escape_string($filter['event_search_input'])."%\" OR event_desc like \"%".mysql_escape_string($filter['event_search_input'])."%\" ";
	}else{
		$key = " 1=1 ";
	}
	$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_TABLE . " where ".$key." ORDER BY event_date_begin DESC";
	
	
}else if(isset($_POST['filter']) || (isset($filter['f_event_date_begin']) && isset($filter['f_event_date_end']))){
	
	if(isset($_POST['f_event_category']) || isset($filter['f_event_category'])){
		if(isset($_POST['f_event_category'])){
				$f_cat = $_POST['f_event_category'];
		}else if(isset($filter['f_event_category'])){
			$f_cat = $filter['f_event_category'];
		}
	}
	if($f_cat == 'all' ){
		$f_cat = " 1=1 ";
		$filter['f_event_category'] = 'all';
	}else{
		$filter['f_event_category'] = $f_cat;
		$f_cat = " event_category = $f_cat ";	
	}
	
	if(isset($_POST['f_event_date_begin']) && isset($_POST['f_event_date_end'])){
			$f_event_date_begin = date($_POST['f_event_date_begin']);
			$f_event_date_end = date($_POST['f_event_date_end']);
			$filter['f_event_date_begin'] = $f_event_date_begin;
			$filter['f_event_date_end'] = $f_event_date_end;
	}else if(isset($filter['f_event_date_begin']) && isset($filter['f_event_date_end'])){
			$f_event_date_begin = date($filter['f_event_date_begin']);
			$f_event_date_end = date($filter['f_event_date_end']);
	}
	
	if(!empty($f_event_date_begin) && !empty($f_event_date_end)){
		$f_date = ' event_date_begin >= "'.mysql_escape_string($f_event_date_begin).'" AND event_date_end <= "'.mysql_escape_string($f_event_date_end). '"';
	}else{
		$f_date = " 1=1 ";
	}
	
	$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_TABLE . " where ".mysql_escape_string($f_cat). " AND ".$f_date." ORDER BY event_date_begin DESC, event_time_begin DESC, event_id DESC";
}else{
	$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_TABLE . " ORDER BY event_date_begin DESC, event_time_begin DESC, event_id DESC";
}
	$events = $wpdb->get_results($sql);	
	live_calendar_event_list($events, $page_no, $filter);
?>
</div>
<?php
}

function live_calendar_event_list($events = '', $page_no = 1, $filter = ''){
	global $wpdb;	
	if(empty($events)){
			$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_TABLE . " ORDER BY event_date_begin DESC, event_time_begin DESC, event_id DESC";	
			$events = $wpdb->get_results($sql);	
	}
	if ( !empty($events) ){
		$total	=	count($events);
		$per_page = 30;
		$show_flag = 0;
?>
<table class="widefat page fixed" width="100%" cellpadding="0" cellspacing="0">
	<thead>
		<tr>
				<th class="manage-column" scope="col"><?php _e('ID','live-calendar') ?></th>
				<th class="manage-column" scope="col" colspan="2"><?php _e('Title','live-calendar') ?></th>
				<th class="manage-column" scope="col"><?php _e('City','live-calendar') ?></th>
				<th class="manage-column" scope="col"><?php _e('Start Date','live-calendar') ?></th>
				<th class="manage-column" scope="col"><?php _e('End Date','live-calendar') ?></th>
		    <th class="manage-column" scope="col" colspan="2"><?php _e('Start ','live-calendar') ?> - <?php _e('End Time','live-calendar') ?></th>
		    <th class="manage-column" scope="col"><?php _e('Category','live-calendar') ?></th>
				<th class="manage-column" scope="col"><?php _e('Edit','live-calendar') ?></th>
				<th class="manage-column" scope="col"><?php _e('Delete','live-calendar') ?></th>
		</tr>
	</thead>
<?php
	$class = '';
	foreach ( $events as $event ){
			$class = ($class == 'alternate') ? '' : 'alternate';
			$show_flag = $show_flag + 1;
			if($show_flag > (($page_no-1)*$per_page) and $show_flag <= ($page_no*$per_page)){
?>
					<tr class="<?php echo $class; ?>">
						<td scope="row"><?php echo stripslashes($event->event_id); ?></th>
						<td colspan="2"><?php echo stripslashes($event->event_title); ?></td>
						<td><?php echo stripslashes($event->event_city); ?></td>
						<td><?php echo stripslashes($event->event_date_begin); ?></td>
						<td><?php echo stripslashes($event->event_date_end); ?></td>
						<td colspan="2">
<?php 
							echo date("H:i",strtotime(htmlspecialchars(stripslashes($event->event_time_begin))));
?> - <?php 
							echo date("H:i",strtotime(htmlspecialchars(stripslashes($event->event_time_end))));
?>
			 			</td>
<?php
						$sql = "SELECT * FROM " . WP_LIVE_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($event->event_category);
      			$this_cat = $wpdb->get_row($sql);
?>
						<td style="background-color:<?php echo stripslashes($this_cat->category_colour);?>;"><?php echo stripslashes($this_cat->category_name); 
?>
						</td>
<?php 
						unset($this_cat); 
?>
						<td><a href="<?php bloginfo('wpurl') ?>/wp-admin/admin.php?page=live-calendar-events&amp;action=edit&amp;event_id=<?php echo stripslashes($event->event_id);?>" class='edit'><?php echo __('Edit','live-calendar'); ?></a>
						</td>
						<td><a href="<?php bloginfo('wpurl') ?>/wp-admin/admin.php?page=live-calendar-events&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?','live-calendar'); ?>')"><?php echo __('Delete','live-calendar'); ?></a>
						</td>
					</tr>
<?php
 			}
 			}
 			if(floor($total/$per_page)!=0){
 					if(!empty($filter['f_event_date_begin']) && !empty($filter['f_event_date_end'])){
 						  $filterstring = '&f_event_date_begin='.$filter['f_event_date_begin'].'&f_event_date_end='.$filter['f_event_date_end'];
 					}
					if(!empty($filter['f_event_category'])){						
						 $filterstring .= '&f_event_category='.$filter['f_event_category'];
					}
					if(!empty($filter['event_search_input'])){
						 $filterstring .= '&event_search_input='.$filter['event_search_input'];
					}
					
?>
		<tfoot>
			<tr>
				<td colspan="2"></td>
				<td colspan="1"><a href="<?php bloginfo('wpurl') ?>/wp-admin/admin.php?page=live-calendar-events&amp;page_no=<?php echo (($page_no-1)==0) ? 1 : ($page_no-1); echo $filterstring; ?>" > <<
<?php
				_e('PrePage','live-calendar');
?> 			</a></td>
				<td colspan="3"><p align="center"><?php _e('Current:','live-calendar'); echo $page_no; ?>/<?php echo ceil($total/$per_page); ?></p></td>
				<td colspan="1"><a href="<?php bloginfo('wpurl') ?>/wp-admin/admin.php?page=live-calendar-events&amp;page_no=
<?php 
				if($page_no == ceil($total/$per_page)){
	  				echo $page_no;
	  		}else{ 
	  				echo $page_no+1 ;
	  		}
	  		echo $filterstring;
?>"    >
<?php
			 _e('NextPage','live-calendar');
?> 
			>></a></td>
			<td colspan="2"></td>
		<tr>
	</tfoot>
<?php
	}
?>
</table>
<?php
			}
	}

?>
Return current item: Live - Calendar