Bug 11810: Input fields in OPAC suggestion form are a bit short (Bootstrap)
[koha.git] / suggestion / suggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # Copyright 2006-2010 BibLibre
5
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 require Exporter;
23 use CGI;
24 use C4::Auth;    # get_template_and_user
25 use C4::Output;
26 use C4::Suggestions;
27 use C4::Koha; #GetItemTypes
28 use C4::Branch;
29 use C4::Budgets;
30 use C4::Search;
31 use C4::Dates qw(format_date);
32 use C4::Members;
33 use C4::Debug;
34 use URI::Escape;
35
36 sub Init{
37     my $suggestion= shift @_;
38     # "Managed by" is used only when a suggestion is being edited (not when created)
39     if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
40         # new suggestion
41         $suggestion->{'suggesteddate'} = C4::Dates->today;
42         $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
43     }
44     else {
45         # editing of an existing suggestion
46         $suggestion->{'manageddate'} = C4::Dates->today;
47         $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
48         # suggesteddate, when coming from the DB, needs to be formated
49         $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'});
50     }
51     foreach my $date ( qw(rejecteddate accepteddate) ){
52     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
53                                 "":
54                                 format_date($suggestion->{$date}) 
55                               );
56     }
57     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
58 }
59
60 sub GetCriteriumDesc{
61     my ($criteriumvalue,$displayby)=@_;
62     unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED) ) {
63         return GetAuthorisedValueByCode('SUGGEST_STATUS', $criteriumvalue ) || "Unknown";
64     }
65     return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
66     return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
67     return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
68     if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
69         my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
70         return "" unless $borr;
71         return $$borr{surname} . ", " . $$borr{firstname};
72     }
73     if ( $displayby =~ /budgetid/) {
74         my $budget = GetBudget($criteriumvalue);
75         return "" unless $budget;
76         return $$budget{budget_name};
77     }
78 }
79
80 my $input           = CGI->new;
81 my $redirect  = $input->param('redirect');
82 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
83 my $op              = $input->param('op')||'else';
84 my @editsuggestions = $input->param('edit_field');
85 my $suggestedby     = $input->param('suggestedby');
86 my $returnsuggestedby = $input->param('returnsuggestedby');
87 my $returnsuggested = $input->param('returnsuggested');
88 my $managedby       = $input->param('managedby');
89 my $displayby       = $input->param('displayby') || '';
90 my $tabcode         = $input->param('tabcode');
91
92 # filter informations which are not suggestion related.
93 my $suggestion_ref  = $input->Vars;
94
95 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
96 foreach (keys %$suggestion_ref){
97     delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
98 }
99 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
100         {
101             template_name   => "suggestion/suggestion.tmpl",
102             query           => $input,
103             type            => "intranet",
104             flagsrequired   => { catalogue => 1 },
105         }
106     );
107
108 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
109 $template->param('borrowernumber' => $borrowernumber);
110
111 #########################################
112 ##  Operations
113 ##
114 if ( $op =~ /save/i ) {
115         if ( $$suggestion_ref{"STATUS"} ) {
116         if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
117             $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today;
118             $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
119         }
120         $$suggestion_ref{"manageddate"} = C4::Dates->today;
121         $$suggestion_ref{"managedby"}   = C4::Context->userenv->{number};
122     }
123     if ( $$suggestion_ref{'suggestionid'} > 0 ) {
124         &ModSuggestion($suggestion_ref);
125     } else {
126         ###FIXME:Search here if suggestion already exists.
127         my $suggestions_loop =
128             SearchSuggestion( $suggestion_ref );
129         if (@$suggestions_loop>=1){
130             #some suggestion are answering the request Donot Add
131         } 
132         else {    
133             ## Adding some informations related to suggestion
134             &NewSuggestion($suggestion_ref);
135         }
136         # empty fields, to avoid filter in "SearchSuggestion"
137     }  
138     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
139     $op = 'else';
140
141     if( $redirect eq 'purchase_suggestions' ) {
142         print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
143     }
144
145 }
146 elsif ($op=~/add/) {
147     #Adds suggestion  
148     Init($suggestion_ref);
149     $op ='save';
150
151 elsif ($op=~/edit/) {
152     #Edit suggestion  
153     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
154     Init($suggestion_ref);
155     $op ='save';
156 }  
157 elsif ($op eq "change" ) {
158     # set accepted/rejected/managed informations if applicable
159     # ie= if the librarian has choosen some action on the suggestions
160     if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
161         $$suggestion_ref{"accepteddate"}=C4::Dates->today;
162         $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number};
163     } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
164         $$suggestion_ref{"rejecteddate"}=C4::Dates->today;
165         $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number};
166     }
167     if ($$suggestion_ref{"STATUS"}){
168         $$suggestion_ref{"manageddate"}=C4::Dates->today;
169         $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
170     }
171     if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
172         if ( $reason eq "other" ) {
173                 $reason = $$suggestion_ref{"other_reason$tabcode"};
174         }
175         $$suggestion_ref{'reason'}=$reason;
176     }
177     delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
178      foreach (keys %$suggestion_ref){
179         delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
180     }
181     foreach my $suggestionid (@editsuggestions) {
182         next unless $suggestionid;
183         $$suggestion_ref{'suggestionid'}=$suggestionid;
184         &ModSuggestion($suggestion_ref);
185     }
186     my $params = '';
187     foreach my $key (
188         qw(
189         displayby branchcode title author isbn publishercode copyrightdate
190         collectiontitle suggestedby suggesteddate_from suggesteddate_to
191         manageddate_from manageddate_to accepteddate_from
192         accepteddate_to budgetid
193         )
194       )
195     {
196         $params .= $key . '=' . uri_escape($input->param($key)) . '&'
197           if defined($input->param($key));
198     }
199     print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
200 }elsif ($op eq "delete" ) {
201     foreach my $delete_field (@editsuggestions) {
202         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
203     }
204     $op = 'else';
205 }
206 elsif ( $op eq 'show' ) {
207     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
208     $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode};
209     my $budget = GetBudget $$suggestion_ref{budgetid};
210     $$suggestion_ref{budgetname} = $$budget{budget_name};
211     Init($suggestion_ref);
212 }
213 if ($op=~/else/) {
214     $op='else';
215     
216     $displayby||="STATUS";
217     delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
218     # distinct values of display by
219     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
220     my (@criteria_dv, $criteria_has_empty);
221     foreach (@$criteria_list) {
222         if ($_->{value}) {
223             push @criteria_dv, $_->{value};
224         } else {
225             $criteria_has_empty = 1;
226         }
227     }
228     # agregate null and empty values under empty value
229     push @criteria_dv, '' if $criteria_has_empty;
230
231     my @allsuggestions;
232     my $reasonsloop = GetAuthorisedValues("SUGGEST");
233     foreach my $criteriumvalue ( @criteria_dv ) {
234         # By default, display suggestions from current working branch
235         unless ( exists $$suggestion_ref{'branchcode'} ) {
236             $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
237         }
238         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
239
240         next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
241         $$suggestion_ref{$displayby} = $criteriumvalue;
242
243         my $suggestions = &SearchSuggestion($suggestion_ref);
244         foreach my $suggestion (@$suggestions) {
245             if ($suggestion->{budgetid}){
246                 my $bud = GetBudget( $suggestion->{budgetid} );
247                 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
248             }
249             foreach my $date (qw(suggesteddate manageddate accepteddate)) {
250                 if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") {
251                     $suggestion->{$date} = format_date( $suggestion->{$date} );
252                 } else {
253                     $suggestion->{$date} = "";
254                 }
255             }
256         }
257         push @allsuggestions,{
258                             "suggestiontype"=>$criteriumvalue||"suggest",
259                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
260                             "suggestionscount"=>scalar(@$suggestions),             
261                             'suggestions_loop'=>$suggestions,
262                             'reasonsloop'     => $reasonsloop,
263                             };
264
265         delete $$suggestion_ref{$displayby} unless $definedvalue;
266     }
267
268     $template->param(
269         "displayby"=> $displayby,
270         "notabs"=> $displayby eq "",
271         suggestions       => \@allsuggestions,
272     );
273 }
274
275 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
276 #    $debug || warn $$suggestion_ref{$element};
277     if ($$suggestion_ref{$element}){
278         my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
279         $template->param(
280             $element."_borrowernumber"=>$$member{borrowernumber},
281             $element."_firstname"=>$$member{firstname},
282             $element."_surname"=>$$member{surname},
283             $element."_branchcode"=>$$member{branchcode},
284             $element."_description"=>$$member{description},
285             $element."_category_type"=>$$member{category_type}
286         );
287     }
288 }
289 $template->param(
290     %$suggestion_ref,  
291     "op_$op"                => 1,
292     "op"             =>$op,
293 );
294
295 if(defined($returnsuggested) and $returnsuggested ne "noone")
296 {
297     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
298 }
299
300 ####################
301 ## Initializing selection lists
302
303 #branch display management
304 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : '';
305 my $onlymine =
306      C4::Context->preference('IndependentBranches')
307   && C4::Context->userenv
308   && !C4::Context->IsSuperLibrarian()
309   && C4::Context->userenv->{branch};
310 my $branches = GetBranches($onlymine);
311 my @branchloop;
312
313 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
314     my %row = (
315         value      => $thisbranch,
316         branchname => $branches->{$thisbranch}->{'branchname'},
317         selected   => ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
318     );
319     push @branchloop, \%row;
320 }
321 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
322
323 $template->param( branchloop => \@branchloop,
324                 branchfilter => $branchfilter);
325
326 # the index parameter is different for item-level itemtypes
327 my $supportlist = GetSupportList();
328
329 foreach my $support (@$supportlist) {
330     $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
331         ? $$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
332         : 0;
333     if ( $$support{'imageurl'} ) {
334         $$support{'imageurl'} = getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
335     } else {
336         delete $$support{'imageurl'};
337     }
338 }
339 $template->param(itemtypeloop=>$supportlist);
340 $template->param( returnsuggestedby => $returnsuggestedby );
341
342 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
343 $template->param(patron_reason_loop=>$patron_reason_loop);
344
345 #Budgets management
346 my $budgets = [];
347 if ($branchfilter) {
348     my $searchbudgets = { budget_branchcode => $branchfilter };
349     $budgets = GetBudgets($searchbudgets);
350 } else {
351     $budgets = GetBudgets(undef);
352 }
353
354 my @budgets_loop;
355 foreach my $budget ( @{$budgets} ) {
356     next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
357
358     ## Please see file perltidy.ERR
359     $budget->{'selected'} = 1
360         if ($$suggestion_ref{'budgetid'}
361         && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
362
363     push @budgets_loop, $budget;
364 }
365
366 $template->param( budgetsloop => \@budgets_loop);
367 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
368
369 # get currencies and rates
370 my @rates = GetCurrencies();
371 my $count = scalar @rates;
372 my $active_currency = GetCurrency();
373 my $selected_currency;
374 if ($$suggestion_ref{'currency'}) {
375     $selected_currency = $$suggestion_ref{'currency'};
376 }
377 else {
378     $selected_currency = $active_currency->{currency};
379 }
380
381 my @loop_currency = ();
382 for ( my $i = 0 ; $i < $count ; $i++ ) {
383     my %line;
384     $line{currcode} = $rates[$i]->{'currency'};
385     $line{rate}     = $rates[$i]->{'rate'};
386     $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
387     push @loop_currency, \%line;
388 }
389 $template->param(
390     loop_currency => \@loop_currency,
391     price        => sprintf("%.2f", $$suggestion_ref{'price'}||0),
392     total            => sprintf("%.2f", $$suggestion_ref{'total'}||0),
393 );
394
395 # lists of distinct values (without empty) for filters
396 my %hashlists;
397 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
398     my $values_list;
399     $values_list = GetDistinctValues( "suggestions." . $field );
400     my @codes_list = map {
401         {   'code' => $$_{'value'},
402             'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
403             'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
404         }
405     } grep {
406         $$_{'value'}
407     } @$values_list;
408     $hashlists{ lc($field) . "_loop" } = \@codes_list;
409 }
410
411 $template->param(
412     %hashlists,
413     borrowernumber           => $input->param('borrowernumber'),
414     SuggestionStatuses       => GetAuthorisedValues('SUGGEST_STATUS'),
415 );
416 output_html_with_http_headers $input, $cookie, $template->output;