There was already 2 Koha::Branch[es] using Koha::Object[s] before.
For this new rewrite, it seems preferable to start with good basis and
name the new modules Koha::Library and Koha::Libraries.
Tested both patches together, works as expected.
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
+++ /dev/null
-package Koha::Branch;
-
-# Copyright ByWater Solutions 2014
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha 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.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-use Modern::Perl;
-
-use Carp;
-
-use Koha::Database;
-
-use base qw(Koha::Object);
-
-=head1 NAME
-
-Koha::Branch - Koha Branch object class
-
-=head1 API
-
-=head2 Class Methods
-
-=cut
-
-=head3 type
-
-=cut
-
-sub type {
- return 'Branch';
-}
-
-=head1 AUTHOR
-
-Kyle M Hall <kyle@bywatersolutions.com>
-
-=cut
-
-1;
+++ /dev/null
-package Koha::Branches;
-
-# Copyright ByWater Solutions 2014
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha 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.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-use Modern::Perl;
-
-use Carp;
-
-use Koha::Database;
-
-use Koha::Branch;
-
-use base qw(Koha::Objects);
-
-=head1 NAME
-
-Koha::Branches - Koha Branch object set class
-
-=head1 API
-
-=head2 Class Methods
-
-=cut
-
-=head3 type
-
-=cut
-
-sub type {
- return 'Branch';
-}
-
-=head3 object_class
-
-=cut
-
-sub object_class {
- return 'Koha::Branch';
-}
-
-=head1 AUTHOR
-
-Kyle M Hall <kyle@bywatersolutions.com>
-
-=cut
-
-1;
use Carp;
use C4::Context qw(preference);
-use Koha::DateUtils qw(dt_from_string);
+use Koha::DateUtils qw(dt_from_string);
use Koha::Borrowers;
use Koha::Biblios;
-use Koha::Branches;
use Koha::Items;
+use Koha::Libraries;
use base qw(Koha::Object);
=head3 branch
-Returns the related Koha::Branch object for this Hold
+Returns the related Koha::Library object for this Hold
=cut
sub branch {
my ($self) = @_;
- $self->{_branch} ||= Koha::Branches->find( $self->branchcode() );
+ $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
return $self->{_branch};
}
use Koha::Database;
-use Koha::Branches;
use Koha::Borrowers;
+use Koha::Libraries;
use base qw(Koha::Object);
sub home_branch {
my ($self) = @_;
- $self->{_home_branch} ||= Koha::Branches->find( $self->homebranch() );
+ $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
return $self->{_home_branch};
}
sub holding_branch {
my ($self) = @_;
- $self->{_holding_branch} ||= Koha::Branches->find( $self->holdingbranch() );
+ $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
return $self->{_holding_branch};
}
--- /dev/null
+package Koha::Libraries;
+
+# Copyright 2015 Koha Development team
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha 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.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+
+use Koha::Library;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Libraries - Koha Library Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+ return 'Branch';
+}
+
+sub object_class {
+ return 'Koha::Library';
+}
+
+1;
--- /dev/null
+package Koha::Library;
+
+# Copyright 2015 Koha Development team
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha 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.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Library - Koha Library Object class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+sub get_categories {
+ my ( $self, $params ) = @_;
+ # TODO This should return Koha::LibraryCategories
+ return $self->{_result}->categorycodes( $params );
+}
+
+sub update_categories {
+ my ( $self, $categories ) = @_;
+ $self->_result->delete_related( 'branchrelations' );
+ $self->add_to_categories( $categories );
+}
+
+sub add_to_categories {
+ my ( $self, $categories ) = @_;
+ for my $category ( @$categories ) {
+ $self->_result->add_to_categorycodes( $category->_result );
+ }
+}
+
+=head3 type
+
+=cut
+
+sub type {
+ return 'Branch';
+}
+
+1;
--- /dev/null
+package Koha::LibraryCategories;
+
+# Copyright 2015 Koha Development team
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha 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.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+
+use Koha::LibraryCategory;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::LibraryCategories - Koha Library Category Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+ return 'Branchcategory';
+}
+
+sub object_class {
+ return 'Koha::LibraryCategory';
+}
+
+1;
--- /dev/null
+package Koha::LibraryCategory;
+
+# Copyright 2015 Koha Development team
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha 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.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::LibraryCategory - Koha Library Category Object class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+sub new {
+ my ( $self, $params ) = @_;
+ $params->{categorycode} = uc( $params->{categorycode} );
+ return $self->SUPER::new( $params );
+}
+
+sub branchcodes{
+ my ( $self, $params ) = @_;
+ # TODO This should return Koha::Libraries
+ return $self->{_result}->branchcodes( $params );
+}
+
+=head3 type
+
+=cut
+
+sub type {
+ return 'Branchcategory';
+}
+
+1;
is( ref($item), 'Koha::Item', "Got Koha::Item" );
my $homebranch = $item->home_branch();
- is( ref($homebranch), 'Koha::Branch', "Got Koha::Branch from home_branch method" );
+ is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
my $holdingbranch = $item->holding_branch();
- is( ref($holdingbranch), 'Koha::Branch', "Got Koha::Branch from holding_branch method" );
+ is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
$schema->storage->txn_rollback;
--- /dev/null
+#!/usr/bin/perl
+
+# Copyright 2015 Koha Development team
+#
+# This file is part of Koha
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 9;
+
+use Koha::Library;
+use Koha::Libraries;
+use Koha::LibraryCategory;
+use Koha::LibraryCategories;
+use Koha::Database;
+
+use t::lib::TestBuilder;
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+my $builder = t::lib::TestBuilder->new;
+my $nb_of_libraries = Koha::Libraries->search->count;
+my $nb_of_categories = Koha::LibraryCategories->search->count;
+my $new_library_1 = Koha::Library->new({
+ branchcode => 'my_bc_1',
+ branchname => 'my_branchname_1',
+ branchnotes => 'my_branchnotes_1',
+})->store;
+my $new_library_2 = Koha::Library->new({
+ branchcode => 'my_bc_2',
+ branchname => 'my_branchname_2',
+ branchnotes => 'my_branchnotes_2',
+})->store;
+my $new_category_1 = Koha::LibraryCategory->new({
+ categorycode => 'my_cc_1',
+ categoryname => 'my_categoryname_1',
+ codedescription => 'my_codedescription_1',
+ categorytype => 'properties',
+} )->store;
+my $new_category_2 = Koha::LibraryCategory->new( {
+ categorycode => 'my_cc_2',
+ categoryname => 'my_categoryname_2',
+ codedescription => 'my_codedescription_2',
+ categorytype => 'searchdomain',
+} )->store;
+my $new_category_3 = Koha::LibraryCategory->new( {
+ categorycode => 'my_cc_3',
+ categoryname => 'my_categoryname_3',
+ codedescription => 'my_codedescription_3',
+ categorytype => 'searchdomain',
+} )->store;
+
+is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
+is( Koha::LibraryCategories->search->count, $nb_of_categories + 3, 'The 3 library categories should have been added' );
+
+$new_library_1->add_to_categories( [$new_category_1] );
+$new_library_2->add_to_categories( [$new_category_2] );
+my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
+is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
+is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 1, '1 library should have been linked to the category 1' );
+
+$retrieved_library_1->update_categories( [ $new_category_2, $new_category_3 ] );
+is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 2, '2 libraries should have been linked to the category 2' );
+
+my $retrieved_category_2 = Koha::LibraryCategories->find( $new_category_2->categorycode );
+is( $retrieved_category_2->branchcodes->count, 2, '2 libraries should have been linked to the category_2' );
+is( $retrieved_category_2->categorycode, uc('my_cc_2'), 'The Koha::LibraryCategory constructor should have upercased the categorycode' );
+
+$retrieved_library_1->delete;
+is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
+
+$retrieved_category_2->delete;
+is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
+
+$schema->storage->txn_rollback;
+1;