Merging Katipo changes...
[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 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Koha;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use HTML::Template;
49 use C4::Context;
50
51
52 # Fix me, shouldnt we store this stuff in the systempreferences table? 
53
54 my %tabsysprefs;
55 # Acquisitions
56         $tabsysprefs{acquisitions}="Acquisitions";
57         $tabsysprefs{gist}="Acquisitions";
58 # Admin
59         $tabsysprefs{dateformat}="Admin";
60         $tabsysprefs{insecure}="Admin";
61         $tabsysprefs{KohaAdmin}="Admin";
62 # Authorities
63         $tabsysprefs{authoritysep}="Authorities";
64 # Catalogue
65         $tabsysprefs{advancedMARCEditor}="Catalogue";
66         $tabsysprefs{autoBarcode}="Catalogue";
67         $tabsysprefs{hide_marc}="Catalogue";
68         $tabsysprefs{IntranetBiblioDefaultView} = "Catalogue";
69         $tabsysprefs{ISBD}="Catalogue";
70         $tabsysprefs{itemcallnumber}="Catalogue";
71         $tabsysprefs{LabelMARCView}="Catalogue";
72         $tabsysprefs{marc}="Catalogue";
73         $tabsysprefs{marcflavour}="Catalogue";
74         $tabsysprefs{serialsadditems}="Catalogue";
75         $tabsysprefs{sortbynonfiling}="Catalogue";
76 # Circulation
77         $tabsysprefs{maxoutstanding}="Circulation";
78         $tabsysprefs{maxreserves}="Circulation";
79         $tabsysprefs{noissuescharge}="Circulation";
80         $tabsysprefs{patronimages}="Circulation";
81         $tabsysprefs{printcirculationslips}="Circulation";
82         $tabsysprefs{ReturnBeforeExpiry}="Circulation";
83 # Members
84         $tabsysprefs{automembernum}="Members";
85         $tabsysprefs{checkdigit}="Members";
86         $tabsysprefs{NotifyBorrowerDeparture}="Members";
87         $tabsysprefs{memberofinstitution}="Members";
88         $tabsysprefs{ReadingHistory}="Members";
89 # OPAC
90         $tabsysprefs{AmazonAssocTag}="OPAC";
91         $tabsysprefs{AmazonContent}="OPAC";
92         $tabsysprefs{AmazonDevKey}="OPAC";
93         $tabsysprefs{AnonSuggestions}="OPAC";
94         $tabsysprefs{BiblioDefaultView}="OPAC";
95         $tabsysprefs{Disable_Dictionary}="OPAC";
96         $tabsysprefs{hidelostitems}="OPAC";
97         $tabsysprefs{LibraryName}="OPAC";
98         $tabsysprefs{opacbookbag}="OPAC";
99         $tabsysprefs{opaccolorstylesheet}="OPAC";
100         $tabsysprefs{opaccredits}="OPAC";
101         $tabsysprefs{opaclanguages}="OPAC";
102         $tabsysprefs{opaclanguagesdisplay}="OPAC";
103         $tabsysprefs{opaclargeimage}="OPAC";
104         $tabsysprefs{opaclayoutstylesheet}="OPAC";
105         $tabsysprefs{OpacNav}="OPAC";
106         $tabsysprefs{OpacPasswordChange}="OPAC";
107         $tabsysprefs{opacreadinghistory}="OPAC";
108         $tabsysprefs{opacsmallimage}="OPAC";
109         $tabsysprefs{opacstylesheet}="OPAC";
110         $tabsysprefs{opacthemes}="OPAC";
111         $tabsysprefs{opacuserlogin}="OPAC";
112         $tabsysprefs{SubscriptionHistory}="OPAC";
113         $tabsysprefs{suggestion}="OPAC";
114         $tabsysprefs{virtualshelves}="OPAC";
115
116 sub StringSearch  {
117         my ($env,$searchstring,$type)=@_;
118         my $dbh = C4::Context->dbh;
119         $searchstring=~ s/\'/\\\'/g;
120         my @data=split(' ',$searchstring);
121         my $count=@data;
122         my @results;
123         my $cnt=0;
124         if ($type){
125                 foreach my $syspref (sort keys %tabsysprefs){
126                         if ($tabsysprefs{$syspref} eq $type){
127                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
128                                 $sth->execute($syspref);
129                                 while (my $data=$sth->fetchrow_hashref){
130                                         $data->{value} =~ s/</&lt;/g;
131                                         $data->{value} =~ s/>/&lt;/g;
132                                         $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
133                                         push(@results,$data);
134                                         $cnt++;
135                                 }
136                                 $sth->finish;
137                         }
138                 }
139         } else {
140                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
141                 foreach my $syspref (keys %tabsysprefs){
142                         $strsth .= $dbh->quote($syspref).",";
143                 }
144                 $strsth =~ s/,$/) /;
145                 $strsth .= " order by variable";
146                 #warn $strsth;
147                 my $sth=$dbh->prepare($strsth);
148                 $sth->execute();
149                 while (my $data=$sth->fetchrow_hashref){
150                         $data->{value}=substr($data->{value},0,100);
151                         push(@results,$data);
152                         $cnt++;
153                 }
154                 $sth->finish;
155         }
156         return ($cnt,\@results);
157 }
158
159
160 my $input = new CGI;
161 my $searchfield=$input->param('searchfield');
162 my $offset=$input->param('offset');
163 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
164
165 my ($template, $borrowernumber, $cookie)
166     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
167                              query => $input,
168                              type => "intranet",
169                              authnotrequired => 0,
170                              flagsrequired => {parameters => 1},
171                              debug => 1,
172                              });
173 my $pagesize=100;
174 my $op = $input->param('op');
175 $searchfield=~ s/\,//g;
176
177 if ($op) {
178 $template->param(script_name => $script_name,
179                                                 $op              => 1,); # we show only the TMPL_VAR names $op
180 } else {
181 $template->param(script_name => $script_name,
182                                                 else              => 1,); # we show only the TMPL_VAR names $op
183 }
184
185 if ($op eq 'update_and_reedit') {
186         foreach ($input->param) {
187         }
188         my $value='';
189         if (my $currentorder=$input->param('currentorder')) {
190                 my @currentorder=split /\|/, $currentorder;
191                 my $orderchanged=0;
192                 foreach my $param ($input->param) {
193                         if ($param=~m#up-(\d+).x#) {
194                                 my $temp=$currentorder[$1];
195                                 $currentorder[$1]=$currentorder[$1-1];
196                                 $currentorder[$1-1]=$temp;
197                                 $orderchanged=1;
198                                 last;
199                         } elsif ($param=~m#down-(\d+).x#) {
200                                 my $temp=$currentorder[$1];
201                                 $currentorder[$1]=$currentorder[$1+1];
202                                 $currentorder[$1+1]=$temp;
203                                 $orderchanged=1;
204                                 last;
205                         }
206                 }
207                 $value=join ' ', @currentorder;
208                 if ($orderchanged) {
209                         $op='add_form';
210                         $template->param(script_name => $script_name,
211                                                         $op              => 1); # we show only the TMPL_VAR names $op
212                 } else {
213                         $op='';
214                         $searchfield='';
215                         $template->param(script_name => $script_name,
216                                                         else              => 1); # we show only the TMPL_VAR names $op
217                 }
218         }
219         my $dbh = C4::Context->dbh;
220         my $query="select * from systempreferences where variable=?";
221         my $sth=$dbh->prepare($query);
222         $sth->execute($input->param('variable'));
223         if ($sth->rows) {
224                 unless (C4::Context->config('demo') eq 1) {
225                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
226                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
227                         $sth->finish;
228                 }
229     } else {
230                 unless (C4::Context->config('demo') eq 1) {
231                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
232                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
233                         $sth->finish;
234                 }
235         }
236         $sth->finish;
237
238 }
239
240 ################## ADD_FORM ##################################
241 # called by default. Used to create form to add or  modify a record
242
243 if ($op eq 'add_form') {
244         #---- if primkey exists, it's a modify action, so read values to modify...
245         my $data;
246         if ($searchfield) {
247                 my $dbh = C4::Context->dbh;
248                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
249                 $sth->execute($searchfield);
250                 $data=$sth->fetchrow_hashref;
251                 $sth->finish;
252                 $template->param(modify => 1);
253         }
254
255         my @options;
256         foreach my $option (split(/\|/, $data->{'options'})) {
257                 my $selected='0';
258                 $option eq $data->{'value'} and $selected=1;
259                 push @options, { option => $option, selected => $selected };
260         }
261         if ($data->{'type'} eq 'Choice') {
262                 $template->param('type-choice' => 1);
263         } elsif ($data->{'type'} eq 'YesNo') {
264                 $template->param('type-yesno' => 1);
265                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
266                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
267         } elsif ($data->{'type'} eq 'Integer') {
268                 $template->param('type-free' => 1);
269                 $template->param('fieldlength' => $data->{'options'});
270         } elsif ($data->{'type'} eq 'Textarea') {
271                 $template->param('type-textarea' => 1);
272                 $data->{options} =~ /(.*)\|(.*)/;
273                 $template->param('cols' => $1, 'rows' => $2);;
274         } elsif ($data->{'type'} eq 'Float') {
275                 $template->param('type-free' => 1);
276                 $template->param('fieldlength' => $data->{'options'});
277         } elsif ($data->{'type'} eq 'Themes') {
278                 $template->param('type-choice' => 1);
279                 my $type='';
280                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
281                 @options=();
282                 my $currently_selected_themes;
283                 my $counter=0;
284                 foreach my $theme (split /\s+/, $data->{'value'}) {
285                     push @options, { option => $theme, counter => $counter };
286                     $currently_selected_themes->{$theme}=1;
287                     $counter++;
288                 }
289                 foreach my $theme (getallthemes($type)) {
290                         my $selected='0';
291                         next if $currently_selected_themes->{$theme};
292                         push @options, { option => $theme, counter => $counter };
293                         $counter++;
294                 }
295         } elsif ($data->{'type'} eq 'Languages') {
296                 $template->param('type-choice' => 1);
297                 my $type='';
298                 @options=();
299                 my $currently_selected_languages;
300                 my $counter=0;
301                 foreach my $language (split /\s+/, $data->{'value'}) {
302                     next if $language eq 'images';
303                     push @options, { option => $language, counter => $counter };
304                     $currently_selected_languages->{$language}=1;
305                     $counter++;
306                 }
307                 foreach my $language (getalllanguages()) {
308                         next if $language eq 'images';
309                         my $selected='0';
310                         next if $currently_selected_languages->{$language};
311                         push @options, { option => $language, counter => $counter };
312                         $counter++;
313                 }
314         } else {
315                 $template->param('type-free' => 1);
316                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
317         }
318         $template->param(explanation => $data->{'explanation'},
319                          value => $data->{'value'},
320                          type => $data->{'type'},
321                          options => \@options,
322                          preftype => $data->{'type'},
323                          prefoptions => $data->{'options'},
324                          searchfield => $searchfield);
325
326 ################## ADD_VALIDATE ##################################
327 # called by add_form, used to insert/modify data in DB
328 } elsif ($op eq 'add_validate') {
329         my $dbh = C4::Context->dbh;
330         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
331         $sth->execute($input->param('variable'));
332         if ($sth->rows) {
333                 unless (C4::Context->config('demo') eq 1) {
334                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
335                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
336                         $sth->finish;
337                 }
338         } else {
339                 unless (C4::Context->config('demo') eq 1) {
340                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
341                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
342                         $sth->finish;
343                 }
344         }
345         $sth->finish;
346         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
347         exit;
348 ################## DELETE_CONFIRM ##################################
349 # called by default form, used to confirm deletion of data in DB
350 } elsif ($op eq 'delete_confirm') {
351         my $dbh = C4::Context->dbh;
352         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
353         $sth->execute($searchfield);
354         my $data=$sth->fetchrow_hashref;
355         $sth->finish;
356         $template->param(searchfield => $searchfield,
357                                                         Tvalue => $data->{'value'},
358                                                         );
359
360                                                                                                         # END $OP eq DELETE_CONFIRM
361 ################## DELETE_CONFIRMED ##################################
362 # called by delete_confirm, used to effectively confirm deletion of data in DB
363 } elsif ($op eq 'delete_confirmed') {
364         my $dbh = C4::Context->dbh;
365         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
366         $sth->execute($searchfield);
367         $sth->finish;
368                                                                                                         # END $OP eq DELETE_CONFIRMED
369 ################## DEFAULT ##################################
370 } else { # DEFAULT
371         #Adding tab management for system preferences
372         my $tab=$input->param('tab');
373         
374         my $env;
375         my ($count,$results)=StringSearch($env,$searchfield,$tab);
376         my $toggle=0;
377         my @loop_data = ();
378         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
379                 if ($toggle eq 0){
380                         $toggle=1;
381                 } else {
382                         $toggle=0;
383                 }
384                 my %row_data;  # get a fresh hash for the row data
385                 $row_data{variable} = $results->[$i]{'variable'};
386                 $row_data{value} = $results->[$i]{'value'};
387                 $row_data{explanation} = $results->[$i]{'explanation'};
388                 $row_data{toggle} = $toggle;
389                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
390                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
391                 push(@loop_data, \%row_data);
392         }
393         $tab=($tab?$tab:"Others");
394         $template->param(loop => \@loop_data, $tab => 1);
395         if ($offset>0) {
396                 my $prevpage = $offset-$pagesize;
397                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
398         }
399         if ($offset+$pagesize<$count) {
400                 my $nextpage =$offset+$pagesize;
401                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
402         }
403 } #---- END $OP eq DEFAULT
404 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
405                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
406                 IntranetNav => C4::Context->preference("IntranetNav"),
407                 );
408 output_html_with_http_headers $input, $cookie, $template->output;