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

(-)a/Koha/AuthorisedValue.pm (+244 lines)
Line 0 Link Here
1
package Koha::AuthorisedValue;
2
3
use Modern::Perl;
4
5
use base qw(Class::Accessor);
6
7
use C4::Context;
8
9
sub new {
10
    my ( $class, $args ) = @_;
11
    my $av = $args->{av};
12
13
    unless ( $av ) {
14
        $av->{id} = $args->{id};
15
        $av->{category} = $args->{category};
16
        $av->{authorised_value} = $args->{authorised_value};
17
        $av->{lib} = $args->{lib};
18
        $av->{lib_opac} = $args->{lib_opac};
19
        $av->{imageurl} = $args->{imageurl};
20
        $av->{branches_limitations} = $args->{branches_limitations};
21
    }
22
23
    my $self = $class->SUPER::new( $av );
24
25
    bless $self, $class;
26
    return $self;
27
}
28
29
sub fetch {
30
    my ( $self ) = @_;
31
    my $dbh = C4::Context->dbh;
32
    my $query = q{
33
        SELECT *
34
        FROM authorised_values
35
    };
36
    if ( $self->{id} ) {
37
        $query .= q{ WHERE id = ? }
38
    } else {
39
        $query .= q{ WHERE category = ?
40
            AND authorised_value = ?
41
        };
42
    }
43
    my $data = $dbh->selectrow_hashref(
44
        $query, {},
45
        ( $self->{id} ? $self->{id} : ( $self->{category}, $self->{authorised_value} ) )
46
    );
47
    die "This authorised value does not exist"
48
        . ( $self->{id} ? "id=".$self->{id} : "category=$self->{category} and authorised_value=$self->{authorised_value}" )
49
            unless $data;
50
    $self->{id} = $data->{id};
51
    $self->{category} = $data->{category};
52
    $self->{authorised_value} = $data->{authorised_value};
53
    $self->{lib} = $data->{lib};
54
    $self->{lib_opac} = $data->{lib_opac};
55
    $self->{imageurl} = $data->{imageurl};
56
    return $self;
57
}
58
59
sub update {
60
    my ( $self ) = @_;
61
    my $dbh = C4::Context->dbh;
62
    local $dbh->{RaiseError} = 1;
63
64
    die "There is no id defined for this authorised value. I cannot update it" unless $self->{id};
65
66
    my $query = q{
67
        UPDATE authorised_values
68
        SET category = ?,
69
            authorised_value = ?,
70
            lib = ?,
71
            lib_opac = ?,
72
            imageurl = ?
73
        WHERE id = ?
74
    };
75
    $dbh->do( $query, {}, $self->{category}, $self->{authorised_value}, $self->{lib}, $self->{lib_opac}, $self->{imageurl}, $self->{id} );
76
77
    $query = q{
78
        DELETE FROM authorised_values_branches
79
        WHERE av_id = ?
80
    };
81
    $dbh->do( $query, {}, $self->{id} );
82
    $query = q{
83
        INSERT INTO authorised_values_branches
84
        ( av_id, branchcode ) VALUES ( ?, ? )
85
    };
86
    my $sth = $dbh->prepare( $query );
87
    for my $branch ( @{ $self->{branches_limitations} } ) {
88
        next unless $branch->{branchcode};
89
        $sth->execute( $self->{id}, $branch->{branchcode} );
90
    }
91
}
92
93
sub delete {
94
    my ( $self ) = @_;
95
    my $dbh = C4::Context->dbh;
96
    local $dbh->{RaiseError} = 1;
97
    my $query = q{
98
        DELETE FROM authorised_values
99
        WHERE id = ?
100
    };
101
    $dbh->do( $query, {}, $self->{id} );
102
}
103
104
sub insert {
105
    my ( $self ) = @_;
106
    my $dbh = C4::Context->dbh;
107
    local $dbh->{RaiseError} = 1;
108
    my $query = q{
109
        INSERT INTO authorised_values
110
        ( category, authorised_value, lib, lib_opac, imageurl )
111
        VALUES (?, ?, ?, ?, ?)
112
    };
113
    $dbh->do( $query, {}, $self->{category}, $self->{authorised_value}, $self->{lib}, $self->{lib_opac}, $self->{imageurl} );
114
    $self->{id} = $dbh->{'mysql_insertid'};
115
    $query = q{
116
        INSERT INTO authorised_values_branches
117
        ( av_id, branchcode ) VALUES ( ?, ? )
118
    };
119
    my $sth = $dbh->prepare( $query );
120
    for my $branch ( @{ $self->{branches_limitations} } ) {
121
        next unless $branch->{branchcode};
122
        $sth->execute( $self->{id}, $branch->{branchcode} );
123
    }
124
}
125
126
sub branches_limitations {
127
    my ( $self, $limitations ) = @_;
128
    my $dbh = C4::Context->dbh;
129
    unless ( defined $limitations ) {
130
        my $sth = $dbh->prepare( q{
131
            SELECT b.branchcode, b.branchname
132
            FROM authorised_values_branches AS avb,
133
                 branches AS b
134
            WHERE avb.branchcode = b.branchcode
135
                AND avb.av_id = ?
136
        } );
137
        $sth->execute( $self->{id} );
138
        my @limitations;
139
        while ( my $r = $sth->fetchrow_hashref ) {
140
            push @limitations, {
141
                branchcode => $r->{branchcode},
142
                branchname => $r->{branchname},
143
            }
144
        }
145
        $self->{branches_limitations} = \@limitations;
146
        return $self->{branches_limitations};
147
    } else {
148
        die "setter not implemented yet";
149
    }
150
}
151
152
1;
153
154
__END__
155
156
=head1 NAME
157
158
Koha::AuthorisedValue
159
160
=head1 SYNOPSIS
161
162
    use Koha::AuthorisedValue;
