weekly view for RTx::Calendar, 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 </%args>
16
17 <& /Elements/Header, Title => $title &>
18 <& /Elements/Tabs &>
19
20 <&| /Widgets/TitleBox,
21      title => loc('Calendar for ') . $rtdate->GetMonth($Month) . " $Year" ,
22      title_class=> 'inverse',
23      color => "#993333" &>
24
25 % my($PMonth, $PYear, $NMonth, $NYear);
26 % unless ( $WeekDay ) {
27
28     <table width="100%">
29       <tr>
30         <td align="left">
31 % ($PMonth, $PYear) = ($Month - 1, $Year);
32 % if ($PMonth < 0) {
33 %    $PYear--;
34 %    $PMonth = 11;
35 % }
36           <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
37         </td>
38         <th align="center">
39           <font size="+1"><% $rtdate->GetMonth($Month). " $Year" %></font>
40         </th>
41         <td align="right">
42 % ($NMonth, $NYear) = ($Month + 1, $Year);
43 % if ($NMonth > 11) {
44 %    $NYear++;
45 %    $NMonth = 0;
46 % }
47           <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
48         </td>
49       </tr>
50     </table>
51 % }
52
53 <table class="rtxcalendar">
54
55 <thead>
56 <tr>
57 <td class="controls"></td>
58 % for ( @{$week{$weekstart}} ) {
59 <th width="14%"><%$rtdate->GetWeekday($_)%></th>
60 % }
61 </tr>
62 </thead>
63
64 <tbody>
65 <tr>
66 % if ( $WeekDay ) {
67   <& td_week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
68 % } else {
69   <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
70 % }
71 % while ($date <= $end) {
72 %
73 %   my $offmonth = $date->month != ($Month + 1);
74 %   my $is_today     = (DateTime->compare($today,     $date) == 0);
75 %   my $is_yesterday = (DateTime->compare($yesterday, $date) == 0);
76 %   my $is_aweekago  = (DateTime->compare($aweekago,  $date) == 0);
77
78     <td class="<% ($offmonth && !$WeekDay) ? 'offmonth'
79                     : $is_today     ? 'today'
80                     : $is_yesterday ? 'yesterday'
81                     : $is_aweekago  ? 'aweekago'
82                     : ''
83                %>"
84     >
85       <div class="<% $is_today ? 'todays'
86                        : ($offmonth && !$WeekDay) ? 'offmonth'
87                        :'' %>calendardate"
88       ><% ( $WeekDay ? $rtdate->GetMonth($date->month). ' ' : '' ).
89           $date->day
90        %></div>
91
92 %     my $sp = 3;
93 %     for my $t ( @{ $Tickets{$date->strftime("%F")} } ) {
94 %       $sp--;
95         <& /Elements/CalendarEvent, Object => $t, Date => $date, DateTypes => \%DateTypes &>
96 %     }
97       <% ($sp>0) ? '<BR>'x$sp : '' |n %>
98
99     </td>
100
101 %   $date = $set->next($date);
102 %   if ( $date->day_of_week == $startday_of_week ) {
103       </tr>
104 %     if ( $date <= $end ) {
105         <tr>
106 %         if ( $WeekDay ) {
107             <& td_week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
108 %         } else {
109             <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
110 %         }
111 %     }
112 %   }
113
114 % }
115 </tbody>
116 </table>
117
118 % unless ( $WeekDay ) {
119
120     <table width="100%">
121       <tr>
122         <td align="left">
123           <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
124         </td>
125
126         <td valign="top" align="center">
127           <form action="<%$RT::WebPath%>/Search/Calendar.html?<%$QueryString%>" method="post">
128
129             <select name="Month">
130 %             for (0..11) {
131                 <option value="<%$_%>" <% $_ == $Month ? 'selected' : ''%> ><%$rtdate->GetMonth($_)%></option>
132 %             }
133             </select>
134
135 %           my $year = (localtime)[5] + 1900;
136             <select name="Year">
137 %             for ( ($year-5) .. ($year+5)) {
138                 <option value="<%$_%>" <% $_ == $Year ? 'selected' : ''%>><%$_%></option>
139 %             }
140             </select>
141
142 %# <& /Elements/Submit&>
143             <input type="submit" value="<% loc('Submit') %>" class="button" />
144
145           </form>
146         </td>
147
148         <td align="right">
149           <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
150         </td>
151       </tr>
152   </table>
153
154 % }
155
156 % #XXX an option to turn off "Calendar Preferences and Help" for embedded
157 % # (and weekly?) use
158 <table width="100%">
159 <tr>
160
161 <td valign="top" rowspan=9>
162   <BR>
163   <a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
164 </td>
165
166 % foreach my $legend (keys %legend) {
167     <tr>
168       <td align="right">
169         <img src="<%$RT::WebImagesURL%>/<%$legend%>.png" />
170       </td>
171       <td align="left">
172 %       my $more = 0;
173 %       foreach ( @{$legend{$legend}} ) {
174           <% $more++ ? ', ' : '' %>
175           <&|/l&><% $_ %></&>
176 %       }
177       </td>
178     </tr>
179 % }
180
181 </table>
182
183 </&>
184
185 <%ONCE>
186
187 my %legend = (
188   'created'     => ['Created'],
189   'due'         => ['Due'],
190   'resolved'    => ['Resolved'],
191   'updated'     => ['Last Updated'],
192   'created_due' => ['Created','Due'],
193   'reminder'    => ['Reminders'],
194   'started'     => ['Started'],
195   'starts_due'  => ['Starts','Due'],
196 );
197
198 </%ONCE>
199 <%INIT>
200 use RTx::Calendar qw(FirstDay LastDay LastDayOfWeek);
201
202 my $title = loc("Calendar");
203
204 my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;
205
206 my $rtdate = RT::Date->new($session{'CurrentUser'});
207
208 my $weekstart = 'Sunday'; #RT::SiteConfig?  user pref?
209 my %week = (
210   'Saturday' => [6,0..5],
211   'Sunday'   => [0..6],
212   'Monday'   => [1..6,0],
213 );
214 my $startday_of_week = ${$week{$weekstart}}[0]  || 7;
215 my $endday_of_week   = ${$week{$weekstart}}[-1] || 7;
216
217 my $today = DateTime->today;
218 my $yesterday = $today->clone->subtract( days=>1 );
219 my $aweekago  = $today->clone->subtract( days=>7 );
220
221 my( $date, $end);
222 if ( $WeekDay ) {
223   $date = DateTime->new( year=>$WeekYear, month=>$WeekMonth, day=>$WeekDay );
224   $end  = LastDayOfWeek( $WeekYear, $WeekMonth, $WeekDay, $endday_of_week );
225 } else {
226   $date = FirstDay($Year, $Month + 1, $startday_of_week );
227   $end  = LastDay ($Year, $Month + 1, $endday_of_week );
228 }
229
230 # use this to loop over days until $end
231 my $set = DateTime::Set->from_recurrence(
232     next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
233 );
234
235 my $QueryString =
236       $m->comp(
237         '/Elements/QueryString',
238         Query   => $Query,
239         Format  => $Format,
240         Order   => $Order,
241         OrderBy => $OrderBy,
242         Rows    => $RowsPerPage
243       )
244       if ($Query);
245
246 $QueryString ||= 'NewQuery=1';
247
248 # Default Query and Format
249 my $TempFormat = "__Starts__ __Due__";
250 my $TempQuery = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
251  AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody'  )
252  AND ( Type = 'reminder' OR 'Type' = 'ticket' )";
253
254 if ( my $Search = RTx::Calendar::SearchDefaultCalendar($session{CurrentUser}) ) {
255   $TempFormat = $Search->SubValue('Format');
256   $TempQuery = $Search->SubValue('Query');
257 }
258
259 # we overide them if needed
260 $TempQuery  = $Query  if $Query;
261 $TempFormat = $Format if $Format;
262
263 # we search all date types in Format string
264 my @Dates = grep { $TempFormat =~ m/__${_}(Relative)?__/ } @DateTypes;
265
266 # used to display or not a date in Element/CalendarEvent
267 my %DateTypes = map { $_ => 1 } @Dates;
268
269 $TempQuery .= RTx::Calendar::DatesClauses(\@Dates, $date->strftime("%F"), $end->strftime("%F"));
270
271 # print STDERR ("-" x 30), "\n", $TempQuery, "\n";
272
273 my %Tickets = RTx::Calendar::FindTickets($session{'CurrentUser'}, $TempQuery, \@Dates, $date->strftime("%F"), $end->strftime("%F"));
274
275 </%INIT>
276 <%def td_week_expand>
277 <%args>
278   $date  => undef
279   $Month => undef
280   $Year  => undef
281   $QueryString => undef
282 </%args>
283   <td class="controls">
284     <a href="<%$RT::WebPath%>/Search/Calendar.html?WeekDay=<% $date->day %>&WeekMonth=<% $date->month %>&WeekYear=<% $date->year %>&OrigMonth=<% $Month %>&OrigYear=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-expand.gif"></a>
285   </td>
286 </%def>
287
288 <%def td_week_collapse>
289 <%args>
290   $date  => undef
291   $Month => undef
292   $Year  => undef
293   $QueryString => undef
294 </%args>
295   <td class="controls">
296     <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<% $Month %>&Year=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-collapse.gif"></a>
297   </td>
298 </%def>