|
Lines 23-28
use Carp;
Link Here
|
| 23 |
use List::MoreUtils qw( none ); |
23 |
use List::MoreUtils qw( none ); |
| 24 |
|
24 |
|
| 25 |
use Koha::Database; |
25 |
use Koha::Database; |
|
|
26 |
use Koha::Exceptions; |
| 26 |
|
27 |
|
| 27 |
=head1 NAME |
28 |
=head1 NAME |
| 28 |
|
29 |
|
|
Lines 170-175
sub search_related {
Link Here
|
| 170 |
} |
171 |
} |
| 171 |
} |
172 |
} |
| 172 |
|
173 |
|
|
|
174 |
=head3 search_for_api |
| 175 |
|
| 176 |
my @objects = Koha::Objects->earch_for_api( $c ); |
| 177 |
|
| 178 |
Searches for objects given a controller object I<$c>. |
| 179 |
|
| 180 |
=cut |
| 181 |
|
| 182 |
sub search_for_api { |
| 183 |
my ( $self, $c ) = @_; |
| 184 |
|
| 185 |
my $args = $c->validation->output; |
| 186 |
my $attributes; |
| 187 |
|
| 188 |
# Extract reserved params |
| 189 |
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args); |
| 190 |
|
| 191 |
# Merge sorting into query attributes |
| 192 |
$c->dbic_merge_sorting( |
| 193 |
{ |
| 194 |
attributes => $attributes, |
| 195 |
params => $reserved_params |
| 196 |
} |
| 197 |
); |
| 198 |
|
| 199 |
# Merge pagination into query attributes |
| 200 |
$c->dbic_merge_pagination( |
| 201 |
{ |
| 202 |
attributes => $attributes, |
| 203 |
params => $reserved_params |
| 204 |
} |
| 205 |
); |
| 206 |
|
| 207 |
# Perform search |
| 208 |
my $objects = $self->search( $filtered_params, $attributes ); |
| 209 |
$c->add_pagination_headers({ total => $objects->count, params => $args }) |
| 210 |
if $objects->is_paged; |
| 211 |
|
| 212 |
return $objects; |
| 213 |
} |
| 214 |
|
| 215 |
=head2 _build_query_params_from_api |
| 216 |
|
| 217 |
my $params = _build_query_params_from_api( $filtered_params, $reserved_params ); |
| 218 |
|
| 219 |
Builds the params for searching on DBIC based on the selected matching algorithm. |
| 220 |
Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is |
| 221 |
I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception |
| 222 |
is raised. |
| 223 |
|
| 224 |
=cut |
| 225 |
|
| 226 |
sub _build_query_params_from_api { |
| 227 |
|
| 228 |
my ( $filtered_params, $reserved_params ) = @_; |
| 229 |
|
| 230 |
my $params; |
| 231 |
my $match = $reserved_params->{_match} // 'contains'; |
| 232 |
|
| 233 |
foreach my $param ( keys %{$filtered_params} ) { |
| 234 |
if ( $match eq 'contains' ) { |
| 235 |
$params->{$param} = |
| 236 |
{ like => '%' . $filtered_params->{$param} . '%' }; |
| 237 |
} |
| 238 |
elsif ( $match eq 'starts_with' ) { |
| 239 |
$params->{$param} = { like => $filtered_params->{$param} . '%' }; |
| 240 |
} |
| 241 |
elsif ( $match eq 'ends_with' ) { |
| 242 |
$params->{$param} = { like => '%' . $filtered_params->{$param} }; |
| 243 |
} |
| 244 |
elsif ( $match eq 'exact' ) { |
| 245 |
$params->{$param} = $filtered_params->{$param}; |
| 246 |
} |
| 247 |
else { |
| 248 |
Koha::Exceptions::WrongParameter->throw( |
| 249 |
"Invalid value for _match param ($match)"); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return $params; |
| 254 |
} |
| 255 |
|
| 173 |
=head3 single |
256 |
=head3 single |
| 174 |
|
257 |
|
| 175 |
my $object = Koha::Objects->search({}, { rows => 1 })->single |
258 |
my $object = Koha::Objects->search({}, { rows => 1 })->single |
| 176 |
- |
|
|