163
    my $av1 = Koha::AuthorisedValue->new({id => $id});
164
    my $av2 = Koha::AuthorisedValue->new({av => $av});
165
    my $av3 = Koha::AuthorisedValue->new({category => $category, authorised_value => $authorised_value});
166
    $av1->fetch;
167
    $av2->delete;
168
169
=head1 DESCRIPTION
170
171
Class for managing an authorised value into Koha.
172
173
=head1 METHODS
174
175
=head2 new
176
177
Create a new Koha::AuthorisedValue object. This method can be called using several ways.
178
No sql query is done in this method.
179
180
=over 4
181
182
=item B<av>
183
184
    The caller already has all information about the authorised value.
185
    It does want to retrieve them from the database.
186
    The new object is created using these values.
187
188
=item B<id>
189
190
    The caller just knows the id of the authorised value.
191
192
=item B<category and authorised_value>
193
194
    The caller knows the category and the value of the authorised value.
195
196
=back
197
198
=head2 fetch
199
200
The information will be retrieved from the database.
201
202
=head2 update
203
204
If the AuthorisedValue object has been modified and the values have to be modified into the database, call this method.
205
206
=head2 delete
207
208
Remove a the record in the database using the id the object.
209
210
=head2 insert
211
212
Insert a new AuthorisedValue object into the database.
213
214
=head2 branches_limitations
215
216
In the interest of being pleasant with the performance, the branches limitations is not retrieve with the fetch method.
217
If they are required, you should call this method.
218
219
=head1 AUTHOR
220
221
Jonathan Druart <jonathan.druart at biblibre.com>
222
223
=head1 COPYRIGHT
224
225
Copyright 2013 BibLibre
226
227
=head1 LICENSE
228
229
This file is part of Koha.
230
231
Koha is free software; you can redistribute it and/or modify it under the
232
terms of the GNU General Public License as published by the Free Software
233
Foundation; either version 2 of the License, or (at your option) any later
234
version.
235
236
Koha is distributed in the hope that it will be useful, but WITHOUT ANY
237
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
238
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
239
240
You should have received a copy of the GNU General Public License along
241
with Koha; if not, write to the Free Software Foundation, Inc.,
242
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
243
244
=cut
(-)a/Koha/AuthorisedValues.pm (+160 lines)
Line 0 Link Here
1
package Koha::AuthorisedValues;
2
3
use Modern::Perl;
4
use List::MoreUtils qw( any uniq );
5
6
use Koha::AuthorisedValue;
7
8
use base qw(Class::Accessor);
9
10
use C4::Context;
11
12
__PACKAGE__->mk_accessors(qw( authorised_values ));
13
14
sub new {
15
    my ( $class, $args ) = @_;
16
17
    my $category = $args->{category};
18
    my $value = $args->{value};
19
    my @authorised_values;
20
21
    my $dbh = C4::Context->dbh;
22
    my $query = q{
23
        SELECT *
24
        FROM authorised_values
25
    };
26
    $query .= " WHERE category = ?"
27
        if defined $category;
28
    $query .= " AND authorised_value = ?"
29
        if defined $value;
30
    my $sth = $dbh->prepare( $query );
31
    $sth->execute(
32
        defined $category ? $category : (),
33
        defined $value ? $value : ()
34
    );
35
36
    while ( my $av = $sth->fetchrow_hashref ) {
37
        push @authorised_values,
38
            Koha::AuthorisedValue->new({ av => $av });
39
    }
40
41
    my $self = $class->SUPER::new( { authorised_values => \@authorised_values } );
42
43
    bless $self, $class;
44
    return $self;
45
}
46
47
sub all {
48
    my ( $self ) = @_;
49
    return wantarray
50
        ? @{ $self->authorised_values }
51
        : $self->authorised_values;
52
}
53
54
sub select {
55
    my ( $self, $select_value ) = @_;
56
    my @avs = $self->all;
57
    for my $av ( @avs ) {
58
        if ( $av->{authorised_value} eq $select_value ) {
59
            $av->{selected} = 1;
60
        }
61
    }
62
    $self->{authorised_values} = \@avs;
63
}
64
65
sub filter {
66
    my ( $self, $filters ) = @_;
67
    my @avs = $self->all;
68
    while( my ( $key, $value ) = each %$filters ) {
69
        @avs = map {($_->{$key} and $_->{$key} eq $value) ? $_ : ()} @avs;
70
    }
71
    return \@avs;
72
}
73
74
sub categories {
75
    my ( $self ) = @_;
76
    my @avs = $self->all;
77
    my @categories = uniq map {
78
        $_->{category}
79
    } @avs;
80
81
    return wantarray
82
        ? @categories
83
        : \@categories;
84
}
85
86
1;
87
88
__END__
89
90
=head1 NAME
91
92
Koha::AuthorisedValues
93
94
=head1 SYNOPSIS
95
96
    use Koha::AuthorisedValues;
