This patch adds new methods in Koha::Library::Groups and Koha::Library.
1) For Koha::Library::Groups adds get_root_ancestor that returns all root
groups for a given search parameters, for example
Koha::Library::Groups->get_root_ancestor( { id => $group_id } )
2) For Koha::Library adds
2.1) get_hold_libraries: returns all libraries (including self) that
belongs to the same holdgroups. If $self belongs to several holdgroups
it will return a distinct list of all libraries belonging to them.
2.2) validate_hold_sibling: Returns 1 if the given parameters matches any of the
libraries that belong to any of the holdgroups this library belongs. For example
$library->validate_hold_sibling( { branchcode => $branchcode } )
To test:
1) apply this patch
2) prove t/db_dependent/Koha/Libraries.t t/db_dependent/LibraryGroups.t
SUCCESS => green letters :-D
3) Sign off
Sponsored-by: VOKAL
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
};
}
+=head3 get_hold_libraries
+
+Return all libraries (including self) that belong to the same hold groups
+
+=cut
+
+sub get_hold_libraries {
+ my ( $self ) = @_;
+ my $library_groups = $self->library_groups;
+ my @hold_libraries;
+ while ( my $library_group = $library_groups->next ) {
+ my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
+ if($root->ft_local_hold_group) {
+ push @hold_libraries, $root->all_libraries;
+ }
+ }
+
+ my %seen;
+ @hold_libraries =
+ grep { !$seen{ $_->id }++ } @hold_libraries;
+
+ return @hold_libraries;
+}
+
+=head3 validate_hold_sibling
+
+Return if given library is a valid hold group member
+
+=cut
+
+sub validate_hold_sibling {
+ my ( $self, $params ) = @_;
+ my @hold_libraries = $self->get_hold_libraries;
+
+ foreach (@hold_libraries) {
+ my $hold_library = $_;
+ my $is_valid = 1;
+ foreach my $key (keys %$params) {
+ unless($hold_library->$key eq $params->{$key}) {
+ $is_valid=0;
+ last;
+ }
+ }
+ if($is_valid) {
+ #Found one library that meets all search parameters
+ return 1;
+ }
+ }
+ return 0;
+}
+
=head2 Internal methods
=head3 _type
return $self->search( { $field => 1 } );
}
+
+=head3 get_root_ancestor
+
+my $root_ancestor = $self->get_root_ancestor( {id => $group_id } )
+
+Retrieve root ancestor group for a specified id.
+
+=cut
+
+sub get_root_ancestor {
+ my ( $self, $params ) = @_;
+ my $row = $self->find($params);
+ return $row unless $row->parent_id;
+ return $self->get_root_ancestor( { id => $row->parent_id } );
+}
+
+
=head3 type
=cut
use Modern::Perl;
-use Test::More tests => 8;
+use Test::More tests => 9;
use C4::Biblio;
use C4::Context;
$schema->storage->txn_rollback;
};
+
+subtest 'get_hold_libraries and validate_hold_sibling' => sub {
+
+ plan tests => 5;
+
+ $schema->storage->txn_begin;
+
+ my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
+
+ my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
+ my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
+ my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
+
+ my @hold_libraries = ($library1, $library2);
+
+ my @result = $library1->get_hold_libraries();
+
+ ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
+
+ my %map = map {$_->branchcode, 1} @result;
+
+ foreach my $hold_library ( @hold_libraries ) {
+ ok(exists $map{$hold_library->branchcode}, 'library in hold group');
+ }
+
+ ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
+ ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
+
+ $schema->storage->txn_rollback;
+
+};
use List::MoreUtils 'any';
-use Test::More tests => 20;
+use Test::More tests => 21;
use t::lib::TestBuilder;
use Koha::Database;
@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
is( ref(($groupX->all_libraries)[0]), 'Koha::Library', '->all_libraries should return a list of Koha::Library - in the future it should be fixed to return a Koha::Libraries iterator instead'); # FIXME
+
+subtest 'Koha::Library::Groups->get_root_ancestor' => sub {
+ plan tests => 2;
+
+ my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store();
+ my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id, branchcode => $library1->{branchcode} })->store();
+ my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store();
+ my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id, branchcode => $library2->{branchcode} })->store();
+ my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store();
+ my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id, title => "Group Z1" })->store();
+ my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id, title => "Group Z2" })->store();
+ my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id, branchcode => $library2->{branchcode} })->store();
+
+ my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed);
+ my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed);
+
+ is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor");
+ ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids");
+};
\ No newline at end of file