Location: PHPKode > projects > Myphpim > MyPhPim-01.05/calendar.php3
<?php

//****************************************************************************************
//                Copyright (C) 2000 Koen de Boeve
//
//        This program is free software; you can redistribute it and/or
//        modify it under the terms of the GNU General Public License
//        as published by the Free Software Foundation; either version 2
//        of the License, or (at your option) any later version.
//
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
//
//        You should have received a copy of the GNU General Public License
//        along with this program; if not, write to the Free Software
//        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//        Version : MyPhPim-01.05
//        Author : Koen de Boeve
//        Contact: hide@address.com
//**************************************************************************************** 

include "conf/global_conf.inc";

session_start();

$html = new HtmlCl;
$db = new DB;
$cal = new CalendarCl;

if ( $menu ) {
	switch ( $menu ) {
		case "main":
				echo "<html>\n";
                                echo "<head>\n";
                                echo "<style type=\"text/css\">\n";
                                echo "<!--\n";
                                echo "body { margin: 0 }\n";
                                echo "A{text-decoration:none}\n";
                                echo "-->\n";
                                echo "</style>\n";
				echo "</head>\n";
				echo "<body bgcolor=darkseagreen text=blue vlink=blue alink=blue link=blue marginheight=0>\n";
				echo "<table border=0 width=100%><tr><td align=left><font size=-1><a href=calendar.php3?menu=create&date=$date target=bodyframe>| New | </a></font></td><td align=right><font size=-1><a href=calendar.php3?menu=sharing target=bodyframe>| Sharing | </a></font></td></tr></table>\n";
				echo "</body>\n";
				echo "</html>\n";
				break;;
		case "list":
				$form = new FormCl;
				$user_id = $db->UserId ( $user );
				if ( !$date ) {
					if ( !$month_id || !$year) {
						$day_id = date ( 'd' );
						$month_id = date ( 'm' );
						$year = date ( 'Y' );
					}
				} else {
					$parts = $cal->Cal_Splitdate ( $date );
					$day_id = $parts[3];
					$month_id = $parts[2];
					$year = $parts[1];	
				}
				if ( !$day_id ) {
					$date = date ( 'Y-m-d' );
				} else {
					$date = $year . "-" . $month_id . "-" . $day_id;
				}
				
				$next = 0;
				// Lets see if we enabled public sharing for calendar
				
				$query = "SELECT sharetype FROM calendar_subscribed WHERE user_id=$user_id AND sharetype='public'";
				$result = $db->query ( $query );
				$numrow = $db->numrows ( $result );
				if ( $numrow != 0 ) {
					$query = "SELECT cal_id FROM calendar_shares WHERE sharetype='public'";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$calid[$i] = $db->result ( $result , $i, "cal_id" );
					}
					$next = $i;
				}
		
				// From which users do we wish to see Calendar Events?

				$query = "SELECT sharedfrom FROM calendar_subscribed WHERE user_id=$user_id AND sharetype='user'";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
					$sub_query = "SELECT cal_id FROM calendar WHERE owner_id=$sharedfrom";
					$sub_result = $db->query ( $sub_query );
					$numsub_rows = $db->numrows ( $sub_result );
					for ( $j = 0; $j < $numsub_rows; $j++ ) {
						$cal_id = $db->result ( $sub_result, $j, "cal_id" );
						$sub_sub_query = "SELECT shareusers FROM calendar_shares WHERE cal_id=$cal_id";
						$sub_sub_result = $db->query ( $sub_sub_query );
						$numsub_sub_rows = $db->numrows ( $sub_sub_result );
						for ( $k = 0; $k < $numsub_sub_rows; $k++ ) {
							$shareusers = $db->result ( $sub_sub_result, $k, "shareusers" );
							if ( $shareusers == $user_id ) {
								$calid[$next] = $cal_id;
								$next++;
							}
						}
					}
				}				
				

				// To which Groups do we belong?
				$query = "SELECT sharedfrom FROM calendar_subscribed WHERE user_id=$user_id AND sharetype='group'";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
					$sub_query = "SELECT cal_id FROM calendar_shares WHERE sharegroups=$sharedfrom";	
					$sub_result = $db->query ( $sub_query );
					$numsub_rows = $db->numrows ( $sub_result );
					for ( $j = 0; $j < $numsub_rows; $j++ ) {
						$calid[$next] = $db->result ( $sub_result, $j, "cal_id" );
						$next++;
					}
				}
				
				// Finally see if we have events created by ourself
				$query = "SELECT cal_id FROM calendar WHERE owner_id=$user_id";
				$result = $db->query ( $query ) ;
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$calid[$next] = $db->result ( $result, $i, "cal_id" );
					$next++;
				}
				$numcalids = count ( $calid );
				if ( $numcalids > 0 ) {
					$calid = $cal->Cal_SortByStartTime ( $calid );
				}
				if ( $showwk == y ) {
					session_register ( 'showweek' );
					$showweek = "y";
					//setcookie ( "showweek", "y", "");
					header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=list&date=$date" ); 
					
				} elseif ( $showd == y ) {
					session_register ( 'showweek' );
					$showweek = "n";
					//setcookie ( "showweek", "n", "");
					header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=list&date=$date" ); 
				} else {
					;
				}
				if ( $showweek == "y" ) {
					$weekview = TRUE;
				} else {
					$weekview = FALSE;
				}
				if ( $keepday == y ) {
					session_register ( 'olddate' );
					$olddate = $date;
					//setcookie ( "olddate", $date, "");
				} else {
					$date = $olddate;
				}
				$html->htmlheader ( "",0 );	
				$counter = 0;
				echo "<br><table border=0 width=100% cellspacing=0 cellpadding=0><tr><td width=15% align=center valign=top>\n";
				$days = $cal->Cal_ReturnQuantDaysInMonth($month_id,$year);
			 	$tooday = date ( 'Y-m-d' );			
				$str_today = $cal->Cal_ShowDateAsString ( $tooday );
				$cal->Cal_Showsmallyear ( $year, $month_id );	
				$cal->Cal_Showmsmallmonth ( $month_id , $year, $days );	
				echo "<font size=-1>Today is: <br><b> <a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$tooday&keepday=y>$str_today</a></b></font>";
				echo "</td><td valign=top>\n";
				$numcalids = count ( $calid );
				if ( $monthview ) {
					echo "<table width=100% bgcolor=lightgrey>";
					for ( $i = 1; $i <= $days; $i++ ) {
						if ( $i < 10 ) {
							$day = "0" . $i;
						} else {
							$day = $i;
						}
						$date = $year . "-" . $month_id . "-" . $day;
						if ( $i == 1 || $i == 6 || $i == 11 || $i == 16 || $i == 21 || $i == 26 || $i == 31) {
							unset ( $entries );
							echo "<tr><td width=14% height=90 valign=top><font size=-1><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$date&keepday=y>$i</a></font><font size=-2><br></font>";
							$j = 0;
							for ( $k = 0; $k < $numcalids; $k++ ) {
                                        			$repeat = $cal->Cal_ReturnRepeatdays ( $calid[$k], $date );
                                        			if ( $repeat ) {
                                                			$entries[$j] = $cal->Cal_EventSummary ( $calid[$k] );
                                                			$j++;
                                        			}
                                			} 
							$cal->Cal_PrintByMonth ( $entries );
							echo "</td>";
						} elseif ( $i == 5 || $i == 10 || $i == 15 || $i == 20 || $i == 25 || $i == $days ) {
							unset ( $entries );
							echo "<td width=14% height=90 valign=top><font size=-1><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$date&keepday=y>$i</a></font><font size=-2><br></font>";

							$j = 0;
                                                        for ( $k = 0; $k < $numcalids; $k++ ) {
                                                               $repeat = $cal->Cal_ReturnRepeatdays ( $calid[$k], $date );
                                                                if ( $repeat ) {
                                                                        $entries[$j] = $cal->Cal_EventSummary ( $calid[$k] );
                                                                        $j++;
                                                                }
                                                        } 
                                                        $cal->Cal_PrintByMonth ( $entries );
							echo "</td></tr>";
						} else {
							unset ( $entries );
							echo "<td width=14% height=90 valign=top><font size=-1><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$date&keepday=y>$i</a></font><font size=-2><br></font>";
							$j = 0;
                                                        for ( $k = 0; $k < $numcalids; $k++ ) {
                                                                $repeat = $cal->Cal_ReturnRepeatdays ( $calid[$k], $date );
                                                                if ( $repeat ) {
                                                                        $entries[$j] = $cal->Cal_EventSummary ( $calid[$k] );
                                                                        $j++;
                                                                }
                                                        } 
                                                        $cal->Cal_PrintByMonth ( $entries );
							echo "</td>";
						}
					}
					echo "</table>";
				} else if ( $weekview ) {
					$cal->Cal_Showsmallweek ( $date, $calid );	
					
				} else {
					$cal->Cal_ShowDayview ( $user, $date );
					for ( $i = 0; $i < $numcalids; $i++ ) {
						$repeat = $cal->Cal_ReturnRepeatdays ( $calid[$i], $date );
						if ( $repeat ) {
							$entries[$counter] = $cal->Cal_EventSummary ( $calid[$i] );
							$counter++;
						}
					}
					$cal->Cal_PrintDayView ( $entries );
				}
				echo "</td></tr></table>";
				$html->htmlfooter();
				break;;
		case "create" :
				if ( !$date ) {
					$date = date ( 'Y-m-d' );
				}
				$datearr = $cal->Cal_Splitdate ( $date );	
				$day = $datearr[3];
				$month = $datearr[2];
				$year = $datearr[1];
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );
				$form = new FormCl;
				$html->htmlheader ( "Create New Event For $user", 0 );
				echo "<table border=0>\n";
				$form->init ( "calendar.php3", "get", "calendar" );
				echo "<tr><td valign=top><font size=-1>\n";
				$sharetypes = array ( "private", "public" );
				$select[0] = "distribute";
				$form->radio ( "VER", "sharedtype", $sharetypes, NULL );
				echo "<br>";
				$form->radio ( "VER", "sharedtype", $select, NULL ); 
					$mainquery = "SELECT group_id, groupname FROM groups WHERE visible='public' ";
					$query = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$group_id = $db->result ( $result, $i, "group_id" );
						$mainquery = $mainquery . "OR group_id=$group_id ";
					}
					$mainquery = $mainquery . " ORDER BY groupname";
					$result = $db->query ( $mainquery );
					$numrows = $db->numrows ( $result );
					for  ( $i = 0; $i < $numrows; $i++ ) {
						$groupid[$i] = $db->result ( $result, $i, "group_id" );
						$groupnames[$i] = $db->result ( $result, $i, "groupname" );
					}
					echo "</font></td></tr><tr><td>&nbsp</td><td valign=bottom><font size=-1><b>groups: </b><br>\n";
					$form->multiselect ( "groupids", $groupid, $groupnames, "NO-ONE", 2);
						
				echo "<br><b>Users:</b><br>\n";
				$query = "SELECT user_id, user FROM users where user!='$user' ORDER BY user";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$userid[$i] = $db->result ( $result, $i, "user_id" );
					$username[$i] = $db->result ( $result, $i, "user" );
				}
				$form->multiselect ( "userids", $userid, $username, "NO-ONE", 2 );
				echo "</font></td></tr>\n";
	

				// -->>>> Add Code Here <<<<--				
				echo "<tr><td><font size=-1>Title:</font></td><td>\n";
				$form->text('title','',80,60,0);
				echo "</td></tr>";
				echo "<tr><td><font size=-1>Note:</font></td><td>\n";
				$form->textarea('note','',60,5 );
				echo "</td></tr>";
				echo "<tr><td><font size=-1>Date:</font></td><td>\n";
				$form->text('day',$day,2,2,0);
				$form->text('month',$month,2,2,0);
				$form->text('year',$year,4,4,0);
				echo "</td></tr>";
				echo "<tr><td><font size=-1>No Specific Time</font></td><td><font size=-1>";
				$form->checkbox( 'nospectime', 1, 'Y');
				echo "</font></td></tr>";
				echo "<tr><td><font size=-1>Starttime:</font></td><td><font size=-1>\n";
				$hour_arr = array ( '','00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23' );
				$min_arr = array ( '','00','15','30','45' );
				$form->simpleselect ( 'starthour', $hour_arr, $hour_arr, '' );
				$form->simpleselect ( 'startminutes', $min_arr, $min_arr, '' );
				echo "</font></td></tr>";
				echo "<tr><td><font size=-1>Stoptime:</font></td><td><font size=-1>\n";
				$form->simpleselect ( 'stophour', $hour_arr, $hour_arr, '' );
				$form->simpleselect ( 'stopminutes', $min_arr, $min_arr, '' );
				echo "</font></td></tr><tr><td><font size=-1>";
				$vars = array ( 'Y', 'N' );
				echo "Repeats: </font></td><td><font size=-1>";
				$form->radio( 'hor', 'repeats', $vars, 'N' );
				echo "</font></td></tr><tr><td><font size=-1>";
				$repeattype = array ( "", "DAY", "WEEK", "MONTH", "YEAR" );
				echo "Repeat Every";
				echo "</font></td><td><font size=-1>";
				$form->text ( 'interv', $interv, 2, 2, 0 );
				$form->simpleselect ( 'repeattype', $repeattype, $repeattype, '' );
				echo "</font></td></tr><tr><td><font size=-1>";
				echo "Repeat until: </font></td><td><font size=-1>";
				$form->text('untilday',$untilday,2,2,0);
				$form->text('untilmonth',$untilmonth,2,2,0);
				$form->text('untilyear',$untilyear,4,4,0);
				echo "</font></td></tr><tr><td><font size=-1>";
				echo "Create this Event <br>For another User:</font></td><td><font size=-1>";
                                $form->checkbox ( "otheruser", 0, 0 );
				echo "</font></td></tr><tr><td><font size=-1>Users:</font></td><td><font size=-1>";
				$form->multiselect ( "foruser", $userid, $username, "NO-ONE", 2 );
				echo "</font></td></tr><tr><td><font size=-1>groups:</font></td><td><font size=-1>";
				$form->multiselect ( "forgroup", $groupid, $groupnames, "NO-ONE", 2);
				echo "</font></td></tr><tr><td><font size=-1>comments:</font></td><td><font size=-1>";
				$form->textarea('comments','',60,5 );
				echo "</font></td></tr><tr><td colspan=2 align=center>";
				$form->submit ( "addevent", "Save" );
				echo "</font></td></tr>\n";
				$form->close();
				echo "</table>\n";
				$html->htmlfooter();
				break;;
		case "detail" :
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result ,0 , "user_id" );
				$query = "SELECT sharetype FROM calendar_shares WHERE cal_id=$cal_id";
				$result = $db->query ( $query ); 
				$numrows = $db->numrows ( $result );
				if ( $numrows != 0 ) {
					if (!$sharedtype ) {
						$sharedtype = $db->result ( $result, 0, "sharetype" );
					}
					$query = "SELECT sharegroups FROM calendar_shares WHERE cal_id=$cal_id AND sharegroups!=0";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$sharedgroup = $db->result ( $result, $i, "sharegroups" );
						$sub_query = "SELECT groupname FROM groups WHERE group_id=$sharedgroup";
						$sub_result = $db->query ( $sub_query );
						$sharedgroupnames[$i] = $db->result ( $sub_result, 0, "groupname" );
					} 
					$query = "SELECT shareusers FROM calendar_shares WHERE cal_id=$cal_id AND shareusers!=0";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$shareduser = $db->result ( $result, $i, "shareusers" );
						$sub_query = "SELECT user FROM users WHERE user_id=$shareduser";
						$sub_result = $db->query ( $sub_query ) ;
						$sharedusernames[$i] = $db->result ( $sub_result, 0, "user" );
					}
				} else {
					$sharedusernames[0]="fake";
					$sharedgroupnames[0]="alsofake";
				}
				$form = new FormCl;
				$html->htmlheader ( "Event For $user", 0 );
				echo "<table border=0>\n";
				$form->init ( "calendar.php3", "get", "calendar" );
				echo "<tr><td colspan=2 align=left><font size=-1>\n";
				$sharetypes = array ( "private", "public" );
                                $select[0] = "distribute";
				if ( ! $sharedtype ) { $sharedtype = 'private'; }
                                $form->radio ( "VER", "sharedtype", $sharetypes, $sharedtype );
                                echo "<br>";
                                $form->radio ( "VER", "sharedtype", $select, $sharedtype );
				$mainquery = "SELECT group_id, groupname FROM groups WHERE visible='public' ";
                                $query = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
                                $result = $db->query ( $query );
                                $numrows = $db->numrows ( $result );
                                for ( $i = 0; $i < $numrows; $i++ ) {
                                	$group_id = $db->result ( $result, $i, "group_id" );
                                	$mainquery = $mainquery . "OR group_id=$group_id ";
                                }
                                $mainquery = $mainquery . " ORDER BY groupname";
                                $result = $db->query ( $mainquery );
                                $numrows = $db->numrows ( $result );
                                for  ( $i = 0; $i < $numrows; $i++ ) {
                                	$groupid[$i] = $db->result ( $result, $i, "group_id" );
                                	$groupnames[$i] = $db->result ( $result, $i, "groupname" );
                                }
                                echo "</font></td></tr><tr><td>&nbsp</td><td valign=bottom><font size=-1><b>groups: </b><br>\n";
                                $form->multiselect ( "groupids", $groupid, $groupnames, $sharedgroupnames, 2);

                                echo "<br><b>Users:</b><br>\n";
                                $query = "SELECT user_id, user FROM users ORDER BY user";
                                $result = $db->query ( $query );
                                $numrows = $db->numrows ( $result );
                                for ( $i = 0; $i < $numrows; $i++ ) {
                                        $userid[$i] = $db->result ( $result, $i, "user_id" );
                                        $username[$i] = $db->result ( $result, $i, "user" );
                                }
                                $form->multiselect ( "userids", $userid, $username, $sharedusernames, 2 );

				$query = "SELECT * FROM calendar WHERE cal_id=$cal_id";
				$result = $db->query ( $query );
				$title = $db->result ( $result, 0, "title");
				$note = $db->result ( $result, 0, "note");
				$date = $db->result ( $result, 0, "date");
				$starttime = $db->result ( $result, 0, "starttime");
				$stoptime = $db->result ( $result, 0, "stoptime");
				$rep = $db->result ( $result, 0, "repeat");
				$rep = strtoupper ( $rep );
				$interv = $db->result ( $result, 0, "interv");
				$type = $db->result ( $result, 0, "repeattype");
				$until = $db->result ( $result, 0, "until");
				echo "<tr><td><font size=-1>Title:</font></td><td>\n";
				$form->text('title',$title,80,60,0);
				echo "</td></tr>";
				echo "<tr><td><font size=-1>Note:</font></td><td>\n";
				$form->textarea('note',$note,60,5 );
				echo "</td></tr>";
				echo "<tr><td><font size=-1>Date:</font></td><td>\n";
				$date_arr = $cal->Cal_Splitdate ( $date );
				$form->text('day',$date_arr[3],2,2,0);
				$form->text('month',$date_arr[2],2,2,0);
				$form->text('year',$date_arr[1],4,4,0);
				echo "</td></tr>";
				echo "<tr><td><font size=-1>No Specific Time</font></td><td><font size=-1>";
				if ( $starttime ) {
					$form->checkbox( 'nospectime', 0, 'Y');
				} else {
					$form->checkbox( 'nospectime', 1, 'Y');
				}
				echo "</font></td></tr>";
				echo "<tr><td><font size=-1>Starttime:</font></td><td><font size=-1>\n";
				$hour_arr = array ( '','00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23' );
				$min_arr = array ( '','00','15','30','45' );
				$starttime_arr = $cal->Cal_SplitTime ( $starttime );
				$stoptime_arr = $cal->Cal_SplitTime ( $stoptime );
				$form->simpleselect ( 'starthour', $hour_arr, $hour_arr, $starttime_arr->hour );
				$form->simpleselect ( 'startminutes', $min_arr, $min_arr, $starttime_arr->min );
				echo "</font></td></tr>";
				echo "<tr><td><font size=-1>Stoptime:</font></td><td><font size=-1>\n";
				$form->simpleselect ( 'stophour', $hour_arr, $hour_arr, $stoptime_arr->hour );
				$form->simpleselect ( 'stopminutes', $min_arr, $min_arr, $stoptime_arr->min );
				echo "</font></td></tr><tr><td><font size=-1>";
				$vars = array ( 'Y', 'N' );
				echo "Repeats: </font></td><td><font size=-1>";
				$form->radio( 'hor', 'repeats', $vars, $rep );
				echo "</font></td></tr><tr><td><font size=-1>";
				$repeattype = array ( "", "DAY", "WEEK", "MONTH", "YEAR" );
				echo "Repeat Every";
				echo "</font></td><td><font size=-1>";
				$form->text ( 'interv', $interv, 2, 2, 0 );
				$form->simpleselect ( 'repeattype', $repeattype, $repeattype, $type );
				echo "</font></td></tr><tr><td><font size=-1>";
				echo "Repeat until: </font></td><td><font size=-1>";
				$until_arr = $cal->Cal_Splitdate ( $until );
				$form->text('untilday',$until_arr[3],2,2,0);
				$form->text('untilmonth',$until_arr[2],2,2,0);
				$form->text('untilyear',$until_arr[1],4,4,0);
				echo "</font></td></tr><tr><td colspan=2 align=center>";
				echo "<tr><td colspan=2><fontsize=-1>Allow Others To Update This Item:</font></td></tr>\n";
                                echo "<tr><td>&nbsp</td><td><font size=-1>";
				if ( !$calupdate ) {
                                        $calupdate = $db->result ( $result, 0, "calupdate" );
                                }
                                if ( !$caldelete ) {
                                        $caldelete = $db->result ( $result, 0, "caldelete" );
                                }
                                if ( $calupdate == "Y" ) {
                                        $form->checkbox ( "calupdate", 1, 0 );
                                } else {
                                        $form->checkbox ( "calupdate", 0, 0 );
                                }
                                echo "</font></td></tr>\n";
                                echo "<tr><td colspan=2><fontsize=-1>Allow Others To Delete This Item:</font></td></tr>\n";
                                echo "<tr><td>&nbsp</td><td><font size=-1>";
                                if ( $caldelete == "Y" ) {
                                        $form->checkbox ( "caldelete", 1, 0 );
                                } else {
                                        $form->checkbox ( "caldelete", 0, 0 );
                                }
                                echo "</font></td></tr>\n";
				 
				echo "<tr><td colspan=2 align=center><font size=-1>\n";
				$form->hidden ( "cal_id", $cal_id );
				$form->submit ( "updatecal", "Save" );
				$form->submit ( "deletecal", "Delete" );
				echo "</font></td></tr>\n";
				$form->close();
				echo "</table>\n";
				$html->htmlfooter();
				break;;
		case "sharing" :
				$form = new FormCl;
				$html->htmlheader ( "Calendar Sharings For $user", 0 );
				echo "<b>Active Shares</b><p>\n";
				echo "<table border=0>\n";
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );
				$query = "SELECT * FROM calendar_subscribed WHERE user_id=$user_id";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$subscribe_id = $db->result ( $result, $i, "subscrib_id" );
					$sharetype = $db->result ( $result, $i, "sharetype" );
					if ( $sharetype == "group" ) {
						$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
						$query = "SELECT groupname from groups WHERE group_id=$sharedfrom";
						$result1 = $db->query ( $query );
						$sharename = $db->result ( $result1, 0, "groupname" );
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>$sharename</font></td>\n";
					} else if ( $sharetype == "user" ) {
						$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
						$query = "SELECT user from users WHERE user_id=$sharedfrom";
						$result1 = $db->query ( $query );
						$sharename = $db->result ( $result1, 0, "user" );
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>$sharename</font></td>\n";
					} else {
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>&nbsp</font></td>\n";
					}
					echo "<td><font size=-1><a CLASS=blue href=calendar.php3?deletesharing=1&subscrib_id=$subscribe_id> | Remove Share | </a></font></td></tr>\n";
					
						
				}
				echo "</table><hr noshade>";
				echo "<b>Add A Share</b><p>\n";	
						
				$form->init ( "calendar.php3", "post", "calendar" );
				$sharetypes = array ( "group", "user", "public" );
				echo "<font size=-1>Share Type: ";
				if ( !$share_type ) { $share_type = "public" ; }
				$form->SimpleSelect ( "share_type onChange=window.location.href=\"calendar.php3?menu=sharing&share_type=\"+(options[selectedIndex].value)", $sharetypes, $sharetypes, $share_type );
				echo "</font><br>\n";
				if ( $share_type == "group" ) {
					echo "<font size=-1><b>Groups: </b>";
					echo "<select name=sharedfrom size=5>";
					$query = "SELECT group_id, groupname FROM groups WHERE owner=$user_id OR visible='public'";
					$tmpquery = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
					$tmpresult  = $db->query ( $tmpquery );
					$numrows = $db->numrows ( $tmpresult );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$group_id = $db->result ( $tmpresult, $i, "group_id" );
						$query = $query . " OR group_id=$group_id";
					}
					$query = $query . " ORDER BY groupname";
				
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$groupids[$i] = $db->result ( $result, $i, "group_id" );
						$groupnames[$i] = $db->result ( $result, $i, "groupname" );
						echo "<option value=$groupids[$i]>$groupnames[$i]</option>";
					}
					echo "</select></font><br>\n";
				} else if ( $share_type == "user" ) {
					echo "<font size=-1><b>Users: </b>";
					echo "<select name=sharedfrom size=5>";
				
					$query = "SELECT user_id, user FROM users WHERE user!='$user' ORDER BY user";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$userids[$i] = $db->result ( $result, $i, "user_id" );
						$usernames[$i] = $db->result ( $result, $i, "user" );
						echo "<option value=$userids[$i]>$usernames[$i]</option>";
					}
					echo "</select></font><br>\n";
				}
				$form->hidden ( "user_id", $user_id ); 
				$form->submit ( "addshare", "Add" );			
				$form->close ();	
				$html->htmlfooter ();
				break;;
	}
}

