r8984@llin: dpavlin | 2005-11-20 02:51:47 +0100
[webpac2] / web / browse.cgi
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Cwd qw/abs_path/;
6 use CGI::Carp qw(fatalsToBrowser);
7 use CGI::Simple;
8 use File::Slurp;
9 use Data::Dumper;
10 use Text::Iconv;
11
12 use lib '../lib';
13
14 use WebPAC::DB;
15 use WebPAC::Output::TT;
16
17 my $abs_path = abs_path($0);
18 $abs_path =~ s#/[^/]*$#/../#;
19
20 my $db_path = $abs_path . '/db/';
21 my $template_path = "$abs_path/conf/output/tt";
22 opendir(my $dir, $template_path) || die "can't open template path $template_path: $!";
23 my @templates = grep { /\.tt$/i } readdir($dir);
24 my $css_file = 'user.css';
25
26 my $iconv_utf8 = new Text::Iconv('ISO-8859-2', 'UTF-8');
27 my $iconv_loc = new Text::Iconv('UTF-8', 'ISO-8859-2');
28
29 my $db = new WebPAC::DB(
30         path => $db_path,
31         read_only => 1,
32         debug => 1,
33 );
34
35 my $out = new WebPAC::Output::TT(
36         include_path => $template_path,
37         filters => { foo => sub { shift } },
38 );
39
40 my $q = new CGI::Simple;
41 my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );
42
43 my $rec = $q->param('rec') || 1;
44 my $template_filename = $q->param('template') || $templates[0];
45
46 print $q->header( -charset    => 'utf-8' );
47
48 ##---- some handy subs
49
50 sub update_file($$) {
51         my ($path, $content) = @_;
52
53         $content = $iconv_loc->convert( $content ) || die "no content?";
54
55         sub _conv_js {
56                 my $t = shift || return;
57                 return $iconv_loc->convert(chr(hex($t)));
58         }
59         $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
60         $content =~ s/^[\n\r]+//s;
61         $content =~ s/[\n\r]+$//s;
62
63         write_file($path . '.new', $content) || die "can't save ${path}.new $!";
64         rename $path . '.new', $path || die "can't rename to $path: $!";
65 }
66
67 sub get_file_in_html($) {
68         my ($path) = @_;
69
70         die "no path?" unless ($path);
71
72         my $content = read_file($path) || die "can't read $path: $!";
73         $content = $q->escapeHTML($iconv_utf8->convert($content));
74
75         return $content;
76 }
77
78 ##----
79
80 if ($q->path_info =~ m#xml#) {
81
82         my $ds = $db->load_ds($rec);
83
84         if ($ds) {
85                 print qq{<response>
86 <action type='html' target='div_record' errorCode='' errorMessage='' >
87                 }, $iconv_utf8->convert( $out->apply(
88                         template => $template_filename,
89                         data => $ds,
90                 ) ), qq{
91
92 </action>
93 <action type='javascript' errorCode='' errorMessage='' >
94 <!--
95         var el = iwfGetById('div_record_nr');
96         if (el) el.innerHTML = '# <b>$rec</b>';
97         //iwfShow('div_record');
98         iwfOpacity('div_record', 100);
99 //-->
100 </action>
101 </response>
102 };
103                 exit;
104         } else {
105                 print qq{<response>
106 <action type='html' target='div_record' errorCode='' errorMessage='' >
107
108 <b>Record $rec not found!</b>
109 </action>
110 <action type='javascript' errorCode='' errorMessage='' >
111 <!--
112         var el = iwfGetById('div_record_nr');
113         if (el) el.innerHTML = '<strike>&nbsp;$rec&nbsp;</strike>';
114 //-->
115 </action>
116 </response>
117 };
118                 exit;
119         }
120
121 } elsif ($q->path_info =~ m#template#) {
122
123                 my $template_path = $out->{'include_path'} . '/' . $template_filename;
124
125                 if ($q->param('save_template')) {
126
127                         update_file($template_path, $q->param('tt_template'));
128
129                         print qq{<response>
130 <action type='html' target='div_template_status' errorCode='' errorMessage='' >
131 <tt>$template_filename</tt> saved
132 </action>
133 <action type='js'>
134 <!--
135 iwfShow('div_template_status', 1);
136 reload_rec();
137 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
138 -->
139 </action>
140 </response>
141                         };
142                         exit;
143
144                 }
145
146                 my $tmpl = get_file_in_html($template_path);
147
148                 print qq{<response>
149 <action type='html' target='div_template' errorCode='' errorMessage='' >
150
151 <form name="frmEditor" action="$self" method="post" iwfTarget="div_template_status" >
152
153 <textarea name="tt_template" cols="80" rows="10" style="display: block;">
154 $tmpl
155 </textarea>
156
157 <br/>
158 <input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
159 <!--
160 <input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" /> checkin
161 -->
162 &nbsp;&nbsp;<span id="div_template_status" style="color: #808080;">idle</span>
163
164 <input type='hidden' value='hidden post value' name='hidValue' />
165
166 </form>
167 </action>
168 <action type='js'>
169 <!--
170 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
171 -->
172 </action>
173 </response>
174                 };
175
176                 exit;
177
178 } elsif ($q->path_info =~ m#css#) {
179
180                 my $css_path = $abs_path . '/web/' . $css_file;
181
182
183                 if ($q->param('save_css')) {
184                         update_file($css_path, $q->param('user_css'));
185
186                         print qq{<response>
187 <action type='html' target='div_css_status' errorCode='' errorMessage='' >
188 <tt>$css_file</tt> saved
189 </action>
190 <action type='js'>
191 <!--
192 iwfShow('div_css_status', 1);
193 // switch css
194 css_rnd++;
195 iwfLog('loading user.css?'+css_rnd);
196 iwfGetById('user_css_link').href = 'user.css?'+css_rnd;
197 iwfHideGentlyDelay('div_css_status', 2, 2000, 1);
198 -->
199 </action>
200 </response>
201                         };
202                         exit;
203
204                 }
205
206                 my $user_css = get_file_in_html($css_path);
207
208                 print qq{<response>
209 <action type='html' target='div_css' errorCode='' errorMessage='' >
210
211 <form name="frmCSSEditor" action="$self" method="post" iwfTarget="div_css_status" >
212
213 <textarea name="user_css" cols="80" rows="10" style="display: block; width: 100%;">
214 $user_css
215 </textarea>
216
217 <br/>
218 <input type="button" name="save_css" value="Save" onclick="javascript:iwfRequest(this);" />
219 &nbsp;&nbsp;<span id="div_css_status" style="color: #808080;">idle</span>
220 </form>
221 </action>
222 <action type='js'>
223 <!--
224 iwfLog('loaded CSS template');
225 -->
226 </action>
227 </response>
228                 };
229
230                 exit;
231
232 } else {
233
234         my $template_form = qq{
235                 <form action="$self" method="get" style="display: inline;">
236                 <select name="template">
237         };
238         foreach my $t (@templates) {
239                 my $s = '';
240                 $s = ' selected' if ($t eq $template_filename);
241                 $template_form .= qq{<option$s>$t</option>};
242         }
243         $template_form .= qq{
244                 </select>
245                 <input type="submit" name="ch_template" value="Switch"/>
246                 </form>
247         };
248
249         print <<"_END_OF_HEAD_";
250 <html>
251 <head>
252 <title>WebPAC simple browse interface</title>
253
254 <link id="user_css_link" href="user.css" type="text/css" rel="stylesheet"> 
255
256 <script type='text/javascript' src='iwf/iwfcore.js'></script>
257 <script type='text/javascript' src='iwf/iwfgui.js'></script>
258 <script type='text/javascript' src='iwf/iwfxml.js'></script>
259 <script type='text/javascript' src='iwf/iwfajax.js'></script>
260 <script type='text/javascript' src='iwf/iwconfig.js'></script>
261 <script type='text/javascript'>
262
263 var rec = $rec ;
264 var url = '$self';
265 var template_filename = '$template_filename';
266
267 var css_rnd = 0;
268
269 function update_status(text) {
270         var el = iwfGetById('div_record_nr');
271         if (el) el.innerHTML = text;
272 }
273
274 function load_rec(nr) {
275         if (nr == 1) {
276                 iwfHide('a_left_arr', 1);
277         } else {
278                 iwfShow('a_left_arr', 1);
279         }
280         update_status(nr+'...');
281         iwfRequest( url+'/xml/?template='+template_filename+'&rec='+nr, 'div_record' );
282         iwfOpacity('div_record', 30);
283 }
284
285 function inc_rec() {
286         rec++;
287         load_rec(rec);
288         return false;
289 }
290
291 function dec_rec() {
292         if (rec > 1) {
293                 rec--;
294                 load_rec(rec);
295         }
296         return false;
297 }
298
299 function reload_rec() {
300         load_rec(rec);
301         return false;
302 }
303
304 function edit_template() {
305         iwfHideGently('div_css', 30, 1);
306         iwfShowGently('div_template', 30, 1);
307         return false;
308 }
309
310 function edit_css() {
311         iwfHideGently('div_template', 30, 1);
312         iwfShowGently('div_css', 30, 1);
313         return false;
314 }
315
316 function init_page() {
317         iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
318         iwfLog('div_template = ' + iwfX('div_template') + ':' + iwfY('div_template'));
319
320         iwfX('div_css', iwfX('div_template'));
321         iwfY('div_css', iwfY('div_template'));
322
323         iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
324
325         load_rec(rec);
326
327         // load template editor
328         iwfRequest( url+'/template/?template='+template_filename, 'div_template' );
329         // load css editor
330         iwfRequest( url+'/css/', 'div_css' );
331 }
332
333 </script>
334
335 </head>
336 <body onload="init_page();">
337
338 <div id="iwfLog">
339 </div>
340
341 db_path = <tt>$db_path</tt><br/>
342 template = $template_form<br/>
343 css = <tt>$css_file</tt>
344
345 <div style="background: #e0e0e0; padding: 0.5em; display: block;">
346         <a id="a_left_arr" href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>
347         <span id="div_record_nr"> none </span>
348
349         <a id="a_right_arr" href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>
350         <a id="a_reload" href="$self?rec=}, $rec, qq{" onClick="return reload_rec();">&#8634;</a>
351         <a href="#" onClick="iwfShowLog(); return false;">&#9636;</a>
352
353 </div>
354
355 <div>
356
357 <div style="display: block;">
358 Editor
359 <a id="a_template" href="#" onClick="return edit_template();">template</a>
360 <a id="a_css" href="#" onClick="return edit_css();">css</a>
361
362 <div id="div_template">
363 <span style="color: #808080;"> no template loaded yet. </span>
364 </div>
365
366 <div id="div_css" style="position: absolute; display: none;">
367 <span style="color: #808080;"> no CSS loaded yet. </span>
368 </div>
369
370 </div>
371
372 <div id="div_record" style="display: block;">
373 <span style="color: #808080;"> no record loaded yet. </span>
374 </div>
375
376
377 </body>
378 </html>
379 _END_OF_HEAD_
380
381 }