HTML-CalendarMonthDB

 view release on metacpan or  search on metacpan

CalendarMonthDB.pm  view on Meta::CPAN

	use DBI;
   	# det defaults	
   	($self->{'dbuser'}) || ($self->{'dbuser'} = 'nobody');
	($self->{'dbpass'}) || ($self->{'dbpass'} = '');
  	($self->{'dbcalendar'}) || ($self->{'dbcalendar'} = '');
        ($self->{'dbclient'}) || ($self->{'dbclient'} = '');
	($self->{'dbhost'}) || ($self->{'dbhost'} = '');
   }


   # Set the default calendar header
   $self->{'header'} = sprintf("<center><font size=+2>%s %d</font></center>",
                               Date::Calc::Month_to_Text($self->{'month'}),$self->{'year'});

   # Get the monthname now so monthname() is fast and efficient
   $self->{'monthname'} = Date::Calc::Month_to_Text($self->{'month'});

   # Initialize the (empty) cell content so the keys are representative of the month
   map { $self->{'content'}->{$_} = ''; } (1 .. Date::Calc::Days_in_Month($self->{'year'},$self->{'month'}));
   # Initialize the non-standard date buckets: weekdays, etc.
   foreach my $day ('sunday','monday','tuesday','wednesday','thursday','friday','saturday') {
      $self->{'content'}->{$day."s"} = ''; # "Mondays", "Tuesdays", etc.
      foreach my $which (1 .. 5) { $self->{'content'}->{$which.$day} = ''; } # "2Sunday", "3Wednesday", etc.
   }

   # All done!
   bless $self,$class; return $self;
}

sub as_HTML_list {
   my $self = shift;
   my $self1 = {}; %$self1 = @_; # Load ourselves up from the args 
   my $editurl = $self1->{'editurl'} if $self1->{'editurl'};
   my $addurl = $self1->{'addurl'} if $self1->{'addurl'};
   my $html = '';
   my(@days,$weeks,$WEEK,$DAY);

   my $border = $self->border();
   my $tablewidth = $self->width();
   my $header = $self->header();
   my $bgcolor = $self->bgcolor() || '';
   my $cellalignment = $self->cellalignment(); 
   my $weekdaycolor = $self->weekdaycolor() || $self->bgcolor();
   my $weekendcolor = $self->weekendcolor() || $self->bgcolor();
   my $todaycolor = $self->todaycolor() || $self->bgcolor();
   my $contentcolor = $self->contentcolor() || $self->contentcolor();
   my $weekdaycontentcolor = $self->weekdaycontentcolor() || $self->contentcolor(); 
   my $weekendcontentcolor = $self->weekendcontentcolor() || $self->contentcolor(); 
   my $todaycontentcolor = $self->todaycontentcolor() || $self->contentcolor();
   my $bordercolor = $self->bordercolor() || $self->bordercolor();
   my $weekdaybordercolor = $self->weekdaybordercolor() || $self->bordercolor(); 
   my $weekendbordercolor = $self->weekendbordercolor() || $self->bordercolor(); 
   my $todaybordercolor = $self->todaybordercolor() || $self->bordercolor();
   my $weekdayheadercolor = $self->weekdayheadercolor() || $self->bgcolor();
   my $weekendheadercolor = $self->weekendheadercolor() || $self->bgcolor();
   my $headercolor = $self->headercolor() || $self->bgcolor();
   # Get today's date, in case there's a todaycolor()
   my($todayyear,$todaymonth,$todaydate) = Date::Calc::Today();


   # add javascript popup description code
   $html .= $self->jscode();

   @days = (1 .. Date::Calc::Days_in_Month($self->year(),$self->month() ) );
   foreach (1 .. (Date::Calc::Day_of_Week($self->year(),$self->month(),1)%7) ) {
      unshift(@days,0);
   }
   $weeks = int((scalar(@days)+6)/7);

   $html .= "<table width=\"$tablewidth\" border=\"$border\" bgcolor=\"$bgcolor\" bordercolor=\"$bordercolor\"><tr><td colspan=2 bgcolor=\"$headercolor\">$header</td></tr>";
	
foreach $WEEK (0 .. ($weeks-1)) {
    foreach $DAY (0 .. 6) {
    my($content,$thisday,$thisbgcolor,$thisbordercolor,$thiscontentcolor);
    $thisday = $days[((7*$WEEK)+$DAY)];
    if ($thisday) {
    $content=$self->getcontent($thisday);
	if ($self->year == $todayyear && $self->month == $todaymonth && $thisday == $todaydate)
                                           { $thisbgcolor = $todaycolor;
                                             $thisbordercolor = $todaybordercolor;
                                             $thiscontentcolor = $todaycontentcolor;
                                            }
         elsif (($DAY == 0) || ($DAY == 6)){ $thisbgcolor = $weekendcolor;
                                             $thisbordercolor = $weekendbordercolor;
                                             $thiscontentcolor = $weekendcontentcolor;
                                            }
         else                               { $thisbgcolor = $weekdaycolor;
                                                $thisbordercolor = $weekdaybordercolor;
                                                $thiscontentcolor = $weekdaycontentcolor;
                                            }

	
	if (!$content) {
        $content = '&nbsp;'; 
	}
  	$html .= "<tr><td width=\"2%\"><b>$thisday</b>";
	# Add admin links if specified in function call 
        if ($addurl) {
	        my $calid = $self->calendarid();
       		my $mdate = $self->month().'_'.$thisday.'_'.$self->year();
                $html .= "<br><font size=\"1\"><a href=\"#\" onclick=popup(\"$addurl?date=$mdate&calid=$calid&view=list\")>[Add]</a></font>";
                }
        if ($editurl) {
        	my $calid = $self->calendarid();
        	my $mdate = $self->month().'_'.$thisday.'_'.$self->year();
                $html .= "<br><font size=\"1\"><a href=\"#\" onclick=popup(\"$editurl?date=$mdate&calid=$calid&view=list\")>[Edit]</a></font>";
	}
	$html .= "</td><td bgcolor=\"$thisbgcolor\" bordercolor=\"$thisbordercolor\" align=\"$cellalignment\"><font color=\"$thiscontentcolor\">$content</font></td></tr>\n"; 
  	} 
   } 
   }
   $html .= '</table>';
   return $html;
}

