-$template->param(action => $script_name);
-if ($op eq 'add') {
- # If the user has pressed the "add new branch" button.
- heading("Branches: Add Branch");
- $template->param('heading-branches-add-branch-p' => 1);
- editbranchform();
-
-} elsif ($op eq 'edit') {
- # if the user has pressed the "edit branch settings" button.
- heading("Branches: Edit Branch");
- $template->param('heading-branches-edit-branch-p' => 1);
- $template->param(add => 1);
- editbranchform($branchcode);
-} elsif ($op eq 'add_validate') {
- # confirm settings change...
- my $params = $input->Vars;
- unless ($params->{'branchcode'} && $params->{'branchname'}) {
- $template->param(else => 1);
- default ("MESSAGE1");
- } else {
- setbranchinfo($params);
- $template->param(else => 1);
- default ("MESSAGE2");
- }
-} elsif ($op eq 'delete') {
- # if the user has pressed the "delete branch" button.
- my $message = checkdatabasefor($branchcode);
- if ($message) {
- $template->param(else => 1);
- default($message);
- } else {
- $template->param(branchname => $branchname);
- $template->param(delete_confirm => 1);
- $template->param(branchcode => $branchcode);
- }
-} elsif ($op eq 'delete_confirmed') {
- # actually delete branch and return to the main screen....
- deletebranch($branchcode);
- $template->param(else => 1);
- default("MESSAGE3");
-} 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);
-} elsif ($op eq 'addcategory_validate') {
- # confirm settings change...
- my $params = $input->Vars;
- unless ($params->{'categorycode'} && $params->{'categoryname'}) {
- $template->param(else => 1);
- default ("MESSAGE4");
- } else {
- setcategoryinfo($params);
- $template->param(else => 1);
- default ("MESSAGE5");
- }
-} elsif ($op eq 'delete_category') {
- # if the user has pressed the "delete branch" button.
- my $message = checkcategorycode($categorycode);
- if ($message) {
- $template->param(else => 1);
- default($message);
- } else {
- $template->param(delete_category => 1);
- $template->param(categorycode => $categorycode);
- }
-} elsif ($op eq 'categorydelete_confirmed') {
- # actually delete branch and return to the main screen....
- deletecategory($categorycode);
- $template->param(else => 1);
- default("MESSAGE6");
-
-} else {
- # if no operation has been set...
- default();
+else {
+ $template->param(
+ script_name => $script_name,
+ else => 1
+ ); # we show only the TMPL_VAR names $op
+}
+$template->param( action => $script_name );
+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 $error = ModBranch($params);
+ # 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
+ $template->param(%$params);
+ $template->param(branch_name => $params->{branchname});
+ $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 $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
+ $sth->execute( $branchcode, $branchcode );
+ my ($total) = $sth->fetchrow_array;
+ $sth->finish;
+
+ my $message;
+
+ if ($total) {
+ $message = "MESSAGE7";
+ }
+
+ if ($message) {
+ $template->param( else => 1 );
+ default($message,$template);
+ }
+ else {
+ $template->param( branchname => $branchname );
+ $template->param( delete_confirm => 1 );
+ $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' ) {
+
+ # confirm settings change...
+ my $params = $input->Vars;
+ unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
+ $template->param( else => 1 );
+ default("MESSAGE4",$template);
+ }
+ else {
+ ModBranchCategoryInfo($params);
+ $template->param( else => 1 );
+ 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 );
+ }