Main Koha release repository https://koha-community.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

151 rader
4.4 KiB

  1. package Koha::Template::Plugin::Branches;
  2. # Copyright ByWater Solutions 2012
  3. # Copyright BibLibre 2014
  4. # This file is part of Koha.
  5. #
  6. # Koha is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Koha is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  18. use Modern::Perl;
  19. use Template::Plugin;
  20. use base qw( Template::Plugin );
  21. use C4::Koha;
  22. use C4::Context;
  23. use Koha::Libraries;
  24. sub GetName {
  25. my ( $self, $branchcode ) = @_;
  26. my $l = Koha::Libraries->find($branchcode);
  27. return $l ? $l->branchname : q{};
  28. }
  29. sub GetLoggedInBranchcode {
  30. my ($self) = @_;
  31. return C4::Context::mybranch;
  32. }
  33. sub GetLoggedInBranchname {
  34. my ($self) = @_;
  35. return C4::Context->userenv ? C4::Context->userenv->{'branchname'} : q{};
  36. }
  37. sub GetURL {
  38. my ( $self, $branchcode ) = @_;
  39. my $query = "SELECT branchurl FROM branches WHERE branchcode = ?";
  40. my $sth = C4::Context->dbh->prepare($query);
  41. $sth->execute($branchcode);
  42. my $b = $sth->fetchrow_hashref();
  43. return $b->{branchurl};
  44. }
  45. sub all {
  46. my ( $self, $params ) = @_;
  47. my $selected = $params->{selected};
  48. my $selecteds = $params->{selecteds};
  49. my $unfiltered = $params->{unfiltered} || 0;
  50. my $search_params = $params->{search_params} || {};
  51. if ( !$unfiltered ) {
  52. $search_params->{only_from_group} = $params->{only_from_group} || 0;
  53. }
  54. my $libraries = $unfiltered
  55. ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
  56. : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
  57. if (defined $selecteds) {
  58. # For a select multiple, must be a Koha::Libraries
  59. my @selected_branchcodes = $selecteds ? $selecteds->get_column( ['branchcode'] ) : ();
  60. $libraries = [ map {
  61. my $l = $_;
  62. $l->{selected} = 1
  63. if grep { $_ eq $l->{branchcode} } @selected_branchcodes;
  64. $l;
  65. } @$libraries ];
  66. }
  67. else {
  68. for my $l ( @$libraries ) {
  69. if ( defined $selected and $l->{branchcode} eq $selected
  70. or not defined $selected and C4::Context->userenv and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} )
  71. ) {
  72. $l->{selected} = 1;
  73. }
  74. }
  75. }
  76. return $libraries;
  77. }
  78. sub InIndependentBranchesMode {
  79. my ( $self ) = @_;
  80. return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
  81. }
  82. sub pickup_locations {
  83. my ( $self, $params ) = @_;
  84. my $search_params = $params->{search_params} || {};
  85. my $selected = $params->{selected};
  86. my @libraries;
  87. if(defined $search_params->{item} || defined $search_params->{biblio}) {
  88. my $item = $search_params->{'item'};
  89. my $biblio = $search_params->{'biblio'};
  90. my $patron = $search_params->{'patron'};
  91. unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
  92. $patron = Koha::Patrons->find($patron);
  93. }
  94. if ($item) {
  95. $item = Koha::Items->find($item)
  96. unless ref($item) eq 'Koha::Item';
  97. @libraries = $item->pickup_locations( { patron => $patron } )
  98. if defined $item;
  99. }
  100. elsif ($biblio) {
  101. $biblio = Koha::Biblios->find($biblio)
  102. unless ref($biblio) eq 'Koha::Biblio';
  103. @libraries = @{ $biblio->pickup_locations( { patron => $patron } ) }
  104. if defined $biblio;
  105. }
  106. }
  107. @libraries = Koha::Libraries->search( { pickup_location => 1 },
  108. { order_by => ['branchname'] } )->as_list
  109. unless @libraries;
  110. @libraries = map { $_->unblessed } @libraries;
  111. for my $l (@libraries) {
  112. if ( defined $selected and $l->{branchcode} eq $selected
  113. or not defined $selected
  114. and C4::Context->userenv
  115. and $l->{branchcode} eq C4::Context->userenv->{branch} )
  116. {
  117. $l->{selected} = 1;
  118. }
  119. }
  120. return \@libraries;
  121. }
  122. 1;