- my ($name,$input_name,$data) = @_;
- my $dbh=C4::Context->dbh;
- my $query=qq{SELECT * FROM authorised_values WHERE category=? order by lib};
- my $sth=$dbh->prepare($query);
- $sth->execute($name);
- my $CGISort;
- if ($sth->rows>0){
- my @values;
- my %labels;
-
- for (my $i =0;$i<$sth->rows;$i++){
- my $results = $sth->fetchrow_hashref;
- push @values, $results->{authorised_value};
- $labels{$results->{authorised_value}}=$results->{lib};
- }
- $CGISort= CGI::scrolling_list(
- -name => $input_name,
- -id => $input_name,
- -values => \@values,
- -labels => \%labels,
- -default=> $data,
- -size => 1,
- -multiple => 0);
- }
- $sth->finish;
- return $CGISort;
+ my ( $name, $input_name, $data ) = @_;
+ my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
+
+ my $dbh=C4::Context->dbh;
+ my $query = qq{
+ SELECT *
+ FROM authorised_values
+ };
+ $query .= qq{
+ LEFT JOIN authorised_values_branches ON ( id = av_id )
+ } if $branch_limit;
+ $query .= qq{
+ WHERE category = ?
+ };
+ $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL )} if $branch_limit;
+ $query .= qq{ GROUP BY lib ORDER BY lib};
+
+ my $sth=$dbh->prepare($query);
+ $sth->execute( $name, $branch_limit ? $branch_limit : () );
+ my $CGISort;
+ if ($sth->rows>0){
+ my @values;
+ my %labels;
+
+ for (my $i =0;$i<$sth->rows;$i++){
+ my $results = $sth->fetchrow_hashref;
+ push @values, $results->{authorised_value};
+ $labels{$results->{authorised_value}}=$results->{lib};
+ }
+ $CGISort= CGI::scrolling_list(
+ -name => $input_name,
+ -id => $input_name,
+ -values => \@values,
+ -labels => \%labels,
+ -default=> $data,
+ -size => 1,
+ -multiple => 0);
+ }
+ $sth->finish;
+ return $CGISort;