-$template->param(
- script_name => $script_name,
- action => $script_name,
-);
-$template->param( ($op || 'else') => 1 );
-
-if ( $op eq 'add' ) {
-
- # If the user has pressed the "add new branch" button.
- $template->param( 'heading_branches_add_branch_p' => 1 );
- editbranchform($branchcode,$template);
-
-}
-elsif ( $op eq 'edit' ) {
-
- # if the user has pressed the "edit branch settings" button.
- $template->param( 'heading_branches_add_branch_p' => 0,
- 'add' => 1, );
- editbranchform($branchcode,$template);
-}
-elsif ( $op eq 'add_validate' ) {
-
- # confirm settings change...
- my $params = $input->Vars;
- unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
- $template->param( else => 1 );
- default("MESSAGE1",$template);
- }
- else {
- my $mod_branch = 1;
- if ($params->{add}) {
- my ($existing) =
- C4::Context->dbh->selectrow_array("SELECT count(*) FROM branches WHERE branchcode = ?", {}, $branchcode);
- if ($existing > 0) {
- $mod_branch = 0;
- _branch_to_template($params, $template); # preserve most (FIXME) of user's input
- $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, 'ERROR1' => 1 );
- }
- }
- if ($mod_branch) {
- my $error = ModBranch($params); # FIXME: causes warnings to log on duplicate branchcode
- # if error saving, stay on edit and rise error
- if ($error) {
- # copy input parameters back to form
- # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
- editbranchform($branchcode,$template);
- $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, "ERROR$error" => 1 );
- } else {
- $template->param( else => 1);
- default("MESSAGE2",$template);
- }
- }
- }
-}
-elsif ( $op eq 'delete' ) {
- # if the user has pressed the "delete branch" button.
-
- # check to see if the branchcode is being used in the database somewhere....
- my $dbh = C4::Context->dbh;
- my $sthitems = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
- my $sthborrowers = $dbh->prepare("select count(*) from borrowers where branchcode=?");
- $sthitems->execute( $branchcode, $branchcode );
- $sthborrowers->execute( $branchcode );
- my ($totalitems) = $sthitems->fetchrow_array;
- my ($totalborrowers) = $sthborrowers->fetchrow_array;
- if ($totalitems && !$totalborrowers) {
- $template->param( else => 1 );
- default("MESSAGE10", $template);
- }
- elsif (!$totalitems && $totalborrowers){
- $template->param( else => 1 );
- default("MESSAGE11", $template);
- }
- elsif ($totalitems && $totalborrowers){
- $template->param( else => 1 );
- default("MESSAGE7", $template);
- }
- else {
- $template->param( delete_confirm => 1 );
- $template->param( branchname => $branchname );
- $template->param( branchcode => $branchcode );
- }
-}
-elsif ( $op eq 'delete_confirmed' ) {
-
- # actually delete branch and return to the main screen....
- DelBranch($branchcode);
- $template->param( else => 1 );
- default("MESSAGE3",$template);
-}
-elsif ( $op eq 'editcategory' ) {
-
- # If the user has pressed the "add new category" or "modify" buttons.
- $template->param( 'heading_branches_edit_category_p' => 1 );
- editcatform($categorycode,$template);
-}
-elsif ( $op eq 'addcategory_validate' ) {
-
- $template->param( else => 1 );
- # confirm settings change...
- my $params = $input->Vars;
- unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
- default("MESSAGE4",$template);
- }
- elsif ($input->param('add')){
- # doing an add must check the code is unique
- if (CheckCategoryUnique($input->param('categorycode'))){
- ModBranchCategoryInfo($params);
- default("MESSAGE5",$template);
- }
- else {
- default("MESSAGE9",$template);
- }
- }
- else {
- ModBranchCategoryInfo($params);
- default("MESSAGE5",$template);
- }
-}
-elsif ( $op eq 'delete_category' ) {
-
- # if the user has pressed the "delete branch" button.
- my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
- if ($message) {
- $template->param( else => 1 );
- default($message,$template);
- }
- else {
- $template->param( delete_category => 1 );
- $template->param( categorycode => $categorycode );
- }
-}
-elsif ( $op eq 'categorydelete_confirmed' ) {
-
- # actually delete branch and return to the main screen....
- DelBranchCategory($categorycode);
- $template->param( else => 1 );
- default("MESSAGE6",$template);
-
-}
-else {
- # if no operation has been set...
- default("",$template);
-}
-
-################################################################################
-#
-# html output functions....
-
-sub default {
- my $message = shift || '';
- my $innertemplate = shift or return;
- $innertemplate->param($message => 1) if $message;
- $innertemplate->param(
- 'heading_branches_p' => 1,
- );
- branchinfotable("",$innertemplate);
-}
-
-sub editbranchform {
- my ($branchcode,$innertemplate) = @_;
- # initiate the scrolling-list to select the printers
- my $printers = GetPrinters();
- my @printerloop;
- my $data;
- my $oldprinter = "";