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