my $searchfield = $input->param('description');
my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
my $itemtype = $input->param('itemtype');
-my $op = $input->param('op');
+my $op = $input->param('op') // 'list';
+my @messages;
$searchfield =~ s/\,//g;
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
$template->param(script_name => $script_name);
if ($op) {
$template->param($op => 1); # we show only the TMPL_VAR names $op
-} else {
- $template->param(else => 1);
}
my $dbh = C4::Context->dbh;
# called by add_form, used to insert/modify data in DB
}
elsif ( $op eq 'add_validate' ) {
- my $query = "
+ my $is_a_modif = $input->param('is_a_modif');
+ my ( $already_exists ) = $dbh->selectrow_array(q|
SELECT itemtype
FROM itemtypes
WHERE itemtype = ?
- ";
- my $sth = $dbh->prepare($query);
- $sth->execute($itemtype);
- if ( $sth->fetchrow ) { # it's a modification
+ |, undef, $itemtype );
+ if ( $already_exists and $is_a_modif ) { # it's a modification
my $query2 = '
UPDATE itemtypes
SET description = ?
, sip_media_type = ?
WHERE itemtype = ?
';
- $sth = $dbh->prepare($query2);
+ my $sth = $dbh->prepare($query2);
$sth->execute(
$input->param('description'),
$input->param('rentalcharge'),
$input->param('itemtype')
);
}
- else { # add a new itemtype & not modif an old
+ elsif ( not $already_exists and not $is_a_modif ) {
my $query = "
INSERT INTO itemtypes
(itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type)
$sip_media_type,
);
}
+ else {
+ push @messages, {
+ type => 'error',
+ code => 'already_exists',
+ };
+ }
- print $input->redirect('itemtypes.pl');
- exit;
-
+ $searchfield = '';
+ $op = 'list';
# END $OP eq ADD_VALIDATE
################## DELETE_CONFIRM ##################################
# called by default form, used to confirm deletion of data in DB
# END $OP eq DELETE_CONFIRMED
################## DEFAULT ##################################
}
-else { # DEFAULT
+
+if ( $op eq 'list' ) {
my ($results) = StringSearch( $searchfield, 'web' );
my @loop;
foreach my $itemtype ( @{$results} ) {
}
$template->param(
- loop => \@loop,
+ loop => \@loop,
+ else => 1,
+ messages => \@messages,
);
-} #---- END $OP eq DEFAULT
+}
output_html_with_http_headers $input, $cookie, $template->output;
<a class="btn btn-small" id="newitemtype" href="/cgi-bin/koha/admin/itemtypes.pl?op=add_form"><i class="icon-plus"></i> New item type</a>
</div>[% END %]
+[% FOREACH message IN messages %]
+ [% IF message.type == 'success' %]
+ <div class="dialog message">
+ [% ELSIF message.type == 'warning' %]
+ <div class="dialog alert">
+ [% ELSIF message.type == 'error' %]
+ <div class="dialog error" style="margin:auto;">
+ [% END %]
+ [% IF message.code == 'already_exists' %]
+ This item type already exists.
+ [% END %]
+ </div>
+[% END %]
+
+
[% IF ( add_form ) %]
[% IF ( itemtype ) %]
<h3>Modify item type</h3>
<ol>
[% IF ( itemtype ) %]
<li>
+ <input type="hidden" name="is_a_modif" value="1" />
<span class="label">Item type: </span> <input type="hidden" name="itemtype" value="[% itemtype %]" />
[% itemtype %]
</li>