sub as_HTML {
   my $self = shift;
   my $self1 = {}; %$self1 = @_; # Load ourselves up from the args
   my $editurl = $self1->{'editurl'} if $self1->{'editurl'};
   my $addurl = $self1->{'addurl'} if $self1->{'addurl'};
   my $html = '';
   my(@days,$weeks,$WEEK,$DAY);
  
   # add javascript popup description code
   $html .= $self->jscode();

   # To make the grid even, pad the start of the series with 0s
   @days = (1 .. Date::Calc::Days_in_Month($self->year(),$self->month() ) );
   foreach (1 .. (Date::Calc::Day_of_Week($self->year(),$self->month(),1)%7) ) {
      unshift(@days,0);
   }
   $weeks = int((scalar(@days)+6)/7);

   # Define some scalars for generating the table
   my $border = $self->border();
   my $tablewidth = $self->width();
   $tablewidth =~ m/^(\d+)(\%?)$/; my $cellwidth = (int($1/7))||'14'; if ($2) { $cellwidth .= '%'; }
   my $header = $self->header();
   my $cellalignment = $self->cellalignment();
   my $bgcolor = $self->bgcolor() || '';
   my $weekdaycolor = $self->weekdaycolor() || $self->bgcolor();
   my $weekendcolor = $self->weekendcolor() || $self->bgcolor();
   my $todaycolor = $self->todaycolor() || $self->bgcolor();
   my $contentcolor = $self->contentcolor() || $self->contentcolor();
   my $weekdaycontentcolor = $self->weekdaycontentcolor() || $self->contentcolor();
   my $weekendcontentcolor = $self->weekendcontentcolor() || $self->contentcolor();
   my $todaycontentcolor = $self->todaycontentcolor() || $self->contentcolor();
   my $bordercolor = $self->bordercolor() || $self->bordercolor();
   my $weekdaybordercolor = $self->weekdaybordercolor() || $self->bordercolor();
   my $weekendbordercolor = $self->weekendbordercolor() || $self->bordercolor();
   my $todaybordercolor = $self->todaybordercolor() || $self->bordercolor();
   my $weekdayheadercolor = $self->weekdayheadercolor() || $self->bgcolor();
   my $weekendheadercolor = $self->weekendheadercolor() || $self->bgcolor();
   my $headercolor = $self->headercolor() || $self->bgcolor();
   # Get today's date, in case there's a todaycolor()
   my($todayyear,$todaymonth,$todaydate) = Date::Calc::Today();

   $html .= "<TABLE BORDER=\"$border\" WIDTH=\"$tablewidth\" BGCOLOR=\"$bgcolor\" BORDERCOLOR=\"$bordercolor\">\n";
   $html .= "<tr><td colspan=7 bgcolor=\"$headercolor\">$header</td></tr>\n" if $header;
   if ($self->showweekdayheaders) {
      # Ultimately, this will display a hashref contents instead of a static week...
      #$html .= "<tr>\n<th>Sunday</th>\n<th>Monday</th>\n<th>Tuesday</th>\n<th>Wednesday</th>\n<th>Thursday</th>\n<th>Friday</th>\n<th>Saturday</th>\n</tr>\n";
      $html .= "<tr>\n<th bgcolor=\"$weekendheadercolor\">Sunday</th>\n<th bgcolor=\"$weekdayheadercolor\">Monday</th>\n<th bgcolor=\"$weekdayheadercolor\">Tuesday</th>\n<th bgcolor=\"$weekdayheadercolor\">Wednesday</th>\n<th bgcolor=\"$weekdayheader...
   }
   foreach $WEEK (0 .. ($weeks-1)) {
      $html .= "<TR>\n";
      foreach $DAY (0 .. 6) {
         my($thiscontent,$thisday,$thisbgcolor,$thisbordercolor,$thiscontentcolor);
         $thisday = $days[((7*$WEEK)+$DAY)];
         # Get the cell content
         if (! $thisday) { # If it's a dummy cell, no content
            $thiscontent = '&nbsp;'; }
         else { # A real date cell with potential content
            # Get the content
            if ($self->showdatenumbers()) { 
                $thiscontent = "<p><b>$thisday</b>";
	
		# Add admin links if specified in function call	
		if ($addurl) {
			my $calid = $self->calendarid();
                        my $mdate = $self->month().'_'.$thisday.'_'.$self->year();
                        $thiscontent .= " <font size=\"1\"><a href=\"#\" onclick=popup(\"$addurl?date=$mdate&calid=$calid&view=standard\")>[Add]</a></font>";
                }
		if ($editurl) {
                        my $calid = $self->calendarid();
                        my $mdate = $self->month().'_'.$thisday.'_'.$self->year();                        
			$thiscontent .= " <font size=\"1\"><a href=\"#\" onclick=popup(\"$editurl?date=$mdate&calid=$calid&view=standard\")>[Edit]</a></font>";
                }
		$thiscontent .= "</p>\n";
            }
            # Content for this specific date
            $thiscontent .= $self->getcontent($thisday);
            # Content for "2nd Wednesday", etc.
            $thiscontent .= $self->getcontent(int(1+($thisday/7.1)).('sunday','monday','tuesday','wednesday','thursday','friday','saturday')[$DAY]);
            # Content for "Wednesdays", etc.
            $thiscontent .= $self->getcontent(('sundays','mondays','tuesdays','wednesdays','thursdays','fridays','saturdays')[$DAY]);
            # Normalize if there's no content
            $thiscontent .= '&nbsp;';
         }
         # Get the cell's coloration
         if ($self->year == $todayyear && $self->month == $todaymonth && $thisday == $todaydate)
                                              { $thisbgcolor = $todaycolor;
                                                $thisbordercolor = $todaybordercolor;
                                                $thiscontentcolor = $todaycontentcolor;
                                              }
         elsif (($DAY == 0) || ($DAY == 6))   { $thisbgcolor = $weekendcolor;
                                                $thisbordercolor = $weekendbordercolor;
                                                $thiscontentcolor = $weekendcontentcolor;
                                              }
         else                                 { $thisbgcolor = $weekdaycolor;
                                                $thisbordercolor = $weekdaybordercolor;
                                                $thiscontentcolor = $weekdaycontentcolor;
                                              }
         # Done with this cell - push it into the table
         $html .= "<TD WIDTH=\"$cellwidth\" VALIGN=\"$cellalignment\" ALIGN=\"$cellalignment\" BGCOLOR=\"$thisbgcolor\" BORDERCOLOR=\"$thisbordercolor\"><FONT COLOR=\"$thiscontentcolor\">$thiscontent</FONT></TD>\n";
      }
      $html .= "</TR>\n";
   }
   $html .= "</TABLE>\n";
   return $html;
}

