classification sources part 1 - system preferences
[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
73 # Authorities
74     $tabsysprefs{authoritysep}="Authorities";
75     $tabsysprefs{AuthDisplayHierarchy}="Authorities";
76 # Catalogue
77     $tabsysprefs{advancedMARCEditor}="Catalogue";
78     $tabsysprefs{autoBarcode}="Catalogue";
79     $tabsysprefs{hide_marc}="Catalogue";
80     $tabsysprefs{IntranetBiblioDefaultView} = "Catalogue";
81     $tabsysprefs{ISBD}="Catalogue";
82     $tabsysprefs{itemcallnumber}="Catalogue";
83     $tabsysprefs{kohaspsuggest} = "Catalogue";
84     $tabsysprefs{LabelMARCView}="Catalogue";
85     $tabsysprefs{marc}="Catalogue";
86     $tabsysprefs{marcflavour}="Catalogue";
87     $tabsysprefs{serialsadditems}="Catalogue";
88     $tabsysprefs{sortbynonfiling}="Catalogue";
89     $tabsysprefs{MARCOrgCode}="Catalogue";
90     $tabsysprefs{z3950AuthorAuthFields}="Catalogue";
91     $tabsysprefs{z3950NormalizeAuthor}="Catalogue";
92     $tabsysprefs{Stemming}="Catalogue";
93     $tabsysprefs{WeightFields}="Catalogue";
94     $tabsysprefs{expandedSearchOption}="Catalogue";
95     $tabsysprefs{NoZebra}="Catalogue";
96     $tabsysprefs{NoZebraIndexes}="Catalogue";
97     $tabsysprefs{ReceiveBackIssues}="Catalogue";
98     $tabsysprefs{DefaultClassificationSource}="Catalogue";
99     
100 # Circulation
101     $tabsysprefs{maxoutstanding}="Circulation";
102     $tabsysprefs{maxreserves}="Circulation";
103     $tabsysprefs{noissuescharge}="Circulation";
104     $tabsysprefs{IssuingInProcess}="Circulation";
105     $tabsysprefs{patronimages}="Circulation";
106     $tabsysprefs{printcirculationslips}="Circulation";
107     $tabsysprefs{ReturnBeforeExpiry}="Circulation";
108     $tabsysprefs{SpecifyDueDate}="Circulation";
109     $tabsysprefs{AutomaticItemReturn}="Circulation";
110     $tabsysprefs{ReservesMaxPickUpDelay}="Circulation";
111     $tabsysprefs{TransfersMaxDaysWarning}="Circulation";
112     $tabsysprefs{useDaysMode}="Circulation";
113     $tabsysprefs{ReservesNeedReturns}="Circulation";
114     $tabsysprefs{CircAutocompl}="Circulation";
115
116 # Intranet
117     $tabsysprefs{TemplateEncoding}="Intranet";
118     $tabsysprefs{template}="Intranet";
119     $tabsysprefs{intranetstylesheet}="Intranet";
120     $tabsysprefs{IntranetNav}="Intranet";
121     $tabsysprefs{intranetcolorstylesheet}="Intranet";
122     $tabsysprefs{intranetuserjs}="Intranet";
123 # Members
124     $tabsysprefs{automembernum}="Members";
125     $tabsysprefs{checkdigit}="Members";
126     $tabsysprefs{intranetreadinghistory}="Members";
127     $tabsysprefs{NotifyBorrowerDeparture}="Members";
128     $tabsysprefs{memberofinstitution}="Members";
129     $tabsysprefs{ReadingHistory}="Members";
130     $tabsysprefs{BorrowerMandatoryField}="Members";
131     $tabsysprefs{borrowerRelationship}="Members";
132     $tabsysprefs{BorrowersTitles}="Members";    
133     $tabsysprefs{patronimages}="Members";
134     $tabsysprefs{MinPasswordLength}="Members";
135     $tabsysprefs{uppercasesurnames}="Members";
136     $tabsysprefs{NoReturnSetLost}="Members";
137     $tabsysprefs{MaxFine}="Members";
138
139 # OPAC
140     $tabsysprefs{AmazonAssocTag}="OPAC";
141     $tabsysprefs{AmazonContent}="OPAC";
142     $tabsysprefs{AmazonDevKey}="OPAC";
143     $tabsysprefs{BiblioDefaultView}="OPAC";
144     $tabsysprefs{LibraryName}="OPAC";
145     $tabsysprefs{opaccolorstylesheet}="OPAC";
146     $tabsysprefs{opaccredits}="OPAC";
147     $tabsysprefs{opaclanguages}="OPAC";
148     $tabsysprefs{opaclargeimage}="OPAC";
149     $tabsysprefs{opaclayoutstylesheet}="OPAC";
150     $tabsysprefs{OpacNav}="OPAC";
151     $tabsysprefs{opacsmallimage}="OPAC";
152     $tabsysprefs{opacstylesheet}="OPAC";
153     $tabsysprefs{opacthemes}="OPAC";
154     $tabsysprefs{opacuserjs}="OPAC";
155     $tabsysprefs{SubscriptionHistory}="OPAC";
156     $tabsysprefs{opacheader}="OPAC";
157     
158 # OPACFeatures
159     $tabsysprefs{Disable_Dictionary}="OPACFeatures";
160     $tabsysprefs{hidelostitems}="OPACFeatures";
161     $tabsysprefs{opacbookbag}="OPACFeatures";
162     $tabsysprefs{opaclanguagesdisplay}="OPACFeatures";
163     $tabsysprefs{OpacPasswordChange}="OPACFeatures";
164     $tabsysprefs{opacreadinghistory}="OPACFeatures";
165     $tabsysprefs{virtualshelves}="OPACFeatures";
166     $tabsysprefs{RequestOnOpac}="OPACFeatures";
167     $tabsysprefs{reviewson}="OPACFeatures";
168     $tabsysprefs{OpacTopissues}="OPACFeatures";
169     $tabsysprefs{OpacAuthorities}="OPACFeatures";
170     $tabsysprefs{OpacCloud}="OPACFeatures";
171     $tabsysprefs{opacuserlogin}="OPACFeatures";
172     $tabsysprefs{AnonSuggestions}="OPACFeatures";
173     $tabsysprefs{suggestion}="OPACFeatures";
174     $tabsysprefs{OpacTopissue}="OPACFeatures";
175     $tabsysprefs{OpacBrowser}="OPACFeatures";
176
177 # LOGFeatures
178     $tabsysprefs{CataloguingLog}  = "LOGFeatures";
179     $tabsysprefs{BorrowersLog}    = "LOGFeatures";
180     $tabsysprefs{SubscriptionLog} = "LOGFeatures";
181     $tabsysprefs{IssueLog}        = "LOGFeatures";
182     $tabsysprefs{ReturnLog}       = "LOGFeatures";
183     $tabsysprefs{LetterLog}       = "LOGFeatures";
184     $tabsysprefs{FinesLog}        = "LOGFeatures";
185     
186 sub StringSearch  {
187     my ($searchstring,$type)=@_;
188     my $dbh = C4::Context->dbh;
189     $searchstring=~ s/\'/\\\'/g;
190     my @data=split(' ',$searchstring);
191     my $count=@data;
192     my @results;
193     my $cnt=0;
194     if ($type){
195         foreach my $syspref (sort keys %tabsysprefs){
196             if ($tabsysprefs{$syspref} eq $type){
197                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
198                 $sth->execute($syspref);
199                 while (my $data=$sth->fetchrow_hashref){
200                     $data->{value} =~ s/</&lt;/g;
201                     $data->{value} =~ s/>/&gt;/g;
202                     $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
203                     push(@results,$data);
204                     $cnt++;
205                 }
206                 $sth->finish;
207             }
208         }
209     } else {
210         my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";
211         foreach my $syspref (keys %tabsysprefs){
212             $strsth .= $dbh->quote($syspref).",";
213         }
214         $strsth =~ s/,$/) /;
215         $strsth .= " order by variable";
216         my $sth=$dbh->prepare($strsth);
217         $sth->execute();
218         while (my $data=$sth->fetchrow_hashref){
219             $data->{value}=substr($data->{value},0,100);
220             push(@results,$data);
221             $cnt++;
222         }
223         $sth->finish;
224     }
225     return ($cnt,\@results);
226 }
227
228 my $input = new CGI;
229 my $searchfield=$input->param('searchfield');
230 my $offset=$input->param('offset');
231 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
232
233 my ($template, $borrowernumber, $cookie)
234     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
235                  query => $input,
236                  type => "intranet",
237                  authnotrequired => 0,
238                  flagsrequired => {parameters => 1},
239                  debug => 1,
240                  });
241 my $pagesize=100;
242 my $op = $input->param('op');
243 $searchfield=~ s/\,//g;
244
245 if ($op) {
246 $template->param(script_name => $script_name,
247                         $op              => 1); # we show only the TMPL_VAR names $op
248 } else {
249 $template->param(script_name => $script_name,
250                         else              => 1); # we show only the TMPL_VAR names $op
251 }
252
253 if ($op eq 'update_and_reedit') {
254     foreach ($input->param) {
255     }
256     my $value='';
257     if (my $currentorder=$input->param('currentorder')) {
258         my @currentorder=split /\|/, $currentorder;
259         my $orderchanged=0;
260         foreach my $param ($input->param) {
261             if ($param=~m#up-(\d+).x#) {
262                 my $temp=$currentorder[$1];
263                 $currentorder[$1]=$currentorder[$1-1];
264                 $currentorder[$1-1]=$temp;
265                 $orderchanged=1;
266                 last;
267             } elsif ($param=~m#down-(\d+).x#) {
268                 my $temp=$currentorder[$1];
269                 $currentorder[$1]=$currentorder[$1+1];
270                 $currentorder[$1+1]=$temp;
271                 $orderchanged=1;
272                 last;
273             }
274         }
275         $value=join ' ', @currentorder;
276         if ($orderchanged) {
277             $op='add_form';
278             $template->param(script_name => $script_name,
279                             $op              => 1); # we show only the TMPL_VAR names $op
280         } else {
281             $op='';
282             $searchfield='';
283             $template->param(script_name => $script_name,
284                             else              => 1); # we show only the TMPL_VAR names $op
285         }
286     }
287     my $dbh = C4::Context->dbh;
288     my $query="select * from systempreferences where variable=?";
289     my $sth=$dbh->prepare($query);
290     $sth->execute($input->param('variable'));
291     if ($sth->rows) {
292         unless (C4::Context->config('demo') eq 1) {
293             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
294             $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
295             $sth->finish;
296         }
297     } else {
298         unless (C4::Context->config('demo') eq 1) {
299             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
300             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
301             $sth->finish;
302         }
303     }
304     $sth->finish;
305
306 }
307
308 ################## ADD_FORM ##################################
309 # called by default. Used to create form to add or  modify a record
310
311 if ($op eq 'add_form') {
312     #---- if primkey exists, it's a modify action, so read values to modify...
313     my $data;
314     if ($searchfield) {
315         my $dbh = C4::Context->dbh;
316         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
317         $sth->execute($searchfield);
318         $data=$sth->fetchrow_hashref;
319         $sth->finish;
320         $template->param(modify => 1);
321     }
322
323     my @options;
324     foreach my $option (split(/\|/, $data->{'options'})) {
325         my $selected='0';
326         $option eq $data->{'value'} and $selected=1;
327         push @options, { option => $option, selected => $selected };
328     }
329     if ($data->{'type'} eq 'Choice') {
330         $template->param('type-choice' => 1);
331     } elsif ($data->{'type'} eq 'YesNo') {
332         $template->param('type-yesno' => 1);
333         $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
334         ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
335     } elsif ($data->{'type'} eq 'Integer') {
336         $template->param('type-free' => 1);
337         $template->param('fieldlength' => $data->{'options'});
338     } elsif ($data->{'type'} eq 'Textarea') {
339         $template->param('type-textarea' => 1);
340         $data->{options} =~ /(.*)\|(.*)/;
341         $template->param('cols' => $1, 'rows' => $2);;
342     } elsif ($data->{'type'} eq 'Float') {
343         $template->param('type-free' => 1);
344         $template->param('fieldlength' => $data->{'options'});
345     } elsif ($data->{'type'} eq 'Themes') {
346         $template->param('type-choice' => 1);
347         my $type='';
348         ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
349         @options=();
350         my $currently_selected_themes;
351         my $counter=0;
352         foreach my $theme (split /\s+/, $data->{'value'}) {
353             push @options, { option => $theme, counter => $counter };
354             $currently_selected_themes->{$theme}=1;
355             $counter++;
356         }
357         foreach my $theme (getallthemes($type)) {
358             my $selected='0';
359             next if $currently_selected_themes->{$theme};
360             push @options, { option => $theme, counter => $counter };
361             $counter++;
362         }
363     } elsif ($data->{'type'} eq 'ClassSource') {
364         $template->param('type-choice' => 1);
365         my $type='';
366         @options=();
367         my $sources = GetClassSources();
368         my $counter=0;
369         foreach my $cn_source (sort keys %$sources) {
370             if ($cn_source eq $data->{'value'}) {
371                 push @options, { option => $cn_source, counter => $counter, selected => 1 };
372             } else {
373                 push @options, { option => $cn_source, counter => $counter };
374             }
375             $counter++; 
376         }
377     } elsif ($data->{'type'} eq 'Languages') {
378         $template->param('type-choice' => 1);
379         my $type='';
380         @options=();
381         my $currently_selected_languages;
382         my $counter=0;
383         foreach my $language (split /\s+/, $data->{'value'}) {
384             next if $language eq 'images';
385             push @options, { option => $language, counter => $counter };
386             $currently_selected_languages->{$language}=1;
387             $counter++;
388         }
389                 my $langavail = getTranslatedLanguages();
390         foreach my $language (@$langavail) {
391             my $selected='0';
392             next if $currently_selected_languages->{$language->{'language_code'}};
393                         #FIXME: could add language_name and language_locale_name for better display
394             push @options, { option => $language->{'language_code'}, counter => $counter };
395             $counter++;
396         }
397     } else {
398         $template->param('type-free' => 1);
399         $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
400     }
401     $template->param(explanation => $data->{'explanation'},
402              value => $data->{'value'},
403              type => $data->{'type'},
404              options => \@options,
405              preftype => $data->{'type'},
406              prefoptions => $data->{'options'},
407              searchfield => $searchfield);
408
409 ################## ADD_VALIDATE ##################################
410 # called by add_form, used to insert/modify data in DB
411 } elsif ($op eq 'add_validate') {
412     my $dbh = C4::Context->dbh;
413     my $sth=$dbh->prepare("select * from systempreferences where variable=?");
414     $sth->execute($input->param('variable'));
415     if ($sth->rows) {
416         unless (C4::Context->config('demo') eq 1) {
417             my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
418             $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
419             $sth->finish;
420         }
421     } else {
422         unless (C4::Context->config('demo') eq 1) {
423             my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
424             $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
425             $sth->finish;
426         }
427     }
428     $sth->finish;
429     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
430     exit;
431 ################## DELETE_CONFIRM ##################################
432 # called by default form, used to confirm deletion of data in DB
433 } elsif ($op eq 'delete_confirm') {
434     my $dbh = C4::Context->dbh;
435     my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
436     $sth->execute($searchfield);
437     my $data=$sth->fetchrow_hashref;
438     $sth->finish;
439     $template->param(searchfield => $searchfield,
440                             Tvalue => $data->{'value'},
441                             );
442
443                                                     # END $OP eq DELETE_CONFIRM
444 ################## DELETE_CONFIRMED ##################################
445 # called by delete_confirm, used to effectively confirm deletion of data in DB
446 } elsif ($op eq 'delete_confirmed') {
447     my $dbh = C4::Context->dbh;
448     my $sth=$dbh->prepare("delete from systempreferences where variable=?");
449     $sth->execute($searchfield);
450     $sth->finish;
451                                                     # END $OP eq DELETE_CONFIRMED
452 ################## DEFAULT ##################################
453 } else { # DEFAULT
454     #Adding tab management for system preferences
455     my $tab=$input->param('tab');
456     
457     my ($count,$results)=StringSearch($searchfield,$tab);
458     my $toggle=0;
459     my @loop_data = ();
460     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
461           if ($toggle eq 0){
462             $toggle=1;
463           } else {
464             $toggle=0;
465           }
466         my %row_data;  # get a fresh hash for the row data
467         $row_data{variable} = $results->[$i]{'variable'};
468         $row_data{value} = $results->[$i]{'value'};
469                 $row_data{yes} = 1 if ($results->[$i]{'value'} == 1);
470                 $row_data{yesno} = 1 if ($results->[$i]{'type'} eq 'YesNo');
471         $row_data{explanation} = $results->[$i]{'explanation'};
472         $row_data{toggle} = $toggle;
473         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
474         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
475         push(@loop_data, \%row_data);
476     }
477     $tab=($tab?$tab:"Others");
478     $template->param(loop => \@loop_data, $tab => 1);
479     if ($offset>0) {
480         my $prevpage = $offset-$pagesize;
481         $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
482     }
483     if ($offset+$pagesize<$count) {
484         my $nextpage =$offset+$pagesize;
485         $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
486     }
487     $template->param(        tab => $tab,
488             );
489 } #---- END $OP eq DEFAULT
490 output_html_with_http_headers $input, $cookie, $template->output;