Split off koha-common.
[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 #use warnings; FIXME - Bug 2505
20 require Exporter;
21 use CGI;
22 use C4::Auth;    # get_template_and_user
23 use C4::Output;
24 use C4::Suggestions;
25 use C4::Koha; #GetItemTypes
26 use C4::Branch;
27 use C4::Budgets;
28 use C4::Search;
29 use C4::Dates qw(format_date);
30 use C4::Members;
31 use C4::Debug;
32
33 sub Init{
34     my $suggestion= shift @_;
35     foreach my $date qw(suggesteddate manageddate){
36         $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
37                                 $suggestion->{$date}=C4::Dates->today:
38                                 format_date($suggestion->{$date}) 
39                               );
40     }               
41     foreach my $date qw(rejecteddate accepteddate){
42     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
43                                 "":
44                                 format_date($suggestion->{$date}) 
45                               );
46         }
47     $suggestion->{'managedby'}=C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
48     $suggestion->{'createdby'}=C4::Context->userenv->{"number"} unless ($suggestion->{'createdby'});
49     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
50 }
51
52 sub GetCriteriumDesc{
53     my ($criteriumvalue,$displayby)=@_;
54     return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
55     return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
56     return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
57     if ($displayby =~/managedby/||$displayby =~/acceptedby/){
58         my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
59         return "" unless $borr;
60 #               warn '$borr : ',Data::Dumper::Dumper($borr);
61         return $$borr{firstname}.", ".$$borr{surname};
62     }  
63 }
64
65 my $input           = CGI->new;
66 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
67 my $op              = $input->param('op')||'else';
68 my @editsuggestions = $input->param('edit_field');
69 my $branchfilter   = $input->param('branchcode');
70 my $suggestedby    = $input->param('suggestedby');
71 my $managedby    = $input->param('managedby');
72 my $displayby    = $input->param('displayby');
73 my $tabcode    = $input->param('tabcode');
74
75 # filter informations which are not suggestion related.
76 my $suggestion_ref  = $input->Vars;
77 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
78 foreach (keys %$suggestion_ref){
79     delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
80 }
81 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
82         {
83             template_name   => "suggestion/suggestion.tmpl",
84             query           => $input,
85             type            => "intranet",
86             flagsrequired   => { catalogue => 1 },
87         }
88     );
89
90 #########################################
91 ##  Operations
92 ##
93 if ($op =~/save/i){
94     if ($$suggestion_ref{'suggestionid'}>0){
95     &ModSuggestion($suggestion_ref);
96     }  
97     else {
98         ###FIXME:Search here if suggestion already exists.
99         my $suggestions_loop =
100             SearchSuggestion( $suggestion_ref );
101         if (@$suggestions_loop>=1){
102             #some suggestion are answering the request Donot Add        
103         } 
104         else {    
105             ## Adding some informations related to suggestion
106             &NewSuggestion($suggestion_ref);
107         }
108         # empty fields, to avoid filter in "SearchSuggestion"
109     }  
110     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
111     $op = 'else';
112 }
113 elsif ($op=~/add/) {
114     #Adds suggestion  
115     Init($suggestion_ref);
116     $op ='save';
117
118 elsif ($op=~/edit/) {
119     #Edit suggestion  
120     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
121     Init($suggestion_ref);
122     $op ='save';
123 }  
124 elsif ($op eq "change" ) {
125         if ($$suggestion_ref{"STATUS"}){
126                 if (my $tmpstatus=lc($$suggestion_ref{"STATUS"}) =~/ACCEPTED|REJECTED/i){
127                         $$suggestion_ref{"$tmpstatus"."date"}=C4::Dates->today;
128                         $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
129                 }
130                 $$suggestion_ref{"manageddate"}=C4::Dates->today;
131                 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
132         }
133         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
134                 if ( $reason eq "other" ) {
135                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
136                 }
137                 $$suggestion_ref{'reason'}=$reason;
138         }
139         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
140         foreach (keys %$suggestion_ref){
141                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
142         }
143     foreach my $suggestionid (@editsuggestions) {
144         next unless $suggestionid;
145         $$suggestion_ref{'suggestionid'}=$suggestionid;
146         &ModSuggestion($suggestion_ref);
147     }
148     $op = 'else';
149 }elsif ($op eq "delete" ) {
150     foreach my $delete_field (@editsuggestions) {
151         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
152     }
153     $op = 'else';
154 }
155 if ($op=~/else/) {
156     $op='else';
157     
158     $displayby||="STATUS";
159     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
160     my @allsuggestions;
161     my $reasonsloop = GetAuthorisedValues("SUGGEST");
162     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
163         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
164         
165         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
166         $$suggestion_ref{$displayby}=$criteriumvalue;
167 #        warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
168     
169         my $suggestions = &SearchSuggestion($suggestion_ref);
170         foreach my $suggestion (@$suggestions){
171             $suggestion->{budget_name}=GetBudget($suggestion->{budgetid})->{budget_name} if $suggestion->{budgetid};
172             foreach my $date qw(suggesteddate manageddate accepteddate){
173                 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
174                 $suggestion->{$date}=format_date($suggestion->{$date}) ;
175                 } else {
176                 $suggestion->{$date}="" ;
177                 }             
178             }    
179         }
180         push @allsuggestions,{
181                             "suggestiontype"=>$criteriumvalue||"suggest",
182                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
183                             "suggestionscount"=>scalar(@$suggestions),             
184                             'suggestions_loop'=>$suggestions,
185                             'reasonsloop'     => $reasonsloop,
186                             };
187
188         delete $$suggestion_ref{$displayby} unless $definedvalue;
189     }
190
191     $template->param(
192         "displayby"=> $displayby,
193         "notabs"=> $displayby eq "",
194         suggestions       => \@allsuggestions,
195     );
196 }
197
198 foreach my $element qw(managedby suggestedby){
199 #    $debug || warn $$suggestion_ref{$element};
200     if ($$suggestion_ref{$element}){
201         my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
202         $template->param(
203             $element."_borrowernumber"=>$$member{borrowernumber},
204             $element."_firstname"=>$$member{firstname},
205             $element."_surname"=>$$member{surname},
206             $element."_branchcode"=>$$member{branchcode},
207             $element."_description"=>$$member{description},
208             $element."_category_type"=>$$member{category_type}
209         );
210     }
211 }
212 $template->param(
213     %$suggestion_ref,  
214     "op_$op"                => 1,
215     dateformat    => C4::Context->preference("dateformat"),
216     "op"             =>$op,
217 );
218
219
220 ####################
221 ## Initializing selection lists
222
223 #branch display management
224 my $onlymine=C4::Context->preference('IndependantBranches') && 
225             C4::Context->userenv && 
226             C4::Context->userenv->{flags}!=1 && 
227             C4::Context->userenv->{branch};
228 my $branches = GetBranches($onlymine);
229 my @branchloop;
230
231 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
232     my %row = (
233         value      => $thisbranch,
234         branchname => $branches->{$thisbranch}->{'branchname'},
235         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
236                     ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
237     );
238     push @branchloop, \%row;
239 }
240 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
241
242 $template->param( branchloop => \@branchloop,
243                 branchfilter => $branchfilter);
244
245 # the index parameter is different for item-level itemtypes
246 my $supportlist=GetSupportList();                               
247 foreach my $support(@$supportlist){
248     $$support{'selected'}= $$support{'code'} eq $$suggestion_ref{'itemtype'};
249     if ($$support{'imageurl'}){
250         $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
251     }
252     else {
253     delete $$support{'imageurl'}
254     }
255 }
256 $template->param(itemtypeloop=>$supportlist);
257
258 #Budgets management
259 my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter;
260 my $budgets = GetBudgets($searchbudgets);
261
262 foreach my $budget (@$budgets){
263     $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
264 };
265
266 $template->param( budgetsloop => $budgets);
267
268 my %hashlists; 
269 foreach my $field qw(managedby acceptedby suggestedby STATUS){
270     my $values_list;
271     $values_list=GetDistinctValues("suggestions.".$field) ;
272     my @codes_list = map{
273                         { 'code'=>$$_{'value'},
274                         'desc'=>GetCriteriumDesc($$_{'value'},$field),
275                         'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
276                         }
277                     } @$values_list;
278     $hashlists{lc($field)."_loop"}=\@codes_list;
279 }
280 $template->param(%hashlists);
281
282 output_html_with_http_headers $input, $cookie, $template->output;