sub jscode {
my $self = shift;
my $jscode = '<div id="object1" style="position:absolute; visibility:show; left:350px; top:-50px; z-index:2">layer hidden off the screen</div><SCRIPT LANGUAGE="JavaScript">
<!-- Begin
window.onLoad=setupDescriptions();
function popup(url) {
window.open(url, "popup", "height=480,width=580,menubar=no,scrollbars=yes,status=no,toolbar=no,screenX=100,screenY=0,left=100,top=100"); 
}
function setupDescriptions() {
var x = navigator.appVersion;
y = x.substring(0,4);
if (y>=4) setVariables();
}
var x,y,a,b;
function setVariables(){
if (navigator.appName == "Netscape") {
h=".left=";
v=".top=";
dS="document.";
sD="";
}
else 
{
h=".pixelLeft=";
v=".pixelTop=";
dS="";
sD=".style";
   }
}
var isNav = (navigator.appName.indexOf("Netscape") !=-1);
function popLayer(a){
var popWidth;
if(isNav) {
	if ((window.innerWidth - x) > (window.innerWidth/2)) {
		popWidth = window.innerWidth/2;
	}
	else {
		popWidth = window.innerWidth - x - 40;
	}
}
else { 
	if ((document.body.clientWidth - x) > (document.body.clientWidth/2)) {
		popWidth =  document.body.clientWidth/2;
	}
	else {
		popWidth = document.body.clientWidth - x - 40;
	}
}
desc = "<table cellpadding=3 border=1 bgcolor=ffff11 width=" + popWidth + "><td align=center>";'.$self->jsDescs.'
desc += "</td></table>";

if(isNav) {
document.object1.document.write(desc);
document.object1.document.close();
document.object1.left=x+5;
document.object1.top=y;
}
else {
object1.innerHTML=desc;
eval(dS+"object1"+sD+h+(x+5));
eval(dS+"object1"+sD+v+y);
   }
}
function hideLayer(a){
if(isNav) {
eval(document.object1.top=a);
}



( run in 1.721 second using v1.01-cache-2.11-cpan-364913b4093 )