<&|/Widgets/TitleBox,
    title => loc("Calendar"),
    title_href => "Search/Calendar.html" &>

<table class="rtxcalendar">
<thead>
<tr>
% my $date = $begin->clone;
% while ( $date <= $end ) {
<th width="14%"><%$rtdate->GetWeekday($date->day_of_week % 7)%></th>
% $date = $set->next($date);
% }
</tr>
</thead>
<tbody>
<tr>
<%perl>
my %week_ticket_position;
my $day_of_week = 1;
$date = $begin->clone;

while ($date <= $end) {
  my @classes = ();
  push @classes, "today"     if (DateTime->compare($today,     $date) == 0);
  push @classes, "yesterday" if (DateTime->compare($yesterday, $date) == 0);
  push @classes, "weekday-$day_of_week";
  for my $t ( @{ $Tickets->{ $date->strftime("%F") } || [] } ) {
    # check if ticket was already displayed this week, if not, we need to find a
    # position for it
    unless ( grep { $week_ticket_position{$_}{id} eq $t->id } keys %week_ticket_position ) {
      # new tickets should assume the first empty spot.
      my $i = 1;
      my $free_index = 0;
      for my $index ( sort { $a <=> $b } keys %week_ticket_position ) {
        if ( $week_ticket_position{$index}{id} eq "" ) {
          $free_index = $i;
          last;
        }
        $i++;
      }
      # if we found a free spot, we place the ticket there
      if ( $free_index != 0 ) {
        $week_ticket_position{$free_index}{id} = $t->id;
        $week_ticket_position{$free_index}{TicketObj} = $t;
      }
      # if not, we add it to the end of the array
      else {
        $week_ticket_position{((scalar keys %week_ticket_position)+1)}{id} = $t->id;
        $week_ticket_position{((scalar keys %week_ticket_position))}{TicketObj} = $t;
      }
    }
  }
</%perl>

    <td class="<% join(' ', @classes) %>"><div class="inside-day">
      <div class="calendardate"><%$date->day%></div>
%      for my $index ( sort { $a <=> $b } keys %week_ticket_position ) {
%       if ( grep { $_->id eq $week_ticket_position{$index}{id} }
%                 @{ $Tickets->{ $date->strftime("%F") } || [] } ) {
%         my $t = $week_ticket_position{$index}{TicketObj};
        <& /Elements/CalendarEvent,
          Object              => $t,
          Date                => $date,
          DateTypes           => \%DateTypes,
          DayOfWeek           => $day_of_week,
          TicketsSpanningDays => $TicketsSpanningDays,
          WeekTicketPosition  => \%week_ticket_position,
          CurrentPostion      => $index,
        &>
%       }
%       else {
%         # if there's no ticket for this position, we add an empty space
             <div class="day">&nbsp;</div>
%       }
%     }
    </div></td>
% $date = $set->next($date);
% $day_of_week = $day_of_week + 1;
% }
</tr>
</tbody>
</table>

 </&>

<%INIT>

use RTx::Calendar;

my $title = loc("Calendar");

my $rtdate = RT::Date->new($session{'CurrentUser'});

my @DateTypes = qw/Created Starts Started Due LastUpdated Resolved/;

my $today = DateTime->today;
my $yesterday = $today->clone->subtract( days=>1 );

# this line is used to debug MyCalendar
# $today = DateTime->new(year => 2007, month => 4, day => 11);

my $begin = $today->clone->subtract( days => 3);
my $end   = $today->clone->add( days => 3);

# use this to loop over days until $end
my $set = DateTime::Set->from_recurrence(
    next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
);

my $Query = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
 AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody'  )
 AND ( Type = 'reminder' OR 'Type' = 'ticket' )";
my $Format = "__Starts__ __Due__";

if ( my $Search = RTx::Calendar::SearchDefaultCalendar($session{CurrentUser}) ) {
  $Format = $Search->SubValue('Format');
  $Query = $Search->SubValue('Query');
}

# we search all date types in Format string
my @CoreDates    = grep { $Format =~ m/__${_}(Relative)?__/ } @DateTypes;
my @CustomFields = ( $Format =~ /__(CustomField\.\{.*\})__/g );
my @DateCustomFields;

for my $CustomField (@CustomFields) {
    my $LintCustomField = $CustomField;
    $LintCustomField =~ s/CustomField\.\{(.*)\}/$1/;
    my $CustomFieldObj = RT::CustomField->new( RT->SystemUser );
    $CustomFieldObj->LoadByName( Name => $LintCustomField );
    push @DateCustomFields, $CustomField
        if $CustomFieldObj->id
        && ( $CustomFieldObj->Type eq 'Date'
        || $CustomFieldObj->Type eq 'DateTime' );
}

my @Dates = (@CoreDates, @DateCustomFields);
@Dates = map { $_ =~ s/^CustomField\.(.*)$/CF.$1/; $_ } @Dates;
# used to display or not a date in Element/CalendarEvent
my %DateTypes = map { $_ => 1 } @Dates;

$Query .= RTx::Calendar::DatesClauses(\@Dates, $begin->strftime("%F"), $end->strftime("%F"));

$m->callback( CallbackName => 'BeforeFindTickets', ARGSRef => \%ARGS, QueryRef => \$Query, FormatRef => \$Format );

my ($Tickets, $TicketsSpanningDays) = RTx::Calendar::FindTickets($session{'CurrentUser'}, $Query, \@Dates);
</%INIT>
