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
|
%# BEGIN LICENSE BLOCK
%#
%# Copyright (c) 2002-2003 Jesse Vincent <jesse@bestpractical.com>
%#
%# This program is free software; you can redistribute it and/or modify
%# it under the terms of version 2 of the GNU General Public License
%# as published by the Free Software Foundation.
%#
%# A copy of that license should have arrived with this
%# software, but in any event can be snarfed from www.gnu.org.
%#
%# This program 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.
%#
%# END LICENSE BLOCK
<& /RTFM/Article/Elements/Tabs, current_tab => "RTFM/Article/Search.html", Title => loc("Search for articles") &>
<& Elements/ShowSearchResults, articles => $articles &>
<& Elements/ShowSearchCriteria, dates => \%dates, RefersTo => $RefersTo, customfields => $customfields, ReferredToBy => $ReferredToBy, %ARGS &>
<%init>
my @results;
my $articles = RT::FM::ArticleCollection->new( $session{'CurrentUser'} );
my $customfields = RT::FM::CustomFieldCollection->new( $session{'CurrentUser'} );
if ( $ARGS{'Class'} ) {
my @Classes =
( ref $ARGS{'Class'} eq 'ARRAY' )
? @{ $ARGS{'Class'} }
: ( $ARGS{'Class'} );
foreach my $class (@Classes) {
$customfields->LimitToClass($class);
}
}
else {
$customfields->UnLimit();
}
# Don't want to search for a null class when there is no class specced
my %dates;
foreach my $date qw(Created< Created> LastUpdated< LastUpdated>) {
next unless ( $ARGS{$date} );
my $seconds = parsedate( $ARGS{$date}, FUZZY => 1, PREFER_PAST => 1 );
my $date_obj = RT::Date->new( $session{'CurrentUser'} );
$date_obj->Set( Format => 'unix', Value => $seconds );
$dates{$date} = $date_obj;
if ( $date =~ /^(.*?)<$/i ) {
$articles->Limit( FIELD => $1, OPERATOR => "<=", ENTRYAGGREGATOR => "AND", VALUE => $date_obj->ISO );
}
if ( $date =~ /^(.*?)>$/i ) {
$articles->Limit( FIELD => $1, OPERATOR => ">=", ENTRYAGGREGATOR => "AND", VALUE => $date_obj->ISO );
}
}
foreach my $link ( split ( /\s+/, $RefersTo ) ) {
next unless ($link);
$articles->LimitRefersTo($link);
}
foreach my $link ( split ( /\s+/, $ReferredToBy ) ) {
next unless ($link);
$articles->LimitReferredToBy($link);
}
my %cfs;
my $all_cfs = RT::FM::CustomFieldCollection->new($session{'CurrentUser'});
$all_cfs->UnLimit();
while ( my $cf = $all_cfs->Next ) {
$cfs{ $cf->Name } = $cf->Id;
}
foreach my $field ( keys %cfs ) {
my @MatchLike = (ref $ARGS{ $field."~" } eq 'ARRAY' ) ? @{ $ARGS{ $field."~" } } : ( $ARGS{$field."~" } );
my @NoMatchLike = (ref $ARGS{ $field."!~" } eq 'ARRAY' ) ? @{ $ARGS{ $field."!~" } } : ( $ARGS{$field."!~" } );
my @Match = (ref $ARGS{ $field } eq 'ARRAY' ) ? @{ $ARGS{ $field } } : ( $ARGS{$field } );
my @NoMatch = (ref $ARGS{ $field."!" } eq 'ARRAY' ) ? @{ $ARGS{ $field."!" } } : ( $ARGS{$field."!" } );
foreach my $val (@MatchLike) {
next unless $val;
push @Match, "~".$val;
}
foreach my $val (@NoMatchLike) {
next unless $val;
push @NoMatch, "~".$val;
}
foreach my $value (@Match) {
next unless $value;
my $op;
if ( $value =~ /^~(.*)$/ ) {
$value = "%$1%";
$op = 'LIKE';
}
else {
$op = '=';
}
$articles->LimitCustomField( FIELD => $cfs{$field},
VALUE => $value,
ENTRYAGGREGATOR => 'OR',
OPERATOR => $op );
}
foreach my $value (@NoMatch) {
next unless $value;
my $op;
if ( $value =~ /^~(.*)$/ ) {
$value = "%$1%";
$op = 'NOT LIKE';
}
else {
$op = '!=';
}
$articles->LimitCustomField( FIELD => $cfs{$field},
VALUE => $value,
ENTRYAGGREGATOR => 'OR',
OPERATOR => $op );
}
}
foreach my $field qw(Name Summary Class) {
my @MatchLike = (ref $ARGS{ $field."~" } eq 'ARRAY' ) ? @{ $ARGS{ $field."~" } } : ( $ARGS{$field."~" } );
my @NoMatchLike = (ref $ARGS{ $field."!~" } eq 'ARRAY' ) ? @{ $ARGS{ $field."!~" } } : ( $ARGS{$field."!~" } );
my @Match = (ref $ARGS{ $field } eq 'ARRAY' ) ? @{ $ARGS{ $field } } : ( $ARGS{$field } );
my @NoMatch = (ref $ARGS{ $field."!" } eq 'ARRAY' ) ? @{ $ARGS{ $field."!" } } : ( $ARGS{$field."!" } );
foreach my $val (@MatchLike) {
next unless $val;
push @Match, "~".$val;
}
foreach my $val (@NoMatchLike) {
next unless $val;
push @NoMatch, "~".$val;
}
my $op;
foreach my $value (@Match) {
if ( $value =~ /^~(.*)$/ ) {
$value = "%$1%";
$op = 'LIKE';
}
else {
$op = '=';
}
# preprocess Classes, so we can search on class
if ( $field eq 'Class' && $value ) {
my $class = RT::FM::Class->new($RT::SystemUser);
$class->Load($value);
$value = $class->Id;
}
# now that we've pruned the value, get out if it's different.
next unless $value;
$articles->Limit( SUBCLAUSE => $field . 'Match',
FIELD => $field,
OPERATOR => $op,
VALUE => $value,
ENTRYAGGREGATOR => 'OR' );
}
foreach my $value (@NoMatch) {
# preprocess Classes, so we can search on class
if ( $value =~ /^~(.*)/ ) {
$value = "%$1%";
$op = 'NOT LIKE';
}
else {
$op = '!=';
}
if ( $field eq 'Class' ) {
my $class = RT::FM::Class->new($RT::SystemUser);
$class->Load($value);
$value = $class->Id;
}
# now that we've pruned the value, get out if it's different.
next unless $value;
$articles->Limit( SUBCLAUSE => $field . 'NoMatch',
OPERATOR => $op,
VALUE => $value,
FIELD => $field,
ENTRYAGGREGATOR => 'AND' );
}
}
</%init>
</%init>
<%ARGS>
$CreatedBefore => ''
$CreatedAfter => ''
$LastUpdatedBefore => ''
$LastUpdatedAfter => ''
$RefersTo => undef
$ReferredToBy => undef
</%ARGS>
|