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