d86a5b5cbc4188c3036ac6bee4710a7f790da3ca
[freeside.git] / rt / share / html / Search / Calendar.html
1 <%ARGS>
2 $Month       => (localtime)[4]
3 $Year        => (localtime)[5] + 1900
4 $Query       => undef
5 $Format      => undef
6 $Order       => undef
7 $OrderBy     => undef
8 $RowsPerPage => undef
9 $NewQuery    => 0
10 $WeekDay     => undef
11 $WeekMonth   => undef
12 $WeekYear    => undef
13 $OrigMonth   => undef
14 $OrigYear    => undef
15 $Embed       => undef
16 $Display     => undef
17 @DisplayArgs => ()
18 $slots       => $default_slots
19 </%ARGS>
20
21 % my $title;
22 % if ( $WeekMonth ) {
23 %   if ( $start->month == $end->month ) {
24 %     $title = $rtdate->GetMonth( $start->month -1 ). ' '.
25 %              $start->day. '-'. $end->day. ', '. $start->year;
26 %   } elsif ( $start->year == $end->year ) {
27 %     $title =
28 %       $rtdate->GetMonth( $start->month -1 ). ' '. $start->day. ' - '.
29 %       $rtdate->GetMonth( $end->month  -1 ). ' '. $end->day.  ', '. $end->year;
30 %   } else {
31 %     $title =
32 %       $rtdate->GetMonth( $start->month -1 ).' '.$start->day.', '.$start->year.
33 %       ' - '.
34 %       $rtdate->GetMonth( $end->month  -1 ). ' '.$end->day.  ', '. $end->year;
35 %   }
36 % } else {
37 %   $title = $rtdate->GetMonth($Month) . " $Year" 
38 % }
39
40 % unless ( $Embed ) {
41
42 <& /Elements/Header, Title => $title &>
43 <& /Elements/Tabs &>
44
45 % }
46
47 <&| /Widgets/TitleBox,
48      title => loc('Calendar for '). $title,
49      title_class=> 'inverse',
50      color => "#993333" &>
51
52 % my($PMonth, $PYear, $NMonth, $NYear);
53 % unless ( $WeekDay ) {
54
55     <table width="100%">
56       <tr>
57         <td align="left">
58 % ($PMonth, $PYear) = ($Month - 1, $Year);
59 % if ($PMonth < 0) {
60 %    $PYear--;
61 %    $PMonth = 11;
62 % }
63           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
64         </td>
65         <th align="center">
66           <font size="+1"><% $rtdate->GetMonth($Month). " $Year" %></font>
67         </th>
68         <td align="right">
69 % ($NMonth, $NYear) = ($Month + 1, $Year);
70 % if ($NMonth > 11) {
71 %    $NYear++;
72 %    $NMonth = 0;
73 % }
74           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
75         </td>
76       </tr>
77     </table>
78 % }
79
80 <table class="rtxcalendar">
81
82   <thead>
83
84 % unless ( $WeekDay ) {
85
86     <tr>
87       <td class="labels" colspan=<% $WeekDay ? 2 : 1 %>></td>
88 %     for ( @{$week{$weekstart}} ) {
89         <th colspan=<% $WeekDay ? $slots : 1 %>><%$rtdate->GetWeekday($_)%></th>
90 %     }
91     </tr>
92   </thead>
93   <tbody>
94
95 % }
96
97 <tr>
98
99 % unless ( $WeekDay ) {
100   <& td_week_expand, date=>$start, Month=>$Month, Year=>$Year, QueryString=>$QueryString, Embed=>$Embed, &>
101 % }
102
103 % if ( $WeekDay ) {
104
105     <td class="labels" colspan=2></td>
106
107 %   my $date = $start;
108 %   my $sday = 0;
109 %   while ($date <= $end) {
110 %
111 %     my $is_today     = (DateTime->compare($today,     $date) == 0);
112 %     my $is_yesterday = (DateTime->compare($yesterday, $date) == 0);
113 %     my $is_aweekago  = (DateTime->compare($aweekago,  $date) == 0);
114
115       <th colspan=<%$slots%>
116           class="labels"
117 %#                 <%   $is_today     ? 'today'
118 %#                    : $is_yesterday ? 'yesterday'
119 %#                    : $is_aweekago  ? 'aweekago'
120 %#                    : ''
121 %#                 %>"
122 %#        <div class="<%   $is_today ? 'todays'
123 %#                       : ''
124 %#                    %>calendardate"
125       ><% $rtdate->GetWeekday( $week{$weekstart}->[$sday++] ). ' '.
126           $rtdate->GetMonth($date->month-1). ' '.
127           $date->day
128        %>
129       </th>
130
131 %     $date = $set->next($date);
132 %     if ( $date->day_of_week == $startday_of_week ) {
133         </tr>
134 %       if ( $date <= $end ) { #a second week? not going to work for week view yet
135           <tr>
136               <td class="controls" rowspan=<% $rowspan + 2 %> valign="middle">
137                 <& week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString, Embed=>$Embed, &>
138               </td>
139 %       }
140 %     }
141
142 %   } #while ($date <= $end)
143
144     </tr>
145
146         </thead>
147         <tbody>
148
149     <tr>
150
151   <td class="controls" rowspan=<% $rowspan+1 %> valign="middle">
152     <& week_collapse, date=>$start, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString, Embed=>$Embed, &>
153   </td>
154
155 %     #slot header callback
156 %     if ( defined($Display) && $Display =~ /^(\w+)$/ ) {
157 %
158 %       my $el = "/Elements/CalendarSlotHeader$1";
159         <td class="labels"></td>
160 %       for (0..6) {
161           <& $el, Date      => $date,
162                   DateTypes => \%DateTypes,
163                   slots     => $slots,
164                   @DisplayArgs,
165           &>
166 %       }
167 %     }
168     </tr>
169
170 %   foreach my $row ( @week_rows ) {
171
172       <tr>
173       <& td_time, $row &>
174
175 %     $date = $start;
176 %     my $sday = 0;
177 %     while ($date <= $end) {
178 %
179 %       my $is_today     = (DateTime->compare($today,     $date) == 0);
180 %       my $is_yesterday = (DateTime->compare($yesterday, $date) == 0);
181 %       my $is_aweekago  = (DateTime->compare($aweekago,  $date) == 0);
182
183 %     #slot callback
184 %     if ( defined($Display) && $Display =~ /^(\w+)$/ ) {
185 %       my $el = "/Elements/CalendarSlot$1";
186         <& $el, Date      => $date,
187                 DateTypes => \%DateTypes,
188                 Tickets   => $Tickets{$date->strftime("%F")} || [],
189                 slots     => $slots,
190                 sday      => $sday,
191                 tod_row   => $row,
192                 timestep  => $timestep,
193                 @DisplayArgs,
194         &>
195 %     } else {
196
197 %       #just display the normal events for this day?
198         <td class="weekly" colspan="<%$slots%>">
199 %         for my $Ticket ( @{ $Tickets{$date->strftime("%F")} } ) {
200 %           my %dt =
201 %             map { $_=>1 }
202 %             grep {
203 %               my $meth = $_.'Obj';
204 %
205 %               my($m, $h) = ($Ticket->$meth->Localtime('user'))[1,2];
206 %               my $tod = $h*60 + $m;
207 %
208 %               LocalDate($Ticket->$meth->Unix) eq $date->strftime('%F') #today
209 %                 && $tod >= $row && $tod < ($row+$timestep); #and in timeslot
210 %             } keys %DateTypes;
211 %           next unless keys %dt;
212             <& /Elements/CalendarEvent,
213                  Object    => $Ticket,
214                  Date      => $date,
215                  DateTypes => \%dt
216             &>
217 %           unless ( $Ticket eq ${ $Tickets{$date->strftime("%F")} }[-1] ) { #hmm, no.. not with "next unless $dt" :/
218               <BR>
219 %           }
220 %         }
221         </td>
222
223 %     }
224
225 %       $date = $set->next($date);
226 %       if ( $date->day_of_week == $startday_of_week ) {
227           </tr>
228 %         if ( $date <= $end ) { #a second week? not going to work for week view yet
229             <tr>
230               <td class="controls" rowspan=<% $rowspan + 2 %> valign="middle">
231                   <& week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString, Embed=>$Embed, &>
232                 </td>
233 %         }
234 %       }
235 %
236 %       $sday++;
237 %     } #while ($date <= $end)
238       </tr>
239 %   } #foreach my $row ( @week_rows )
240 %
241 % } else {
242 %
243 %   my $date = $start;
244 %   while ($date <= $end) {
245 %
246 %     my $offmonth = !$WeekDay && $date->month != ($Month + 1);
247 %     my $is_today     = (DateTime->compare($today,     $date) == 0);
248 %     my $is_yesterday = (DateTime->compare($yesterday, $date) == 0);
249 %     my $is_aweekago  = (DateTime->compare($aweekago,  $date) == 0);
250
251       <td
252           class="<%   $offmonth     ? 'offmonth'
253                     : $is_today     ? 'today'
254                     : $is_yesterday ? 'yesterday'
255                     : $is_aweekago  ? 'aweekago'
256                     : ''
257                  %>"
258       >
259         <div class="<%   $is_today ? 'todays'
260                        : $offmonth ? 'offmonth'
261                        : ''
262                     %>calendardate"
263         ><% $date->day %></div>
264
265 %       if ( defined($Display) && $Display =~ /^(\w+)$/ ) {
266 %
267 %         my $el = "/Elements/CalendarDay$1";
268           <& $el, CurrentUser => $session{CurrentUser},
269                   Tickets     => $Tickets{$date->strftime("%F")},
270                   Date        => $date,
271                   DateTypes   => \%DateTypes,
272                   @DisplayArgs,
273           &>
274 %
275 %       } else {
276 %
277 %         my $sp = 3;
278 %         for my $t ( @{ $Tickets{$date->strftime("%F")} } ) {
279 %           $sp--;
280             <& /Elements/CalendarEvent, Object => $t, Date => $date, DateTypes => \%DateTypes &>
281 %         }
282           <% ($sp>0) ? '<BR>'x$sp : '' |n %>
283 %
284 %       }
285
286       </td>
287
288 %     $date = $set->next($date);
289 %     if ( $date->day_of_week == $startday_of_week ) {
290         </tr>
291 %       if ( $date <= $end ) {
292           <tr>
293             <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString, Embed=>$Embed, &>
294 %       }
295 %     }
296
297 %   } #while ($date <= $end)
298
299 % }
300 </tbody>
301 </table>
302
303 % unless ( $WeekDay ) {
304
305     <table width="100%">
306       <tr>
307         <td align="left">
308           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
309         </td>
310
311         <td valign="top" align="center">
312           <form action="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?<%$QueryString%>" method="post">
313
314             <select name="Month">
315 %             for (0..11) {
316                 <option value="<%$_%>" <% $_ == $Month ? 'selected' : ''%> ><%$rtdate->GetMonth($_)%></option>
317 %             }
318             </select>
319
320 %           my $year = (localtime)[5] + 1900;
321             <select name="Year">
322 %             for ( ($year-5) .. ($year+5)) {
323                 <option value="<%$_%>" <% $_ == $Year ? 'selected' : ''%>><%$_%></option>
324 %             }
325             </select>
326
327 %# <& /Elements/Submit&>
328             <input type="submit" value="<% loc('Submit') %>" class="button" />
329
330           </form>
331         </td>
332
333         <td align="right">
334           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
335         </td>
336       </tr>
337   </table>
338
339 % }
340
341 % unless ( $Embed ) {
342
343 <table width="100%">
344 <tr>
345
346 <td valign="top" rowspan=9>
347   <BR>
348   <a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
349 </td>
350
351 % foreach my $legend (keys %legend) {
352     <tr>
353       <td align="right">
354         <img src="<%$RT::WebImagesURL%>/<%$legend%>.png" />
355       </td>
356       <td align="left">
357 %       my $more = 0;
358 %       foreach ( @{$legend{$legend}} ) {
359           <% $more++ ? ', ' : '' %>
360           <&|/l&><% $_ %></&>
361 %       }
362       </td>
363     </tr>
364 % }
365
366 </table>
367
368 % }
369
370 </&>
371
372 <%ONCE>
373
374 my %legend = (
375   'created'     => ['Created'],
376   'due'         => ['Due'],
377   'resolved'    => ['Resolved'],
378   'updated'     => ['Last Updated'],
379   'created_due' => ['Created','Due'],
380   'reminder'    => ['Reminders'],
381   'started'     => ['Started'],
382   'starts_due'  => ['Starts','Due'],
383 );
384
385 my $stime    = RT->Config->Get('CalendarWeeklyStartMin');
386 $stime = 480 unless $stime =~ /^\d+$/; #8am
387 my $etime    = RT->Config->Get('CalendarWeeklyEndMin');
388 $etime = 1080 unless $etime =~ /^\d+$/; #6pm
389
390 my $timestep =  RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h
391 my $rowspan = ($etime-$stime) / $timestep;
392
393 my $default_slots = RT->Config->Get('CalendarWeeklySlots') || 5;
394
395 my $wt = $stime;
396 my @week_rows = ();
397 while ( $wt < $etime ) { push @week_rows, $wt; $wt+=$timestep }
398
399 </%ONCE>
400 <%INIT>
401 use RTx::Calendar qw( FirstDay LastDay LastDayOfWeek DatesClauses LocalDate
402                       SearchDefaultCalendar FindTickets );
403
404 $Embed =~ /^[\w\.]*$/ or die 'xss';
405
406 my $title = loc("Calendar");
407
408 my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;
409
410 my $rtdate = RT::Date->new($session{'CurrentUser'});
411
412 my $time_zone = $session{'CurrentUser'}->UserObj->Timezone
413                   || RT->Config->Get('Timezone');
414
415 my $weekstart = 'Sunday'; #RT::SiteConfig?  user pref?
416 my %week = (
417   'Saturday' => [6,0..5],
418   'Sunday'   => [0..6],
419   'Monday'   => [1..6,0],
420 );
421 my $startday_of_week = ${$week{$weekstart}}[0]  || 7;
422 my $endday_of_week   = ${$week{$weekstart}}[-1] || 7;
423
424 my $today = DateTime->today( time_zone=>$time_zone );
425 my $yesterday = $today->clone->subtract( days=>1 );
426 my $aweekago  = $today->clone->subtract( days=>7 );
427
428 my( $start, $end );
429 if ( $WeekDay ) {
430   $start = DateTime->new( year      => $WeekYear,
431                           month     => $WeekMonth+1,
432                           day       => $WeekDay,
433                           time_zone => $time_zone,
434                         );
435   $end  = LastDayOfWeek( $WeekYear, $WeekMonth+1, $WeekDay, $endday_of_week );
436 } else {
437   $start = FirstDay($Year, $Month + 1, $startday_of_week );
438   $end   = LastDay ($Year, $Month + 1, $endday_of_week );
439 }
440
441 # use this to loop over days until $end
442 my $set = DateTime::Set->from_recurrence(
443     next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
444 );
445
446 my $QueryString;
447 if ($Query) {
448   $QueryString =
449       $m->comp(
450         '/Elements/QueryString',
451         Query   => $Query,
452         Format  => $Format,
453         Order   => $Order,
454         OrderBy => $OrderBy,
455         Rows    => $RowsPerPage,
456         @DisplayArgs,
457       );
458 } else {
459   $QueryString =
460       $m->comp(
461         '/Elements/QueryString',
462         NewQuery => 1,
463         @DisplayArgs,
464       );
465 }
466
467 # Default Query and Format
468 my $TempFormat = "__Starts__ __Due__";
469 my $TempQuery = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
470  AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody'  )
471  AND ( Type = 'reminder' OR 'Type' = 'ticket' )";
472
473 if ( my $Search = SearchDefaultCalendar($session{CurrentUser}) ) {
474   $TempFormat = $Search->SubValue('Format');
475   $TempQuery = $Search->SubValue('Query');
476 }
477
478 # we overide them if needed
479 $TempQuery  = $Query  if $Query;
480 $TempFormat = $Format if $Format;
481
482 # we search all date types in Format string
483 my @Dates = grep { $TempFormat =~ m/__${_}(Relative)?__/ } @DateTypes;
484
485 # used to display or not a date in Element/CalendarEvent
486 my %DateTypes = map { $_ => 1 } @Dates;
487
488 $TempQuery .= DatesClauses(\@Dates, $start->strftime("%F"), $end->strftime("%F"));
489
490 # print STDERR ("-" x 30), "\n", $TempQuery, "\n";
491
492 my %Tickets = FindTickets($session{'CurrentUser'}, $TempQuery, \@Dates, $start->strftime("%F"), $end->strftime("%F"));
493
494 </%INIT>
495 <%def td_week_expand>
496 <%args>
497   $date  => undef
498   $Month => undef
499   $Year  => undef
500   $QueryString => undef
501   $Embed => undef
502 </%args>
503   <td class="controls">
504     <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?WeekDay=<% $date->day %>&WeekMonth=<% $date->month -1 %>&WeekYear=<% $date->year %>&OrigMonth=<% $Month %>&OrigYear=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-expand.gif"></a>
505   </td>
506 </%def>
507
508 <%def week_collapse>
509 <%args>
510   $date  => undef
511   $Month => undef
512   $Year  => undef
513   $QueryString => undef
514   $Embed => undef
515 </%args>
516     <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<% $Month %>&Year=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-collapse.gif" STYLE="height:384px;width:11px"></a>
517 </%def>
518
519 <%def td_time>
520 % my( $min ) = shift;
521     <td class="labels"><% pretty_time($min) |n %></td>
522 <%init>
523 sub pretty_time {
524   my $t = shift;
525
526   return 'Midnight' if $t == 0 || $t == 1440;
527   return 'Noon'     if $t == 720;
528
529   my $h = int( $t / 60 );
530   my $m = $t % 60;
531
532   my $ap = 'AM';
533   if    ( $h == 0 || $h == 24 ) { $h = 12; }
534   elsif ( $h == 12 )           { $ap = 'PM'; }
535   elsif ( $h > 12 )            { $ap = 'PM'; $h -= 12; }
536
537   sprintf('%02d:%02d&nbsp;'.$ap, $h, $m);
538
539 }
540 </%init>
541 </%def>