installers (calendaring), RT#16584
[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 %               RTx::Calendar::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, Tickets   => $Tickets{$date->strftime("%F")},
269                   Date      => $date,
270                   DateTypes => \%DateTypes,
271                   @DisplayArgs,
272           &>
273 %
274 %       } else {
275 %
276 %         my $sp = 3;
277 %         for my $t ( @{ $Tickets{$date->strftime("%F")} } ) {
278 %           $sp--;
279             <& /Elements/CalendarEvent, Object => $t, Date => $date, DateTypes => \%DateTypes &>
280 %         }
281           <% ($sp>0) ? '<BR>'x$sp : '' |n %>
282 %
283 %       }
284
285       </td>
286
287 %     $date = $set->next($date);
288 %     if ( $date->day_of_week == $startday_of_week ) {
289         </tr>
290 %       if ( $date <= $end ) {
291           <tr>
292             <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString, Embed=>$Embed, &>
293 %       }
294 %     }
295
296 %   } #while ($date <= $end)
297
298 % }
299 </tbody>
300 </table>
301
302 % unless ( $WeekDay ) {
303
304     <table width="100%">
305       <tr>
306         <td align="left">
307           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
308         </td>
309
310         <td valign="top" align="center">
311           <form action="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?<%$QueryString%>" method="post">
312
313             <select name="Month">
314 %             for (0..11) {
315                 <option value="<%$_%>" <% $_ == $Month ? 'selected' : ''%> ><%$rtdate->GetMonth($_)%></option>
316 %             }
317             </select>
318
319 %           my $year = (localtime)[5] + 1900;
320             <select name="Year">
321 %             for ( ($year-5) .. ($year+5)) {
322                 <option value="<%$_%>" <% $_ == $Year ? 'selected' : ''%>><%$_%></option>
323 %             }
324             </select>
325
326 %# <& /Elements/Submit&>
327             <input type="submit" value="<% loc('Submit') %>" class="button" />
328
329           </form>
330         </td>
331
332         <td align="right">
333           <a href="<%$RT::WebPath%>/Search/<%$Embed||'Calendar.html'%>?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
334         </td>
335       </tr>
336   </table>
337
338 % }
339
340 % unless ( $Embed ) {
341
342 <table width="100%">
343 <tr>
344
345 <td valign="top" rowspan=9>
346   <BR>
347   <a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
348 </td>
349
350 % foreach my $legend (keys %legend) {
351     <tr>
352       <td align="right">
353         <img src="<%$RT::WebImagesURL%>/<%$legend%>.png" />
354       </td>
355       <td align="left">
356 %       my $more = 0;
357 %       foreach ( @{$legend{$legend}} ) {
358           <% $more++ ? ', ' : '' %>
359           <&|/l&><% $_ %></&>
360 %       }
361       </td>
362     </tr>
363 % }
364
365 </table>
366
367 % }
368
369 </&>
370
371 <%ONCE>
372
373 my %legend = (
374   'created'     => ['Created'],
375   'due'         => ['Due'],
376   'resolved'    => ['Resolved'],
377   'updated'     => ['Last Updated'],
378   'created_due' => ['Created','Due'],
379   'reminder'    => ['Reminders'],
380   'started'     => ['Started'],
381   'starts_due'  => ['Starts','Due'],
382 );
383
384 my $stime    = RT->Config->Get('CalendarWeeklyStartMin');
385 $stime = 480 unless $stime =~ /^\d+$/; #8am
386 my $etime    = RT->Config->Get('CalendarWeeklyEndMin');
387 $etime = 1080 unless $etime =~ /^\d+$/; #6pm
388
389 my $timestep =  RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h
390 my $rowspan = ($etime-$stime) / $timestep;
391
392 my $default_slots = RT->Config->Get('CalendarWeeklySlots') || 5;
393
394 my $wt = $stime;
395 my @week_rows = ();
396 while ( $wt < $etime ) { push @week_rows, $wt; $wt+=$timestep }
397
398 </%ONCE>
399 <%INIT>
400 use RTx::Calendar qw(FirstDay LastDay LastDayOfWeek);
401
402 $Embed =~ /^[\w\.]*$/ or die 'xss';
403
404 my $title = loc("Calendar");
405
406 my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;
407
408 my $rtdate = RT::Date->new($session{'CurrentUser'});
409
410 my $time_zone = $session{'CurrentUser'}->UserObj->Timezone
411                   || RT->Config->Get('Timezone');
412
413 my $weekstart = 'Sunday'; #RT::SiteConfig?  user pref?
414 my %week = (
415   'Saturday' => [6,0..5],
416   'Sunday'   => [0..6],
417   'Monday'   => [1..6,0],
418 );
419 my $startday_of_week = ${$week{$weekstart}}[0]  || 7;
420 my $endday_of_week   = ${$week{$weekstart}}[-1] || 7;
421
422 my $today = DateTime->today( time_zone=>$time_zone );
423 my $yesterday = $today->clone->subtract( days=>1 );
424 my $aweekago  = $today->clone->subtract( days=>7 );
425
426 my( $start, $end );
427 if ( $WeekDay ) {
428   $start = DateTime->new( year      => $WeekYear,
429                           month     => $WeekMonth+1,
430                           day       => $WeekDay,
431                           time_zone => $time_zone,
432                         );
433   $end  = LastDayOfWeek( $WeekYear, $WeekMonth+1, $WeekDay, $endday_of_week );
434 } else {
435   $start = FirstDay($Year, $Month + 1, $startday_of_week );
436   $end   = LastDay ($Year, $Month + 1, $endday_of_week );
437 }
438
439 # use this to loop over days until $end
440 my $set = DateTime::Set->from_recurrence(
441     next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
442 );
443
444 my $QueryString;
445 if ($Query) {
446   $QueryString =
447       $m->comp(
448         '/Elements/QueryString',
449         Query   => $Query,
450         Format  => $Format,
451         Order   => $Order,
452         OrderBy => $OrderBy,
453         Rows    => $RowsPerPage,
454         @DisplayArgs,
455       );
456 } else {
457   $QueryString =
458       $m->comp(
459         '/Elements/QueryString',
460         NewQuery => 1,
461         @DisplayArgs,
462       );
463 }
464
465 # Default Query and Format
466 my $TempFormat = "__Starts__ __Due__";
467 my $TempQuery = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
468  AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody'  )
469  AND ( Type = 'reminder' OR 'Type' = 'ticket' )";
470
471 if ( my $Search = RTx::Calendar::SearchDefaultCalendar($session{CurrentUser}) ) {
472   $TempFormat = $Search->SubValue('Format');
473   $TempQuery = $Search->SubValue('Query');
474 }
475
476 # we overide them if needed
477 $TempQuery  = $Query  if $Query;
478 $TempFormat = $Format if $Format;
479
480 # we search all date types in Format string
481 my @Dates = grep { $TempFormat =~ m/__${_}(Relative)?__/ } @DateTypes;
482
483 # used to display or not a date in Element/CalendarEvent
484 my %DateTypes = map { $_ => 1 } @Dates;
485
486 $TempQuery .= RTx::Calendar::DatesClauses(\@Dates, $start->strftime("%F"), $end->strftime("%F"));
487
488 # print STDERR ("-" x 30), "\n", $TempQuery, "\n";
489
490 my %Tickets = RTx::Calendar::FindTickets($session{'CurrentUser'}, $TempQuery, \@Dates, $start->strftime("%F"), $end->strftime("%F"));
491
492 </%INIT>
493 <%def td_week_expand>
494 <%args>
495   $date  => undef
496   $Month => undef
497   $Year  => undef
498   $QueryString => undef
499   $Embed => undef
500 </%args>
501   <td class="controls">
502     <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>
503   </td>
504 </%def>
505
506 <%def week_collapse>
507 <%args>
508   $date  => undef
509   $Month => undef
510   $Year  => undef
511   $QueryString => undef
512   $Embed => undef
513 </%args>
514     <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>
515 </%def>
516
517 <%def td_time>
518 % my( $min ) = shift;
519     <td class="labels"><% pretty_time($min) |n %></td>
520 <%init>
521 sub pretty_time {
522   my $t = shift;
523
524   return 'Midnight' if $t == 0 || $t == 1440;
525   return 'Noon'     if $t == 720;
526
527   my $h = int( $t / 60 );
528   my $m = $t % 60;
529
530   my $ap = 'AM';
531   if    ( $h == 0 || $h == 24 ) { $h = 12; }
532   elsif ( $h == 12 )           { $ap = 'PM'; }
533   elsif ( $h > 12 )            { $ap = 'PM'; $h -= 12; }
534
535   sprintf('%02d:%02d&nbsp;'.$ap, $h, $m);
536
537 }
538 </%init>
539 </%def>