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 |
220 |
|
221 |
=cut |
222 |
|
223 |
sub _build_query_params_from_api { |
224 |
|
225 |
my ( $filtered_params, $reserved_params ) = @_; |
226 |
|
227 |
my $params; |
228 |
my $match = $reserved_params->{_match} // 'contains'; |
229 |
|
230 |
foreach my $param ( keys %{$filtered_params} ) { |
231 |
if ( $match eq 'contains' ) { |
232 |
$params->{$param} = |
233 |
{ like => '%' . $filtered_params->{$param} . '%' }; |
234 |
} |
235 |
elsif ( $match eq 'starts_with' ) { |
236 |
$params->{$param} = { like => $filtered_params->{$param} . '%' }; |
237 |
} |
238 |
elsif ( $match eq 'ends_with' ) { |
239 |
$params->{$param} = { like => '%' . $filtered_params->{$param} }; |
240 |
} |
241 |
elsif ( $match eq 'exact' ) { |
242 |
$params->{$param} = $filtered_params->{$param}; |
243 |
} |
244 |
else { |
245 |
Koha::Exceptions::WrongParameter->throw( |
246 |
"Invalid value for _match param ($match)"); |
247 |
} |
248 |
} |
249 |
|
250 |
return $params; |
251 |
} |
252 |
|
173 |
=head3 single |
253 |
=head3 single |
174 |
|
254 |
|
175 |
my $object = Koha::Objects->search({}, { rows => 1 })->single |
255 |
my $object = Koha::Objects->search({}, { rows => 1 })->single |
176 |
- |
|
|