import rt 3.6.6
[freeside.git] / rt / lib / RT / Search / Googleish.pm
1
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
7 #                                          <jesse@bestpractical.com>
8
9 # (Except where explicitly superseded by other copyright notices)
10
11
12 # LICENSE:
13
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
17 # from www.gnu.org.
18
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/copyleft/gpl.html.
29
30
31 # CONTRIBUTION SUBMISSION POLICY:
32
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
38
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
47
48 # END BPS TAGGED BLOCK }}}
49 =head1 NAME
50
51   RT::Search::Googlish
52
53 =head1 SYNOPSIS
54
55 =head1 DESCRIPTION
56
57 Use the argument passed in as a "Google-style" set of keywords
58
59 =head1 METHODS
60
61
62 =begin testing
63
64 ok (require RT::Search::Generic);
65
66 =end testing
67
68
69 =cut
70
71 package RT::Search::Googleish;
72
73 use strict;
74 use base qw(RT::Search::Generic);
75
76
77 # sub _Init {{{
78 sub _Init {
79     my $self = shift;
80     my %args = @_;
81
82     $self->{'Queues'} = delete($args{'Queues'}) || [];
83     $self->SUPER::_Init(%args);
84 }
85 # }}}
86
87 # {{{ sub Describe 
88 sub Describe  {
89   my $self = shift;
90   return ($self->loc("No description for [_1]", ref $self));
91 }
92 # }}}
93
94 # {{{ sub QueryToSQL
95 sub QueryToSQL {
96     my $self     = shift;
97     my $query    = shift || $self->Argument;
98     my @keywords = split /\s+/, $query;
99     my (
100         @tql_clauses,  @owner_clauses, @queue_clauses,
101         @user_clauses, @id_clauses,    @status_clauses
102     );
103     my ( $Queue, $User );
104     for my $key (@keywords) {
105
106         # Is this a ticket number? If so, go to it.
107         if ( $key =~ m/^\d+$/ ) {
108             push @id_clauses, "id = '$key'";
109         }
110
111         elsif ( $key =~ /\w+\@\w+/ ) {
112             push @user_clauses, "Requestor LIKE '$key'";
113         }
114
115         # Is there a status with this name?
116         elsif (
117             $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser )
118             and $Queue->IsValidStatus($key)
119           )
120         {
121             push @status_clauses, "Status = '" . $key . "'";
122         }
123
124         # Is there a owner named $key?
125         # Is there a queue named $key?
126         elsif ( $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser )
127             and $Queue->Load($key) )
128         {
129             push @queue_clauses, "Queue = '" . $Queue->Name . "'";
130         }
131
132         # Is there a owner named $key?
133         elsif ( $User = RT::User->new( $self->TicketsObj->CurrentUser )
134             and $User->Load($key)
135             and $User->Privileged )
136         {
137             push @owner_clauses, "Owner = '" . $User->Name . "'";
138         }
139
140         elsif ($key =~ /^fulltext:(.*?)$/i) {
141             $key = $1;
142             $key =~ s/['\\].*//g;
143             push @tql_clauses, "Content LIKE '$key'";
144
145         }
146
147         # Else, subject must contain $key
148         else {
149             $key =~ s/['\\].*//g;
150             push @tql_clauses, "Subject LIKE '$key'";
151         }
152     }
153
154     # restrict to any queues requested by the caller
155     for my $queue (@{ $self->{'Queues'} }) {
156         my $QueueObj = RT::Queue->new($self->TicketsObj->CurrentUser);
157         $QueueObj->Load($queue) or next;
158         push @queue_clauses, "Queue = '" . $QueueObj->Name . "'";
159     }
160
161     push @tql_clauses, join( " OR ", sort @id_clauses );
162     push @tql_clauses, join( " OR ", sort @owner_clauses );
163     push @tql_clauses, join( " OR ", sort @status_clauses );
164     push @tql_clauses, join( " OR ", sort @user_clauses );
165     push @tql_clauses, join( " OR ", sort @queue_clauses );
166     @tql_clauses = grep { $_ ? $_ = "( $_ )" : undef } @tql_clauses;
167     return join " AND ", sort @tql_clauses;
168 }
169 # }}}
170
171 # {{{ sub Prepare
172 sub Prepare  {
173   my $self = shift;
174   my $tql = $self->QueryToSQL($self->Argument);
175
176   $RT::Logger->crit($tql);
177
178   $self->TicketsObj->FromSQL($tql);
179   return(1);
180 }
181 # }}}
182
183 eval "require RT::Search::Googleish_Vendor";
184 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Search/Googleish_Vendor.pm});
185 eval "require RT::Search::Googleish_Local";
186 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Search/Googleish_Local.pm});
187
188 1;