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

(-)a/Koha/Patron/Attribute.pm (+19 lines)
Lines 52-57 sub store { Link Here
52
    Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
52
    Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
53
        unless $self->repeatable_ok();
53
        unless $self->repeatable_ok();
54
54
55
    $self->do_trim_value_if_needed();
56
55
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
57
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
56
        unless $self->unique_ok();
58
        unless $self->unique_ok();
57
59
Lines 189-194 sub unique_ok { Link Here
189
    return $ok;
191
    return $ok;
190
}
192
}
191
193
194
=head3 do_trim_value_if_needed
195
196
---
197
198
=cut
199
200
sub do_trim_value_if_needed {
201
202
    my ( $self ) = @_;
203
204
    if( $self->type->trim_value) {
205
        my $value_to_trim = $self->attribute;
206
        $value_to_trim =~ s/^\s+|\s+$//g;
207
        $self->attribute($value_to_trim);
208
    }
209
}
210
192
=head2 Internal methods
211
=head2 Internal methods
193
212
194
=head3 _type
213
=head3 _type
(-)a/Koha/Patron/Attribute/Type.pm (+21 lines)
Lines 46-51 sub store { Link Here
46
46
47
    $self->check_repeatables;
47
    $self->check_repeatables;
48
    $self->check_unique_ids;
48
    $self->check_unique_ids;
49
    $self->check_untrimmed_values;
49
50
50
    return $self->SUPER::store();
51
    return $self->SUPER::store();
51
}
52
}
Lines 114-120 sub check_unique_ids { Link Here
114
    return $self;
115
    return $self;
115
}
116
}
116
117
118
=head3 check_untrimmed_values
117
119
120
=cut
121
122
sub check_untrimmed_values {
123
    my ($self) = @_;
124
125
    return $self unless $self->trim_value;
126
127
    my $count = $self->attributes->search(
128
        {
129
            attribute => { -regexp => '^\s+|\s+$' }
130
        }
131
    )->count;
132
133
        Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty->throw(
134
        property => 'trim_value' )
135
      if $count;
136
137
    return $self;
138
}
118
139
119
=head3 _type
140
=head3 _type
120
141
(-)a/Koha/Schema/Result/BorrowerAttributeType.pm (+10 lines)
Lines 55-60 defines whether one patron/borrower can have multiple values for this custom fie Link Here
55
55
56
defines if this value needs to be unique (1 for yes, 0 for no)
56
defines if this value needs to be unique (1 for yes, 0 for no)
57
57
58
=head2 trim_value
59
60
  data_type: 'tinyint'
61
  default_value: 0
62
  is_nullable: 0
63
64
defines if this value needs to be trimmed of whitespaces (1 for yes, 0 for no)
65
58
=head2 opac_display
66
=head2 opac_display
59
67
60
  data_type: 'tinyint'
68
  data_type: 'tinyint'
