For instance, the return date does not rely on the borrower expiration date. A system...
[koha-ffzg.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 my %tabsysprefs;
53 $tabsysprefs{acquisitions}="Acquisitions";
54 $tabsysprefs{gist}="Acquisitions";
55 $tabsysprefs{authoritysep}="Authorities";
56 $tabsysprefs{ISBD}="Catalogue";
57 $tabsysprefs{marc}="Catalogue";
58 $tabsysprefs{marcflavour}="Catalogue";
59 $tabsysprefs{SubscriptionHistory}="Catalogue";
60 $tabsysprefs{maxoutstanding}="Circulation";
61 $tabsysprefs{printcirculationslips}="Circulation";
62 $tabsysprefs{ReturnBeforeExpiry}="Circulation";
63 $tabsysprefs{suggestion}="Acquisitions";
64 $tabsysprefs{automembernum}="Members";
65 $tabsysprefs{noissuescharge}="Circulation";
66 $tabsysprefs{opacthemes}="OPAC";
67 $tabsysprefs{opaclanguages}="OPAC";
68 $tabsysprefs{LibraryName}="OPAC";
69 $tabsysprefs{opacstylesheet}="OPAC";
70 $tabsysprefs{BiblioDefaultView}="OPAC";
71 $tabsysprefs{hidelostitem}="OPAC";
72 $tabsysprefs{KohaAdmin}="Admin";
73 $tabsysprefs{checkdigit}="Admin";
74 $tabsysprefs{dateformat}="Admin";
75 $tabsysprefs{insecure}="Admin";
76 $tabsysprefs{ldapinfos}="Admin";
77 $tabsysprefs{ldapserver}="Admin";
78 $tabsysprefs{itemcallnumber}="Catalogue";
79 $tabsysprefs{maxreserves}="Circulation";
80 $tabsysprefs{virtualshelves}="OPAC";
81
82 sub StringSearch  {
83         my ($env,$searchstring,$type)=@_;
84         my $dbh = C4::Context->dbh;
85         $searchstring=~ s/\'/\\\'/g;
86         my @data=split(' ',$searchstring);
87         my $count=@data;
88         my @results;
89         my $cnt=0;
90         if ($type){
91                 foreach my $syspref (sort keys %tabsysprefs){
92                         if ($tabsysprefs{$syspref} eq $type){
93                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
94                                 $sth->execute($syspref);
95                                 while (my $data=$sth->fetchrow_hashref){
96                                         push(@results,$data);
97                                         $cnt ++;
98                                 }
99                                 $sth->finish;
100                         }
101                 }
102         }else {
103                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
104                 foreach my $syspref (keys %tabsysprefs){
105                         $strsth .= $dbh->quote($syspref).",";
106                 }
107                 $strsth =~ s/,$/) /;
108                 $strsth .= " order by variable";
109                 warn $strsth;
110                 my $sth=$dbh->prepare($strsth);
111                 $sth->execute();
112                 while (my $data=$sth->fetchrow_hashref){
113                         push(@results,$data);
114                         $cnt ++;
115                 }
116                 $sth->finish;
117         }
118         return ($cnt,\@results);
119 }
120
121
122 my $input = new CGI;
123 my $searchfield=$input->param('searchfield');
124 my $offset=$input->param('offset');
125 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
126
127 my ($template, $borrowernumber, $cookie)
128     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
129                              query => $input,
130                              type => "intranet",
131                              authnotrequired => 0,
132                              flagsrequired => {parameters => 1},
133                              debug => 1,
134                              });
135 my $pagesize=100;
136 my $op = $input->param('op');
137 $searchfield=~ s/\,//g;
138
139 if ($op) {
140 $template->param(script_name => $script_name,
141                                                 $op              => 1,); # we show only the TMPL_VAR names $op
142 } else {
143 $template->param(script_name => $script_name,
144                                                 else              => 1,); # we show only the TMPL_VAR names $op
145 }
146
147 if ($op eq 'update_and_reedit') {
148         foreach ($input->param) {
149         }
150         my $value='';
151         if (my $currentorder=$input->param('currentorder')) {
152                 my @currentorder=split /\|/, $currentorder;
153                 my $orderchanged=0;
154                 foreach my $param ($input->param) {
155                         if ($param=~m#up-(\d+).x#) {
156                                 my $temp=$currentorder[$1];
157                                 $currentorder[$1]=$currentorder[$1-1];
158                                 $currentorder[$1-1]=$temp;
159                                 $orderchanged=1;
160                                 last;
161                         } elsif ($param=~m#down-(\d+).x#) {
162                                 my $temp=$currentorder[$1];
163                                 $currentorder[$1]=$currentorder[$1+1];
164                                 $currentorder[$1+1]=$temp;
165                                 $orderchanged=1;
166                                 last;
167                         }
168                 }
169                 $value=join ' ', @currentorder;
170                 if ($orderchanged) {
171                         $op='add_form';
172                         $template->param(script_name => $script_name,
173                                                         $op              => 1); # we show only the TMPL_VAR names $op
174                 } else {
175                         $op='';
176                         $searchfield='';
177                         $template->param(script_name => $script_name,
178                                                         else              => 1); # we show only the TMPL_VAR names $op
179                 }
180         }
181         my $dbh = C4::Context->dbh;
182         my $query="select * from systempreferences where variable=?";
183         my $sth=$dbh->prepare($query);
184         $sth->execute($input->param('variable'));
185         if ($sth->rows) {
186                 unless (C4::Context->config('demo') eq 1) {
187                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
188                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
189                         $sth->finish;
190                 }
191     } else {
192                 unless (C4::Context->config('demo') eq 1) {
193                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
194                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
195                         $sth->finish;
196                 }
197         }
198         $sth->finish;
199
200 }
201
202 ################## ADD_FORM ##################################
203 # called by default. Used to create form to add or  modify a record
204
205 if ($op eq 'add_form') {
206         #---- if primkey exists, it's a modify action, so read values to modify...
207         my $data;
208         if ($searchfield) {
209                 my $dbh = C4::Context->dbh;
210                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
211                 $sth->execute($searchfield);
212                 $data=$sth->fetchrow_hashref;
213                 $sth->finish;
214                 $template->param(modify => 1);
215         }
216
217         my @options;
218         foreach my $option (split(/\|/, $data->{'options'})) {
219                 my $selected='0';
220                 $option eq $data->{'value'} and $selected=1;
221                 push @options, { option => $option, selected => $selected };
222         }
223         if ($data->{'type'} eq 'Choice') {
224                 $template->param('type-choice' => 1);
225         } elsif ($data->{'type'} eq 'YesNo') {
226                 $template->param('type-yesno' => 1);
227                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
228                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
229         } elsif ($data->{'type'} eq 'Integer') {
230                 $template->param('type-free' => 1);
231                 $template->param('fieldlength' => $data->{'options'});
232         } elsif ($data->{'type'} eq 'Textarea') {
233                 $template->param('type-textarea' => 1);
234                 $data->{options} =~ /(.*)\|(.*)/;
235                 $template->param('cols' => $1, 'rows' => $2);;
236         } elsif ($data->{'type'} eq 'Float') {
237                 $template->param('type-free' => 1);
238                 $template->param('fieldlength' => $data->{'options'});
239         } elsif ($data->{'type'} eq 'Themes') {
240                 $template->param('type-choice' => 1);
241                 my $type='';
242                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
243                 @options=();
244                 my $currently_selected_themes;
245                 my $counter=0;
246                 foreach my $theme (split /\s+/, $data->{'value'}) {
247                     push @options, { option => $theme, counter => $counter };
248                     $currently_selected_themes->{$theme}=1;
249                     $counter++;
250                 }
251                 foreach my $theme (getallthemes($type)) {
252                         my $selected='0';
253                         next if $currently_selected_themes->{$theme};
254                         push @options, { option => $theme, counter => $counter };
255                         $counter++;
256                 }
257         } elsif ($data->{'type'} eq 'Languages') {
258                 $template->param('type-choice' => 1);
259                 my $type='';
260                 @options=();
261                 my $currently_selected_languages;
262                 my $counter=0;
263                 foreach my $language (split /\s+/, $data->{'value'}) {
264                     next if $language eq 'images';
265                     push @options, { option => $language, counter => $counter };
266                     $currently_selected_languages->{$language}=1;
267                     $counter++;
268                 }
269                 foreach my $language (getalllanguages()) {
270                         next if $language eq 'images';
271                         my $selected='0';
272                         next if $currently_selected_languages->{$language};
273                         push @options, { option => $language, counter => $counter };
274                         $counter++;
275                 }
276         } else {
277                 $template->param('type-free' => 1);
278                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
279         }
280         $template->param(explanation => $data->{'explanation'},
281                          value => $data->{'value'},
282                          type => $data->{'type'},
283                          options => \@options,
284                          preftype => $data->{'type'},
285                          prefoptions => $data->{'options'},
286                          searchfield => $searchfield);
287
288 ################## ADD_VALIDATE ##################################
289 # called by add_form, used to insert/modify data in DB
290 } elsif ($op eq 'add_validate') {
291         my $dbh = C4::Context->dbh;
292         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
293         $sth->execute($input->param('variable'));
294         if ($sth->rows) {
295                 unless (C4::Context->config('demo') eq 1) {
296                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
297                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
298                         $sth->finish;
299                 }
300         } else {
301                 unless (C4::Context->config('demo') eq 1) {
302                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
303                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
304                         $sth->finish;
305                 }
306         }
307         $sth->finish;
308         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
309         exit;
310 ################## DELETE_CONFIRM ##################################
311 # called by default form, used to confirm deletion of data in DB
312 } elsif ($op eq 'delete_confirm') {
313         my $dbh = C4::Context->dbh;
314         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
315         $sth->execute($searchfield);
316         my $data=$sth->fetchrow_hashref;
317         $sth->finish;
318         $template->param(searchfield => $searchfield,
319                                                         Tvalue => $data->{'value'},
320                                                         );
321
322                                                                                                         # END $OP eq DELETE_CONFIRM
323 ################## DELETE_CONFIRMED ##################################
324 # called by delete_confirm, used to effectively confirm deletion of data in DB
325 } elsif ($op eq 'delete_confirmed') {
326         my $dbh = C4::Context->dbh;
327         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
328         $sth->execute($searchfield);
329         $sth->finish;
330                                                                                                         # END $OP eq DELETE_CONFIRMED
331 ################## DEFAULT ##################################
332 } else { # DEFAULT
333         #Adding tab management for system preferences
334         my $tab=$input->param('tab');
335         
336         if  ($searchfield ne '') {
337                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield</strong></p>");
338         }
339         my $env;
340         my ($count,$results)=StringSearch($env,$searchfield,$tab);
341         my $toggle=0;
342         my @loop_data = ();
343         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
344                 if ($toggle eq 0){
345                         $toggle=1;
346                 } else {
347                         $toggle=0;
348                 }
349                 my %row_data;  # get a fresh hash for the row data
350                 $row_data{variable} = $results->[$i]{'variable'};
351                 $row_data{value} = $results->[$i]{'value'};
352                 $row_data{explanation} = $results->[$i]{'explanation'};
353                 $row_data{toggle} = $toggle;
354                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
355                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
356                 push(@loop_data, \%row_data);
357         }
358         $template->param(loop => \@loop_data, $tab => 1);
359         if ($offset>0) {
360                 my $prevpage = $offset-$pagesize;
361                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
362         }
363         if ($offset+$pagesize<$count) {
364                 my $nextpage =$offset+$pagesize;
365                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
366         }
367 } #---- END $OP eq DEFAULT
368
369 output_html_with_http_headers $input, $cookie, $template->output;