<?PHP

//
//  Functions to calculate the next und previous day. These are used to show all days
//  of the current month.
//

/*

    This file was written by Thorsten Gunkel <tgunkel@gmx.de> for
    his pages at <url:http://www.tgunkel.de>.
    Copyright (C) 2004 Thorsten Gunkel

    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 version 2 of the License.

    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

*/


// FIXME: Usage: ....

// A day
class cal_day
{
  var 
$day;     // day of month
  
var $dow;     // weekday
  
var $month;   // month of year
  
var $year;    // year
  
var $content// text connected to this day
  
  // contructor
  
function cal_day($day,$month,$year,$dow)
    {
      
$this->day=$day;
      
$this->month=$month;
      
$this->year=$year;
      
$this->dow=$dow;
      
$this->content=array();
    }

  
// add text connected to this day
  
function add_content($txt)
    {
      
$this->content[]=$txt;
    }
 
  
// get text connect to this day
  
function get_content($txt)
    {
      return 
$this->content;
    }
   
  
// output
  
function out()
     {
       
$tmp_txt='';
       
$tmp_txt.='  <td class="';
       switch(
$this->when_is_this_day())
         {
         case -
1$tmp_txt.='cal-day-pre'; break;
         case  
0$tmp_txt.='cal-day-today'; break;
         case  
1$tmp_txt.='cal-day-post'; break;
         }
       
$tmp_txt.='">';
       
$tmp_txt.='<div class="';
       switch(
$this->when_is_this_day())
         {
         case -
1$tmp_txt.='cal-daynr'; break;
         case  
0$tmp_txt.='cal-daynr-today'; break;
         case  
1$tmp_txt.='cal-daynr'; break;
         }
       
$tmp_txt.='">'.$this->day.'</div>';
       
$tmp_list=(count($this->content)!=0);
       if (
$tmp_list$tmp_txt.='<ul class="cal-day-ul">';
       foreach (
$this->content as $txt)
         {
           
$tmp_txt.='<li class="cal-day-li">'.$txt.'</li>';
         }
       if (
$tmp_list$tmp_txt.='</ul>';
       
$tmp_txt.='</td>'."\n";
       return 
$tmp_txt;
     }

  function 
when_is_this_day()
    {
      
$today=getdate(time());
      
$diff=(366*($this->year-$today["year"]))+(31*($this->month-$today["mon"]))+($this->day-$today["mday"]);
      if (
$diff>0)
        {
          return 
1;
        }
      else
        {
          if (
$diff<0)
            return -
1;
          else
            return 
0;
        }
    }
}

// A month
class cal_month
{
  var 
$month;  // month of year
  
var $year;   // year
  
var $days;   // days of month
  
var $size;   // nr of days in this month

  //FIXME new paramter wod_names to enable translations, or lang paramter
  // contructor
  
function cal_month($month,$year)
    {
      
$this->month=$month;
      
$this->year=$year;
      
$this->days=array();
      
// calculate nr of days in this month
      
switch($this->month)
        {
        case 
4:
        case 
6:
        case 
9:
        case 
11:
          
$this->size=30;
          break;
        case 
1:
        case 
3:
        case 
5:
        case 
7:
        case 
8:
        case 
10:
        case 
12:
          
$this->size=31;
          break;
        case 
2:
          if ((((
$this->year 4)==0) && (($this->year 100)!=0)) || (($this->year 400)==0))
            
$this->size=29;
          else
            
$this->size=28;
          break;
        default:
          
$this->size=-1;
          echo 
"ERROR: Illegal Month";
          exit();
        }
      
      
$dow=$this->get_dow(1,$this->month,$this->year);
      
      for (
$c1=1$c1<=$this->size$c1++)
        {
          
$this->days[$c1]=new cal_day($c1,$this->month,$this->year,$dow);
          
$dow=($dow+1)-(floor(7*floor(($dow+1)/7)));
        }
    }

  
// access a day of this month
  
function &access($day)
    {
      
$day=preg_replace('/^0+/','',$day);
      if (!
array_key_exists($day,$this->days))
        exit(
'ERROR: Invalid Day'.$day);
      return 
$this->days[$day];
    }

   
// output
   
function out()
     {
       
//arsort($this->days,SORT_NUMERIC);
       
$tmp_txt='';
       
//$tmp_txt.='<table border="0">'."\n";
       
$tmp_txt.='<table class="cal-all">';
       
$tmp_txt.=' <tr><th colspan="7" class="cal-month-row">'.$this->month.' / '.$this->year.'</th></tr>'."\n";
       
$tmp_txt.=' <tr class="cal-wod-row">'."\n";
       
$tmp_txt.='  <th class="cal-wod">Mo</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">Di</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">Mi</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">Do</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">Fr</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">Sa</th>'."\n";
       
$tmp_txt.='  <th class="cal-wod">So</th>'."\n";
       
$tmp_txt.=' </tr>'."\n";
       
$tmp_txt.=' <tr>'."\n";;
       
$start_dow=$this->days[1]->dow;
       
$end_dow=$this->days[$this->size]->dow;

       for (
$c1=($start_dow); $c1>0$c1--)
         {
           
$tmp_txt.='  <td class="cal-day"></td>'."\n";
         }
       foreach (
$this->days as $day)
         {
           if ((
$day->day>1) && ($day->dow==0))
              
$tmp_txt.=' </tr>'."\n".'<tr>'."\n";
           
$tmp_txt.=$day->out();
         }
       for (
$c1=(6-$end_dow); $c1>0$c1--)
         {
           
$tmp_txt.='  <td class="cal-day"></td>'."\n";
         }
       
       
$tmp_txt.=' </tr>'."\n";
       
$tmp_txt.='</table>';
       return 
$tmp_txt;
     }

   
// get day ot the week, 0==Monday
   
function get_dow($day,$month,$year)
     {
       
$datum=getdate(mktime (000,$month$day$year));
       
$dow=$datum['wday']+6;
       
$dow=$dow-(floor(7*floor(($dow)/7)));
       return 
$dow;
     }
}

// A year
class cal_year
{
  var 
$year;
  var 
$months;

  
// contructor
  
function cal_year($year)
    {
      
$this->year=$year;
      
$this->months=array();
    }

  
// access a month of this year
  
function &access($month)
    {
      
$month=(int)$month;
      if (!
array_key_exists($month,$this->months))
        {
          if ((
$month=(int)$month) && ($month>0) && ($month<13))
                    {
            
$this->months[$month]=new cal_month($month,$this->year);
                        
// sort now month to its correct place
                        
krsort($this->monthsSORT_NUMERIC);
                    }
          else
            exit(
'ERROR: Invalid Month');
        }
      return 
$this->months[$month];
    }

   
// output
   
function out()
     {
       
//arsort($this->months,SORT_NUMERIC);
       
$tmp_txt='';
           
       foreach (
$this->months as $month)
         {
           
$tmp_txt.=$month->out();
         }
       return 
$tmp_txt;
     }
}

// A clendar
class calendar
{
  var 
$years;  // years in this calendar

  // contructor  
  
function calendar()
    {
      
$this->years=array();
    }

  
// access a year of this cleandar
  
function &access($year)
    {
      
$year=(int)$year;
      if (!
array_key_exists($year,$this->years))
            {
        
$this->years[$year]=new cal_year($year);
                
// sort now year to its correct place
                
krsort($this->yearsSORT_NUMERIC);
            }
      return 
$this->years[$year];
    }

   
// output 
   
function out()
     { 
       
//arsort($this->years,SORT_NUMERIC);
       
$tmp_txt='';
       foreach (
$this->years as $year)
         {
           
$tmp_txt.=$year->out();
         }
       return 
$tmp_txt;
     }
}

?>