View | Details | Raw Unified | Return to bug 28031
Collapse All | Expand All

(-)a/Koha/Patron/Attribute.pm (-20 / +22 lines)
Lines 51-58 sub store { Link Here
51
    Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $self->code )
51
    Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $self->code )
52
        unless $type;
52
        unless $type;
53
53
54
    $self->check_repeatable($type);
54
    Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
55
    $self->check_unique_id($type);
55
        unless $self->repeatable_ok($type);
56
57
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
58
        unless $self->unique_ok($type);
56
59
57
    return $self->SUPER::store();
60
    return $self->SUPER::store();
58
}
61
}
Lines 133-152 sub to_api_mapping { Link Here
133
    };
136
    };
134
}
137
}
135
138
136
=head2 Internal methods
139
=head3 repeatable_ok
137
138
=head3 check_repeatable
139
140
140
check_repeatable checks if the attribute type is repeatable and throws and exception
141
Checks if the attribute type is repeatable and returns a boolean representing 
141
if the attribute type isn't repeatable and there's already an attribute with the same
142
whether storing the current object state would break the repeatable constraint.
142
code for the given patron.
143
143
144
=cut
144
=cut
145
145
146
sub check_repeatable {
146
sub repeatable_ok {
147
147
148
    my ( $self, $type ) = @_;
148
    my ( $self, $type ) = @_;
149
149
150
    my $ok = 1;
150
    if ( !$type->repeatable ) {
151
    if ( !$type->repeatable ) {
151
        my $params = {
152
        my $params = {
152
            borrowernumber => $self->borrowernumber,
153
            borrowernumber => $self->borrowernumber,
Lines 156-180 sub check_repeatable { Link Here
156
        $params->{id} = { '!=' => $self->id }
157
        $params->{id} = { '!=' => $self->id }
157
            if $self->in_storage;
158
            if $self->in_storage;
158
159
159
        Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
160
        $ok = 0 if Koha::Patron::Attributes->search($params)->count > 0;
160
            if Koha::Patron::Attributes->search($params)->count > 0;
161
    }
161
    }
162
162
163
    return $self;
163
    return $ok;
164
}
164
}
165
165
166
=head3 check_unique_id
166
=head3 unique_ok
167
167
168
check_unique_id checks if the attribute type is marked as unique id and throws and exception
168
Checks if the attribute type is marked as unique and returns a boolean representing
169
if the attribute type is a unique id and there's already an attribute with the same
169
whether storing the current object state would break the unique constraint.
170
code and value on the database.
171
170
172
=cut
171
=cut
173
172
174
sub check_unique_id {
173
sub unique_ok {
175
174
176
    my ( $self, $type ) = @_;
175
    my ( $self, $type ) = @_;
177
176
177
    my $ok = 1;
178
    if ( $type->unique_id ) {
178
    if ( $type->unique_id ) {
179
        my $params = { code => $self->code, attribute => $self->attribute };
179
        my $params = { code => $self->code, attribute => $self->attribute };
180
180
Lines 184-196 sub check_unique_id { Link Here
184
        my $unique_count = Koha::Patron::Attributes
184
        my $unique_count = Koha::Patron::Attributes
185
            ->search( $params )
185
            ->search( $params )
186
            ->count;
186
            ->count;
187
        Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
187
188
            if $unique_count > 0;
188
        $ok = 0 if $unique_count > 0;
189
    }
189
    }
190
190
191
    return $self;
191
    return $ok;
192
}
192
}
193
193
194
=head2 Internal methods
195
194
=head3 _type
196
=head3 _type
195
197
196
=cut
198
=cut
(-)a/members/memberentry.pl (-2 / +1 lines)
Lines 411-418 if ($op eq 'save' || $op eq 'insert'){ Link Here
411
      for my $attr ( @$extended_patron_attributes ) {
411
      for my $attr ( @$extended_patron_attributes ) {
412
          $attr->{borrowernumber} = $borrowernumber if $borrowernumber;
412
          $attr->{borrowernumber} = $borrowernumber if $borrowernumber;
413
          my $attribute = Koha::Patron::Attribute->new($attr);
413
          my $attribute = Koha::Patron::Attribute->new($attr);
414
          eval {$attribute->check_unique_id};
414
          if ( !$attribute->unique_ok ) {
415
          if ( $@ ) {
416
              push @errors, "ERROR_extended_unique_id_failed";
415
              push @errors, "ERROR_extended_unique_id_failed";
417
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
416
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
418
              $template->param(
417
              $template->param(
(-)a/opac/opac-memberentry.pl (-3 / +1 lines)
Lines 107-114 my $conflicting_attribute = 0; Link Here
107
107
108
foreach my $attr (@$attributes) {
108
foreach my $attr (@$attributes) {
109
    my $attribute = Koha::Patron::Attribute->new($attr);
109
    my $attribute = Koha::Patron::Attribute->new($attr);
110
    eval {$attribute->check_unique_id};
110
    if ( !$$attribute->unique_ok ) {
111
    if ( $@ ) {
112
        my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
111
        my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
113
        $template->param(
112
        $template->param(
114
            extended_unique_id_failed_code => $attr->{code},
113
            extended_unique_id_failed_code => $attr->{code},
115
- 

Return to bug 28031