Modifying Members : Add Mod and GetMember
[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 # strip any DOS-newlines that TinyMCE may have sneaked in
38 $help =~ s/\r//g;
39 my $error;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "help/edithelp.tmpl",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => {
48             catalogue        => 1,
49             circulate        => 1,
50             parameters       => 1,
51             borrowers        => 1,
52             permissions      => 1,
53             reserveforothers => 1,
54             borrow           => 1,
55             reserveforself   => 1,
56             editcatalogue    => 1,
57             updatecharges    => 1,
58         },
59         debug => 1,
60     }
61 );
62
63 sub _get_filepath ($;$) {
64     my $referer = shift;
65     $referer =~ /.*koha\/(.+)\.pl.*/;
66     my $from   = "help/$1.tmpl";
67     my $htdocs = C4::Context->config('intrahtdocs');
68     my ($theme, $lang) = themelanguage( $htdocs, $from, "intranet", $input );
69         $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
70         return "$htdocs/$theme/$lang/modules/$from";
71 }
72
73 if ( $type eq 'addnew' ) {
74     $type = 'create';
75 }
76 elsif ( $type eq 'create' || $type eq 'save' ) {
77         my $file = _get_filepath($referer);
78         unless (open (OUTFILE, ">$file")) {$error = "Cannot write file: '$file'";} else {
79         #open (OUTFILE, ">$file") or die "Cannot write file: '$file'";  # unlikely death, since we just checked
80         # file is open write to it
81         print OUTFILE "<!-- TMPL_INCLUDE NAME=\"help-top.inc\" -->\n";
82                 print OUTFILE ($type eq 'create') ? "<div class=\"main\">\n$help\n</div>" : $help;
83         print OUTFILE "\n<!-- TMPL_INCLUDE NAME=\"help-bottom.inc\" -->\n";
84         close OUTFILE;
85                 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
86     }
87     
88 }
89 elsif ( $type eq 'modify' ) {
90     # open file load data, kill include calls, pass data to the template
91         my $file = _get_filepath($referer, 1);  # 2nd argument triggers themelanguage call
92         if (! -r $file) {
93                 $error = "Cannot read file: '$file'.";
94         } else {
95                 (-w $file) or $error = 
96                         "WARNING: You will not be able save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
97         open (INFILE, $file) or die "Cannot read file '$file'";         # unlikely death, since we just checked
98                 my $help = '';
99                 while ( my $inp = <INFILE> ) {
100                         unless ( $inp =~ /TMPL\_INCLUDE/ ) {
101                                 $help .= $inp;
102                         }
103                 }
104                 close INFILE;
105         $template->param( 'help' => $help );
106                 $type = 'save';
107         }
108 }
109
110 $template->param(
111     'referer' => $referer,
112     'type'    => $type,
113 );
114 ($error) and $template->param('error' => $error);
115 output_html_with_http_headers $input, "", $template->output;