-# called by add_form, used to insert/modify data in DB
-} elsif ($op eq 'add_validate') {
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("replace itemtypes (itemtype,description,renewalsallowed,rentalcharge,notforloan) values (?,?,?,?,?)");
- $sth->execute(
- $input->param('itemtype'),$input->param('description'),
- $input->param('renewalsallowed'),$input->param('rentalcharge'),
- $input->param('notforloan')?1:0);
- $sth->finish;
- print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
- exit;
- # END $OP eq ADD_VALIDATE
+ # called by add_form, used to insert/modify data in DB
+}
+elsif ( $op eq 'add_validate' ) {
+ my $query = "
+ SELECT itemtype
+ FROM itemtypes
+ WHERE itemtype = ?
+ ";
+ my $sth = $dbh->prepare($query);
+ $sth->execute($itemtype);
+ if ( $sth->fetchrow ) { # it's a modification
+ my $query2 = '
+ UPDATE itemtypes
+ SET description = ?
+ , rentalcharge = ?
+ , notforloan = ?
+ , imageurl = ?
+ , summary = ?
+ WHERE itemtype = ?
+ ';
+ $sth = $dbh->prepare($query2);
+ $sth->execute(
+ $input->param('description'),
+ $input->param('rentalcharge'),
+ ( $input->param('notforloan') ? 1 : 0 ),
+ (
+ $input->param('image') eq 'removeImage' ? '' : (
+ $input->param('image') eq 'remoteImage'
+ ? $input->param('remoteImage')
+ : $input->param('image') . ""
+ )
+ ),
+ $input->param('summary'),
+ $input->param('itemtype')
+ );
+ }
+ else { # add a new itemtype & not modif an old
+ my $query = "
+ INSERT INTO itemtypes
+ (itemtype,description,rentalcharge, notforloan, imageurl,summary)
+ VALUES
+ (?,?,?,?,?,?);
+ ";
+ my $sth = $dbh->prepare($query);
+ my $image = $input->param('image');
+ $sth->execute(
+ $input->param('itemtype'),
+ $input->param('description'),
+ $input->param('rentalcharge'),
+ $input->param('notforloan') ? 1 : 0,
+ $image eq 'removeImage' ? '' :
+ $image eq 'remoteImage' ? $input->param('remoteImage') :
+ $image,
+ $input->param('summary'),
+ );
+ }
+
+ print $input->redirect('itemtypes.pl');
+ exit;
+
+ # END $OP eq ADD_VALIDATE