| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Jifty::Action::Record::Search - Automagic search action | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | The class is a base class for L<Jifty::Action>s that serve to provide | ||
| 11 | an interface to general searches through L<Jifty::Record> objects. To | ||
| 12 | use it, subclass it and override the C<record_class> method to return | ||
| 13 | the fully qualified name of the model to do searches over. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | package Jifty::Action::Record::Search; | ||
| 18 | use base qw/Jifty::Action::Record/; | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =head2 arguments | ||
| 23 | |||
| 24 | Remove validators from arguments, as well as ``mandatory'' | ||
| 25 | restrictions. Remove any arguments that render as password fields, or | ||
| 26 | refer to collections. | ||
| 27 | |||
| 28 | Generate additional search arguments for each field based on the | ||
| 29 | following criteria: | ||
| 30 | |||
| 31 | =over 4 | ||
| 32 | |||
| 33 | =item C<text>, C<char> or C<varchar> fields | ||
| 34 | |||
| 35 | Create C<field>_contains and C<field>_lacks arguments | ||
| 36 | |||
| 37 | =item C<date>, or C<timestamp> fields | ||
| 38 | |||
| 39 | Create C<field>_before, C<field>_after, C<field>_since and | ||
| 40 | C<field>_until arguments. | ||
| 41 | |||
| 42 | =item C<integer>, C<float>, C<double>, C<decimal> or C<numeric> fields | ||
| 43 | |||
| 44 | Generate C<field>_lt, C<field>_gt, C<field>_le and C<field>_ge arguments, as | ||
| 45 | well as a C<field>_dwim field that accepts a prefixed comparison operator in | ||
| 46 | the search value, such as C<< >100 >> and C<< !100 >>. | ||
| 47 | |||
| 48 | =back | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub arguments { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | # The args processing here is involved, so only calculate them once | ||
| 56 | return $self->_cached_arguments if $self->_cached_arguments; | ||
| 57 | |||
| 58 | # Iterate through all the arguments setup by Jifty::Action::Record | ||
| 59 | my $args = $self->SUPER::arguments; | ||
| 60 | for my $field (keys %$args) { | ||
| 61 | |||
| 62 | # Figure out what information we know about the field | ||
| 63 | my $info = $args->{$field}; | ||
| 64 | my $column = $self->record->column($field); | ||
| 65 | |||
| 66 | # We don't care about validation and mandatories on search | ||
| 67 | delete $info->{validator}; | ||
| 68 | delete $info->{mandatory}; | ||
| 69 | |||
| 70 | # If the column has a set of valid values, deal with those | ||
| 71 | if ($info->{valid_values}) { | ||
| 72 | my $valid_values = $info->{valid_values}; | ||
| 73 | |||
| 74 | # Canonicalize the valid values | ||
| 75 | local $@; | ||
| 76 | $info->{valid_values} = $valid_values = (eval { [ @$valid_values ] } || [$valid_values]); | ||
| 77 | |||
| 78 | # For radio display, display an "any" label (empty looks weird) | ||
| 79 | if (defined $info->{render_as} and lc $info->{render_as} eq 'radio') { | ||
| 80 | if (@$valid_values > 1) { | ||
| 81 | unshift @$valid_values, { display => _("(any)"), value => '' }; | ||
| 82 | $info->{default_value} ||= ''; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | # We've got only one choice anyway... | ||
| 86 | $info->{default_value} ||= $valid_values->[0]; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # If not radio, add a blank options | ||
| 91 | else { | ||
| 92 | unshift @$valid_values, ""; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | # You can't search passwords, so remove the fields | ||
| 97 | if(defined $info->{'render_as'} and lc $info->{'render_as'} eq 'password') { | ||
| 98 | delete $args->{$field}; | ||
| 99 | next; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Warn if we have a search field without an actual column | ||
| 103 | warn "No column for: $field" unless($column); | ||
| 104 | |||
| 105 | # Drop out X-to-many columns from the search | ||
| 106 | if(defined(my $refers_to = $column->refers_to)) { | ||
| 107 | delete $args->{$field} | ||
| 108 | if UNIVERSAL::isa($refers_to, 'Jifty::Collection'); | ||
| 109 | } | ||
| 110 | if ($info->{container}) { | ||
| 111 | delete $args->{$field}; | ||
| 112 | next; | ||
| 113 | } | ||
| 114 | |||
| 115 | # XXX TODO: What about booleans? Checkbox doesn't quite work, | ||
| 116 | # since there are three choices: yes, no, either. | ||
| 117 | |||
| 118 | # Magic _id refers_to columns | ||
| 119 | next if($field =~ /^(.*)_id$/ && $self->record->column($1)); | ||
| 120 | |||
| 121 | # Setup the field label for the comparison operator selection | ||
| 122 | my $label = $info->{label} || $field; | ||
| 123 | |||
| 124 | # Add the "X is not" operator | ||
| 125 | $args->{"${field}_not"} = { %$info, label => _("%1 is not", $label) }; | ||
| 126 | |||
| 127 | # The operators available depend on the type | ||
| 128 | my $type = lc($column->type); | ||
| 129 | |||
| 130 | # Add operators available for text fields | ||
| 131 | if($type =~ /(?:text|char)/) { | ||
| 132 | |||
| 133 | # Show a text entry box (rather than a textarea) | ||
| 134 | $info->{render_as} = 'text'; | ||
| 135 | |||
| 136 | # Add the "X contains" operator | ||
| 137 | $args->{"${field}_contains"} = { %$info, label => _("%1 contains", $label) }; | ||
| 138 | |||
| 139 | # Add the "X lacks" operator (i.e., opposite of "X contains") | ||
| 140 | $args->{"${field}_lacks"} = { %$info, label => _("%1 lacks", $label) }; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Handle date, datetime, time, and timestamp fields | ||
| 144 | elsif($type =~ /(?:date|time)/) { | ||
| 145 | |||
| 146 | # Add the "X after" date/time operation | ||
| 147 | $args->{"${field}_after"} = { %$info, label => _("%1 after", $label) }; | ||
| 148 | |||
| 149 | # Add the "X before" date/time operation | ||
| 150 | $args->{"${field}_before"} = { %$info, label => _("%1 before", $label) }; | ||
| 151 | |||
| 152 | # Add the "X since" date/time operation | ||
| 153 | $args->{"${field}_since"} = { %$info, label => _("%1 since", $label) }; | ||
| 154 | |||
| 155 | # Add the "X until" date/time operation | ||
| 156 | $args->{"${field}_until"} = { %$info, label => _("%1 until", $label) }; | ||
| 157 | } | ||
| 158 | |||
| 159 | # Handle number fields | ||
| 160 | elsif( $type =~ /(?:int|float|double|decimal|numeric)/ | ||
| 161 | && !$column->refers_to) { | ||
| 162 | |||
| 163 | # Add the "X greater than" operation | ||
| 164 | $args->{"${field}_gt"} = { %$info, label => _("%1 greater than", $label) }; | ||
| 165 | |||
| 166 | # Add the "X less than" operation | ||
| 167 | $args->{"${field}_lt"} = { %$info, label => _("%1 less than", $label) }; | ||
| 168 | |||
| 169 | # Add the "X greater than or equal to" operation | ||
| 170 | $args->{"${field}_ge"} = { %$info, label => _("%1 greater or equal to", $label) }; | ||
| 171 | |||
| 172 | # Add the "X less than or equal to" operation | ||
| 173 | $args->{"${field}_le"} = { %$info, label => _("%1 less or equal to", $label) }; | ||
| 174 | |||
| 175 | # Add the "X is whatever the heck I say it is" operation | ||
| 176 | $args->{"${field}_dwim"} = { %$info, hints => _('!=>< allowed') }; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | # Add generic contains/lacks search boxes for all fields | ||
| 181 | $args->{contains} = { type => 'text', label => _('Any field contains') }; | ||
| 182 | $args->{lacks} = { type => 'text', label => _('No field contains') }; | ||
| 183 | |||
| 184 | # Cache the results so we don't have to do THAT again | ||
| 185 | return $self->_cached_arguments($args); | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 take_action | ||
| 189 | |||
| 190 | Return a collection with the result of the search specified by the | ||
| 191 | given arguments. | ||
| 192 | |||
| 193 | We interpret a C<undef> argument as SQL C<NULL>, and ignore empty or | ||
| 194 | non-present arguments. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub take_action { | ||
| 199 | my $self = shift; | ||
| 200 | |||
| 201 | # Create a generic collection for our record class | ||
| 202 | my $collection = $self->record_class->collection_class->new( | ||
| 203 | record_class => $self->record_class, | ||
| 204 | current_user => $self->record->current_user | ||
| 205 | ); | ||
| 206 | |||
| 207 | # Start with an unlimited collection | ||
| 208 | $collection->find_all_rows; | ||
| 209 | |||
| 210 | # For each field, process the limits | ||
| 211 | for my $field (grep {$self->has_argument($_)} $self->argument_names) { | ||
| 212 | |||
| 213 | # We process contains last, skip it here | ||
| 214 | next if $field eq 'contains'; | ||
| 215 | |||
| 216 | # Get the value set on the field | ||
| 217 | my $value = $self->argument_value($field); | ||
| 218 | |||
| 219 | # Load the column this field belongs to | ||
| 220 | my $column = $self->record->column($field); | ||
| 221 | my $op = undef; | ||
| 222 | |||
| 223 | # A comparison or substring search rather than an exact match? | ||
| 224 | if (!$column) { | ||
| 225 | |||
| 226 | # If we don't have a column, this is a comparison or | ||
| 227 | # substring search. Skip undef values for those, since | ||
| 228 | # NULL makes no sense. | ||
| 229 | next unless defined($value); | ||
| 230 | next if $value =~ /^\s*$/; | ||
| 231 | |||
| 232 | # Decode the field_op name | ||
| 233 | if ($field =~ m{^(.*)_([[:alpha:]]+)$}) { | ||
| 234 | $field = $1; | ||
| 235 | $op = $2; | ||
| 236 | |||
| 237 | # Convert each operator into limit operators | ||
| 238 | if($op eq 'not') { | ||
| 239 | $op = '!='; | ||
| 240 | } elsif($op eq 'contains') { | ||
| 241 | $op = 'LIKE'; | ||
| 242 | $value = "%$value%"; | ||
| 243 | } elsif($op eq 'lacks') { | ||
| 244 | $op = 'NOT LIKE'; | ||
| 245 | $value = "%$value%"; | ||
| 246 | } elsif($op eq 'after' || $op eq 'gt') { | ||
| 247 | $op = '>'; | ||
| 248 | } elsif($op eq 'before' || $op eq 'lt') { | ||
| 249 | $op = '<'; | ||
| 250 | } elsif($op eq 'since' || $op eq 'ge') { | ||
| 251 | $op = '>='; | ||
| 252 | } elsif($op eq 'until' || $op eq 'le') { | ||
| 253 | $op = '<='; | ||
| 254 | } elsif($op eq 'dwim') { | ||
| 255 | $op = '='; | ||
| 256 | if (defined($value) and $value =~ s/^\s*([<>!=]{1,2})\s*//) { | ||
| 257 | $op = $1; | ||
| 258 | $op = '!=' if $op eq '!'; | ||
| 259 | $op = '=' if $op eq '=='; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | # Doesn't look like a field_op, skip it | ||
| 265 | else { | ||
| 266 | next; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | # Now, add the limit if we have a value set | ||
| 271 | if (defined($value)) { | ||
| 272 | next if $value =~ /^\s*$/; # skip blank values! | ||
| 273 | |||
| 274 | # Allow != and NOT LIKE to match NULL columns | ||
| 275 | if ($op && $op =~ /^(?:!=|NOT LIKE)$/) { | ||
| 276 | $collection->limit( | ||
| 277 | column => $field, | ||
| 278 | value => $value, | ||
| 279 | operator => $op, | ||
| 280 | entry_aggregator => 'OR', | ||
| 281 | case_sensitive => 0, | ||
| 282 | ); | ||
| 283 | $collection->limit( | ||
| 284 | column => $field, | ||
| 285 | value => 'NULL', | ||
| 286 | operator => 'IS', | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | |||
| 290 | # For any others, just the facts please | ||
| 291 | else { | ||
| 292 | $collection->limit( | ||
| 293 | column => $field, | ||
| 294 | value => $value, | ||
| 295 | operator => $op || "=", | ||
| 296 | entry_aggregator => 'AND', | ||
| 297 | $op ? (case_sensitive => 0) : (), | ||
| 298 | ); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | # The value is not defined at all, so expect a NULL | ||
| 303 | else { | ||
| 304 | $collection->limit( | ||
| 305 | column => $field, | ||
| 306 | value => 'NULL', | ||
| 307 | operator => 'IS' | ||
| 308 | ); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # Handle the general contains last | ||
| 313 | if($self->has_argument('contains')) { | ||
| 314 | |||
| 315 | # See if any column contains the text described | ||
| 316 | my $any = $self->argument_value('contains'); | ||
| 317 | if (length $any) { | ||
| 318 | for my $col ($self->record->columns) { | ||
| 319 | if($col->type =~ /(?:text|varchar)/) { | ||
| 320 | $collection->limit(column => $col->name, | ||
| 321 | value => "%$any%", | ||
| 322 | operator => 'LIKE', | ||
| 323 | entry_aggregator => 'OR', | ||
| 324 | subclause => 'contains'); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | # Add the limited collection to the results | ||
| 331 | $self->result->content(search => $collection); | ||
| 332 | $self->result->success; | ||
| 333 | } | ||
| 334 | |||
| 335 | =head1 SEE ALSO | ||
| 336 | |||
| 337 | L<Jifty::Action::Record>, L<Jifty::Collection> | ||
| 338 | |||
| 339 | =head1 LICENSE | ||
| 340 | |||
| 341 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||
| 342 | Jifty is distributed under the same terms as Perl itself. | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | 1; |