From 6681da37425cfadea68bd3ade31e54db745a7d96 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 8 Jan 2016 09:54:45 +0000
Subject: [PATCH] Bug 15163: Wip - stuck

---
 C4/Members/Attributes.pm                           | 35 +++++++++++-
 .../prog/en/modules/members/memberentrygen.tt      | 11 +++-
 members/memberentry.pl                             |  8 ++-
 t/db_dependent/Members_Attributes.t                | 65 +++++++++++++---------
 4 files changed, 86 insertions(+), 33 deletions(-)

diff --git a/C4/Members/Attributes.pm b/C4/Members/Attributes.pm
index b25a813..43828dc 100644
--- a/C4/Members/Attributes.pm
+++ b/C4/Members/Attributes.pm
@@ -71,7 +71,6 @@ marked for OPAC display are returned.
 sub GetBorrowerAttributes {
     my $borrowernumber = shift;
     my $opac_only = @_ ? shift : 0;
-    my $branch_limit = @_ ? shift : 0;
 
     my $dbh = C4::Context->dbh();
     my $query = "SELECT code, description, attribute, lib, password, display_checkout, category_code, class
@@ -220,8 +219,8 @@ sub SetBorrowerAttributes {
     my $attr_list = shift;
 
     my $dbh = C4::Context->dbh;
-    my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
-    $delsth->execute($borrowernumber);
+
+    DeleteBorrowerAttributes( $borrowernumber );
 
     my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
                              VALUES (?, ?, ?, ?)");
@@ -236,6 +235,36 @@ sub SetBorrowerAttributes {
     return 1; # borrower attributes successfully set
 }
 
+=head2 DeleteBorrowerAttributes
+
+  DeleteBorrowerAttributes($borrowernumber);
+
+Delete borrower attributes for the patron identified by C<$borrowernumber>.
+
+=cut
+
+sub DeleteBorrowerAttributes {
+    my $borrowernumber = shift;
+    my $no_branch_limit = @_ ? shift : 0;
+    my $branch_limit = $no_branch_limit
+        ? 0
+        : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
+
+    my $dbh = C4::Context->dbh;
+    my $query = q{
+        DELETE FROM borrower_attributes
+        };
+    $query .= q{
+        LEFT JOIN borrower_attribute_types_branches ON bat_code = code
+        WHERE b_branchcode = ? OR b_branchcode IS NULL
+    } if $branch_limit;
+    $query .= q{
+        WHERE borrowernumber = ?
+    };
+
+    $dbh->do( $query, undef, $branch_limit ? $branch_limit : (), $borrowernumber );
+}
+
 =head2 DeleteBorrowerAttribute
 
   DeleteBorrowerAttribute($borrowernumber, $attribute);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
index e646890..37490c6 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
@@ -1022,8 +1022,12 @@
         [% END %]
         <ol class="attributes_table">
             [% FOREACH patron_attribute IN pa_loo.items %]
-                <li data-category_code="[% patron_attribute.category_code %]">
-                    <label for="[% patron_attribute.form_id %]">[% patron_attribute.description %]: </label>
+                [% IF patron_attribute.limited_to_another_branch %]
+                    <input type="hidden" id="[% patron_attribute.form_id %]_code" name="[% patron_attribute.form_id %]_code" value="[% patron_attribute.code |html %]" />
+                    <input type="hidden" id="[% patron_attribute.form_id %]" name="[% patron_attribute.form_id %]" value="[% patron_attribute.value |html %]" />
+                [% ELSE %]
+                    <li data-category_code="[% patron_attribute.category_code %]">
+                        <label for="[% patron_attribute.form_id %]">[% patron_attribute.description %]: </label>
                         <input type="hidden" id="[% patron_attribute.form_id %]_code" name="[% patron_attribute.form_id %]_code" value="[% patron_attribute.code |html %]" />
                         [% IF ( patron_attribute.use_dropdown ) %]
                             <select id="[% patron_attribute.form_id %]" name="[% patron_attribute.form_id %]">
@@ -1051,7 +1055,8 @@
                         [% IF ( patron_attribute.repeatable ) %]
                         <a href="#" class="clone-field" onclick="clone_entry(this); return false;">New</a>
                         [% END %]
-                </li>
+                    </li>
+                [% END %]
             [% END %]
         </ol>
         [% IF pa_loo.class %]</fieldset>[% END %]
diff --git a/members/memberentry.pl b/members/memberentry.pl
index ae54383..2a7726e 100755
--- a/members/memberentry.pl
+++ b/members/memberentry.pl
@@ -745,7 +745,7 @@ sub patron_attributes_form {
     my $template = shift;
     my $borrowernumber = shift;
 
-    my @types = C4::Members::AttributeTypes::GetAttributeTypes();
+    my @types = C4::Members::AttributeTypes::GetAttributeTypes(1, 1);
     if (scalar(@types) == 0) {
         $template->param(no_patron_attribute_types => 1);
         return;
@@ -763,8 +763,13 @@ sub patron_attributes_form {
     my @attribute_loop = ();
     my $i = 0;
     my %items_by_class;
+    my $user_branch = C4::Context->userenv->{"branch"};
     foreach my $type_code (map { $_->{code} } @types) {
         my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
+        my $limited_to_another_branch = 0;
+        if ( my $branch_limitations = $attr_type->branches ) {
+            $limited_to_another_branch = not grep { /^$user_branch$/ } @$branch_limitations;
+        }
         my $entry = {
             class             => $attr_type->class(),
             code              => $attr_type->code(),
@@ -774,6 +779,7 @@ sub patron_attributes_form {
             category          => $attr_type->authorised_value_category(),
             category_code     => $attr_type->category_code(),
             password          => '',
+            limited_to_another_branch => $limited_to_another_branch,
         };
         if (exists $attr_hash{$attr_type->code()}) {
             foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
diff --git a/t/db_dependent/Members_Attributes.t b/t/db_dependent/Members_Attributes.t
index 71e8394..5d56d41 100755
--- a/t/db_dependent/Members_Attributes.t
+++ b/t/db_dependent/Members_Attributes.t
@@ -23,8 +23,9 @@ use C4::Context;
 use C4::Members;
 use C4::Members::AttributeTypes;
 use Koha::Database;
+use t::lib::TestBuilder;
 
-use Test::More tests => 60;
+use Test::More;# tests => 60;
 
 use t::lib::TestBuilder;
 
@@ -44,23 +45,24 @@ $dbh->do(q|DELETE FROM borrower_attribute_types|);
 my $library = $builder->build({
     source => 'Branch',
 });
-my $borrowernumber = AddMember(
-    firstname =>  'my firstname',
-    surname => 'my surname',
-    categorycode => 'S',
-    branchcode => $library->{branchcode},
-);
 
+my $patron = $builder->build(
+    {   source => 'Borrower',
+        value  => {
+            firstname    => 'my firstname',
+            surname      => 'my surname',
+            categorycode => 'S',
+            branchcode => $library->{branchcode},
+        }
+    }
+);
+C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0);
+my $borrowernumber = $patron->{borrowernumber};
 
 my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
 $attribute_type1->unique_id(1);
-my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
-$attribute_type2->opac_display(1);
-$attribute_type2->staff_searchable(1);
-
 my $attribute_types = C4::Members::Attributes::GetAttributes();
 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' );
-
 $attribute_type1->store();
 $attribute_types = C4::Members::Attributes::GetAttributes();
 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' );
@@ -68,6 +70,9 @@ is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the
 $attribute_types = C4::Members::Attributes::GetAttributes(1);
 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
 
+my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
+$attribute_type2->opac_display(1);
+$attribute_type2->staff_searchable(1);
 $attribute_type2->store();
 $attribute_types = C4::Members::Attributes::GetAttributes();
 is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' );
@@ -75,6 +80,10 @@ is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the
 $attribute_types = C4::Members::Attributes::GetAttributes(1);
 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
 
+my $new_library = $builder->build( { source => 'Branch' } );
+my $attribute_type_limited = C4::Members::AttributeTypes->new('my code3', 'my description3');
+$attribute_type_limited->branches([ $new_library->{branchcode} ]);
+$attribute_type_limited->store;
 
 my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
@@ -91,6 +100,10 @@ my $attributes = [
         value => 'my attribute2',
         code => $attribute_type2->code(),
         password => 'my password2',
+    },
+    {
+        value => 'my attribute limited',
+        code => $attribute_type_limited->code(),
     }
 ];
 
@@ -107,7 +120,7 @@ is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
+is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
 is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
 is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
@@ -116,14 +129,12 @@ is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAtt
 is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
 is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
 is( $borrower_attributes->[1]->{password}, $attributes->[1]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
+$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
+is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
 
-$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 1);
-is( @$borrower_attributes, 1, 'GetBorrowerAttributes returns the correct number of borrower attributes with the filter opac_only' );
-is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'GetBorrowerAttributes returns the correct code' );
-is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'GetBorrowerAttributes returns the correct description' );
-is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'GetBorrowerAttributes returns the correct value' );
-is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'GetBorrowerAttributes returns the correct password' );
-
+# TODO This is not implemented yet
+#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
+#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
 
 my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
 is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
@@ -147,7 +158,7 @@ my $attribute = {
 };
 C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 2, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
+is( @$borrower_attributes, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
 is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
 is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
@@ -185,17 +196,17 @@ for my $attr( split(' ', $attributes->[1]->{value}) ) {
 
 C4::Members::Attributes::DeleteBorrowerAttribute();
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without arguments deletes nothing' );
+is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
+is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
 C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
+is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
 
 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
+is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
@@ -203,4 +214,6 @@ is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'Delete
 
 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-is( @$borrower_attributes, 0, 'DeleteBorrowerAttribute deletes a borrower attribute' );
+is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
+
+done_testing;
-- 
2.1.0