Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify Caledar Script

Status
Not open for further replies.

uniopp

Technical User
Oct 7, 2001
152
JP
Hi,
This is a different question from my previous "Calendar" question so I decided to post a new topic and include the full original code here.
I downloaded a free calender script (see below) and I was wondering how difficult it would be to modify it to only show one week of each month at a time instead of all 4 or 5 weeks at the same time.
We can select the month and year by using
Code:
$myCal->{year} = 2004;
$myCal->{month} = 12;
print $myCal->create();
but I really would like to use something like -
Code:
$myCal->{year} = 2004;
[b][red]$myCal->{week} = 40; # only the 40th week of 2004[/red][/b]
print $myCal->create();
I emailed the the original script coder but haven't heard back.
I was hoping that someone here could tell me if there is a simple solution and where to start?
Thanks.

Code:
package CALENDAR;

# +-------------------------------------------------------------------+
# |                 H T M L - C A L E N D A R   (v2.9)                |
# |                                                                   |
# | Copyright Gerd Tentler               [URL unfurl="true"]www.gerd-tentler.de/tools[/URL]    |
# | Created: May 27, 2003                Last modified: Jan. 10, 2009 |
# +-------------------------------------------------------------------+
# | This program may be used and hosted free of charge by anyone for  |
# | personal purpose as long as this copyright notice remains intact. |
# |                                                                   |
# | Obtain permission before selling the code for this program or     |
# | hosting this software on a commercial website or redistributing   |
# | this software over the Internet or in any other medium. In all    |
# | cases copyright must remain intact.                               |
# +-------------------------------------------------------------------+
#
# EXAMPLE #1:  $myCal = CALENDAR->new();
#              print $myCal->create();
#
# EXAMPLE #2:  $myCal = CALENDAR->new(2004, 12);
#              print $myCal->create();
#
# EXAMPLE #3:  $myCal = CALENDAR->new();
#              $myCal->{year} = 2004;
#              $myCal->{month} = 12;
#              print $myCal->create();
#
# Returns HTML code
#===================================================================================================  ======

my $cal_ID = 0;

