Skip to content

Commit

Permalink
* add default query for Calendar.html and the portlet
Browse files Browse the repository at this point in the history
* use Format to display dates
* add documentation for new things
  • Loading branch information
nbc authored and obra committed Jul 28, 2009
1 parent 23f1f68 commit 94462e3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changes for 0.04

* should work with rt 3.6.0 and seems to work with 3.7.2
* display Starts date by default
* you can use Format to display dates you want (Created, Started, Due, Starts, ...)
* if a saved search is named "calendar", use it for the default Calendar.html and for the portlet
* fix a bug in Prefs

Changes for 0.03
Expand Down
3 changes: 1 addition & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
TODO

* add a personnal calendar with a saved query

ALREADY DONE

* (chaka) add a personnal calendar with a saved query
* (ymmv) ICal feeds
* (chaka) put the results of a Query Builder search on a calendar
* (chaka) link from MyCalendar should automatically go to Calendar.html with filter set to Owner=CurrentUser
23 changes: 15 additions & 8 deletions html/Elements/MyCalendar
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ my $title = loc("Calendar");

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

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

my $today = DateTime->today;

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

Expand All @@ -52,20 +55,24 @@ my $set = DateTime::Set->from_recurrence(

my $Query = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody' )
AND ( Type = 'reminder' OR 'Type' = 'ticket' )
AND ( ( Due >= '" . $begin->strftime("%F") . "' AND Due <= '" . $end->strftime("%F") . "')
OR
( Starts >= '" . $begin->strftime("%F") . "' AND Starts <= '" . $end->strftime("%F") . "')
)";
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 @Dates = qw/Starts Due/;
my @Dates = grep { $Format =~ m/__${_}(Relative)?__/ } @DateTypes;

# used to display or not a date in Element/CalendarEvent
my %DateTypes = map { $_ => 1 } @Dates;

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

# print STDERR $Query, "\n";

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

</%INIT>
29 changes: 26 additions & 3 deletions html/Prefs/Calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,35 @@

<&| /Widgets/TitleBox, title => 'Help' &>

<p>If you want to have reminders in a search you need to go in the
"advanced" tab of the query builder and add something like that :
<h3>displaying reminders :</h3>
<p>If you want to have reminders in a search you need to go in the <a
href="<%$RT::WebPath%>/Search/Edit.html"><%loc("Edit Query")%></a> tab
of the <%loc("query builder")%> and add something like that :

<pre>
AND ( Type = 'ticket' OR Type = 'reminder' )
</pre>
</p>

<code>AND ( Type = 'ticket' OR Type = 'reminder' )</code>
<h3>displaying other kind of dates :</h3>
<p>By default RTx::Calendar display Due and Starts dates. You can
select other kind of events you want with the <%loc("Display
Columns")%> section in the <a
href="<%$RT::WebPath%>/Search/Build.html"><%loc("Query
Builder")%></a>. The following one will display the two latter and
LastUpdated dates :

<pre>
'&lt;small&gt;__Due__&lt;/small&gt;',
'&lt;small&gt;__Starts__&lt;/small&gt;',
'&lt;small&gt;__LastUpdated__&lt;/small&gt;'
</pre>
</p>

<h3>changing the default query :</h3>
<p>You can change the default Query of Calendar.html and MyCalendar
portlet by saving a query with the name <code>calendar</code>.</p>

</&>

<& /Prefs/Elements/CalendarFeed &>
Expand Down
17 changes: 9 additions & 8 deletions html/Search/Calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<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</a>
<a href="<%$RT::WebPath%>/Prefs/Calendar.html">Calendar Preferences and Help</a>
</td>
<td align="right">
% my ($NMonth, $NYear) = ($Month + 1, $Year);
Expand Down Expand Up @@ -161,6 +161,11 @@
AND ( Owner = '" . $session{CurrentUser}->Id ."' OR Owner = 'Nobody' )
AND ( Type = 'reminder' OR 'Type' = 'ticket' )";

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

# we overide them if needed
$TempQuery = $Query if $Query;
$TempFormat = $Format if $Format;
Expand All @@ -171,14 +176,10 @@
# used to display or not a date in Element/CalendarEvent
my %DateTypes = map { $_ => 1 } @Dates;

my @DateClauses = map {
"($_ >= '" . $date->strftime("%F") . "' AND $_ <= '" . $end->strftime("%F") . "')"
} @Dates;
$TempQuery .= " AND " . " ( " . join(" OR ", @DateClauses) . " ) "
if @DateClauses;
$TempQuery .= RTx::Calendar::DatesClauses(\@Dates, $date->strftime("%F"), $end->strftime("%F"));

# print STDERR $TempQuery, "\n";
print STDERR ("-" x 30), "\n", $TempQuery, "\n";

my %Tickets = RTx::Calendar::FindTickets($session{'CurrentUser'}, $TempQuery, @Dates);
my %Tickets = RTx::Calendar::FindTickets($session{'CurrentUser'}, $TempQuery, \@Dates, $date->strftime("%F"), $end->strftime("%F"));

</%INIT>
56 changes: 46 additions & 10 deletions lib/RTx/Calendar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,27 @@ sub LocalDate {
sprintf "%4d-%02d-%02d", ($y + 1900), ++$m, $d;
}

sub DatesClauses {
my ($Dates, $begin, $end) = @_;

my $clauses = "";

my @DateClauses = map {
"($_ >= '" . $begin . "' AND $_ <= '" . $end . "')"
} @$Dates;
$clauses .= " AND " . " ( " . join(" OR ", @DateClauses) . " ) "
if @DateClauses;

return $clauses
}

sub FindTickets {
my ($session, $Query, @Dates) = @_;
my ($CurrentUser, $Query, $Dates, $begin, $end) = @_;

$Query .= DatesClauses($Dates, $begin, $end)
if $begin and $end;

my $Tickets = RT::Tickets->new($session);
my $Tickets = RT::Tickets->new($CurrentUser);
$Tickets->FromSQL($Query);

my %Tickets;
Expand All @@ -51,17 +68,39 @@ sub FindTickets {
while ( my $Ticket = $Tickets->Next()) {

# How to find the LastContacted date ?
for my $Date (@Dates) {
for my $Date (@$Dates) {
my $DateObj = $Date . "Obj";
push @{ $Tickets{ RTx::Calendar::LocalDate($Ticket->$DateObj->Unix) } }, $Ticket
push @{ $Tickets{ LocalDate($Ticket->$DateObj->Unix) } }, $Ticket
# if reminder, check it's refering to a ticket
unless ($Ticket->Type eq 'reminder' and not $Ticket->RefersTo->First)
or $AlreadySeen{ RTx::Calendar::LocalDate($Ticket->$DateObj->Unix) }{ $Ticket }++;
or $AlreadySeen{ LocalDate($Ticket->$DateObj->Unix) }{ $Ticket }++;
}
}
return %Tickets;
}

#
# Take a user object and return the search with Description "calendar" if it exists
#
sub SearchDefaultCalendar {
my $CurrentUser = shift;
my $Description = "calendar";

# I'm quite sure the loop isn't usefull but...
my @Objects = $CurrentUser->UserObj;
for my $object (@Objects) {
next unless ref($object) eq 'RT::User' && $object->id == $CurrentUser->Id;
my @searches = $object->Attributes->Named('SavedSearch');
for my $search (@searches) {
next if ($search->SubValue('SearchType')
&& $search->SubValue('SearchType') ne 'Ticket');

return $search
if "calendar" eq $search->Description;
}
}
}


1;

Expand Down Expand Up @@ -120,12 +159,9 @@ $HomepageComponents in etc/RT_SiteConfig.pm like that :
To enable private searches ICal feeds, you need to give
CreateSavedSearch and LoadSavedSearch rights to your users.
=head1 ADVANCED USAGE
If you want to see reminders in a search you need to go in the
"advanced" tab of the query builder and add something like that :
=head1 USAGE
AND ( Type = 'ticket' OR Type = 'reminder' )
A small help section is available in /Prefs/Calendar.html
=head1 UPGRADE FROM 0.02
Expand Down

0 comments on commit 94462e3

Please sign in to comment.