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