summaryrefslogtreecommitdiff
path: root/rt/share/html/Articles/Article/Search.html
blob: 2d1ad731fdd6732acda314441ab4b80f845c08fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<& /Elements/Header, Title => loc("Search for articles") &>
<& /Elements/Tabs &>

% unless ( keys %ARGS ) {
<table width="100%" border="0">
<tr>
<td valign="top" width="50%">
% if (not $classes_configured) {
<& /Articles/Elements/NeedsSetup &>
% } elsif (not @classes) {
<i><&|/l&>You don't have permission to view Articles in any Class</&></i>
% } else {
<ul>
% for my $class (@classes) {
<li><a href="<%RT->Config->Get('WebPath')%>/Articles/Article/Search.html?<% $m->comp('/Elements/QueryString', %filtered, Class => $class->id) %>"><&|/l, $class->Name&>in class [_1]</&></a></li>
% }
</ul>
% }
</td>
<td valign="top" width="50%">
<form action="Search.html" method="get">
<& /Widgets/TitleBoxStart, title => loc('Saved searches') &>
<&|/l&>Load saved search:</&><br />
<& Elements/SelectSavedSearches, Name => 'LoadSavedSearch', Default => $CurrentSearch &>
<input value="<%loc('Load')%>" name="Load" type="submit" />
<& /Widgets/TitleBoxEnd &>
</form>
</td>
</tr>
</table>
%  return;
% }

<& /Elements/ListActions, actions => \@results &>
<div style="float:right;margin-top:15px"><a href="#criteria"><&|/l&>Modify this search...</&></a></div>

% if ($articles->BuildSelectCountQuery =~ /WHERE/i) {
<h2><&|/l&>Search results</&></h2>
<& /Elements/CollectionList,
    Collection => $articles, 
    AllowSorting => 1,
    OrderBy => \@OrderBy,
    Order => \@Order,
    Format => $format,
    GenericQueryArgs => { %filtered, Format => $format, },
    &>
% }

<br />
<br />
<br />
<a name="criteria"></a>
<form action="Search.html" method="get">
<& Elements/ShowSearchCriteria, dates => \%dates, RefersTo => $RefersTo, customfields => $customfields,  ReferredToBy => $ReferredToBy, %ARGS &>
<br />
<br />
<& Elements/ShowSavedSearches, CurrentSearch => $CurrentSearch, 
    Name => ($search ? $search->Name : undef),
    Privacy => ($search ? $search->Privacy : undef) &>

</form>

<div align=right>
<a href="<%RT->Config->Get('WebPath')%>/Articles/Article/Search.html<%$QueryString%>"><&|/l&>Bookmarkable link for this search</&></a><br />
</div>
<%init>
my $Classes = RT::Classes->new($session{'CurrentUser'});
$Classes->LimitToEnabled();

# This is a COUNT(), which doesn't apply ACLs; as such, we don't display
# the warning if there are classes, but the user can't see them.
my $classes_configured = $Classes->Count;
my @classes = @{ $Classes->ItemsArrayRef };
$ARGS{Class} = $classes[0]->id if @classes == 1;

use RT::SavedSearch;
my @results;
my $articles = RT::Articles->new( $session{'CurrentUser'} );
my $format = q{
    '<a href="__WebPath__/Articles/Article/Display.html?id=__id__">__id__</a>/TITLE:#',
    '<a href="__WebPath__/Articles/Article/Display.html?id=__id__">__Name__</a>/TITLE:Name',
    '__ClassName__',
    '__CreatedRelative__',
    '__LastUpdatedRelative__',
    '__Summary__',
    '__Topics__', };

# {{{ Quicksearch logic

# If it is a number, load the article with that ID.  Otherwise, search
# on name and summary.
if ($ARGS{'q'} && $ARGS{'q'} =~ /^(\d+)$/) {
    RT::Interface::Web::Redirect( RT->Config->Get('WebURL') . "Articles/Article/Display.html?id=" . $1 );
}
# }}}

# {{{ Saved search logic

my $search;

# The keys in %ARGS that are not saved and loaded with named searches.
# These need to be treated specially.
my @metakeys = qw/NewSearchName CurrentSearch SearchPrivacy Save Load
    Update Delete/;

if ($CurrentSearch =~ /^(.*-\d+)-SavedSearch-(\d+)$/) {
    $search = RT::SavedSearch->new($session{'CurrentUser'});
    $search->Load($1, $2);
}

# Have we been asked to load a search?

