3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
21 use C4::Auth; # get_template_and_user
24 use C4::Koha; #GetItemTypes
28 use C4::Dates qw(format_date);
33 my $suggestion= shift @_;
34 foreach my $date qw(createdon managedon){
35 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
36 $suggestion->{$date}=C4::Dates->today:
37 format_date($suggestion->{$date})
40 $suggestion->{'acceptedon'}=(($suggestion->{'acceptedon'} eq "0000-00-00" ||$suggestion->{'acceptedon'} eq "")?
42 format_date($suggestion->{'acceptedon'})
44 $suggestion->{'managedby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'managedby'});
45 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
49 my ($criteriumvalue,$displayby)=@_;
50 return ($criteriumvalue eq 'ASKED'?"pending":lc $criteriumvalue) if ($displayby =~/status/i);
51 return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
52 return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
53 if ($displayby =~/managedby/||$displayby =~/acceptedby/){
54 my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
55 return "" unless $borr;
56 # warn '$borr : ',Data::Dumper::Dumper($borr);
57 return $$borr{firstname}.", ".$$borr{surname};
62 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
63 my $op = $input->param('op')||'else';
64 my @editsuggestions = $input->param('edit_field');
65 my $branchfilter = $input->param('branchcode');
66 my $suggestedby = $input->param('suggestedby');
67 my $managedby = $input->param('managedby');
68 my $displayby = $input->param('displayby');
69 my $tabcode = $input->param('tabcode');
71 # filter informations which are not suggestion related.
72 my $suggestion_ref = $input->Vars;
73 delete $$suggestion_ref{$_} foreach qw<suggestedbyme op displayby tabcode edit_field>;
74 foreach (keys %$suggestion_ref){
75 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
77 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
79 template_name => "suggestion/suggestion.tmpl",
82 flagsrequired => { catalogue => 1 },
86 #########################################
90 if ($$suggestion_ref{'suggestionid'}>0){
91 &ModSuggestion($suggestion_ref);
94 ###FIXME:Search here if suggestion already exists.
95 my $suggestions_loop =
96 SearchSuggestion( $suggestion_ref, $suggestedbyme );
97 if (@$suggestions_loop>=1){
98 #some suggestion are answering the request Donot Add
101 ## Adding some informations related to suggestion
102 &NewSuggestion($suggestion_ref);
104 # empty fields, to avoid filter in "SearchSuggestion"
106 map{$$suggestion_ref{$_}=''} keys %$suggestion_ref;
111 Init($suggestion_ref);
114 elsif ($op=~/edit/) {
116 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
117 Init($suggestion_ref);
120 elsif ($op eq "change" ) {
121 if ($$suggestion_ref{"STATUS"}){
122 my $tmpstatus=($$suggestion_ref{"STATUS"} eq "ACCEPTED"?"accepted":"managed");
123 $$suggestion_ref{"$tmpstatus"."on"}=C4::Dates->today;
124 $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
126 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
127 if ( $reason eq "other" ) {
128 $reason = $$suggestion_ref{"other_reason$tabcode"};
130 $$suggestion_ref{'reason'}=$reason;
132 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
133 foreach (keys %$suggestion_ref){
134 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
136 foreach my $suggestionid (@editsuggestions) {
137 next unless $suggestionid;
138 $$suggestion_ref{'suggestionid'}=$suggestionid;
139 &ModSuggestion($suggestion_ref);
142 }elsif ($op eq "delete" ) {
143 foreach my $delete_field (@editsuggestions) {
144 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
151 $displayby||="STATUS";
152 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
154 foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
155 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
157 next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
158 $$suggestion_ref{$displayby}=$criteriumvalue;
159 warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
161 my $suggestions = &SearchSuggestion($suggestion_ref);
162 foreach (@$suggestions){
163 foreach my $date qw(createdon managedon acceptedon){
164 if ($_->{$date} ne "0000-00-00" && $_->{$date} ne "" ){
165 $_->{$date}=format_date($_->{$date}) ;
171 push @allsuggestions,{
172 "suggestiontype"=>$criteriumvalue||"suggest",
173 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
174 "suggestionscount"=>scalar(@$suggestions),
175 'suggestions_loop'=>$suggestions,
178 delete $$suggestion_ref{$displayby} unless $definedvalue;
180 my $reasonsloop = GetAuthorisedValues("SUGGEST");
182 "displayby"=> $displayby,
183 "notabs"=> $displayby eq "",
184 suggestions => \@allsuggestions,
185 reasonsloop => $reasonsloop,
192 dateformat => C4::Context->preference("dateformat"),
198 ## Initializing selection lists
200 #branch display management
201 my @bookfunds = GetBookFunds($branchfilter||'');
202 map{$_->{'selected'}=1 if ($$suggestion_ref{'bookfundid'} && $_->{'bookfundid'} eq $$suggestion_ref{'bookfundid'})} @bookfunds;
203 $template->param( bookfundsloop => \@bookfunds);
205 #branch display management
206 my $onlymine=C4::Context->preference('IndependantBranches') &&
207 C4::Context->userenv &&
208 C4::Context->userenv->{flags}!=1 &&
209 C4::Context->userenv->{branch};
210 my $branches = GetBranches($onlymine);
213 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
215 value => $thisbranch,
216 branchname => $branches->{$thisbranch}->{'branchname'},
217 selected => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
218 ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})
220 push @branchloop, \%row;
222 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
224 $template->param( branchloop => \@branchloop,
225 branchfilter => $branchfilter);
227 # the index parameter is different for item-level itemtypes
228 my $supportlist=GetSupportList();
229 foreach my $support(@$supportlist){
230 $$support{'selected'}= $$support{'code'} eq $$suggestion_ref{'itemtype'};
231 if ($$support{'imageurl'}){
232 $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
235 delete $$support{'imageurl'}
238 $template->param(itemtypeloop=>$supportlist);
241 foreach my $field qw(managedby acceptedby suggestedby STATUS){
243 $values_list=GetDistinctValues("suggestions.".$field) ;
244 my @codes_list = map{
245 { 'code'=>$$_{'value'},
246 'desc'=>GetCriteriumDesc($$_{'value'},$field),
247 'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
250 $hashlists{lc($field)."_loop"}=\@codes_list;
252 $template->param(%hashlists);
254 output_html_with_http_headers $input, $cookie, $template->output;