sub initialize {
  my $this = shift;
#---------------------------------------------------------------------------------------------------------
# Configuration
#---------------------------------------------------------------------------------------------------------
  $this->{tFontFace} = 'Arial, Helvetica'; # title: font family (CSS-spec, e.g. "Arial, Helvetica")
  $this->{tFontSize} = 14;                 # title: font size (pixels)
  $this->{tFontColor} = '#FFFFFF';         # title: font color
  $this->{tBGColor} = '#304B90';           # title: background color

  $this->{hFontFace} = 'Arial, Helvetica'; # heading: font family (CSS-spec, e.g. "Arial, Helvetica")
  $this->{hFontSize} = 12;                 # heading: font size (pixels)
  $this->{hFontColor} = '#FFFFFF';         # heading: font color
  $this->{hBGColor} = '#304B90';           # heading: background color

  $this->{dFontFace} = 'Arial, Helvetica'; # days: font family (CSS-spec, e.g. "Arial, Helvetica")
  $this->{dFontSize} = 14;                 # days: font size (pixels)
  $this->{dFontColor} = '#000000';         # days: font color
  $this->{dBGColor} = '#FFFFFF';           # days: background color

  $this->{wFontFace} = 'Arial, Helvetica'; # weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
  $this->{wFontSize} = 12;                 # weeks: font size (pixels)
  $this->{wFontColor} = '#FFFFFF';         # weeks: font color
  $this->{wBGColor} = '#304B90';           # weeks: background color

  $this->{saFontColor} = '#0000D0';        # Saturdays: font color
  $this->{saBGColor} = '#F6F6FF';          # Saturdays: background color

  $this->{suFontColor} = '#D00000';        # Sundays: font color
  $this->{suBGColor} = '#FFF0F0';          # Sundays: background color

  $this->{tdBorderColor} = '#FF0000';      # today: border color

  $this->{borderColor} = '#304B90';        # border color
  $this->{hilightColor} = '#FFFF00';       # hilight color (works only in combination with link)

  $this->{link} = '';                      # page to link to when day is clicked
  $this->{offset} = 1;                     # week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
  $this->{weekNumbers} = 1;                # view week numbers: 1 = yes, 0 = no

#---------------------------------------------------------------------------------------------------------
# You should change these variables only if you want to translate them into your language:
#---------------------------------------------------------------------------------------------------------
  # weekdays: must start with Saturday because January 1st of year 1 was a Saturday
  @{$this->{weekdays}} = ('Sa', 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr');

  # months: must start with January
  @{$this->{months}} = ('January', 'February', 'March', 'April', 'May', 'June',
                        'July', 'August', 'September', 'October', 'November', 'December');
  # error messages
  @{$this->{error}} = ('Year must be 1 - 3999!', 'Month must be 1 - 12!');

#---------------------------------------------------------------------------------------------------------
# Don't change from here:
#---------------------------------------------------------------------------------------------------------
  $this->{year} = 0;
  $this->{month} = 0;
  $this->{size} = 0;
  @{$this->{mDays}} = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  %{$this->{specDays}} = {};

}

sub new {
  my $year = $_[1];
  my $month = $_[2];
  my $self = {};
  bless $self;
  $self->initialize();
  if($year eq '' && $month eq '') {
    my ($sec, $min, $hr, $day, $m, $y, $wday, $yday, $stime) = localtime(time);
    $year = $y + 1900;
    $month = $m + 1;
  }
  elsif($year ne '' && $month eq '') { $month = 1; }
  $self->{year} = $year;
  $self->{month} = $month;
  return $self;
}

sub set_styles {
  my $this = shift;
  my $html;

  $cal_ID++;
  $html = '<style> .cssTitle' . $cal_ID . ' { ';
  if($this->{tFontFace}) { $html .= 'font-family: ' . $this->{tFontFace} . '; '; }
  if($this->{tFontSize}) { $html .= 'font-size: ' . $this->{tFontSize} . 'px; '; }
  if($this->{tFontColor}) { $html .= 'color: ' . $this->{tFontColor} . '; '; }
  if($this->{tBGColor}) { $html .= 'background-color: ' . $this->{tBGColor} . '; '; }
  $html .= '} .cssHeading' . $cal_ID . ' { ';
  if($this->{hFontFace}) { $html .= 'font-family: ' . $this->{hFontFace} . '; '; }
  if($this->{hFontSize}) { $html .= 'font-size: ' . $this->{hFontSize} . 'px; '; }
  if($this->{hFontColor}) { $html .= 'color: ' . $this->{hFontColor} . '; '; }
  if($this->{hBGColor}) { $html .= 'background-color: ' . $this->{hBGColor} . '; '; }
  $html .= '} .cssDays' . $cal_ID . ' { ';
  if($this->{dFontFace}) { $html .= 'font-family: ' . $this->{dFontFace} . '; '; }
  if($this->{dFontSize}) { $html .= 'font-size: ' . $this->{dFontSize} . 'px; '; }
  if($this->{dFontColor}) { $html .= 'color: ' . $this->{dFontColor} . '; '; }
  if($this->{dBGColor}) { $html .= 'background-color: ' . $this->{dBGColor} . '; '; }
  $html .= '} .cssWeeks' . $cal_ID . ' { ';
  if($this->{wFontFace}) { $html .= 'font-family: ' . $this->{wFontFace} . '; '; }
  if($this->{wFontSize}) { $html .= 'font-size: ' . $this->{wFontSize} . 'px; '; }
  if($this->{wFontColor}) { $html .= 'color: ' . $this->{wFontColor} . '; '; }
  if($this->{wBGColor}) { $html .= 'background-color: ' . $this->{wBGColor} . '; '; }
  $html .= '} .cssSaturdays' . $cal_ID . ' { ';
  if($this->{dFontFace}) { $html .= 'font-family: ' . $this->{dFontFace} . '; '; }
  if($this->{dFontSize}) { $html .= 'font-size: ' . $this->{dFontSize} . 'px; '; }
  if($this->{saFontColor}) { $html .= 'color: ' . $this->{saFontColor} . '; '; }
  if($this->{saBGColor}) { $html .= 'background-color: ' . $this->{saBGColor} . '; '; }
  $html .= '} .cssSundays' . $cal_ID . ' { ';
  if($this->{dFontFace}) { $html .= 'font-family: ' . $this->{dFontFace} . '; '; }
  if($this->{dFontSize}) { $html .= 'font-size: ' . $this->{dFontSize} . 'px; '; }
  if($this->{suFontColor}) { $html .= 'color: ' . $this->{suFontColor} . '; '; }
  if($this->{suBGColor}) { $html .= 'background-color: ' . $this->{suBGColor} . '; '; }
  $html .= '} .cssHilight' . $cal_ID . ' { ';
  if($this->{dFontFace}) { $html .= 'font-family: ' . $this->{dFontFace} . '; '; }
  if($this->{dFontSize}) { $html .= 'font-size: ' . $this->{dFontSize} . 'px; '; }
  if($this->{dFontColor}) { $html .= 'color: ' . $this->{dFontColor} . '; '; }
  if($this->{hilightColor}) { $html .= 'background-color: ' . $this->{hilightColor} . '; '; }
  $html .= 'cursor: default; ';
  $html .= '} </style>';

  return $html;
}

sub leap_year {
  my ($this, $year) = @_;
  return (!($year % 4) && ($year < 1582 || $year % 100 || !($year % 400))) ? 1 : 0;
}

sub get_weekday {
  my ($this, $year, $days) = @_;
  my $i;
  my $a = $days;

  if($year) { $a += ($year - 1) * 365; }
  for($i = 1; $i < $year; $i++) { if($this->leap_year($i)) { $a++; }}
  if($year > 1582 || ($year == 1582 && $days >= 277)) { $a -= 10; }
  if($a) { $a = ($a - $this->{offset}) % 7; }
  elsif($this->{offset}) { $a += 7 - $this->{offset}; }

  return $a;
}

sub get_week {
  my ($this, $year, $days) = @_;
  my $firstWDay = $this->get_weekday($year, 0);
  return int(($days + $firstWDay) / 7) + ($firstWDay <= 3);
}

sub table_cell {
  my ($this, $content, $class, $date, $style) = @_;
  my ($size, $html, $link);

  $size = _round($this->{size} * 1.5);
  $html = '<td align=center width=' . $size . ' class="' . $class . '"';

  if($content ne '&nbsp;' && $class =~ m/day/i) {
    $link = $this->{link};

    if($this->{specDays}{$content}) {
      if($this->{specDays}{$content}[0]) {
        $style .= 'background-color:' . $this->{specDays}{$content}[0] . ';';
      }
      if($this->{specDays}{$content}[1]) {
        $html .= ' title="' . $this->{specDays}{$content}[1] . '"';
      }
      if($this->{specDays}{$content}[2]) {
        $link = $this->{specDays}{$content}[2];
      }
    }
    if($link) {
      $html .= ' onMouseOver="this.className=\'cssHilight' . $cal_ID . '\'"';
      $html .= ' onMouseOut="this.className=\'' . $class . '\'"';
      $html .= ' onClick="document.location.href=\'' . $link . '?date=' . $date . '\'"';
    }
  }
  if($style) { $html .= ' style="' . $style . '"'; }
  $html .= '>' . $content . '</td>';

  return $html;
}

sub table_head {
  my ($this, $content) = @_;
  my ($html, $i, $ind, $wDay);
  my $cols = $this->{weekNumbers} ? 8 : 7;

  $html = '<tr><td colspan=' . $cols . ' class="cssTitle' . $cal_ID . '" align=center><b>' .
          $content . '</b></td></tr><tr>';
  for($i = 0; $i < int @{$this->{weekdays}}; $i++) {
    $ind = ($i + $this->{offset}) % 7;
    $wDay = $this->{weekdays}[$ind];
    $html .= $this->table_cell($wDay, 'cssHeading' . $cal_ID);
  }
  if($this->{weekNumbers}) { $html .= $this->table_cell('&nbsp;', 'cssHeading' . $cal_ID); }
  $html .= '</tr>';

  return $html;
}

sub viewEvent {
  my ($this, $from, $to, $color, $title, $link) = @_;

  if($from > $to) { return; }
  if($from < 1 || $from > 31) { return; }
  if($to < 1 || $to > 31) { return; }

  while($from <= $to) {
    @{$this->{specDays}{$from}} = ($color, $title, $link);
    $from++;
  }
}

sub create {
  my $this = shift;
  my ($html, $i, $start, $stop, $title, $daycount, $inThisMonth);
  my ($days, $weekNr, $ind, $class, $style, $content, $date);
  my ($sec, $min, $hr, $curDay, $curMonth, $curYear, $wday, $yday, $stime) = localtime(time);

  $curYear += 1900;
  $curMonth += 1;

  $this->{size} = ($this->{hFontSize} > $this->{dFontSize}) ? $this->{hFontSize} : $this->{dFontSize};
  if($this->{wFontSize} > $this->{size}) { $this->{size} = $this->{wFontSize}; }

  if($this->{year} < 1 || $this->{year} > 3999) { $html = '<b>' . $this->{error}[0] . '</b>'; }
  elsif($this->{month} < 1 || $this->{month} > 12) { $html = '<b>' . $this->{error}[1] . '</b>'; }
  else {
    $this->{mDays}[1] = $this->leap_year($this->{year}) ? 29 : 28;
    for($i = $days = 0; $i < $this->{month} - 1; $i++) { $days += $this->{mDays}[$i]; }

    $start = $this->get_weekday($this->{year}, $days);
    $stop = $this->{mDays}[$this->{month}-1];

    $html = $this->set_styles();
    $html .= '<table border=0 cellspacing=0 cellpadding=0><tr>';
    $html .= '<td' . ($this->{borderColor} ? ' bgcolor=' . $this->{borderColor}  : '') . '>';
    $html .= '<table border=0 cellspacing=1 cellpadding=3>';
    $title = $this->{months}[$this->{month}-1] . ' ' . $this->{year};
    $html .= $this->table_head($title);
    $daycount = 1;

    if(($this->{year} == $curYear) && ($this->{month} == $curMonth)) { $inThisMonth = 1; }
    else { $inThisMonth = 0; }

    if($this->{weekNumbers}) { $weekNr = $this->get_week($this->{year}, $days); }

    while($daycount <= $stop) {
      $html .= '<tr>';

      for($i = $wdays = 0; $i <= 6; $i++) {
        $ind = ($i + $this->{offset}) % 7;
        if($ind == 0) { $class = 'cssSaturdays'; }
        elsif($ind == 1) { $class = 'cssSundays'; }
        else { $class = 'cssDays'; }

        $style = '';
        $date = $this->{year} . '-' . $this->{month} . '-' . $daycount;

        if(($daycount == 1 && $i < $start) || $daycount > $stop) { $content = '&nbsp;'; }
        else {
          $content = $daycount;
          if($inThisMonth && $daycount == $curDay) {
            $style = 'padding:0px;border:3px solid ' . $this->{tdBorderColor} . ';';
          }
          elsif($this->{year} == 1582 && $this->{month} == 10 && $daycount == 4) { $daycount = 14; }
          $daycount++;
          $wdays++;
        }
        $html .= $this->table_cell($content, $class . $cal_ID, $date, $style);
      }

      if($this->{weekNumbers}) {
        if(!$weekNr) {
          if($this->{year} == 1) { $content = '&nbsp;'; }
          elsif($this->{year} == 1583) { $content = 52; }
          else { $content = $this->get_week($this->{year} - 1, 365); }
        }
        elsif($this->{month} == 12 && $weekNr >= 52 && $wdays < 4) { $content = 1; }
        else { $content = $weekNr; }

        $html .= $this->table_cell($content, 'cssWeeks' . $cal_ID);
        $weekNr++;
      }
      $html .= '</tr>';
    }
    $html .= '</table></td></tr></table>';
  }
  return $html;
}

#---------------------------------------------------------------------------------------------------------
# PRIVATE
#---------------------------------------------------------------------------------------------------------

sub _round {
  my $fl = shift;
  return ($fl >= 0) ? int($fl + 0.5) : int($fl - 0.5);
}

#---------------------------------------------------------------------------------------------------------

return 1;
 
It is not a trivial modification because that module is designed to print a calendar table for 1 individual month.

How do you intend to represent a week that bridges 2 months? With 2 tables?

Annihilannic.
 
I would prefer to do it with one table displaying seven days therefore Sunday September 27 to Saturday October 4 would be week 40 of 2009.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top