if ($ARGS{'Load'}) {
    if ($ARGS{'LoadSavedSearch'} =~ /^(.*-\d+)-SavedSearch-(\d+)$/ ) {
        my $privacy = $1;
        my $search_id = $2;
        
        $search = RT::SavedSearch->new($session{'CurrentUser'});
        my ($ret, $msg) = $search->Load($privacy, $search_id);
        if ($ret) {
            my $searchargs = $search->GetParameter('args');
            # Clean out ARGS and fill it in with the saved args from the 
            # loaded search.
            foreach my $key (@metakeys) {
                $searchargs->{$key} = $ARGS{$key};
            }
            %ARGS = %{$searchargs};
            $CurrentSearch = "$privacy-SavedSearch-$search_id";
        } else {
            push(@results, loc("Error: could not load saved search [_1]: [_2]",
                               $ARGS{'LoadSavedSearch'}, $msg));
        }
    } else {
        push(@results, loc("Invalid [_1] argument", 'LoadSavedSearch'));
    }
}

# ...or have we been asked to save, update, or delete a search?

if ($ARGS{'Save'}) {
    my %searchargs = %ARGS;
    foreach my $key (@metakeys) {
        delete $searchargs{$key};
    }

    $search = RT::SavedSearch->new($session{'CurrentUser'});
    unless ($ARGS{'SearchPrivacy'} =~ /^(.*)-(\d+)$/) {
        # This shouldn't really happen, but hey.
        push(@results, loc("WARNING: Saving search to user-level privacy"));
        $ARGS{'SearchPrivacy'} = 'RT::User-'.$session{'CurrentUser'}->Id;
    }
    my ($ret, $msg) = $search->Save(Privacy => $ARGS{'SearchPrivacy'},
                                    Type => 'Article',
                                    Name => $ARGS{'NewSearchName'},
                                    SearchParams => {'args' => \%searchargs});
    if ($ret) {
        $CurrentSearch = $ARGS{'SearchPrivacy'} . "-SavedSearch-" .
            $search->Id;
        push(@results, loc("Created search [_1]", $search->Name));
    } else {
        undef $search; # if we bomb out creating a search
                        # we don't want to have the empty object hang around
        push(@results, loc("Could not create search: [_1]", $msg));
    }
} elsif ($ARGS{'Update'}) {
    if ($ARGS{'SearchPrivacy'} != $search->Privacy) {
        push(@results,
             loc("Error: cannot change privacy value of existing search"));
    } else {
        my %searchargs = %ARGS;
        foreach my $key (@metakeys) {
            delete $searchargs{$key};
        }

        # We already have a search loaded, because CurrentSearch is set,
        # or else we would not have gotten here.
        my ($ret, $msg) = $search->Update(Name => $ARGS{'NewSearchName'},
                                          SearchParams => \%searchargs);
        if ($ret) {
            push(@results, loc("Search [_1] updated", $search->Name));
        } else {
            push(@results, loc("Error: search [_1] not updated: [_2]",
                               $search->Name, $msg));
        }
    }
} elsif ($ARGS{'Delete'}) {
    # Keep track of this, as we are about to delete the search.
    my $searchname = $search->Name;
    my ($ret, $msg) = $search->Delete;
    if ($ret) {
        $ARGS{'CurrentSearch'} = undef;
        push(@results, loc("Deleted search [_1]", $searchname));
        # Get rid of all the state.
        foreach my $key (keys %ARGS) {
            delete $ARGS{$key};
        }
        $CurrentSearch = 'new';
        $search = undef;
        $RefersTo = undef;
        $ReferredToBy = undef;
    } else {
        push(@results, loc("Could not delete search [_1]: [_2]",
             $searchname, $msg));
    }
}

# }}}

    # Don't want to search for a null class when there is no class specced
my $customfields = RT::CustomFields->new( $session{'CurrentUser'} );
my %dates;

$articles->Search( %ARGS, CustomFields => $customfields, Dates => \%dates,
    OrderBy => \@OrderBy, Order => \@Order, );

$m->callback( %ARGS, _Search => $articles );

my %filtered = %ARGS;
delete $filtered{$_} for (@metakeys, "EditTopics", "ExpandTopics");
delete $filtered{$_} for grep {$filtered{$_} !~ /\S/} keys %filtered;
@filtered{qw(OrderBy Order)} = (\@OrderBy, \@Order);
my $QueryString = "?".$m->comp('/Elements/QueryString', %filtered);
</%init>

<%ARGS>
$CreatedBefore => ''
$CreatedAfter => ''
$LastUpdatedBefore => ''
$LastUpdatedAfter => ''
$RefersTo => undef
$ReferredToBy => undef
$CurrentSearch => 'new'
@OrderBy => ()
@Order   => ()
</%ARGS>