3 # Copyright 2008 LibLime
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use List
::MoreUtils qw
/uniq/;
31 use Koha
::Patron
::Attribute
::Types
;
33 use Koha
::AuthorisedValues
;
35 use Koha
::Patron
::Categories
;
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
40 my $op = $input->param('op') || '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
44 { template_name
=> "admin/patron-attr-types.tt",
48 flagsrequired
=> { parameters
=> 'manage_patron_attributes' }
53 $template->param(script_name
=> $script_name);
55 my $code = $input->param("code");
58 if ($op eq "edit_attribute_type") {
59 edit_attribute_type_form
($template, $code);
60 } elsif ($op eq "edit_attribute_type_confirmed") {
61 $display_list = add_update_attribute_type
('edit', $template, $code);
62 } elsif ($op eq "add_attribute_type") {
63 add_attribute_type_form
($template);
64 } elsif ($op eq "add_attribute_type_confirmed") {
65 $display_list = add_update_attribute_type
('add', $template, $code);
66 } elsif ($op eq "delete_attribute_type") {
67 $display_list = delete_attribute_type_form
($template, $code);
68 } elsif ($op eq "delete_attribute_type_confirmed") {
69 delete_attribute_type
($template, $code);
76 unless (C4
::Context
->preference('ExtendedPatronAttributes')) {
77 $template->param(WARNING_extended_attributes_off
=> 1);
79 patron_attribute_type_list
($template);
82 output_html_with_http_headers
$input, $cookie, $template->output;
86 sub add_attribute_type_form
{
89 my $patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
91 attribute_type_form
=> 1,
92 confirm_op
=> 'add_attribute_type_confirmed',
93 categories
=> $patron_categories,
97 sub error_add_attribute_type_form
{
100 $template->param(description
=> scalar $input->param('description'));
102 $template->param( category_code
=> scalar $input->param('category_code') );
103 $template->param( class => scalar $input->param('class') );
106 attribute_type_form
=> 1,
107 confirm_op
=> 'add_attribute_type_confirmed',
108 authorised_value_category
=> scalar $input->param('authorised_value_category'),
112 sub add_update_attribute_type
{
114 my $template = shift;
117 my $description = $input->param('description');
118 my $repeatable = $input->param('repeatable') ?
1 : 0;
119 my $unique_id = $input->param('unique_id') ?
1 : 0;
120 my $opac_display = $input->param('opac_display') ?
1 : 0;
121 my $opac_editable = $input->param('opac_editable') ?
1 : 0;
122 my $staff_searchable = $input->param('staff_searchable') ?
1 : 0;
123 my $authorised_value_category = $input->param('authorised_value_category');
124 my $display_checkout = $input->param('display_checkout') ?
1 : 0;
125 my $category_code = $input->param('category_code') || undef;
126 my $class = $input->param('class');
128 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($code);
129 if ( $op eq 'edit' ) {
130 $attr_type->description($description);
133 if ($attr_type) { # Already exists
134 $template->param( duplicate_code_error
=> $code );
136 # FIXME Regression here
137 # Form will not be refilled with entered values on error
138 error_add_attribute_type_form
($template);
141 $attr_type = Koha
::Patron
::Attribute
::Type
->new(
144 description
=> $description,
145 repeatable
=> $repeatable,
146 unique_id
=> $unique_id,
152 opac_display
=> $opac_display,
153 opac_editable
=> $opac_editable,
154 staff_searchable
=> $staff_searchable,
155 authorised_value_category
=> $authorised_value_category,
156 display_checkout
=> $display_checkout,
157 category_code
=> $category_code,
162 my @branches = grep { ! /^\s*$/ } $input->multi_param('branches');
163 $attr_type->library_limits( \
@branches );
165 if ( $op eq 'edit' ) {
166 $template->param( edited_attribute_type
=> $attr_type->code() );
169 $template->param( added_attribute_type
=> $attr_type->code() );
175 sub delete_attribute_type_form
{
176 my $template = shift;
179 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($code);
180 my $display_list = 0;
181 if (defined($attr_type)) {
183 delete_attribute_type_form
=> 1,
184 confirm_op
=> "delete_attribute_type_confirmed",
186 description
=> $attr_type->description(),
189 $template->param(ERROR_delete_not_found
=> $code);
192 return $display_list;
195 sub delete_attribute_type
{
196 my $template = shift;
199 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($code);
200 if (defined($attr_type)) {
201 # TODO Check must be done for previous step as well
202 if ( my $num_patrons = Koha
::Patrons
->filter_by_attribute_type($code)->count ) {
203 $template->param(ERROR_delete_in_use
=> $code);
204 $template->param(ERROR_num_patrons
=> $num_patrons );
206 $attr_type->delete();
207 $template->param(deleted_attribute_type
=> $code);
210 # FIXME Really needed?
211 $template->param(ERROR_delete_not_found
=> $code);
215 sub edit_attribute_type_form
{
216 my $template = shift;
219 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($code);
220 $template->param(attribute_type
=> $attr_type);
222 my $patron_categories = Koha
::Patron
::Categories
->search({}, {order_by
=> ['description']});
224 attribute_type_form
=> 1,
225 edit_attribute_type
=> 1,
226 confirm_op
=> 'edit_attribute_type_confirmed',
227 categories
=> $patron_categories,
232 sub patron_attribute_type_list
{
233 my $template = shift;
235 my @attr_types = Koha
::Patron
::Attribute
::Types
->search->as_list;
237 my @classes = uniq
( map { $_->class } @attr_types );
238 @classes = sort @classes;
241 # FIXME This is not efficient and should be improved
242 for my $class (@classes) {
244 for my $attr (@attr_types) {
245 next if $attr->class ne $class;
248 my $av = Koha
::AuthorisedValues
->search({ category
=> 'PA_CLASS', authorised_value
=> $class });
249 my $lib = $av->count ?
$av->next->lib : $class;
250 push @attributes_loop, {
256 $template->param(available_attribute_types
=> \
@attributes_loop);
257 $template->param(display_list
=> 1);