97
    my $avs = Koha::AuthorisedValues->new;
98
    my $categories = $avs->categories;
99
    my $av_for_category = $avs->filter( { category => $category } );
100
    $avs->select('value_to_select');
101
102
=head1 DESCRIPTION
103
104
Class for managing authorised values into Koha.
105
106
=head1 METHODS
107
108
=head2 new
109
110
Create an array of Koha::AuthorisedValue object.
111
2 optional parameters: authorised_value and category, if at least one of them
112
is specified, the constructor will only build matched authorised values.
113
114
=head2 all
115
116
It will get back all constructed objects.
117
118
=head2 select
119
120
Select an authorised value given it value.
121
Set the 'selected' key with 1.
122
123
=head2 filter
124
125
Return a list of authorised values matching the filters.
126
filters is a arrayref of hashref, i.e.:
127
  [{category => $category, selected => 1}]
128
129
=head2 categories
130
131
Return a list of categories exist in the known authorised values.
132
133
=head1 AUTHOR
134
135
Jonathan Druart <jonathan.druart at biblibre.com>
136
137
=head1 COPYRIGHT
138
139
Copyright 2013 BibLibre
140
141
=head1 LICENSE
142
143
Copyright 2013 BibLibre
144
145
This file is part of Koha.
146
147
Koha is free software; you can redistribute it and/or modify it under the
148
terms of the GNU General Public License as published by the Free Software
149
Foundation; either version 2 of the License, or (at your option) any later
150
version.
151
152
Koha is distributed in the hope that it will be useful, but WITHOUT ANY
153
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
154
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
155
156
You should have received a copy of the GNU General Public License along
157
with Koha; if not, write to the Free Software Foundation, Inc.,
158
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
159
160
=cut
(-)a/admin/authorised_values.pl (-202 / +122 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Copyright 2000-2002 Katipo Communications
3
# Copyright 2000-2002 Katipo Communications
4
# Copyright 2013 BibLibre
4
#
5
#
5
# This file is part of Koha.
6
# This file is part of Koha.
6
#
7
#
Lines 17-24 Link Here
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
20
use strict;
21
use Modern::Perl;
21
use warnings;
22
22
23
use CGI;
23
use CGI;
24
use C4::Auth;
24
use C4::Auth;
Lines 26-60 use C4::Branch; Link Here
26
use C4::Context;
26
use C4::Context;
27
use C4::Koha;
27
use C4::Koha;
28
use C4::Output;
28
use C4::Output;
29
use Koha::AuthorisedValue;
30
use Koha::AuthorisedValues;
29
31
30
32
31
sub AuthorizedValuesForCategory {
32
    my ($searchstring) = shift or return;
33
    my $dbh = C4::Context->dbh;
34
    $searchstring=~ s/\'/\\\'/g;
35
    my @data=split(' ',$searchstring);
36
    my $sth=$dbh->prepare('
37
          SELECT  id, category, authorised_value, lib, lib_opac, imageurl
38
            FROM  authorised_values
39
           WHERE  (category = ?)
40
        ORDER BY  category, authorised_value
41
    ');
42
    $sth->execute("$data[0]");
43
    return $sth->fetchall_arrayref({});
44
}
45
46
my $input = new CGI;
33
my $input = new CGI;
47
my $id          = $input->param('id');
34
my $id          = $input->param('id');
48
my $op          = $input->param('op')     || '';
35
my $op          = $input->param('op') || 'list';
49
our $offset      = $input->param('offset') || 0;
36
my $searchfield = $input->param('searchfield');
50
our $searchfield = $input->param('searchfield');
51
$searchfield = '' unless defined $searchfield;
37
$searchfield = '' unless defined $searchfield;
52
$searchfield =~ s/\,//g;
38
$searchfield =~ s/\,//g;
53
our $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
39
my @messages;
54
our $dbh = C4::Context->dbh;
55
40
56
our ($template, $borrowernumber, $cookie)= get_template_and_user({
41
our ($template, $borrowernumber, $cookie)= get_template_and_user({
57
    template_name => "admin/authorised_values.tmpl",
42
    template_name => "admin/authorised_values.tt",
58
    authnotrequired => 0,
43
    authnotrequired => 0,
59
    flagsrequired => {parameters => 'parameters_remaining_permissions'},
44
    flagsrequired => {parameters => 'parameters_remaining_permissions'},
60
    query => $input,
45
    query => $input,
Lines 62-92 our ($template, $borrowernumber, $cookie)= get_template_and_user({ Link Here
62
    debug => 1,
47
    debug => 1,
63
});
48
});
64
49
65
$template->param(  script_name => $script_name,
66
                 ($op||'else') => 1 );
67
################## ADD_FORM ##################################
50
################## ADD_FORM ##################################
68
# called by default. Used to create form to add or  modify a record
51
# called by default. Used to create form to add or  modify a record
69
if ($op eq 'add_form') {
52
if ($op eq 'add_form') {
70
	my $data;
53
    my ( $selected_branches, $category, $av );
71
    my @selected_branches;
54
    if ($id) {
72
	if ($id) {
55
        $av = Koha::AuthorisedValue->new( { id => $id } )->fetch;
73
		my $sth=$dbh->prepare("select id, category, authorised_value, lib, lib_opac, imageurl from authorised_values where id=?");
56
        $selected_branches = $av->branches_limitations;
74
		$sth->execute($id);
57
    } else {
75
		$data=$sth->fetchrow_hashref;
58
        $category = $input->param('category');
76
        $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM authorised_values_branches AS avb, branches AS b WHERE avb.branchcode = b.branchcode AND avb.av_id = ?;");
59
    }
77
        $sth->execute( $id );
78
        while ( my $branch = $sth->fetchrow_hashref ) {
79
            push @selected_branches, $branch;
80
        }
81
	} else {
82
		$data->{'category'} = $input->param('category');
83
	}
84
60
85
    my $branches = GetBranches;
61
    my $branches = GetBranches;
86
    my @branches_loop;
62
    my @branches_loop;
87
63
88
    foreach my $branch (sort keys %$branches) {
64
    foreach my $branch (sort keys %$branches) {
89
        my $selected = ( grep {$_->{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
65
        my $selected = ( grep {$_->{branchcode} eq $branch} @$selected_branches ) ? 1 : 0;
90
        push @branches_loop, {
66
        push @branches_loop, {
91
            branchcode => $branches->{$branch}{branchcode},
67
            branchcode => $branches->{$branch}{branchcode},
92
            branchname => $branches->{$branch}{branchname},
68
            branchname => $branches->{$branch}{branchname},
Lines 96-247 if ($op eq 'add_form') { Link Here
96
72
97
	if ($id) {
73
	if ($id) {
98
		$template->param(action_modify => 1);
74
		$template->param(action_modify => 1);
99
		$template->param('heading_modify_authorized_value_p' => 1);
75
   } elsif ( ! $category ) {
100
	} elsif ( ! $data->{'category'} ) {
101
		$template->param(action_add_category => 1);
76
		$template->param(action_add_category => 1);
102
		$template->param('heading_add_new_category_p' => 1);
103
	} else {
77
	} else {
104
		$template->param(action_add_value => 1);
78
		$template->param(action_add_value => 1);
105
		$template->param('heading_add_authorized_value_p' => 1);
106
	}
79
	}
107
	$template->param('use_heading_flags_p' => 1);
80
108
	$template->param( category        => $data->{'category'},
81
    $template->param(
109
                         authorised_value => $data->{'authorised_value'},
82
        category => ( $av ? $av->{category} : $category ),
110
                         lib              => $data->{'lib'},
83
        authorised_value => $av->{authorised_value},
111
                         lib_opac         => $data->{'lib_opac'},
84
        lib              => $av->{lib},
112
                         id               => $data->{'id'},
85
        lib_opac         => $av->{lib_opac},
113
                         imagesets        => C4::Koha::getImageSets( checked => $data->{'imageurl'} ),
86
        id               => $av->{id},
114
                         offset           => $offset,
87
        imagesets        => C4::Koha::getImageSets( checked => $av->{imageurl} ),
115
                         branches_loop    => \@branches_loop,
88
        branches_loop    => \@branches_loop,
116
                     );
89
     );
117
                          
90
118
################## ADD_VALIDATE ##################################
91
} elsif ($op eq 'add') {
119
# called by add_form, used to insert/modify data in DB
120
} elsif ($op eq 'add_validate') {
121
    my $new_authorised_value = $input->param('authorised_value');
92
    my $new_authorised_value = $input->param('authorised_value');
122
    my $new_category = $input->param('category');
93
    my $new_category = $input->param('category');
123
    my $imageurl     = $input->param( 'imageurl' ) || '';
94
    my $imageurl     = $input->param( 'imageurl' ) || '';
124
	$imageurl = '' if $imageurl =~ /removeImage/;
95
    $imageurl = '' if $imageurl =~ /removeImage/;
125
    my $duplicate_entry = 0;
96
    my $duplicate_entry = 0;
126
    my @branches = $input->param('branches');
97
    my @branches = $input->param('branches');
127
98
128
    if ( $id ) { # Update
99
    if ( $id ) { # Update
129
        my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id = ? ");
100
        my $av = Koha::AuthorisedValue->new( { id => $id } );
130
        $sth->execute($id);
101
131
        my ($category, $authorised_value) = $sth->fetchrow_array();
102
        # TODO in updatedatabase and kohastructure
132
        if ( $authorised_value ne $new_authorised_value ) {
103
        # Add a uniq key on auv.category and av.authorised_values
133
            my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
104
        #if ( $av->{authorised_value} ne $new_authorised_value ) {
134
                "WHERE category = ? AND authorised_value = ? and id <> ? ");
105
        #    my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
135
            $sth->execute($new_category, $new_authorised_value, $id);
106
        #        "WHERE category = ? AND authorised_value = ? and id <> ? ");
136
            ($duplicate_entry) = $sth->fetchrow_array();
107
        #    $sth->execute($new_category, $new_authorised_value, $id);
137
        }
108
        #    ($duplicate_entry) = $sth->fetchrow_array();
138
        unless ( $duplicate_entry ) {
109
        #}
139
            my $sth=$dbh->prepare( 'UPDATE authorised_values
110
        #unless ( $duplicate_entry ) {
140
                                      SET category         = ?,
111
            $av->{lib} = $input->param('lib') || undef;
141
                                          authorised_value = ?,
112
            $av->{lib_opac} = $input->param('lib_opac') || undef;
142
                                          lib              = ?,
113
            $av->{category} = $new_category;
143
                                          lib_opac         = ?,
114
            $av->{authorised_value} = $new_authorised_value;
144
                                          imageurl         = ?
115
            $av->{imageurl} = $imageurl;
145
                                      WHERE id=?' );
116
            $av->{id} = $id;
146
            my $lib = $input->param('lib');
117
            $av->{branches_limitations} = [
147
            my $lib_opac = $input->param('lib_opac');
118
                map { {branchcode => $_, branchname => undef} } @branches
148
            undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
119
            ];
149
            undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
120
            if ( $@ ) {
150
            $sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id);
121
                push @messages, {type => 'error', code => 'error_on_update' };
151
            if ( @branches ) {
122
            } else {
152
                $sth = $dbh->prepare("DELETE FROM authorised_values_branches WHERE av_id = ?");
123
                push @messages, { type => 'message', code => 'success_on_update' };
153
                $sth->execute( $id );
154
                $sth = $dbh->prepare(
155
                    "INSERT INTO authorised_values_branches
156
                                ( av_id, branchcode )
157
                                VALUES ( ?, ? )"
158
                );
159
                for my $branchcode ( @branches ) {
160
                    next if not $branchcode;
161
                    $sth->execute($id, $branchcode);
162
                }
163
            }
124
            }
164
            $sth->finish;
125
            $av->update;
165
            print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
126
            $template->param( update_success => 1 );
166
            exit;
127
        #}
167
        }
168
    }
128
    }
169
    else { # Insert
129
    else { # Insert
170
        my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
130
        my $av = Koha::AuthorisedValue->new( {
171
            "WHERE category = ? AND authorised_value = ? ");
131
            category => $new_category,
172
        $sth->execute($new_category, $new_authorised_value);
132
            authorised_value => $new_authorised_value,
173
        ($duplicate_entry) = $sth->fetchrow_array();
133
            lib => $input->param('lib') || undef,
174
        unless ( $duplicate_entry ) {
134
            lib_opac => $input->param('lib_opac') || undef,
175
            my $sth=$dbh->prepare( 'INSERT INTO authorised_values
135
            imageurl => $imageurl,
176
                                    ( category, authorised_value, lib, lib_opac, imageurl )
136
            branches_limitations => [ map { {branchcode => $_, branchname => undef} } @branches ],
177
                                    values (?, ?, ?, ?, ?)' );
137
        } );
178
    	    my $lib = $input->param('lib');
138
        eval {$av->insert};
179
    	    my $lib_opac = $input->param('lib_opac');
139
        if ( $@ ) {
180
    	    undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
140
            push @messages, {type => 'error', code => 'error_on_insert' };
181
    	    undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
141
        } else {
182
            $sth->execute( $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl );
142
            push @messages, { type => 'message', code => 'success_on_insert' };
183
            $id = $dbh->{'mysql_insertid'};
184
            if ( @branches ) {
185
                $sth = $dbh->prepare(
186
                    "INSERT INTO authorised_values_branches
187
                                ( av_id, branchcode )
188
                                VALUES ( ?, ? )"
189
                );
190
                for my $branchcode ( @branches ) {
191
                    next if not $branchcode;
192
                    $sth->execute($id, $branchcode);
193
                }
194
            }
195
    	    print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
196
    	    exit;
197
        }
143
        }
198
    }
144
    }
199
    if ( $duplicate_entry ) {       
145
    if ( $duplicate_entry ) {
200
        $template->param(duplicate_category => $new_category,
146
        $template->param(duplicate_category => $new_category,
201
                         duplicate_value =>  $new_authorised_value,
147
                         duplicate_value =>  $new_authorised_value);
202
                         else => 1);
148
    }
203
        default_form();
204
     }           
205
	
206
################## DELETE_CONFIRM ##################################
207
# called by default form, used to confirm deletion of data in DB
208
} elsif ($op eq 'delete_confirm') {
209
	my $sth=$dbh->prepare("select category,authorised_value,lib,lib_opac from authorised_values where id=?");
210
	$sth->execute($id);
211
	my $data=$sth->fetchrow_hashref;
212
	$id = $input->param('id') unless $id;
213
	$template->param(searchfield => $searchfield,
214
							Tlib => $data->{'lib'},
215
							Tlib_opac => $data->{'lib_opac'},
216
							Tvalue => $data->{'authorised_value'},
217
							id =>$id,
218
							);
219
149
220
													# END $OP eq DELETE_CONFIRM
150
    $op = 'list';
221
################## DELETE_CONFIRMED ##################################
151
    $searchfield = $new_category;
222
# called by delete_confirm, used to effectively confirm deletion of data in DB
152
} elsif ($op eq 'delete') {
223
} elsif ($op eq 'delete_confirmed') {
153
    my $av = Koha::AuthorisedValue->new( { id => $input->param('id') } );
224
	my $id = $input->param('id');
154
    eval {$av->delete};
225
	my $sth=$dbh->prepare("delete from authorised_values where id=?");
155
    if ( $@ ) {
226
	$sth->execute($id);
156
        push @messages, {type => 'error', code => 'error_on_delete' };
227
	print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>";
157
    } else {
228
	exit;
158
        push @messages, { type => 'message', code => 'success_on_delete' };
229
													# END $OP eq DELETE_CONFIRMED
159
    }
230
################## DEFAULT ##################################
160
231
} else { # DEFAULT
161
    $op = 'list';
232
    default_form();
162
    $template->param( delete_success => 1 );
233
} #---- END $OP eq DEFAULT
163
}
234
output_html_with_http_headers $input, $cookie, $template->output;
235
164
236
exit 0;
165
$template->param(
166
    op => $op,
167
    searchfield => $searchfield,
168
    messages => \@messages,
169
);
237
170
238
sub default_form {
171
if ( $op eq 'list' ) {
239
    # build categories list
172
    # build categories list
240
    my $sth = $dbh->prepare("select distinct category from authorised_values");
173
    my $avs = Koha::AuthorisedValues->new;
241
    $sth->execute;
242
    my @category_list;
174
    my @category_list;
243
    my %categories;    # a hash, to check that some hardcoded categories exist.
175
    my %categories;    # a hash, to check that some hardcoded categories exist.
244
    while ( my ($category) = $sth->fetchrow_array ) {
176
    for my $category ( $avs->categories ) {
245
        push( @category_list, $category );
177
        push( @category_list, $category );
246
        $categories{$category} = 1;
178
        $categories{$category} = 1;
247
    }
179
    }
Lines 251-294 sub default_form { Link Here
251
        push @category_list, $_ unless $categories{$_};
183
        push @category_list, $_ unless $categories{$_};
252
    }
184
    }
253
185
254
	#reorder the list
186
    #reorder the list
255
	@category_list = sort {$a cmp $b} @category_list;
187
    @category_list = sort {$a cmp $b} @category_list;
256
	my $tab_list = CGI::scrolling_list(-name=>'searchfield',
188
257
	        -id=>'searchfield',
189
    $searchfield ||= $category_list[0];
258
			-values=> \@category_list,
190
259
			-default=>"",
191
    my $avs_for_category = $avs->filter( { category => $searchfield } );
260
			-size=>1,
192
    my @loop_data = ();
261
			-multiple=>0,
193
    # builds value list
262
			);
194
    for my $av ( @$avs_for_category ) {
263
	if (!$searchfield) {
195
        my $branches = $av->branches_limitations;
264
		$searchfield=$category_list[0];
196
        my %row_data;  # get a fresh hash for the row data
265
	}
197
        $row_data{category}              = $av->{category};
266
    my ($results) = AuthorizedValuesForCategory($searchfield);
198
        $row_data{authorised_value}      = $av->{authorised_value};
267
    my $count = scalar(@$results);
199
        $row_data{lib}                   = $av->{lib};
268
	my @loop_data = ();
200
        $row_data{lib_opac}              = $av->{lib_opac};
269
	# builds value list
201
        $row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $av->{imageurl} );
270
    my $dbh = C4::Context->dbh;
202
        $row_data{branches}              = $branches;
271
    $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM authorised_values_branches AS avb, branches AS b WHERE avb.branchcode = b.branchcode AND avb.av_id = ?");
203
        $row_data{id}                    = $av->{id};
272
	for (my $i=0; $i < $count; $i++){
204
        push(@loop_data, \%row_data);
273
        $sth->execute( $results->[$i]{id} );
205
    }
274
        my @selected_branches;
275
        while ( my $branch = $sth->fetchrow_hashref ) {
276
            push @selected_branches, $branch;
277
        }
278
		my %row_data;  # get a fresh hash for the row data
279
		$row_data{category}              = $results->[$i]{'category'};
280
		$row_data{authorised_value}      = $results->[$i]{'authorised_value'};
281
		$row_data{lib}                   = $results->[$i]{'lib'};
282
		$row_data{lib_opac}              = $results->[$i]{'lib_opac'};
283
		$row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
284
		$row_data{edit}                  = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
285
		$row_data{delete}                = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
286
        $row_data{branches}              = \@selected_branches;
287
		push(@loop_data, \%row_data);
288
	}
289
206
290
	$template->param( loop     => \@loop_data,
207
    $template->param(
291
                          tab_list => $tab_list,
208
        loop     => \@loop_data,
292
                          category => $searchfield );
209
        category => $searchfield,
293
}
210
        categories => \@category_list,
211
    );
294
212
213
}
214
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt (-48 / +54 lines)
Lines 1-9 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>Koha &rsaquo; Administration &rsaquo; Authorized values [% IF ( add_form ) %] &rsaquo; [% IF ( action_modify ) %]Modify authorized value[% END %]
2
<title>Koha &rsaquo; Administration &rsaquo; Authorized values
3
	   [% IF ( action_add_value ) %] &rsaquo;  New authorized value[% END %]
3
[% IF op == 'add_form' %]
4
	   [% IF ( action_add_category ) %] &rsaquo; New category[% END %][% END %]
4
  [% IF ( action_modify ) %] &rsaquo; Modify authorized value[% END %]
5
[% IF ( delete_confirm ) %] &rsaquo; Confirm deletion[% END %]
5
  [% IF ( action_add_value ) %] &rsaquo;  New authorized value[% END %]
6
[% IF ( else ) %]Authorized values[% END %]</title>
6
  [% IF ( action_add_category ) %] &rsaquo; New category[% END %]
7
[% END %]
8
</title>
7
[% INCLUDE 'doc-head-close.inc' %]
9
[% INCLUDE 'doc-head-close.inc' %]
8
10
9
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
11
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
Lines 25-35 Link Here
25
        $("#branches option:first").attr("selected", "selected");
27
        $("#branches option:first").attr("selected", "selected");
26
    }
28
    }
27
    $('#icons').tabs();
29
    $('#icons').tabs();
30
31
    $("a.delete").click(function(){
32
        return confirm(_("Are you sure you want to delete this authorised value?"));
33
    });
28
});
34
});
29
//]]>
35
//]]>
30
</script>
36
</script>
31
37
32
[% IF ( else ) %]
38
[% IF op == 'list' %]
33
<script type="text/javascript">
39
<script type="text/javascript">
34
//<![CDATA[
40
//<![CDATA[
35
$(document).ready(function() {
41
$(document).ready(function() {
Lines 52-62 $(document).ready(function() { Link Here
52
<body id="admin_authorised_values" class="admin">
58
<body id="admin_authorised_values" class="admin">
53
[% INCLUDE 'header.inc' %]
59
[% INCLUDE 'header.inc' %]
54
[% INCLUDE 'cat-search.inc' %]
60
[% INCLUDE 'cat-search.inc' %]
55
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; [% IF ( add_form ) %] <a href="/cgi-bin/koha/admin/authorised_values.pl">Authorized values</a> &rsaquo; [% IF ( action_modify ) %]Modify authorized value[% END %]
61
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; [% IF op == 'add_form' %] <a href="/cgi-bin/koha/admin/authorised_values.pl">Authorized values</a> &rsaquo; [% IF ( action_modify ) %]Modify authorized value[% END %]
56
	   [% IF ( action_add_value ) %]New authorized value[% END %]
62
	   [% IF ( action_add_value ) %]New authorized value[% END %]
57
	   [% IF ( action_add_category ) %]New category[% END %][% END %]
63
	   [% IF ( action_add_category ) %]New category[% END %][% END %]
58
[% IF ( delete_confirm ) %] <a href="/cgi-bin/koha/admin/authorised_values.pl">Authorized values</a> &rsaquo; Confirm deletion[% END %]
64
[% IF op == 'list' %]Authorized values[% END %]</div>
59
[% IF ( else ) %]Authorized values[% END %]</div>
60
65
61
<div id="doc3" class="yui-t2">
66
<div id="doc3" class="yui-t2">
62
   
67
   
Lines 64-70 $(document).ready(function() { Link Here
64
	<div id="yui-main">
69
	<div id="yui-main">
65
	<div class="yui-b">
70
	<div class="yui-b">
66
71
67
[% IF ( add_form ) %]
72
[% IF op == 'add_form' %]
68
	<h1>
73
	<h1>
69
	   [% IF ( action_modify ) %]Modify authorized value[% END %]
74
	   [% IF ( action_modify ) %]Modify authorized value[% END %]
70
	   [% IF ( action_add_value ) %]New authorized value[% END %]
75
	   [% IF ( action_add_value ) %]New authorized value[% END %]
Lines 73-81 $(document).ready(function() { Link Here
73
78
74
    [% IF ( action_modify ) %]<div class="note"><strong>NOTE:</strong> If you change an authorized value, existing records using it won't be updated.</div>[% END %]
79
    [% IF ( action_modify ) %]<div class="note"><strong>NOTE:</strong> If you change an authorized value, existing records using it won't be updated.</div>[% END %]
75
80
76
	<form action="[% script_name %]" name="Aform" method="post">
81
 <form action="/cgi-bin/koha/admin/authorised_values.pl" name="Aform" method="post">
77
	<input type="hidden" name="op" value="add_validate" />
82
    <input type="hidden" name="op" value="add" />
78
    <input type="hidden" name="offset" value="[% offset %]" />
79
        <fieldset class="rows"><ol>
83
        <fieldset class="rows"><ol>
80
        <li>
84
        <li>
81
        [% IF ( action_add_category ) %]<label for="category">Category: </label>
85
        [% IF ( action_add_category ) %]<label for="category">Category: </label>
Lines 158-189 $(document).ready(function() { Link Here
158
[% END %]
162
[% END %]
159
163
160
164
161
[% IF ( delete_confirm ) %]
165
[% IF op == 'list' %]
162
	<div class="dialog alert">
163
<h3>Confirm deletion</h3>
164
<table>
165
	<tr>
166
		<th>Category</th>
167
		<th>Value</th>
168
		<th>Description</th>
169
		<th>Description (OPAC)</th>
170
	</tr>
171
	<tr>
172
	    <td>[% searchfield %]</td>
173
	    <td>[% Tvalue %]</td>
174
	    <td>[% Tlib %]</td>
175
	    <td>[% Tlib_opac %]</td>
176
	</tr>
177
	</table>
178
	<form action="[% script_name %]" method="post">
179
		<input type="hidden" name="op" value="delete_confirmed" />
180
		<input type="hidden" name="id" value="[% id %]" />
181
        <input type="hidden" name="searchfield" value="[% searchfield %]" /><fieldset class="action"><input type="submit" value="Yes, delete" class="approve" /></form>
182
<form action="[% script_name %]" method="get"><input type="hidden" name="searchfield" value="[% searchfield %]" /><input type="submit" value="No, do not delete" class="deny" /></form>
183
</div>
184
[% END %]
185
186
[% IF ( else ) %]
187
166
188
<div id="toolbar" class="btn-toolbar">
167
<div id="toolbar" class="btn-toolbar">
189
    <a id="addauth" class="btn btn-small" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;category=[% category %]">New authorized value for [% category %]</a>
168
    <a id="addauth" class="btn btn-small" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;category=[% category %]">New authorized value for [% category %]</a>
Lines 193-203 $(document).ready(function() { Link Here
193
<h1>Authorized values</h1>
172
<h1>Authorized values</h1>
194
<div class="note"><strong>NOTE:</strong> If you change an authorized value, existing records using it won't be updated.</div>
173
<div class="note"><strong>NOTE:</strong> If you change an authorized value, existing records using it won't be updated.</div>
195
174
196
[% IF ( duplicate_category ) %]
175
[% FOR m IN messages %]
197
<div class="dialog alert">Could not add value &quot;[% duplicate_value %]&quot; for category &quot;[% duplicate_category %]&quot; &mdash; value already present.
176
    <div class="dialog [% m.type %]">
198
</div>
177
        [% SWITCH m.code %]
178
        [% CASE 'error_on_update' %]
179
            An error occurred when updating this authorised values. Perhaps the value already exists.
180
        [% CASE 'error_on_insert' %]
181
            An error occurred when inserting this authorised values. Perhaps the value or the category already exists.
182
        [% CASE 'error_on_delete' %]
183
            An error occurred when deleteing this authorised values. Check the logs.
184
        [% CASE 'success_on_update' %]
185
            Authorised value updated with success.
186
        [% CASE 'success_on_insert' %]
187
            Authorised value inserted with success.
188
        [% CASE 'success_on_delete' %]
189
            Authorised value deleted with success.
190
        [% CASE %]
191
            [% m.code %]
192
        [% END %]
193
    </div>
199
[% END %]
194
[% END %]
200
<form action="/cgi-bin/koha/admin/authorised_values.pl" method="post" id="category"><label for="searchfield">Show category: </label>[% tab_list %] <input type="submit" value="Submit" /></form>
195
196
<form action="/cgi-bin/koha/admin/authorised_values.pl" method="post" id="category">
197
  <label for="searchfield">Show category: </label>
198
  <select id="searchfield" name="searchfield">
199
  [% FOR c IN categories %]
200
    [% IF c == searchfield %]
201
      <option value="[% c %]" selected="selected">[% c %]</option>
202
    [% ELSE %]
203
      <option value="[% c %]">[% c %]</option>
204
    [% END %]
205
  [% END %]
206
  <input type="submit" value="Submit" />
207
</form>
201
[% IF ( category == 'Bsort1' ) %]
208
[% IF ( category == 'Bsort1' ) %]
202
    <p>An authorized value attached to patrons, that can be used for stats purposes</p>
209
    <p>An authorized value attached to patrons, that can be used for stats purposes</p>
203
[% END %]
210
[% END %]
Lines 276-283 $(document).ready(function() { Link Here
276
            No limitation
283
            No limitation
277
        [% END %]
284
        [% END %]
278
    </td>
285
    </td>
279
	<td><a href="[% loo.edit %]">Edit</a></td>
286
    <td><a href="/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;id=[% loo.id %]">Edit</a></td>
280
	<td><a href="[% loo.delete %]">Delete</a></td>
287
    <td><a class="delete" href="/cgi-bin/koha/admin/authorised_values.pl?op=delete&amp;searchfield=[% searchfield %]&amp;id=[% loo.id %]">Delete</a></td>
281
</tr>
288
</tr>
282
[% END %]
289
[% END %]
283
</tbody></table>[% ELSE %]
290
</tbody></table>[% ELSE %]
Lines 285-292 $(document).ready(function() { Link Here
285
[% END %]
292
[% END %]
286
293
287
[% IF ( isprevpage ) %]
294
[% IF ( isprevpage ) %]
288
<form class="inline" action="[% script_name %]" method="post">
295
<form class="inline" action="/cgi-bin/koha/admin/authorised_values.pl" method="post">
289
<input type="hidden" name="offset" value="[% prevpage %]" /><input type="hidden" name="searchfield" value="[% searchfield %]" />
296
<input type="hidden" name="searchfield" value="[% searchfield %]" />
290
	<input type="submit" value="&lt;&lt; Previous" /></form>
297
	<input type="submit" value="&lt;&lt; Previous" /></form>
291
[% END %] 
298
[% END %] 
292
299
293
- 

Return to bug 10363