weekly view for RTx::Calendar, RT#16594
[freeside.git] / rt / share / html / Search / Calendar.html
index e711b86..a693bd6 100644 (file)
@@ -7,125 +7,308 @@ $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 &>
+
+% my $title;
+% if ( $WeekMonth ) {
+%   if ( $date->month == $end->month ) {
+%     $title = $rtdate->GetMonth( $date->month -1 ). ' '.
+%              $date->day. '-'. $end->day. ', '. $date->year;
+%   } elsif ( $date->year == $end->year ) {
+%     $title =
+%       $rtdate->GetMonth( $date->month -1 ). ' '. $date->day. ' - '.
+%       $rtdate->GetMonth( $end->month  -1 ). ' '. $end->day.  ', '. $end->year;
+%   } else {
+%     $title =
+%       $rtdate->GetMonth( $date->month -1 ). ' '.$date->day. ', '. $date->year.
+%       ' - '.
+%       $rtdate->GetMonth( $end->month  -1 ). ' '.$end->day.  ', '. $end->year;
+%   }
+% } else {
+%   $title = $rtdate->GetMonth($Month) . " $Year" 
+% }
 <&| /Widgets/TitleBox,
-     title => loc('Calendar for ') . $rtdate->GetMonth($Month) . " $Year" ,
+     title => loc('Calendar for '). $title,
      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="labels" colspan=<% $WeekDay ? 2 : 1 %>></td>
+% for ( @{$week{$weekstart}} ) {
 <th width="14%"><%$rtdate->GetWeekday($_)%></th>
 % }
 </tr>
 </thead>
+
 <tbody>
 <tr>
-% while ($date <= $end) {
-%   if ( $date->day_of_week == 1) {
-<th><% $date->week_number %></th>
-%   }
-<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>
+
+% if ( $WeekDay ) {
+  <td class="controls" rowspan=<% $rowspan+1 %> valign="middle">
+    <& week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
+  </td>
+  </td>
+% } else {
+  <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
 % }
+
+% if ( $WeekDay ) {
+
+    <td class="labels"></td>
+
+%   $date = $start;
+%   while ($date <= $end) {
+%
+%     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="<%   $is_today     ? 'today'
+                    : $is_yesterday ? 'yesterday'
+                    : $is_aweekago  ? 'aweekago'
+                    : ''
+                 %>"
+      >
+        <div class="<%   $is_today ? 'todays'
+                       : ''
+                    %>calendardate"
+        ><% $rtdate->GetMonth($date->month-1). ' '. $date->day %></div>
+
+      </td>
+
+%     $date = $set->next($date);
+%     if ( $date->day_of_week == $startday_of_week ) {
+        </tr>
+%       if ( $date <= $end ) { #a second week? not going to work for week view yet
+          <tr>
+              <td class="controls" rowspan=<% $rowspan + 1 %> valign="middle">
+                <& week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
+              </td>
+%       }
+%     }
+
+%   } #while ($date <= $end)
+
+%   foreach my $row ( @week_rows ) {
+
+      <& td_time, $row &>
+
+%     $date = $start;
+%     while ($date <= $end) {
+%
+%       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="<%   $is_today     ? 'today'
+                      : $is_yesterday ? 'yesterday'
+                      : $is_aweekago  ? 'aweekago'
+                      : ''
+                   %>"
+        >
+
+% #XXX display these in a time aware fashion
+%#%       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 ) { #a second week? not going to work for week view yet
+            <tr>
+              <td class="controls" rowspan=<% $rowspan + 1 %> valign="middle">
+                  <& week_collapse, date=>$date, Month=>$OrigMonth, Year=>$OrigYear, QueryString=>$QueryString &>
+                </td>
+%         }
+%       }
+
+%     } #while ($date <= $end)
+%   } #foreach my $row ( @week_rows )
+
+% } else {
+
+%   while ($date <= $end) {
+%
+%     my $offmonth = !$WeekDay && $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     ? 'offmonth'
+                    : $is_today     ? 'today'
+                    : $is_yesterday ? 'yesterday'
+                    : $is_aweekago  ? 'aweekago'
+                    : ''
+                 %>"
+      >
+        <div class="<%   $is_today ? 'todays'
+                       : $offmonth ? 'offmonth'
+                       : ''
+                    %>calendardate"
+        ><% $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>
+            <& td_week_expand, date=>$date, Month=>$Month, Year=>$Year, QueryString=>$QueryString &>
+%       }
+%     }
+
+%   } #while ($date <= $end)
+
 % }
-</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'],
+);
+
+my $stime    = 480;  #8am
+my $etime    = 1080; #6pm
+my $timestep = 30;  #1/2h
+#my $timestep = 120;  #2h
+my $rowspan = ($etime-$stime) / $timestep;
+
+my $wt = $stime;
+my @week_rows = ();
+while ( $wt < $etime ) { push @week_rows, $wt; $wt+=$timestep }
+
+</%ONCE>
 <%INIT>
-use RTx::Calendar;
+use RTx::Calendar qw(FirstDay LastDay LastDayOfWeek);
 
 my $title = loc("Calendar");
 
@@ -133,16 +316,42 @@ my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;
 
 my $rtdate = RT::Date->new($session{'CurrentUser'});
 
-my $today = DateTime->today;
-my $date  = RTx::Calendar::FirstMonday($Year, $Month + 1);
-my $end   = RTx::Calendar::LastSunday($Year, $Month + 1);
+my $time_zone = $session{'CurrentUser'}->UserObj->Timezone
+                  || RT->Config->Get('Timezone');
+
+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( time_zone=>$time_zone );
+my $yesterday = $today->clone->subtract( days=>1 );
+my $aweekago  = $today->clone->subtract( days=>7 );
+
+my( $start, $end );
+if ( $WeekDay ) {
+  $start = DateTime->new( year      => $WeekYear,
+                          month     => $WeekMonth+1,
+                          day       => $WeekDay,
+                          time_zone => $time_zone,
+                        );
+  $end  = LastDayOfWeek( $WeekYear, $WeekMonth+1, $WeekDay, $endday_of_week );
+} else {
+  $start = FirstDay($Year, $Month + 1, $startday_of_week );
+  $end   = LastDay ($Year, $Month + 1, $endday_of_week );
+}
+my $date = $start;
 
 # 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 +368,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 +392,48 @@ $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 -1 %>&WeekYear=<% $date->year %>&OrigMonth=<% $Month %>&OrigYear=<% $Year %>&<%$QueryString%>"><img src="<%$RT::WebPath%>/NoAuth/images/week-expand.gif"></a>
+  </td>
+</%def>
+
+<%def week_collapse>
+<%args>
+  $date  => undef
+  $Month => undef
+  $Year  => undef
+  $QueryString => undef
+</%args>
+    <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>
+</%def>
+
+<%def td_time>
+% my( $min ) = shift;
+    <td class="labels"><% pretty_time($min) |n %></td>
+<%init>
+sub pretty_time {
+  my $t = shift;
+
+  return 'Midnight' if $t == 0 || $t == 1440;
+  return 'Noon'     if $t == 720;
+
+  my $h = int( $t / 60 );
+  my $m = $t % 60;
+
+  my $ap = 'AM';
+  if    ( $h == 0 || $h == 24 ) { $h = 12; }
+  elsif ( $h == 12 )           { $ap = 'PM'; }
+  elsif ( $h > 12 )            { $ap = 'PM'; $h -= 12; }
+
+  sprintf('%02d:%02d&nbsp;'.$ap, $h, $m);
+
+}
+</%init>
+</%def>