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