if  ( $deletesharing ) {
			$query = "DELETE FROM calendar_subscribed WHERE subscrib_id=$subscrib_id";
			$result = $db->query ( $query );
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=sharing" );
}

if ( $addshare ) {
			$query = "SELECT sharetype, sharedfrom FROM calendar_subscribed WHERE user_id=$user_id";
			$result = $db->query ( $query );
			$numrows = $db->numrows ( $result );
			$flag = 0;
			for ( $i = 0; $i < $numrows; $i++ ) {
				$shr = $db->result ( $result, $i, "sharetype" );
				$shrfrm = $db->result ( $result, $i, "sharedfrom" );
				if ( $shr == $share_type && $shrfrm == $sharedfrom ) {
					$flag = 1;
				}
				if ( $shr == "public" && $share_type == "public") {	
					$flag = 1 ;
				}
			}
			if ( $flag == 0 ) {
				$query = "INSERT INTO calendar_subscribed VALUES ( NULL, $user_id, '$share_type', 'Y', '$sharedfrom')";
				$result = $db->query ( $query );
			}
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=sharing" );
			
}

if ( $addevent ) {
		if ( $sharedtype == "distribute" ) {
			$splitted = explode ( "&", $QUERY_STRING );
                	$numsplitted = count ( $splitted ) ;
			$j = 0;
			$l = 0;
                	for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                        	if ( ereg ( "^groupids", $splitted[$i] ) ) {
                                	$parts = explode ( "=", $splitted[$i] );
					$end = $numparts - 1 ;
					$groups[$j] = $parts[1];
					$j++;
				} else if ( ereg ( "^userids", $splitted[$i] ) ) {
					$parts = explode ( "=", $splitted[$i] );
					$users[$l] = $parts[1];
					$l++;
				}
			}
		}
		$user_id = $db->UserId ( $user );
		$event->owner_id = $user_id;
		$event->title = $title;
		$event->note = $note;
		$event->interv = $interv;
		$event->repeattype = $repeattype;
		$event->date = $year . "-" . $month . "-" . $day;
		if ( $nospectime == 'Y' ) {
			$event->starttime = '';
			$event->stoptime = '';
		} else {
			$event->starttime = $starthour . ":" . $startminutes;
			$event->stoptime = $stophour . ":" . $stopminutes;
		}
		if ( $untilyear != '') {
			$event->until = $untilyear . "-" . $untilmonth . "-" . $untilday;
		} else {
			$event->until= 'no';
		} 
		$event->repeat = strtolower ( $repeats );
		$event->calupdate = "N";
		$event->caldelete = "N";
		$event->calwrite = "N";
		$event->calread = "N";
		$vars->event = $event;
		$vars->table = "calendar";
		$vars->numgroups = count ( $groups );
                $vars->numusers = count ( $users );
		$vars->users = $users;
		$vars->groups = $groups;
                $vars->sharedtype = $sharedtype;
		$vars->id_field = "cal_id";

		// ####################################################################################

		// now lets see if this record should be created for another user
		$vars->otheruser = $otheruser;
		$vars->type='event';
		if ( $otheruser == 'Y' ) { 
			$event->owner_id = 0; // if we create this for another user, noone owns this record 
					   // until targetted user accepted it
			$splitted = explode ( "&", $QUERY_STRING );
                        $numsplitted = count ( $splitted ) ;
                        $j = 0;
                        $l = 0;
                        for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                                if ( ereg ( "^forgroup", $splitted[$i] ) ) {
                                        $parts = explode ( "=", $splitted[$i] );
                                        $end = $numparts - 1 ;
                                        $forgroups[$j] = $parts[1];
                                        $j++;
                                } else if ( ereg ( "^foruser", $splitted[$i] ) ) {
                                        $parts = explode ( "=", $splitted[$i] );
                                        $forusers[$l] = $parts[1];
                                        $l++;
                                }
                        }
			$vars->forusers = $forusers;
			$vars->forgroups = $forgroups;
			$vars->comment = $comments;
			$vars->byuser = $user_id;
			$db->InsertForUser ( $vars );
		} else {
			$vars->owner_id = $user_id;
			$last_id = $db->InsertNewRecord ( $vars );
			$db->InsertShares ( $vars, $last_id );
		}

		// ####################################################################################

			 
		header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=list" );  
}
if ( $updatecal ) {
		$updatedate = date ( 'Y-m-d' );
		$date = $year . "-" . $month . "-" . $day;
		if ( $nospectime == 'Y' ) {
                       $starttime = '';
                       $stoptime = '';
                } else {
                        $starttime = $starthour . ":" . $startminutes;
                        $stoptime = $stophour . ":" . $stopminutes;
                }
                if ( $untilyear != '') {
                        $until = $untilyear . "-" . $untilmonth . "-" . $untilday;
                } else {
                        $until= 'no';
                }
                $repeat = strtolower ( $repeats );
		if ( $repeat == 'n' ) {
			$interv = "";
			$repeattype= "";
		}
		$query = " SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
                if ( $sharedtype == "distribute" ) {
			$splitted = explode ( "&", $QUERY_STRING );
                	$numsplitted = count ( $splitted ) ;
			$j = 0;
			$l = 0;
                	for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                        	if ( ereg ( "^groupids", $splitted[$i] ) ) {
                                	$parts = explode ( "=", $splitted[$i] );
					$end = $numparts - 1 ;
					$groups[$j] = $parts[1];
					$j++;
				} else if ( ereg ( "^userids", $splitted[$i] ) ) {
					$parts = explode ( "=", $splitted[$i] );
					$users[$l] = $parts[1];
					$l++;
				}
			}
		}
                $query = "UPDATE calendar SET title='$title', note='$note', date='$date', starttime='$starttime', stoptime='$stoptime', repeat='$repeat', interv='$interv', repeattype='$repeattype', until='$until', lastupdate='$updatedate', updateby=$user_id, caldelete='$caldelete', calupdate='$calupdate' WHERE cal_id=$cal_id"; 
			$result = $db->query ( $query );
			$query = "DELETE FROM calendar_shares WHERE cal_id=$cal_id";
			$result = $db->query ( $query );
			$numgroups = count ( $groups );
			for ( $i = 0; $i < $numgroups; $i++ ) {
				$query = "INSERT INTO calendar_shares VALUES ( NULL, '$sharedtype', '', $groups[$i], $cal_id ) ";
				$result = $db->query ( $query );
			}
			$numusers = count ( $users );
			for  ( $i = 0; $i < $numusers; $i++ ) {
				$query = "INSERT INTO calendar_shares VALUES ( NULL, '$sharedtype', $users[$i], '', $cal_id ) ";
				$result = $db->query ( $query );
			}
			if ( $sharedtype == "public" ) {
				$query = "INSERT INTO calendar_shares VALUES ( NULL, '$sharedtype', '', '', $cal_id ) ";
				$result = $db->query ( $query );
			}
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=list" );  
}
if ( $deletecal ) {
		$query = "SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
		//$numselect = count ( $select );
		if ( $cal_id ) {
			$query = "SELECT owner_id FROM calendar WHERE cal_id=$cal_id";
			$result = $db->query ( $query );
			//$caldelete = $db->result ( $result, 0, "caldelete" );
			$owner = $db->result( $result, 0, "owner_id" );
			if ( $owner == $user_id || $caldelete == "Y" ) {
				$query = "DELETE FROM calendar WHERE cal_id=$cal_id";
				$result = $db->query ( $query );
				$query = "DELETE FROM calendar_shares WHERE cal_id=$cal_id";
				$result = $db->query ( $query );
			}	
		}
		header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=list" );  
		
}		

if ( $return ) {
	header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/calendar.php3?menu=detail&cal_id=$cal_id" );  
}
	
?>
Return current item: Myphpim