undeclared @issued_itemtypes_count_loop;
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 #script to administer the systempref table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 systempreferences.pl
25
26 ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29     - the default screen is build (with all records, or filtered datas).
30     - the   user can clic on add, modify or delete record.
31  if $op=add_form
32     - if primkey exists, this is a modification,so we read the $primkey record
33     - builds the add/modify form
34  if $op=add_validate
35     - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37     - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39     - we delete the record having primkey=$primkey
40
41 =cut
42
43 use strict;
44 use CGI;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Koha;
48 use C4::Languages;
49 use C4::ClassSource;
50 use C4::Output;
51 use C4::Context;
52
53
54 # FIXME, shouldnt we store this stuff in the systempreferences table? 
55
56 my %tabsysprefs;
57 # Acquisitions
58     $tabsysprefs{acquisitions}="Acquisitions";
59     $tabsysprefs{gist}="Acquisitions";
60 # Admin
61     $tabsysprefs{dateformat}="Admin";
62     $tabsysprefs{delimiter}="Admin";
63     $tabsysprefs{IndependantBranches}="Admin";
64     $tabsysprefs{insecure}="Admin";
65     $tabsysprefs{KohaAdmin}="Admin";
66     $tabsysprefs{KohaAdminEmailAddress}="Admin";
67     $tabsysprefs{MIME}="Admin";
68     $tabsysprefs{timeout}="Admin";
69     $tabsysprefs{Intranet_includes}="Admin";
70     $tabsysprefs{AutoLocation}="Admin";
71     $tabsysprefs{DebugLevel}="Admin";
72     $tabsysprefs{SessionStorage}="Admin";
73
74 # Authorities
75     $tabsysprefs{authoritysep}="Authorities";
76     $tabsysprefs{AuthDisplayHierarchy}="Authorities";
77         $tabsysprefs{dontmerge}="Authorities";
78         $tabsysprefs{BiblioAddsAuthorities}="Authorities";
79 # Catalogue
80     $tabsysprefs{advancedMARCEditor}="Catalogue";
81     $tabsysprefs{autoBarcode}="Catalogue";
82     $tabsysprefs{hide_marc}="Catalogue";
83     $tabsysprefs{IntranetBiblioDefaultView} = "Catalogue";
84     $tabsysprefs{ISBD}="Catalogue";
85     $tabsysprefs{itemcallnumber}="Catalogue";
86     $tabsysprefs{kohaspsuggest} = "Catalogue";
87     $tabsysprefs{LabelMARCView}="Catalogue";
88     $tabsysprefs{marc}="Catalogue";
89     $tabsysprefs{marcflavour}="Catalogue";
90     $tabsysprefs{serialsadditems}="Catalogue";
91     $tabsysprefs{sortbynonfiling}="Catalogue";
92     $tabsysprefs{MARCOrgCode}="Catalogue";
93     $tabsysprefs{z3950AuthorAuthFields}="Catalogue";
94     $tabsysprefs{z3950NormalizeAuthor}="Catalogue";
95     $tabsysprefs{Stemming}="Catalogue";
96     $tabsysprefs{WeightFields}="Catalogue";
97     $tabsysprefs{expandedSearchOption}="Catalogue";
98     $tabsysprefs{NoZebra}="Catalogue";
99     $tabsysprefs{NoZebraIndexes}="Catalogue";
100     $tabsysprefs{ReceiveBackIssues}="Catalogue";
101     $tabsysprefs{DefaultClassificationSource}="Catalogue";
102     $tabsysprefs{RoutingSerials}="Catalogue";
103     
104 # Circulation
105     $tabsysprefs{maxoutstanding}="Circulation";
106     $tabsysprefs{maxreserves}="Circulation";
107     $tabsysprefs{noissuescharge}="Circulation";
108     $tabsysprefs{IssuingInProcess}="Circulation";
109     $tabsysprefs{patronimages}="Circulation";
110     $tabsysprefs{printcirculationslips}="Circulation";
111     $tabsysprefs{ReturnBeforeExpiry}="Circulation";
112     $tabsysprefs{SpecifyDueDate}="Circulation";
113     $tabsysprefs{AutomaticItemReturn}="Circulation";
114     $tabsysprefs{ReservesMaxPickUpDelay}="Circulation";
115     $tabsysprefs{TransfersMaxDaysWarning}="Circulation";
116     $tabsysprefs{useDaysMode}="Circulation";
117     $tabsysprefs{ReservesNeedReturns}="Circulation";
118     $tabsysprefs{CircAutocompl}="Circulation";
119
120 # Intranet
121     $tabsysprefs{TemplateEncoding}="Intranet";
122     $tabsysprefs{template}="Intranet";
123     $tabsysprefs{intranetstylesheet}="Intranet";
124     $tabsysprefs{IntranetNav}="Intranet";
125     $tabsysprefs{intranetcolorstylesheet}="Intranet";
126     $tabsysprefs{intranetuserjs}="Intranet";
127 # Members
128     $tabsysprefs{automembernum}="Members";
129     $tabsysprefs{checkdigit}="Members";
130     $tabsysprefs{intranetreadinghistory}="Members";
131     $tabsysprefs{NotifyBorrowerDeparture}="Members";
132     $tabsysprefs{memberofinstitution}="Members";
133     $tabsysprefs{ReadingHistory}="Members";
134     $tabsysprefs{BorrowerMandatoryField}="Members";
135     $tabsysprefs{borrowerRelationship}="Members";
136     $tabsysprefs{BorrowersTitles}="Members";    
137     $tabsysprefs{patronimages}="Members";
138     $tabsysprefs{MinPasswordLength}="Members";
139     $tabsysprefs{uppercasesurnames}="Members";
140     $tabsysprefs{NoReturnSetLost}="Members";
141     $tabsysprefs{MaxFine}="Members";
142     $tabsysprefs{NotifyBorrowerDeparture}="Members";
143
144 # OPAC
145     $tabsysprefs{AmazonAssocTag}="OPAC";
146     $tabsysprefs{AmazonContent}="OPAC";
147     $tabsysprefs{AmazonDevKey}="OPAC";
148     $tabsysprefs{BiblioDefaultView}="OPAC";
149     $tabsysprefs{LibraryName}="OPAC";
150     $tabsysprefs{opaccolorstylesheet}="OPAC";
151     $tabsysprefs{opaccredits}="OPAC";
152     $tabsysprefs{opaclanguages}="OPAC";
153     $tabsysprefs{opaclargeimage}="OPAC";
154     $tabsysprefs{opaclayoutstylesheet}="OPAC";
155     $tabsysprefs{OpacNav}="OPAC";
156     $tabsysprefs{opacsmallimage}="OPAC";
157     $tabsysprefs{opacstylesheet}="OPAC";
158     $tabsysprefs{opacthemes}="OPAC";
159     $tabsysprefs{opacuserjs}="OPAC";
160     $tabsysprefs{SubscriptionHistory}="OPAC";
161     $tabsysprefs{opacheader}="OPAC";
162     
163 # OPACFeatures
164         $tabsysprefs{SearchMyLibraryFirst}="OPACFeatures";
165     $tabsysprefs{Disable_Dictionary}="OPACFeatures";
166     $tabsysprefs{hidelostitems}="OPACFeatures";
167     $tabsysprefs{opacbookbag}="OPACFeatures";
168     $tabsysprefs{opaclanguagesdisplay}="OPACFeatures";
169     $tabsysprefs{OpacPasswordChange}="OPACFeatures";
170     $tabsysprefs{opacreadinghistory}="OPACFeatures";
171     $tabsysprefs{virtualshelves}="OPACFeatures";
172     $tabsysprefs{RequestOnOpac}="OPACFeatures";
173     $tabsysprefs{reviewson}="OPACFeatures";
174     $tabsysprefs{OpacTopissues}="OPACFeatures";
175     $tabsysprefs{OpacAuthorities}="OPACFeatures";
176     $tabsysprefs{OpacCloud}="OPACFeatures";
177     $tabsysprefs{opacuserlogin}="OPACFeatures";
178     $tabsysprefs{AnonSuggestions}="OPACFeatures";
179     $tabsysprefs{suggestion}="OPACFeatures";
180     $tabsysprefs{OpacTopissue}="OPACFeatures";
181     $tabsysprefs{OpacBrowser}="OPACFeatures";
182
183 # LOGFeatures
184     $tabsysprefs{CataloguingLog}  = "LOGFeatures";
185     $tabsysprefs{BorrowersLog}    = "LOGFeatures";
186     $tabsysprefs{SubscriptionLog} = "LOGFeatures";
187     $tabsysprefs{IssueLog}        = "LOGFeatures";
188     $tabsysprefs{ReturnLog}       = "LOGFeatures";
189     $tabsysprefs{LetterLog}       = "LOGFeatures";
190     $tabsysprefs{FinesLog}        = "LOGFeatures";
191     
192 sub StringSearch  {
193     my ($searchstring,$type)=@_;
194     my $dbh = C4::Context->dbh;
195     $searchstring=~ s/\'/\\\'/g;
196     my @data=split(' ',$searchstring);
197     my $count=@data;
198     my @results;
199     my $cnt=0;
200     if ($type){
201         foreach my $syspref (sort keys %tabsysprefs){
202             if ($tabsysprefs{$syspref} eq $type){
203                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
204                 $sth->execute($syspref);
205                 while (my $data=$sth->fetchrow_hashref){
206                     $data->{value} =~ s/</&lt;/g;
207                     $data->{value} =~ s/>/&gt;/g;
208                     $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
209                     push(@results,$data);
210                     $cnt++;
211                 }
212                 $sth->finish;
213             }
214         }
215     } else {
216         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
217         foreach my $syspref (keys %tabsysprefs){
218             $strsth .= $dbh->quote($syspref).",";
219         }
220         $strsth =~ s/,$/) /;
221         $strsth .= " order by variable";
222         my $sth=$dbh->prepare($strsth);
223         $sth->execute();
224         while (my $data=$sth->fetchrow_hashref){
225             $data->{value}=substr($data->{value},0,100);
226             push(@results,$data);
227             $cnt++;
228         }
229         $sth->finish;
230     }
231     return ($cnt,\@results);
232 }
233
234 my $input = new CGI;
235 my $searchfield=$input->param('searchfield');
236 my $offset=$input->param('offset');
237 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
238
239 my ($template, $borrowernumber, $cookie)
240     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
241                  query => $input,
242                  type => "intranet",
243                  authnotrequired => 0,
244                  flagsrequired => {parameters => 1},
245                  debug => 1,
246                  });
247 my $pagesize=100;
248 my $op = $input->param('op');
249 $searchfield=~ s/\,//g;
250
251 if ($op) {
252 $template->param(script_name => $script_name,
253                         $op              => 1); # we show only the TMPL_VAR names $op
254 } else {
255 $template->param(script_name => $script_name,
256                         else              => 1); # we show only the TMPL_VAR names $op
257 }
258
259 if ($op eq 'update_and_reedit') {
260     foreach ($input->param) {
261     }
262     my $value='';
263     if (my $currentorder=$input->param('currentorder')) {
264         my @currentorder=split /\|/, $currentorder;
265         my $orderchanged=0;
266         foreach my $param ($input->param) {
267             if ($param=~m#up-(\d+).x#) {
268                 my $temp=$currentorder[$1];
269                 $currentorder[$1]=$currentorder[$1-1];
270                 $currentorder[$1-1]=$temp;
271                 $orderchanged=1;
272                 last;
273             } elsif ($param=~m#down-(\d+).x#) {
274                 my $temp=$currentorder[$1];
275                 $currentorder[$1]=$currentorder[$1+1];
276                 $currentorder[$1+1]=$temp;
277                 $orderchanged=1;
278                 last;
279             }
280         }
281         $value=join ' ', @currentorder;
282         if ($orderchanged) {
283             $op='add_form';
284             $template->param(script_name => $script_name,
285                             $op              => 1); # we show only the TMPL_VAR names $op
286         } else {
287             $op='';
288             $searchfield='';
289             $template->param(script_name => $script_name,
290                             else              => 1); # we show only the TMPL_VAR names $op
291         }
292     }
293     my $dbh = C4::Context->dbh;
294     my $query="select * from systempreferences where variable=?";
295     my $sth=$dbh->prepare($query);
296     $sth->execute($input->param('variable'));
297     if ($sth->rows) {
298         unless (C4::Context->config('demo') eq 1) {
299             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
300             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
301             $sth->finish;
302         }
303     } else {
304         unless (C4::Context->config('demo') eq 1) {
305             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
306             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
307             $sth->finish;
308         }
309     }
310     $sth->finish;
311
312 }
313
314 ################## ADD_FORM ##################################
315 # called by default. Used to create form to add or  modify a record
316
317 if ($op eq 'add_form') {
318     #---- if primkey exists, it's a modify action, so read values to modify...
319     my $data;
320     if ($searchfield) {
321         my $dbh = C4::Context->dbh;
322         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
323         $sth->execute($searchfield);
324         $data=$sth->fetchrow_hashref;
325         $sth->finish;
326         $template->param(modify => 1);
327     }
328
329     my @options;
330     foreach my $option (split(/\|/, $data->{'options'})) {
331         my $selected='0';
332         $option eq $data->{'value'} and $selected=1;
333         push @options, { option => $option, selected => $selected };
334     }
335     if ($data->{'type'} eq 'Choice') {
336         $template->param('type-choice' => 1);
337     } elsif ($data->{'type'} eq 'YesNo') {
338         $template->param('type-yesno' => 1);
339         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
340         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
341     } elsif ($data->{'type'} eq 'Integer') {
342         $template->param('type-free' => 1);
343         $template->param('fieldlength' => $data->{'options'});
344     } elsif ($data->{'type'} eq 'Textarea') {
345         $template->param('type-textarea' => 1);
346         $data->{options} =~ /(.*)\|(.*)/;
347         $template->param('cols' => $1, 'rows' => $2);;
348     } elsif ($data->{'type'} eq 'Float') {
349         $template->param('type-free' => 1);
350         $template->param('fieldlength' => $data->{'options'});
351     } elsif ($data->{'type'} eq 'Themes') {
352         $template->param('type-choice' => 1);
353         my $type='';
354         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
355         @options=();
356         my $currently_selected_themes;
357         my $counter=0;
358         foreach my $theme (split /\s+/, $data->{'value'}) {
359             push @options, { option => $theme, counter => $counter };
360             $currently_selected_themes->{$theme}=1;
361             $counter++;
362         }
363         foreach my $theme (getallthemes($type)) {
364             my $selected='0';
365             next if $currently_selected_themes->{$theme};
366             push @options, { option => $theme, counter => $counter };
367             $counter++;
368         }
369     } elsif ($data->{'type'} eq 'ClassSource') {
370         $template->param('type-choice' => 1);
371         my $type='';
372         @options=();
373         my $sources = GetClassSources();
374         my $counter=0;
375         foreach my $cn_source (sort keys %$sources) {
376             if ($cn_source eq $data->{'value'}) {
377                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
378             } else {
379                 push @options, { option => $cn_source, counter => $counter };
380             }
381             $counter++; 
382         }
383     } elsif ($data->{'type'} eq 'Languages') {
384         $template->param('type-choice' => 1);
385         my $type='';
386         @options=();
387         my $currently_selected_languages;
388         my $counter=0;
389         foreach my $language (split /\s+/, $data->{'value'}) {
390             next if $language eq 'images';
391             push @options, { option => $language, counter => $counter };
392             $currently_selected_languages->{$language}=1;
393             $counter++;
394         }
395                 my $langavail = getTranslatedLanguages();
396         foreach my $language (@$langavail) {
397             my $selected='0';
398             next if $currently_selected_languages->{$language->{'language_code'}};
399                         #FIXME: could add language_name and language_locale_name for better display
400             push @options, { option => $language->{'language_code'}, counter => $counter };
401             $counter++;
402         }
403     } else {
404         $template->param('type-free' => 1);
405         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
406     }
407     $template->param(explanation => $data->{'explanation'},
408              value => $data->{'value'},
409              type => $data->{'type'},
410              options => \@options,
411              preftype => $data->{'type'},
412              prefoptions => $data->{'options'},
413              searchfield => $searchfield);
414
415 ################## ADD_VALIDATE ##################################
416 # called by add_form, used to insert/modify data in DB
417 } elsif ($op eq 'add_validate') {
418     my $dbh = C4::Context->dbh;
419     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
420     $sth->execute($input->param('variable'));
421     if ($sth->rows) {
422         unless (C4::Context->config('demo') eq 1) {
423             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
424             $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
425             $sth->finish;
426         }
427     } else {
428         unless (C4::Context->config('demo') eq 1) {
429             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
430             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
431             $sth->finish;
432         }
433     }
434     $sth->finish;
435     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
436     exit;
437 ################## DELETE_CONFIRM ##################################
438 # called by default form, used to confirm deletion of data in DB
439 } elsif ($op eq 'delete_confirm') {
440     my $dbh = C4::Context->dbh;
441     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
442     $sth->execute($searchfield);
443     my $data=$sth->fetchrow_hashref;
444     $sth->finish;
445     $template->param(searchfield => $searchfield,
446                             Tvalue => $data->{'value'},
447                             );
448
449                                                     # END $OP eq DELETE_CONFIRM
450 ################## DELETE_CONFIRMED ##################################
451 # called by delete_confirm, used to effectively confirm deletion of data in DB
452 } elsif ($op eq 'delete_confirmed') {
453     my $dbh = C4::Context->dbh;
454     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
455     $sth->execute($searchfield);
456     $sth->finish;
457                                                     # END $OP eq DELETE_CONFIRMED
458 ################## DEFAULT ##################################
459 } else { # DEFAULT
460     #Adding tab management for system preferences
461     my $tab=$input->param('tab');
462     
463     my ($count,$results)=StringSearch($searchfield,$tab);
464     my $toggle=0;
465     my @loop_data = ();
466     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
467           if ($toggle eq 0){
468             $toggle=1;
469           } else {
470             $toggle=0;
471           }
472         my %row_data;  # get a fresh hash for the row data
473         $row_data{variable} = $results->[$i]{'variable'};
474         $row_data{value} = $results->[$i]{'value'};
475                 $row_data{yes} = 1 if ($results->[$i]{'value'} == 1);
476                 $row_data{yesno} = 1 if ($results->[$i]{'type'} eq 'YesNo');
477         $row_data{explanation} = $results->[$i]{'explanation'};
478         $row_data{toggle} = $toggle;
479         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
480         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
481         push(@loop_data, \%row_data);
482     }
483     $tab=($tab?$tab:"Others");
484     $template->param(loop => \@loop_data, $tab => 1);
485     if ($offset>0) {
486         my $prevpage = $offset-$pagesize;
487         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
488     }
489     if ($offset+$pagesize<$count) {
490         my $nextpage =$offset+$pagesize;
491         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
492     }
493     $template->param(        tab => $tab,
494             );
495 } #---- END $OP eq DEFAULT
496 output_html_with_http_headers $input, $cookie, $template->output;