Lines 140-145 __PACKAGE__->add_columns( Link Here
140
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
148
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
141
  "unique_id",
149
  "unique_id",
142
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
150
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
151
  "trim_value",
152
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
143
  "opac_display",
153
  "opac_display",
144
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
154
  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
145
  "opac_editable",
155
  "opac_editable",
(-)a/admin/patron-attr-types.pl (+10 lines)
Lines 114-119 sub add_update_attribute_type { Link Here
114
    my $description               = $input->param('description');
114
    my $description               = $input->param('description');
115
    my $repeatable                = $input->param('repeatable') ? 1 : 0;
115
    my $repeatable                = $input->param('repeatable') ? 1 : 0;
116
    my $unique_id                 = $input->param('unique_id') ? 1 : 0;
116
    my $unique_id                 = $input->param('unique_id') ? 1 : 0;
117
    my $trim_value                = $input->param('trim_value') ? 1 : 0;
117
    my $opac_display              = $input->param('opac_display') ? 1 : 0;
118
    my $opac_display              = $input->param('opac_display') ? 1 : 0;
118
    my $opac_editable             = $input->param('opac_editable') ? 1 : 0;
119
    my $opac_editable             = $input->param('opac_editable') ? 1 : 0;
119
    my $staff_searchable          = $input->param('staff_searchable') ? 1 : 0;
120
    my $staff_searchable          = $input->param('staff_searchable') ? 1 : 0;
Lines 149-154 sub add_update_attribute_type { Link Here
149
        {
150
        {
150
            repeatable                => $repeatable,
151
            repeatable                => $repeatable,
151
            unique_id                 => $unique_id,
152
            unique_id                 => $unique_id,
153
            trim_value                => $trim_value,
152
            opac_display              => $opac_display,
154
            opac_display              => $opac_display,
153
            opac_editable             => $opac_editable,
155
            opac_editable             => $opac_editable,
154
            staff_searchable          => $staff_searchable,
156
            staff_searchable          => $staff_searchable,
Lines 236-247 sub edit_attribute_type_form { Link Here
236
        $can_be_set_to_unique = 0 if $@;
238
        $can_be_set_to_unique = 0 if $@;
237
        $attr_type->unique_id(0);
239
        $attr_type->unique_id(0);
238
    }
240
    }
241
    my $can_be_trimmed = 1;
242
    if ( $attr_type->trim_value == 0 ) {
243
        $attr_type->trim_value(1);
244
        eval {$attr_type->check_untrimmed_values};
245
        $can_be_trimmed = 0 if $@;
246
        $attr_type->trim_value(0);
247
    }
239
    $template->param(
248
    $template->param(
240
        attribute_type => $attr_type,
249
        attribute_type => $attr_type,
241
        attribute_type_form => 1,
250
        attribute_type_form => 1,
242
        edit_attribute_type => 1,
251
        edit_attribute_type => 1,
243
        can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
252
        can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
244
        can_be_set_to_unique => $can_be_set_to_unique,
253
        can_be_set_to_unique => $can_be_set_to_unique,
254
        can_be_trimmed => $can_be_trimmed,
245
        confirm_op => 'edit_attribute_type_confirmed',
255
        confirm_op => 'edit_attribute_type_confirmed',
246
        categories => $patron_categories,
256
        categories => $patron_categories,
247
    );
257
    );
(-)a/installer/data/mysql/atomicupdate/bug_32767-trim-attr-feature.pl (+14 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number => "32767",
5
    description => "New feature that trims attributes (if enabled) to avoid duplicates",
6
    up => sub {
7
        my ($args) = @_;
8
        my ($dbh, $out) = @$args{qw(dbh out)};
9
10
        unless ( column_exists( 'borrower_attribute_types', 'trim_value' ) ) {
11
            $dbh->do( "ALTER TABLE `borrower_attribute_types` ADD COLUMN `trim_value` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this value needs to be trimmed of whitespaces (1 for yes, 0 for no)' AFTER unique_id" );
12
        }
13
    },
14
};
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 1166-1171 CREATE TABLE `borrower_attribute_types` ( Link Here
1166
  `description` varchar(255) NOT NULL COMMENT 'description for each custom field',
1166
  `description` varchar(255) NOT NULL COMMENT 'description for each custom field',
1167
  `repeatable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines whether one patron/borrower can have multiple values for this custom field  (1 for yes, 0 for no)',
1167
  `repeatable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines whether one patron/borrower can have multiple values for this custom field  (1 for yes, 0 for no)',
1168
  `unique_id` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this value needs to be unique (1 for yes, 0 for no)',
1168
  `unique_id` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this value needs to be unique (1 for yes, 0 for no)',
1169
  `trim_value` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this value needs to be trimmed of whitespaces (1 for yes, 0 for no)',
1169
  `opac_display` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is visible to patrons on their account in the OPAC (1 for yes, 0 for no)',
1170
  `opac_display` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is visible to patrons on their account in the OPAC (1 for yes, 0 for no)',
1170
  `opac_editable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is editable by patrons on their account in the OPAC (1 for yes, 0 for no)',
1171
  `opac_editable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is editable by patrons on their account in the OPAC (1 for yes, 0 for no)',
1171
  `staff_searchable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is searchable via the patron search in the staff interface (1 for yes, 0 for no)',
1172
  `staff_searchable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'defines if this field is searchable via the patron search in the staff interface (1 for yes, 0 for no)',
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt (+22 lines)
Lines 143-148 Patron attribute types › Administration › Koha Link Here
143
                  cannot be given to a different record.</span>
143
                  cannot be given to a different record.</span>
144
        </li>
144
        </li>
145
145
146
        [% IF attribute_type AND attribute_type.trim_value %]
147
            <li aria-disabled="true">
148
        [% ELSE %]
149
            <li>
150
        [% END %]
151
            <label for="trim_value">Trim identifier: </label>
152
            [% IF attribute_type %]
153
                [% IF NOT can_be_trimmed %]
154
                    <input type="checkbox" id="trim_value" name="trim_value" disabled="disabled" title="At least one patron has attribute with whitespaces at the start or/end of it." />
155
                    <input type="hidden" name="trim_value" value="0" />
156
                    <span class="hint">At least one patron has attribute with whitespaces at the start or/end of it. Please fix that.</span>
157
                [% ELSIF attribute_type.trim_value %]
158
                    <input type="checkbox" id="trim_value" name="trim_value" checked="checked" />
159
                [% ELSE %]
160
                    <input type="checkbox" id="trim_value" name="trim_value" />
161
                [% END %]
162
            [% ELSE %]
163
                <input type="checkbox" id="trim_value" name="trim_value" />
164
            [% END %]
165
            <span class="hint">Check to always trim whitespaces around the identifier to avoid creating duplicates.</span>
166
        </li>
167
146
       <li><label for="opac_display">Display in OPAC: </label>
168
       <li><label for="opac_display">Display in OPAC: </label>
147
          [% IF attribute_type AND attribute_type.opac_display %]
169
          [% IF attribute_type AND attribute_type.opac_display %]
148
            <input type="checkbox" id="opac_display" name="opac_display" checked="checked" />
170
            <input type="checkbox" id="opac_display" name="opac_display" checked="checked" />
(-)a/members/memberentry.pl (+1 lines)
Lines 363-368 if ($op eq 'save' || $op eq 'insert'){ Link Here
363
      for my $attr ( @$extended_patron_attributes ) {
363
      for my $attr ( @$extended_patron_attributes ) {
364
          $attr->{borrowernumber} = $borrowernumber if $borrowernumber;
364
          $attr->{borrowernumber} = $borrowernumber if $borrowernumber;
365
          my $attribute = Koha::Patron::Attribute->new($attr);
365
          my $attribute = Koha::Patron::Attribute->new($attr);
366
          $attribute->do_trim_value_if_needed;
366
          if ( !$attribute->unique_ok ) {
367
          if ( !$attribute->unique_ok ) {
367
              push @errors, "ERROR_extended_unique_id_failed";
368
              push @errors, "ERROR_extended_unique_id_failed";
368
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
369
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
(-)a/opac/opac-memberentry.pl (-1 / +1 lines)
Lines 112-117 my $conflicting_attribute = 0; Link Here
112
112
113
foreach my $attr (@$attributes) {
113
foreach my $attr (@$attributes) {
114
    my $attribute = Koha::Patron::Attribute->new($attr);
114
    my $attribute = Koha::Patron::Attribute->new($attr);
115
    $attribute->do_trim_value_if_needed;
115
    if ( !$attribute->unique_ok ) {
116
    if ( !$attribute->unique_ok ) {
116
        my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
117
        my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
117
        $template->param(
118
        $template->param(
118
- 

Return to bug 32767