weekly view for RTx::Calendar, RT#16584
[freeside.git] / rt / share / html / Search / Calendar.html
index e711b86..db781d8 100644 (file)
@@ -7,125 +7,197 @@ $Order => undef
 $OrderBy => undef
 $RowsPerPage => undef
 $NewQuery => 0
+$WeekDay => undef
+$WeekMonth => undef
+$WeekYear => undef
+$OrigMonth => undef
+$OrigYear => undef
 </%args>
 
 <& /Elements/Header, Title => $title &>
-<& /Ticket/Elements/Tabs, 
-    current_tab => "Search/Calendar.html?$QueryString",
-    Title => $title &>
+<& /Elements/Tabs &>
+
 <&| /Widgets/TitleBox,
      title => loc('Calendar for ') . $rtdate->GetMonth($Month) . " $Year" ,
      title_class=> 'inverse',
      color => "#993333" &>
 
-<table width="100%">
-<tr>
-<td align="left">
-% my ($PMonth, $PYear) = ($Month - 1, $Year);
+% my($PMonth, $PYear, $NMonth, $NYear);
+% unless ( $WeekDay ) {
+
+    <table width="100%">
+      <tr>
+        <td align="left">
+% ($PMonth, $PYear) = ($Month - 1, $Year);
 % if ($PMonth < 0) {
 %    $PYear--;
 %    $PMonth = 11;
 % }
-<a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
-</td>
-<td align="center">
-<a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
-</td>
-<td align="right">
-% my ($NMonth, $NYear) = ($Month + 1, $Year);
+          <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
+        </td>
+        <th align="center">
+          <font size="+1"><% $rtdate->GetMonth($Month). " $Year" %></font>
+        </th>
+        <td align="right">
+% ($NMonth, $NYear) = ($Month + 1, $Year);
 % if ($NMonth > 11) {
 %    $NYear++;
 %    $NMonth = 0;
 % }
-<a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
-</td>
-</tr>
-</table>
+          <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
+        </td>
+      </tr>
+    </table>
+% }
 
 <table class="rtxcalendar">
+
 <thead>
 <tr>
