Adding ESCAPE=JS to TMPL_Tokenizer
[koha.git] / suggestion / suggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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
17
18 use strict;
19 require Exporter;
20 use CGI;
21 use C4::Auth;    # get_template_and_user
22 use C4::Output;
23 use C4::Suggestions;
24 use C4::Koha; #GetItemTypes
25 use C4::Branch;
26 use C4::Bookfund;
27 use C4::Search;
28 use C4::Dates qw(format_date);
29 use C4::Members;
30 use Data::Dumper;
31
32 sub Init{
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}) 
38                               );
39     }               
40     $suggestion->{'acceptedon'}=(($suggestion->{'acceptedon'} eq "0000-00-00" ||$suggestion->{'acceptedon'} eq "")?
41                                 "":
42                                 format_date($suggestion->{'acceptedon'}) 
43                               );
44     $suggestion->{'managedby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'managedby'});
45     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
46 }
47
48 sub GetCriteriumDesc{
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};
58     }  
59 }
60
61 my $input           = CGI->new;
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');
70
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'));
76 }
77 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
78         {
79             template_name   => "suggestion/suggestion.tmpl",
80             query           => $input,
81             type            => "intranet",
82             flagsrequired   => { catalogue => 1 },
83         }
84     );
85
86 #########################################
87 ##  Operations
88 ##
89 if ($op =~/save/i){
90     if ($$suggestion_ref{'suggestionid'}>0){
91       &ModSuggestion($suggestion_ref);
92     }  
93     else {
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        
99             } 
100             else {    
101                 ## Adding some informations related to suggestion
102                 &NewSuggestion($suggestion_ref);
103         }
104         # empty fields, to avoid filter in "SearchSuggestion"
105     }  
106     map{$$suggestion_ref{$_}=''} keys %$suggestion_ref;
107     $op = 'else';
108 }
109 elsif ($op=~/add/) {
110     #Adds suggestion  
111     Init($suggestion_ref);
112     $op ='save';
113
114 elsif ($op=~/edit/) {
115     #Edit suggestion  
116     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
117     Init($suggestion_ref);
118     $op ='save';
119 }  
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};
125         }
126         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
127                 if ( $reason eq "other" ) {
128                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
129                 }
130                 $$suggestion_ref{'reason'}=$reason;
131         }
132         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
133         foreach (keys %$suggestion_ref){
134                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
135         }
136     foreach my $suggestionid (@editsuggestions) {
137                 next unless $suggestionid;
138                 $$suggestion_ref{'suggestionid'}=$suggestionid;
139         &ModSuggestion($suggestion_ref);
140     }
141     $op = 'else';
142 }elsif ($op eq "delete" ) {
143     foreach my $delete_field (@editsuggestions) {
144         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
145     }
146     $op = 'else';
147 }
148 if ($op=~/else/) {
149     $op='else';
150     
151         $displayby||="STATUS";
152     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
153     my @allsuggestions;
154     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
155         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
156         
157         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
158         $$suggestion_ref{$displayby}=$criteriumvalue;
159         warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
160     
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}) ;
166                 } else {
167                 $_->{$date}="" ;
168                 }             
169             }    
170         }
171         push @allsuggestions,{
172                             "suggestiontype"=>$criteriumvalue||"suggest",
173                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
174                             "suggestionscount"=>scalar(@$suggestions),             
175                             'suggestions_loop'=>$suggestions,
176                                                         };
177                 
178         delete $$suggestion_ref{$displayby} unless $definedvalue;
179     }
180     my $reasonsloop = GetAuthorisedValues("SUGGEST");
181     $template->param(
182         "displayby"=> $displayby,        
183         "notabs"=> $displayby eq "",        
184         suggestions       => \@allsuggestions,
185         reasonsloop       => $reasonsloop,
186     );
187 }
188
189 $template->param(
190     %$suggestion_ref,  
191     "op_$op"                => 1,
192     dateformat    => C4::Context->preference("dateformat"),
193     "op"             =>$op,
194 );
195
196
197 ####################
198 ## Initializing selection lists
199
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);
204
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);
211 my @branchloop;
212
213 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
214      my %row = (
215         value      => $thisbranch,
216         branchname => $branches->{$thisbranch}->{'branchname'},
217         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
218                       ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
219     );
220     push @branchloop, \%row;
221 }
222 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
223
224 $template->param( branchloop => \@branchloop,
225                   branchfilter => $branchfilter);
226
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'} );
233         }
234         else {
235            delete $$support{'imageurl'}
236         }
237 }
238 $template->param(itemtypeloop=>$supportlist);
239
240 my %hashlists; 
241 foreach my $field qw(managedby acceptedby suggestedby STATUS){
242     my $values_list;
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}
248                                                 }
249                                       } @$values_list;
250     $hashlists{lc($field)."_loop"}=\@codes_list;
251 }
252 $template->param(%hashlists);
253
254 output_html_with_http_headers $input, $cookie, $template->output;