/[webpac2]/trunk/web/browse.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/web/browse.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (hide annotations)
Sun Nov 20 20:44:15 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 8644 byte(s)
 r8995@llin:  dpavlin | 2005-11-20 21:45:05 +0100
 fix save template

1 dpavlin 46 #!/usr/bin/perl -w
2    
3 dpavlin 54 use strict;
4    
5 dpavlin 46 use Cwd qw/abs_path/;
6     use CGI::Carp qw(fatalsToBrowser);
7     use CGI::Simple;
8 dpavlin 49 use File::Slurp;
9 dpavlin 54 use Data::Dumper;
10     use Text::Iconv;
11 dpavlin 46
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 dpavlin 72 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 dpavlin 67 my $css_file = 'user.css';
25 dpavlin 46
26 dpavlin 54 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 dpavlin 46 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 dpavlin 72 include_path => $template_path,
37 dpavlin 46 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 dpavlin 72 my $template_filename = $q->param('template') || $templates[0];
45 dpavlin 46
46 dpavlin 54 print $q->header( -charset => 'utf-8' );
47 dpavlin 46
48 dpavlin 67 ##---- 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 dpavlin 68 $content =~ s/^[\n\r]+//s;
61 dpavlin 67 $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 dpavlin 46 if ($q->path_info =~ m#xml#) {
81    
82 dpavlin 70 my $ds = $db->load_ds($rec);
83 dpavlin 46
84 dpavlin 70 if ($ds) {
85 dpavlin 46 print qq{<response>
86 dpavlin 49 <action type='html' target='div_record' errorCode='' errorMessage='' >
87 dpavlin 58 }, $iconv_utf8->convert( $out->apply(
88 dpavlin 72 template => $template_filename,
89 dpavlin 70 data => $ds,
90 dpavlin 58 ) ), qq{
91 dpavlin 46
92 dpavlin 57 </action>
93     <action type='javascript' errorCode='' errorMessage='' >
94 dpavlin 46 <!--
95 dpavlin 49 var el = iwfGetById('div_record_nr');
96 dpavlin 46 if (el) el.innerHTML = '# <b>$rec</b>';
97 dpavlin 49 //iwfShow('div_record');
98     iwfOpacity('div_record', 100);
99 dpavlin 46 //-->
100     </action>
101     </response>
102     };
103     exit;
104     } else {
105 dpavlin 49 print qq{<response>
106     <action type='html' target='div_record' errorCode='' errorMessage='' >
107    
108     <b>Record $rec not found!</b>
109 dpavlin 57 </action>
110     <action type='javascript' errorCode='' errorMessage='' >
111 dpavlin 49 <!--
112     var el = iwfGetById('div_record_nr');
113 dpavlin 59 if (el) el.innerHTML = '<strike>&nbsp;$rec&nbsp;</strike>';
114 dpavlin 49 //-->
115     </action>
116     </response>
117     };
118     exit;
119 dpavlin 46 }
120    
121 dpavlin 49 } elsif ($q->path_info =~ m#template#) {
122    
123 dpavlin 72 my $template_path = $out->{'include_path'} . '/' . $template_filename;
124 dpavlin 49
125 dpavlin 54 if ($q->param('save_template')) {
126    
127 dpavlin 67 update_file($template_path, $q->param('tt_template'));
128 dpavlin 58
129 dpavlin 54 print qq{<response>
130 dpavlin 67 <action type='html' target='div_template_status' errorCode='' errorMessage='' >
131 dpavlin 72 <tt>$template_filename</tt> saved
132 dpavlin 54 </action>
133     <action type='js'>
134     <!--
135 dpavlin 67 iwfShow('div_template_status', 1);
136 dpavlin 54 reload_rec();
137 dpavlin 67 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
138 dpavlin 54 -->
139     </action>
140     </response>
141     };
142     exit;
143    
144     }
145    
146 dpavlin 67 my $tmpl = get_file_in_html($template_path);
147 dpavlin 54
148 dpavlin 49 print qq{<response>
149     <action type='html' target='div_template' errorCode='' errorMessage='' >
150 dpavlin 54
151 dpavlin 67 <form name="frmEditor" action="$self" method="post" iwfTarget="div_template_status" >
152 dpavlin 54
153     <textarea name="tt_template" cols="80" rows="10" style="display: block;">
154 dpavlin 49 $tmpl
155     </textarea>
156 dpavlin 54
157     <br/>
158     <input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
159 dpavlin 61 <!--
160     <input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" /> checkin
161     -->
162 dpavlin 67 &nbsp;&nbsp;<span id="div_template_status" style="color: #808080;">idle</span>
163 dpavlin 54
164 dpavlin 77 <input type="hidden" name="template" value="$template_filename" />
165 dpavlin 54
166     </form>
167 dpavlin 49 </action>
168 dpavlin 61 <action type='js'>
169     <!--
170 dpavlin 67 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
171 dpavlin 61 -->
172     </action>
173 dpavlin 49 </response>
174 dpavlin 54 };
175 dpavlin 49
176 dpavlin 54 exit;
177    
178 dpavlin 67 } 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 dpavlin 46 } else {
233 dpavlin 72
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 dpavlin 67 print <<"_END_OF_HEAD_";
250 dpavlin 46 <html>
251     <head>
252     <title>WebPAC simple browse interface</title>
253 dpavlin 67
254     <link id="user_css_link" href="user.css" type="text/css" rel="stylesheet">
255    
256 dpavlin 46 <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 dpavlin 67 <script type='text/javascript' src='iwf/iwconfig.js'></script>
261 dpavlin 46 <script type='text/javascript'>
262    
263     var rec = $rec ;
264     var url = '$self';
265 dpavlin 72 var template_filename = '$template_filename';
266 dpavlin 46
267 dpavlin 67 var css_rnd = 0;
268    
269 dpavlin 46 function update_status(text) {
270 dpavlin 49 var el = iwfGetById('div_record_nr');
271 dpavlin 46 if (el) el.innerHTML = text;
272     }
273    
274     function load_rec(nr) {
275 dpavlin 59 if (nr == 1) {
276     iwfHide('a_left_arr', 1);
277     } else {
278     iwfShow('a_left_arr', 1);
279     }
280 dpavlin 46 update_status(nr+'...');
281 dpavlin 72 iwfRequest( url+'/xml/?template='+template_filename+'&rec='+nr, 'div_record' );
282 dpavlin 49 iwfOpacity('div_record', 30);
283 dpavlin 46 }
284    
285     function inc_rec() {
286     rec++;
287     load_rec(rec);
288     return false;
289     }
290    
291     function dec_rec() {
292 dpavlin 59 if (rec > 1) {
293     rec--;
294     load_rec(rec);
295     }
296 dpavlin 46 return false;
297     }
298    
299 dpavlin 47 function reload_rec() {
300     load_rec(rec);
301     return false;
302     }
303    
304 dpavlin 67 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 dpavlin 49 function init_page() {
317 dpavlin 67 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 dpavlin 49 load_rec(rec);
326 dpavlin 67
327     // load template editor
328 dpavlin 72 iwfRequest( url+'/template/?template='+template_filename, 'div_template' );
329 dpavlin 67 // load css editor
330     iwfRequest( url+'/css/', 'div_css' );
331 dpavlin 49 }
332    
333 dpavlin 46 </script>
334 dpavlin 67
335 dpavlin 46 </head>
336 dpavlin 49 <body onload="init_page();">
337 dpavlin 46
338 dpavlin 67 <div id="iwfLog">
339     </div>
340    
341 dpavlin 46 db_path = <tt>$db_path</tt><br/>
342 dpavlin 72 template = $template_form<br/>
343 dpavlin 67 css = <tt>$css_file</tt>
344 dpavlin 46
345     <div style="background: #e0e0e0; padding: 0.5em; display: block;">
346 dpavlin 59 <a id="a_left_arr" href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>
347 dpavlin 49 <span id="div_record_nr"> none </span>
348 dpavlin 47
349 dpavlin 59 <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 dpavlin 67 <a href="#" onClick="iwfShowLog(); return false;">&#9636;</a>
352 dpavlin 54
353 dpavlin 46 </div>
354    
355 dpavlin 67 <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 dpavlin 49 <div id="div_template">
363     <span style="color: #808080;"> no template loaded yet. </span>
364     </div>
365    
366 dpavlin 67 <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 dpavlin 49 <div id="div_record" style="display: block;">
373 dpavlin 46 <span style="color: #808080;"> no record loaded yet. </span>
374     </div>
375    
376 dpavlin 49
377 dpavlin 46 </body>
378 dpavlin 67 </html>
379     _END_OF_HEAD_
380 dpavlin 46
381 dpavlin 67 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26