bug: 2269 - adding a perltidyrc file
[koha.git] / edithelp.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Output;
22 use C4::Auth;
23 use CGI;
24
25 use vars qw($debug);
26
27 BEGIN {
28         $debug = $ENV{DEBUG} || 0;
29 }
30
31 our $input = new CGI;
32
33 my $type    = $input->param('type');
34 my $referer = $input->param('referer');
35 my $oldreferer = $referer;
36 my $help    = $input->param('help');
37 my $error;
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "help/edithelp.tmpl",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => {
46             catalogue        => 1,
47             circulate        => 1,
48             parameters       => 1,
49             borrowers        => 1,
50             permissions      => 1,
51             reserveforothers => 1,
52             borrow           => 1,
53             reserveforself   => 1,
54             editcatalogue    => 1,
55             updatecharges    => 1,
56         },
57         debug => 1,
58     }
59 );
60
61 sub _get_filepath ($;$) {
62     my $referer = shift;
63     $referer =~ /.*koha\/(.+)\.pl.*/;
64     my $from   = "help/$1.tmpl";
65     my $htdocs = C4::Context->config('intrahtdocs');
66         my ($theme, $lang);
67         # This split behavior was part of the old script.  I'm not sure why.  -atz
68         if (@_) {
69                 ($theme, $lang) = themelanguage( $htdocs, $from, "intranet", $input );
70         } else {
71                 $theme = C4::Context->preference('template');
72                 $lang  = C4::Context->preference('language') || 'en';
73         }
74         $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
75         return "$htdocs/$theme/$lang/modules/$from";
76 }
77
78 if ( $type eq 'addnew' ) {
79     $type = 'create';
80 }
81 elsif ( $type eq 'create' || $type eq 'save' ) {
82         my $file = _get_filepath($referer);
83         if (! -w $file) {
84                 $error = "Cannot write file: '$file'";
85     } else {
86         open (OUTFILE, ">$file") or die "Cannot write file: '$file'";   # unlikely death, since we just checked
87         # file is open write to it
88         print OUTFILE "<!-- TMPL_INCLUDE NAME=\"help-top.inc\" -->\n";
89                 print OUTFILE ($type eq 'create') ? "<div class=\"main\">\n$help\n</div>" : $help;
90         print OUTFILE "\n<!-- TMPL_INCLUDE NAME=\"help-bottom.inc\" -->\n";
91         close OUTFILE;
92                 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
93     }
94 }
95 elsif ( $type eq 'modify' ) {
96     # open file load data, kill include calls, pass data to the template
97         my $file = _get_filepath($referer, 1);  # 2nd argument triggers themelanguage call
98         if (! -r $file) {
99                 $error = "Cannot read file: '$file'.";
100         } else {
101                 (-w $file) or $error = 
102                         "WARNING: You will not be able save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
103         open (INFILE, $file) or die "Cannot read file '$file'";         # unlikely death, since we just checked
104                 my $help = '';
105                 while ( my $inp = <INFILE> ) {
106                         unless ( $inp =~ /TMPL\_INCLUDE/ ) {
107                                 $help .= $inp;
108                         }
109                 }
110                 close INFILE;
111         $template->param( 'help' => $help );
112                 $type = 'save';
113         }
114 }
115
116 $template->param(
117     'referer' => $referer,
118     'type'    => $type,
119 );
120 ($error) and $template->param('error' => $error);
121 output_html_with_http_headers $input, "", $template->output;