File Coverage

blib/lib/HTML/CalendarMonthSimple/MultiDaysEvent.pm
Criterion Covered Total %
statement 207 228 90.7
branch 68 132 51.5
condition 66 172 38.3
subroutine 5 5 100.0
pod 0 3 0.0
total 346 540 64.0


line stmt bran cond sub pod time code
1             package HTML::CalendarMonthSimple::MultiDaysEvent;
2              
3             our $VERSION = '0.03';
4              
5 1     1   27088 use strict;
  1         2  
  1         45  
6 1     1   5 use base 'HTML::CalendarMonthSimple';
  1         2  
  1         1398  
7              
8             sub new {
9 1     1 0 743 my $class = shift;
10 1         13 my $self = $class->SUPER::new;;
11 1         892 bless $self,$class;
12 1         38 $self->{events}->{$_} = [] for (1..31);
13 1         7 return $self;
14             }
15              
16             sub multidays_HTML {
17 1     1 0 3 my $self = shift;
18 1         3 my %params = @_;
19 1         2 my $html = '';
20 1         2 my(@days,$weeks,$WEEK,$DAY);
21              
22             # To make the grid even, pad the start of the series with 0s
23 1         10 @days = (1 .. Date::Calc::Days_in_Month($self->year(),$self->month() ) );
24 1 50       44 if ($self->weekstartsonmonday()) {
25 0         0 foreach (1 .. (Date::Calc::Day_of_Week($self->year(),
26             $self->month(),1) -1 )) {
27 0         0 unshift(@days,0);
28             }
29             }
30             else {
31 1         11 foreach (1 .. (Date::Calc::Day_of_Week($self->year(),
32             $self->month(),1)%7) ) {
33 1         53 unshift(@days,0);
34             }
35             }
36 1         18 $weeks = int((scalar(@days)+6)/7);
37             # And pad the end as well, to avoid "uninitialized value" warnings
38 1         5 foreach (scalar(@days)+1 .. $weeks*7) {
39 4         7 push(@days,0);
40             }
41              
42             # Define some scalars for generating the table
43 1         8 my $border = $self->border();
44 1         12 my $tablewidth = $self->width();
45 1 50 50     11 $tablewidth =~ m/^(\d+)(\%?)$/; my $cellwidth = (int($1/7))||'14'; if ($2) { $cellwidth .= '%'; }
  1         6  
  1         6  
  1         2  
46 1         10 my $header = $self->header();
47 1         14 my $cellalignment = $self->cellalignment();
48 1         11 my $vcellalignment = $self->vcellalignment();
49 1         13 my $contentfontsize = $self->contentfontsize();
50 1         19 my $bgcolor = $self->bgcolor();
51 1   33     12 my $weekdaycolor = $self->weekdaycolor() || $self->bgcolor();
52 1   33     23 my $weekendcolor = $self->weekendcolor() || $self->bgcolor();
53 1   33     37 my $todaycolor = $self->todaycolor() || $self->bgcolor();
54 1   33     20 my $contentcolor = $self->contentcolor() || $self->contentcolor();
55 1   33     22 my $weekdaycontentcolor = $self->weekdaycontentcolor() || $self->contentcolor();
56 1   33     26 my $weekendcontentcolor = $self->weekendcontentcolor() || $self->contentcolor();
57 1   33     22 my $todaycontentcolor = $self->todaycontentcolor() || $self->contentcolor();
58 1   33     29 my $bordercolor = $self->bordercolor() || $self->bordercolor();
59 1   33     19 my $weekdaybordercolor = $self->weekdaybordercolor() || $self->bordercolor();
60 1   33     21 my $weekendbordercolor = $self->weekendbordercolor() || $self->bordercolor();
61 1   33     19 my $todaybordercolor = $self->todaybordercolor() || $self->bordercolor();
62 1   33     33 my $weekdayheadercolor = $self->weekdayheadercolor() || $self->bgcolor();
63 1   33     27 my $weekendheadercolor = $self->weekendheadercolor() || $self->bgcolor();
64 1   33     20 my $headercontentcolor = $self->headercontentcolor() || $self->contentcolor();
65 1   33     26 my $weekdayheadercontentcolor = $self->weekdayheadercontentcolor() || $self->contentcolor();
66 1   33     23 my $weekendheadercontentcolor = $self->weekendheadercontentcolor() || $self->contentcolor();
67 1   33     21 my $headercolor = $self->headercolor() || $self->bgcolor();
68 1         23 my $cellpadding = $self->cellpadding();
69 1         13 my $cellspacing = $self->cellspacing();
70 1         19 my $sharpborders = $self->sharpborders();
71 1         12 my $cellheight = $self->cellheight();
72 1         11 my $cellclass = $self->cellclass();
73 1         18 my $tableclass = $self->tableclass();
74 1   33     28 my $weekdaycellclass = $self->weekdaycellclass() || $self->cellclass();
75 1   33     102 my $weekendcellclass = $self->weekendcellclass() || $self->cellclass();
76 1   33     19 my $todaycellclass = $self->todaycellclass() || $self->cellclass();
77 1   33     30 my $headerclass = $self->headerclass() || $self->cellclass();
78 1         23 my $nowrap = $self->nowrap();
79              
80             # Get today's date, in case there's a todaycolor()
81 1         13 my($todayyear,$todaymonth,$todaydate) = ($self->today_year(),$self->today_month(),$self->today_date());
82              
83             # the table declaration - sharpborders wraps the table inside a table cell
84 1 50       19 if ($sharpborders) {
85 0         0 $html .= "\n"; \n\n
86 0 0       0 $html .= " class=\"$tableclass\"" if defined $tableclass;
87 0 0       0 $html .= " width=\"$tablewidth\"" if defined $tablewidth;
88 0         0 $html .= " cellpadding=\"0\" cellspacing=\"0\">\n";
89 0         0 $html .= "
90 0         0 $html .= "
91 0 0       0 $html .= " bgcolor=\"$bordercolor\"" if defined $bordercolor;
92 0         0 $html .= ">";
93 0         0 $html .= ""; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n"; \n";
94             }
95             else {
96 1         2 $html .= "
97 1 50       3 $html .= " class=\"$tableclass\"" if defined $tableclass;
98 1 50       5 $html .= " border=\"$border\"" if defined $border;
99 1 50       5 $html .= " width=\"$tablewidth\"" if defined $tablewidth;
100 1 50       4 $html .= " bgcolor=\"$bgcolor\"" if defined $bgcolor;
101 1 50       3 $html .= " bordercolor=\"$bordercolor\"" if defined $bordercolor;
102 1 50       3 $html .= " cellpadding=\"$cellpadding\"" if defined $cellpadding;
103 1 50       4 $html .= " cellspacing=\"$cellspacing\"" if defined $cellspacing;
104 1         2 $html .= ">\n";
105             }
106             # the header
107 1 50       3 if ($header) {
108 1         2 $html .= "
109 1 50       13 $html .= " bgcolor=\"$headercolor\"" if defined $headercolor;
110 1 50       3 $html .= " class=\"$headerclass\"" if defined $headerclass;
111 1         3 $html .= ">";
112 1 50       3 $html .= "" if defined $headercontentcolor;
113 1         2 $html .= $header;
114 1 50       3 $html .= "" if defined $headercontentcolor;
115 1         2 $html .= "
116             }
117             # the names of the days of the week
118 1 50       8 if ($self->showweekdayheaders) {
119 1 50       15 my $celltype = $self->weekdayheadersbig() ? "th" : "td";
120 1         15 my @weekdays = $self->weekdays();
121              
122 1 50       32 my $saturday_html = "<$celltype"
    50          
    50          
    50          
123             . ( defined $weekendheadercolor
124             ? qq| bgcolor="$weekendheadercolor"|
125             : '' )
126             . ( defined $weekendcellclass
127             ? qq| class="$weekendcellclass"|
128             : '' )
129             . ">"
130             . ( defined $weekendheadercontentcolor
131             ? qq||
132             : '' )
133             . $self->saturday()
134             . ( defined $weekendheadercontentcolor
135             ? qq||
136             : '' )
137             . "\n";
138              
139 1 50       49 my $sunday_html = "<$celltype"
    50          
    50          
    50          
140             . ( defined $weekendheadercolor
141             ? qq| bgcolor="$weekendheadercolor"|
142             : '' )
143             . ( defined $weekendcellclass
144             ? qq| class="$weekendcellclass"|
145             : '' )
146             . ">"
147             . ( defined $weekendheadercontentcolor
148             ? qq||
149             : '' )
150             . $self->sunday()
151             . ( defined $weekendheadercontentcolor
152             ? qq||
153             : '' )
154             . "\n";
155            
156 1         19 my $weekday_html = '';
157 1         4 foreach (@weekdays) { # draw the weekday headers
158              
159 5 50       67 $weekday_html .= "<$celltype"
    50          
    50          
    50          
160             . ( defined $weekendheadercolor
161             ? qq| bgcolor="$weekdayheadercolor"|
162             : '' )
163             . ( defined $weekendcellclass
164             ? qq| class="$weekdaycellclass"|
165             : '' )
166             . ">"
167             . ( defined $weekdayheadercontentcolor
168             ? qq||
169             : '' )
170             . $_
171             . ( defined $weekdayheadercontentcolor
172             ? qq||
173             : '' )
174             . "\n";
175             }
176              
177 1         4 $html .= "
178 1 50       4 if ($self->weekstartsonmonday()) {
179 0         0 $html .= $weekday_html
180             . $saturday_html
181             . $sunday_html;
182             }
183             else {
184 1         12 $html .= $sunday_html
185             . $weekday_html
186             . $saturday_html;
187             }
188 1         4 $html .= "
189             }
190              
191 1         2 my $_saturday_index = 6;
192 1         2 my $_sunday_index = 0;
193 1 50       5 if ($self->weekstartsonmonday()) {
194 0         0 $_saturday_index = 5;
195 0         0 $_sunday_index = 6;
196             }
197             # now do each day, the actual date-content-containing cells
198 1         12 foreach $WEEK (0 .. ($weeks-1)) {
199 5         8 my $weekevents = 0;
200 5         7 my %prerow;
201             my %daycol;
202 5         15 my $firstday = $days[(7*$WEEK)];
203 5         9 my $lastday = $firstday + 6;
204 5         5 my $firstevent;
205              
206 5         11 for $DAY (0 .. 6) {
207 35         144 my $thisday = $days[((7*$WEEK)+$DAY)];
208 35 100       34 if ($#{$self->{events}->{$thisday}} > -1) {
  35         136  
209 1 50       5 $firstevent = $thisday unless ($firstevent);
210 1         2 $weekevents += scalar(@{$self->{events}->{$thisday}});
  1         3  
211 1         2 for my $event (sort { $a->{length} <=> $b->{length} } (@{$self->{events}->{$thisday}})) {
  0         0  
  1         4  
212 1         5 for (0 .. $event->{length} - 1) {
213 3 50       52 $prerow{$thisday+$_} = $weekevents unless ($prerow{$thisday+$_});
214             }
215 1 50       6 if ($thisday + ($event->{length} - 1) > $lastday) {
216 0         0 $self->add_event( date => $lastday+1,
217             event => $event->{event},
218             length => $event->{length} -
219             ($lastday - $thisday + 1) );
220 0         0 $event->{length} = $lastday-$thisday+1;
221             }
222             }
223             }
224             }
225 5         10 $html .= "
226 5         10 foreach $DAY ( 0 .. 6 ) {
227 35         37 my($thiscontent,$thisday,$thisbgcolor,$thisbordercolor,$thiscontentcolor,$thiscellclass);
228 35         60 $thisday = $days[((7*$WEEK)+$DAY)];
229              
230             # Get the cell's coloration and CSS class
231 35 100 33     99 if ($self->year == $todayyear && $self->month == $todaymonth && $thisday == $todaydate) {
    100 66        
      100        
232 1   33     17 $thisbgcolor = $self->datecolor($thisday) || $todaycolor;
233 1   33     26 $thisbordercolor = $self->datebordercolor($thisday) || $todaybordercolor;
234 1   33     25 $thiscontentcolor = $self->datecontentcolor($thisday) || $todaycontentcolor;
235 1   33     22 $thiscellclass = $self->datecellclass($thisday) || $todaycellclass;
236             } elsif (($DAY == $_sunday_index) || ($DAY == $_saturday_index)) {
237 10   33     183 $thisbgcolor = $self->datecolor($thisday) || $weekendcolor;
238 10   33     203 $thisbordercolor = $self->datebordercolor($thisday) || $weekendbordercolor;
239 10   33     188 $thiscontentcolor = $self->datecontentcolor($thisday) || $weekendcontentcolor;
240 10   33     267 $thiscellclass = $self->datecellclass($thisday) || $weekendcellclass;
241             } else {
242 24   33     403 $thisbgcolor = $self->datecolor($thisday) || $weekdaycolor;
243 24   33     444 $thisbordercolor = $self->datebordercolor($thisday) || $weekdaybordercolor;
244 24   33     417 $thiscontentcolor = $self->datecontentcolor($thisday) || $weekdaycontentcolor;
245 24   33     430 $thiscellclass = $self->datecellclass($thisday) || $weekdaycellclass;
246             }
247              
248             # mark the date, and we should count the events first.
249 35   66     566 my $rowspan = $prerow{$thisday} || (1 + $weekevents);
250             # Done with this cell - push it into the table
251 35         53 $html .= "
252 35 50       77 $html .= " nowrap" if $nowrap;
253 35 50       69 $html .= " class=\"$thiscellclass\"" if defined $thiscellclass;
254 35 50       61 $html .= " height=\"$cellheight\"" if defined $cellheight;
255 35 50       85 $html .= " width=\"$cellwidth\"" if defined $cellwidth;
256 35         46 $html .= " valign=\"top\"";
257 35 50       260 $html .= " align=\"$cellalignment\"" if defined $cellalignment;
258 35 50       65 $html .= " bgcolor=\"$thisbgcolor\"" if defined $thisbgcolor;
259 35 50       66 $html .= " bordercolor=\"$thisbordercolor\"" if defined $thisbordercolor;
260 35         72 $html .= " rowspan=\"$rowspan\"";
261 35         42 $html .= ">";
262 35 50 33     246 $html .= "
263             defined $contentfontsize);
264 35 50       67 $html .= " color=\"$thiscontentcolor\"" if defined $thiscontentcolor;
265 35 50       69 $html .= " size=\"$contentfontsize\"" if defined $contentfontsize;
266 35 50 33     130 $html .= ">" if (defined $thiscontentcolor ||
267             defined $contentfontsize);
268 35 100       95 $html .= "$thisday\n" if ($thisday);
269 35 50 33     140 $html .= "" if (defined $thiscontentcolor ||
270             defined $contentfontsize);
271 35         72 $html .= "
272             }
273 5         9 $html .= "
274 5         7 my $i = 1;
275 5         11 for $DAY (0 .. 6) {
276 35         51 my $thisday = $days[((7*$WEEK)+$DAY)];
277 35 50       167 if (exists $self->{events}->{$thisday}) {
278 35         50 for my $event (sort { $a->{length} <=> $b->{length} } @{$self->{events}->{$thisday}}) {
  0         0  
  35         119  
279 1         3 my $colspan = $event->{length};
280 1         3 my $preoffset = $weekevents - $i + 1;
281 1         3 $html .= "
282 1         4 for (0 .. ($thisday - $firstevent - 1)) {
283 0 0       0 if ($preoffset) {
284 0         0 $html .= "
285             }
286             }
287 1         6 $html .= "".$event->{event}."
288 1         2 $html .= "
289 1         1 $firstevent = $thisday;
290 1         4 $i++;
291             }
292             }
293             }
294 5         11 $html .= "
295 5         6 foreach $DAY ( 0 .. 6 ) {
296 35         51 my($thiscontent,$thisday,$thisbgcolor,$thisbordercolor,$thiscontentcolor,$thiscellclass);
297 35         61 $thisday = $days[((7*$WEEK)+$DAY)];
298              
299             # Get the cell's coloration and CSS class
300 35 100 33     93 if ($self->year == $todayyear && $self->month == $todaymonth && $thisday == $todaydate) {
    100 66        
      100        
301 1   33     18 $thisbgcolor = $self->datecolor($thisday) || $todaycolor;
302 1   33     32 $thisbordercolor = $self->datebordercolor($thisday) || $todaybordercolor;
303 1   33     22 $thiscontentcolor = $self->datecontentcolor($thisday) || $todaycontentcolor;
304 1   33     29 $thiscellclass = $self->datecellclass($thisday) || $todaycellclass;
305             } elsif (($DAY == $_sunday_index) || ($DAY == $_saturday_index)) {
306 10   33     264 $thisbgcolor = $self->datecolor($thisday) || $weekendcolor;
307 10   33     190 $thisbordercolor = $self->datebordercolor($thisday) || $weekendbordercolor;
308 10   33     187 $thiscontentcolor = $self->datecontentcolor($thisday) || $weekendcontentcolor;
309 10   33     335 $thiscellclass = $self->datecellclass($thisday) || $weekendcellclass;
310             } else {
311 24   33     397 $thisbgcolor = $self->datecolor($thisday) || $weekdaycolor;
312 24   33     433 $thisbordercolor = $self->datebordercolor($thisday) || $weekdaybordercolor;
313 24   33     408 $thiscontentcolor = $self->datecontentcolor($thisday) || $weekdaycontentcolor;
314 24   33     401 $thiscellclass = $self->datecellclass($thisday) || $weekdaycellclass;
315             }
316              
317             # mark the date, and we should count the events first.
318 35   66     640 my $rowspan = $prerow{$thisday} || (1+$weekevents);
319             # Done with this cell - push it into the table
320 35         96 $html .= "
321 35 50       73 $html .= " nowrap" if $nowrap;
322 35 50       71 $html .= " class=\"$thiscellclass\"" if defined $thiscellclass;
323 35 50       59 $html .= " height=\"$cellheight\"" if defined $cellheight;
324 35 50       89 $html .= " width=\"$cellwidth\"" if defined $cellwidth;
325 35 50       82 $html .= " valign=\"$vcellalignment\"" if defined $vcellalignment;
326 35 50       76 $html .= " align=\"$cellalignment\"" if defined $cellalignment;
327 35 50       79 $html .= " bgcolor=\"$thisbgcolor\"" if defined $thisbgcolor;
328 35 50       163 $html .= " bordercolor=\"$thisbordercolor\"" if defined $thisbordercolor;
329 35         38 $html .= "> ";
330 35         90 $html .= "
331             }
332             }
333 1         4 $html .= "
\n";
334              
335             # if sharpborders, we need to break out of the enclosing table cell
336 1 50       5 if ($sharpborders) {
337 0         0 $html .= "
\n";
338             }
339              
340 1         33 return $html;
341             }
342              
343             sub add_event {
344 1     1 0 3 my $self = shift;
345 1         5 my %params = @_;
346 1         2 my $date = $params{date};
347 1         13 $date =~ s/^0*//;
348 1         3 my $event = {};
349 1         3 $event->{event} = $params{event};
350 1   50     5 $event->{length} = $params{length} || 1;
351 1         3 push @{$self->{events}->{$date}}, $event;
  1         3  
352 1         5 return 1;
353             }
354              
355             1;
356              
357             __END__