-} elsif ($op eq 'delete_confirmed') {
- my $dbh = C4::Context->dbh;
- my $bookfundid=uc($input->param('bookfundid'));
- my $query = "delete from aqbookfund where bookfundid='$bookfundid'";
- my $sth=$dbh->prepare($query);
- $sth->execute;
- $sth->finish;
- print "data deleted";
- print "<form action='$script_name' method=post>";
- print "<input type=submit value=OK>";
- print "</form>";
- # END $OP eq DELETE_CONFIRMED
-################## DEFAULT ##################################
-} else { # DEFAULT
- my @inputs=(["text","searchfield",$searchfield],
- ["reset","reset","clr"]);
- print mkheadr(2,'bookfund admin');
- print mkformnotable("$script_name",@inputs);
- print <<printend
-
-printend
- ;
- if ($searchfield ne '') {
- print "You Searched for <b>$searchfield<b><p>";
- }
- print mktablehdr;
- print mktablerow(6,'#99cc33',bold('Book fund'),bold('Start date'),bold('End date'),bold('Budget amount'),
- ' ',' ','/images/background-mem.gif');
- my $env;
- my ($count,$results)=StringSearch($env,$searchfield,'web');
- my $toggle="white";
- for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
- #find out stats
- # my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
- # $fines=$fines+0;
- if ($toggle eq 'white'){
- $toggle="#ffffcc";
- } else {
- $toggle="white";
- }
- print mktablerow(6,$toggle,$results->[$i]{'bookfundid'},
- $results->[$i]{'bookfundname'},$results->[$i]{'bookfundgroup'},
- mklink("$script_name?op=add_form&bookfundid=".$results->[$i]{'bookfundid'},'Edit'),
- mklink("$script_name?op=delete_confirm&bookfundid=".$results->[$i]{'bookfundid'},'Delete'));
- }
- print mktableft;
- print "<form action='$script_name' method=post>";
- print "<input type=hidden name=op value=add_form>";
- if ($offset>0) {
- my $prevpage = $offset-$pagesize;
- print mklink("$script_name?offset=".$prevpage,'<< Prev');
- }
- print " ";
- if ($offset+$pagesize<$count) {
- my $nextpage =$offset+$pagesize;
- print mklink("$script_name?offset=".$nextpage,'Next >>');
- }
- print "<br><input type=image src=\"/images/button-add-new.gif\" WIDTH=188 HEIGHT=44 ALT=\"Add budget\" BORDER=0 ></a><br>";
- print "</form>";
-} #---- END $OP eq DEFAULT
-print endmenu('admin');
-print endpage();
+elsif ($op eq 'delete_confirmed') {
+ DelBookFund($bookfundid, $branchcodeid);
+}
+else { # DEFAULT
+ my ($sth);
+ # filters
+ my @branchloop;
+ foreach my $branchcode (sort keys %{$branches}) {
+ my $row = {
+ code => $branchcode,
+ name => $branches->{$branchcode}->{branchname},
+ };
+ if (defined $input->param('filter_branchcode')
+ and $input->param('filter_branchcode') eq $branchcode) {
+ $row->{selected} = 1;
+ }
+ push @branchloop, $row;
+ }
+
+ my @bookfundids_loop;
+ $sth = GetBookFundsId();
+
+ while (my $row = $sth->fetchrow_hashref) {
+ if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
+ $row->{selected} = 1;
+ }
+ push @bookfundids_loop, $row;
+ }
+
+ $template->param(
+ filter_bookfundids => \@bookfundids_loop,
+ filter_branches => \@branchloop,
+ filter_bookfundname => $input->param('filter_bookfundname') || undef,
+ );
+
+ # searching the bookfunds corresponding to our filtering rules
+ my @results = SearchBookFund(
+ $input->param('filter'),
+ $input->param('filter_bookfundid'),
+ $input->param('filter_bookfundname'),
+ $input->param('filter_branchcode'),
+ );
+
+ # does the book funds have budgets?
+ my @loop_id;
+ $sth = GetBookFundsId();
+ while (my $row = $sth->fetchrow){
+ push @loop_id, $row;
+ }
+
+ my ($id,%nb_budgets_of);
+ foreach $id (@loop_id){
+ my $number = Countbookfund($id);
+ $nb_budgets_of{$id} = $number;
+ }
+
+ # pagination informations
+ my $page = $input->param('page') || 1;
+ my @loop;
+
+ my $first = ($page - 1) * $pagesize;
+
+ # if we are on the last page, the number of the last word to display
+ # must not exceed the length of the results array
+ my $last = min(
+ $first + $pagesize - 1,
+ scalar(@results) - 1,
+ );
+
+ foreach my $result (@results[$first .. $last]) {
+ push @loop, {
+ %{$result},
+ branchname => $branches->{ $result->{branchcode} }->{branchname},
+ has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
+ };
+ }
+
+ $template->param(
+ bookfund => \@loop,
+ pagination_bar => pagination_bar(
+ $script_name,
+ getnbpages(scalar @results, $pagesize),
+ $page,
+ 'page'
+ )
+ );
+}
+
+output_html_with_http_headers $input, $cookie, $template->output;