-<th></th>
-% for (1 .. 6, 0) {
+<td class="controls"></td>
+% for ( @{$week{$weekstart}} ) {
 <th width="14%"><%$rtdate->GetWeekday($_)%></th>
 % }
 </tr>
 </thead>
+
 <tbody>
 <tr>
+% if ( $WeekDay ) {
+  <& td_week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
+% } else {
+  <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
+% }
 % while ($date <= $end) {
-%   if ( $date->day_of_week == 1) {
-<th><% $date->week_number %></th>
+%
+%   my $offmonth = $date->month != ($Month + 1);
+%   my $is_today     = (DateTime->compare($today,     $date) == 0);
+%   my $is_yesterday = (DateTime->compare($yesterday, $date) == 0);
+%   my $is_aweekago  = (DateTime->compare($aweekago,  $date) == 0);
+
+    <td class="<% ($offmonth && !$WeekDay) ? 'offmonth'
+                    : $is_today     ? 'today'
+                    : $is_yesterday ? 'yesterday'
+                    : $is_aweekago  ? 'aweekago'
+                    : ''
+               %>"
+    >
+      <div class="<% $is_today ? 'todays'
+                       : ($offmonth && !$WeekDay) ? 'offmonth'
+                       :'' %>calendardate"
+      ><% ( $WeekDay ? $rtdate->GetMonth($date->month). ' ' : '' ).
+          $date->day
+       %></div>
+
+%     my $sp = 3;
+%     for my $t ( @{ $Tickets{$date->strftime("%F")} } ) {
+%       $sp--;
+        <& /Elements/CalendarEvent, Object => $t, Date => $date, DateTypes => \%DateTypes &>
+%     }
+      <% ($sp>0) ? '<BR>'x$sp : '' |n %>
+
+    </td>
+
+%   $date = $set->next($date);
+%   if ( $date->day_of_week == $startday_of_week ) {
+      </tr>
+%     if ( $date <= $end ) {
+        <tr>
+%         if ( $WeekDay ) {
+            <& td_week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
+%         } else {
+            <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
+%         }
+%     }
 %   }
-<td class="<% $date->month != ($Month + 1) ? 'oddline' : '' %>"
-    style="width:14%;<%  DateTime->compare($today, $date) == 0 ? 'background:#f6f7f8;' : '' %>"
->
-<p class="date"><%$date->day%></p>
-% for my $t ( @{ $Tickets{$date->strftime("%F")} } ) {
-<& /Elements/CalendarEvent, Object => $t, Date => $date, DateTypes => \%DateTypes &>
-% }
-</td>
-% $date = $set->next($date);
-% if ( $date->day_of_week == 1) {
-</tr><tr>
-% }
+
 % }
-</tr>
 </tbody>
 </table>
 
-<table width="100%">
-<tr>
-<td align="left">
-<a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
-</td>
-<td align="right">
-<a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
-</td>
-</tr>
-</table>
+% unless ( $WeekDay ) {
 
+    <table width="100%">
+      <tr>
+        <td align="left">
+          <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$PMonth%>&Year=<%$PYear%>&<%$QueryString%>">«<%$rtdate->GetMonth($PMonth)%></a>
+        </td>
 
-<table width="100%">
-<tr>
-<td valign="top" align="center" width="80%">
-<form action="<%$RT::WebPath%>/Search/Calendar.html?<%$QueryString%>" method="post">
+        <td valign="top" align="center">
+          <form action="<%$RT::WebPath%>/Search/Calendar.html?<%$QueryString%>" method="post">
+
+            <select name="Month">
+%             for (0..11) {
+                <option value="<%$_%>" <% $_ == $Month ? 'selected' : ''%> ><%$rtdate->GetMonth($_)%></option>
+%             }
+            </select>
+
+%           my $year = (localtime)[5] + 1900;
+            <select name="Year">
+%             for ( ($year-5) .. ($year+5)) {
+                <option value="<%$_%>" <% $_ == $Year ? 'selected' : ''%>><%$_%></option>
+%             }
+            </select>
+
+%# <& /Elements/Submit&>
+            <input type="submit" value="<% loc('Submit') %>" class="button" />
+
+          </form>
+        </td>
+
+        <td align="right">
+          <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<%$NMonth%>&Year=<%$NYear%>&<%$QueryString%>"><%$rtdate->GetMonth($NMonth)%>»</a>
+        </td>
+      </tr>
+  </table>
 
-<select name="Month">
-% for (0..11) {
-<option value="<%$_%>" <% $_ == $Month ? 'selected' : ''%> ><%$rtdate->GetMonth($_)%></option>
-% }
-</select>
-% my $year = (localtime)[5] + 1900;
-<select name="Year">
-% for ( ($year-5) .. ($year+5)) {
-<option value="<%$_%>" <% $_ == $Year ? 'selected' : ''%>><%$_%></option>
 % }
-</select>
 
-<& /Elements/Submit&>
-</form>
+% #XXX an option to turn off "Calendar Preferences and Help" for embedded
+% # (and weekly?) use
+<table width="100%">
+<tr>
+
+<td valign="top" rowspan=9>
+  <BR>
+  <a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
 </td>
-<td valign="top" width="50%" align="right">
-<img src="<%$RT::WebImagesURL%>/created.png" /> : <&|/l&>Created</&><br />
-<img src="<%$RT::WebImagesURL%>/due.png" /> : <&|/l&>Due</&><br />
-<img src="<%$RT::WebImagesURL%>/resolved.png" /> : <&|/l&>Resolved</&><br />
-<img src="<%$RT::WebImagesURL%>/updated.png" /> : <&|/l&>Last Updated</&><br />
-<img src="<%$RT::WebImagesURL%>/created_due.png" /> : <&|/l&>Created</&>, <&|/l&>Due</&><br />
-<img src="<%$RT::WebImagesURL%>/reminder.png" /> : <&|/l&>Reminders</&><br />
-<img src="<%$RT::WebImagesURL%>/starts.png" /> : <&|/l&>Starts</&><br />
-<img src="<%$RT::WebImagesURL%>/started.png" /> : <&|/l&>Started</&><br />
-<img src="<%$RT::WebImagesURL%>/starts_due.png" /> : <&|/l&>Starts</&>, <&|/l&>Due</&><br />
 
+% foreach my $legend (keys %legend) {
+    <tr>
+      <td align="right">
+        <img src="<%$RT::WebImagesURL%>/<%$legend%>.png" />
+      </td>
+      <td align="left">
+%       my $more = 0;
+%       foreach ( @{$legend{$legend}} ) {
+          <% $more++ ? ', ' : '' %>
+          <&|/l&><% $_ %></&>
+%       }
+      </td>
+    </tr>
+% }
 
-</td>
 </table>
 
 </&>
 
-</html>
+<%ONCE>
+
+my %legend = (
+  'created'     => ['Created'],
+  'due'         => ['Due'],
+  'resolved'    => ['Resolved'],
+  'updated'     => ['Last Updated'],
+  'created_due' => ['Created','Due'],
+  'reminder'    => ['Reminders'],
+  'started'     => ['Started'],
+  'starts_due'  => ['Starts','Due'],
+);
+
+</%ONCE>
 <%INIT>
-use RTx::Calendar;
+use RTx::Calendar qw(FirstDay LastDay LastDayOfWeek);
 
 my $title = loc("Calendar");
 
@@ -133,16 +205,34 @@ my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;
 
 my $rtdate = RT::Date->new($session{'CurrentUser'});
 
+my $weekstart = 'Sunday'; #RT::SiteConfig?  user pref?
+my %week = (
+  'Saturday' => [6,0..5],
+  'Sunday'   => [0..6],
+  'Monday'   => [1..6,0],
+);
+my $startday_of_week = ${$week{$weekstart}}[0]  || 7;
+my $endday_of_week   = ${$week{$weekstart}}[-1] || 7;
+
 my $today = DateTime->today;
-my $date  = RTx::Calendar::FirstMonday($Year, $Month + 1);
-my $end   = RTx::Calendar::LastSunday($Year, $Month + 1);
+my $yesterday = $today->clone->subtract( days=>1 );
+my $aweekago  = $today->clone->subtract( days=>7 );
+
+my( $date, $end);
+if ( $WeekDay ) {
+  $date = DateTime->new( year=>$WeekYear, month=>$WeekMonth, day=>$WeekDay );
+  $end  = LastDayOfWeek( $WeekYear, $WeekMonth, $WeekDay, $endday_of_week );
+} else {
+  $date = FirstDay($Year, $Month + 1, $startday_of_week );
+  $end  = LastDay ($Year, $Month + 1, $endday_of_week );
+}
 
 # use this to loop over days until $end
 my $set = DateTime::Set->from_recurrence(
     next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
 );
 
-my $QueryString = 
+my $QueryString =
       $m->comp(
         '/Elements/QueryString',
         Query   => $Query,
@@ -159,7 +249,7 @@ $QueryString ||= 'NewQuery=1';
 my $TempFormat = "__Starts__ __Due__";
 my $TempQuery = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
  AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody'  )
- AND ( Type = 'reminder' OR 'Type' = 'ticket' )"; 
+ AND ( Type = 'reminder' OR 'Type' = 'ticket' )";
 
 if ( my $Search = RTx::Calendar::SearchDefaultCalendar($session{CurrentUser}) ) {
   $TempFormat = $Search->SubValue('Format');
@@ -183,3 +273,26 @@ $TempQuery .= RTx::Calendar::DatesClauses(\@Dates, $date->strftime("%F"), $end->
 my %Tickets = RTx::Calendar::FindTickets($session{'CurrentUser'}, $TempQuery, \@Dates, $date->strftime("%F"), $end->strftime("%F"));
 
 </%INIT>
+<%def td_week_expand>
+<%args>
+  $date  => undef
+  $Month => undef
+  $Year  => undef
+  $QueryString => undef
+</%args>
+  <td class="controls">
+    <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>
+  </td>
+</%def>
+
+<%def td_week_collapse>
+<%args>
+  $date  => undef
+  $Month => undef
+  $Year  => undef
+  $QueryString => undef
+</%args>
+  <td class="controls">
+    <a href="<%$RT::WebPath%>/Search/Calendar.html?Month=<% $Month %>&Year=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-collapse.gif"></a>
+  </td>
+</%def>