/[webpac2]/branches/Sack/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

Diff of /branches/Sack/web/browse.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/web/browse.cgi revision 46 by dpavlin, Mon Nov 14 16:13:17 2005 UTC branches/Sack/web/browse.cgi revision 1310 by dpavlin, Mon Sep 21 19:04:14 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3    use strict;
4    
5  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
6  use CGI::Carp qw(fatalsToBrowser);  use CGI::Carp qw(fatalsToBrowser);
7  use CGI::Simple;  use CGI::Simple;
8    use File::Slurp;
9    use Data::Dumper;
10    use Text::Iconv;
11    
12  use lib '../lib';  use lib '../lib';
13    
# Line 13  my $abs_path = abs_path($0); Line 18  my $abs_path = abs_path($0);
18  $abs_path =~ s#/[^/]*$#/../#;  $abs_path =~ s#/[^/]*$#/../#;
19    
20  my $db_path = $abs_path . '/db/';  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(  my $db = new WebPAC::DB(
30          path => $db_path,          path => $db_path,
# Line 21  my $db = new WebPAC::DB( Line 33  my $db = new WebPAC::DB(
33  );  );
34    
35  my $out = new WebPAC::Output::TT(  my $out = new WebPAC::Output::TT(
36          include_path => "$abs_path/conf/output/tt",          include_path => $template_path,
37          filters => { foo => sub { shift } },          filters => { foo => sub { shift } },
38  );  );
39    
# Line 29  my $q = new CGI::Simple; Line 41  my $q = new CGI::Simple;
41  my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );  my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );
42    
43  my $rec = $q->param('rec') || 1;  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  print $q->header;          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    sub template_list_html($) {
79            my $current = shift || die;
80    
81            my $html = qq{ Template\n<ul> };
82    
83            foreach my $t (sort @templates) {
84                    if ($t eq $current) {
85                            $html .= qq{ <li><b>$t</b></li> };
86                    } else {
87                            $html .= qq{ <li><a href="#" onClick="load_template('$t'); return false;">$t</a></li> };
88                    }
89            }
90    
91            $html .= qq{ </ul> };
92            return $html;
93    }
94    
95    ##----
96    
97  if ($q->path_info =~ m#xml#) {  if ($q->path_info =~ m#xml#) {
98    
99          my @ds = $db->load_ds($rec);          my $ds = $db->load_ds($rec);
100    
101          if (@ds) {          if ($ds) {
102                  print qq{<response>                  print qq{<response>
103  <action type='html' target='divRecord' errorCode='' errorMessage='' >  <action type='html' target='div_record' errorCode='' errorMessage='' >
104                  }, $out->apply(                  }, $iconv_utf8->convert( $out->apply(
105                          template => 'html_ffzg.tt',                          template => $template_filename,
106                          data => \@ds,                          data => $ds,
107                  ), qq{                  ) ), qq{
108    
109  <script type='text/javascript'>  </action>
110    <action type='javascript' errorCode='' errorMessage='' >
111  <!--  <!--
112          var el = iwfGetById('divRecordNr');          var el = iwfGetById('div_record_nr');
113          if (el) el.innerHTML = '# <b>$rec</b>';          if (el) el.innerHTML = '# <b>$rec</b>';
114          //iwfShow('divRecord');          //iwfShow('div_record');
115          iwfOpacity('divRecord', 100);          iwfOpacity('div_record', 100);
116  //-->  //-->
 </script>  
   
117  </action>  </action>
118  </response>  </response>
119  };  };
120                  exit;                  exit;
121          } else {          } else {
122                  print qq{                  print qq{<response>
123                          <b>Record $rec not found!</b>  <action type='html' target='div_record' errorCode='' errorMessage='' >
124                  };  
125    <b>Record $rec not found!</b>
126    </action>
127    <action type='javascript' errorCode='' errorMessage='' >
128    <!--
129            var el = iwfGetById('div_record_nr');
130            if (el) el.innerHTML = '<strike>&nbsp;$rec&nbsp;</strike>';
131    //-->
132    </action>
133    </response>
134    };
135                    exit;
136          }          }
137    
138    } elsif ($q->path_info =~ m#template_list#) {
139    
140                    print qq{<response>
141    <action type='html' target='div_template_list' errorCode='' errorMessage='' >
142    <div>} . template_list_html($template_filename) . qq{</div>
143    </action>
144    </response>
145                    };
146    
147                    exit;
148    
149    } elsif ($q->path_info =~ m#template#) {
150    
151                    my $template_path = $out->{'include_path'} . '/' . $template_filename;
152    
153                    if ($q->param('save_template')) {
154    
155                            update_file($template_path, $q->param('tt_template'));
156    
157                            print qq{<response>
158    <action type='html' target='div_template_status' errorCode='' errorMessage='' >
159    <tt>$template_filename</tt> saved
160    </action>
161    <action type='js'>
162    <!--
163    iwfShow('div_template_status', 1);
164    reload_rec();
165    iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
166    -->
167    </action>
168    </response>
169                            };
170                            exit;
171    
172                    }
173    
174                    my $tmpl = get_file_in_html($template_path);
175    
176                    print qq{<response>
177    <action type='html' target='div_template' errorCode='' errorMessage='' >
178    
179    <form name="frmEditor" action="$self" method="post" iwfTarget="div_template_status" >
180    
181    <textarea name="tt_template" cols="80" rows="10" style="display: block;">
182    $tmpl
183    </textarea>
184    
185    <br/>
186    <input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
187    <!--
188    <input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" /> checkin
189    -->
190    &nbsp;&nbsp;<span id="div_template_status" style="color: #808080;">idle</span>
191    
192    <input type="hidden" name="template" value="$template_filename" />
193    
194    </form>
195    </action>
196    <action type='js'>
197    <!--
198    iwfOpacity('div_template', 100);
199    iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
200    -->
201    </action>
202    </response>
203                    };
204    
205                    exit;
206    
207    } elsif ($q->path_info =~ m#css#) {
208    
209                    my $css_path = $abs_path . '/web/' . $css_file;
210    
211    
212                    if ($q->param('save_css')) {
213                            update_file($css_path, $q->param('user_css'));
214    
215                            print qq{<response>
216    <action type='html' target='div_css_status' errorCode='' errorMessage='' >
217    <tt>$css_file</tt> saved
218    </action>
219    <action type='js'>
220    <!--
221    iwfShow('div_css_status', 1);
222    // switch css
223    css_rnd++;
224    iwfLog('loading user.css?'+css_rnd);
225    iwfGetById('user_css_link').href = 'user.css?'+css_rnd;
226    iwfHideGentlyDelay('div_css_status', 2, 2000, 1);
227    -->
228    </action>
229    </response>
230                            };
231                            exit;
232    
233                    }
234    
235                    my $user_css = get_file_in_html($css_path);
236    
237                    print qq{<response>
238    <action type='html' target='div_css' errorCode='' errorMessage='' >
239    
240    <form name="frmCSSEditor" action="$self" method="post" iwfTarget="div_css_status" >
241    
242    <textarea name="user_css" cols="80" rows="10" style="display: block; width: 100%;">
243    $user_css
244    </textarea>
245    
246    <br/>
247    <input type="button" name="save_css" value="Save" onclick="javascript:iwfRequest(this);" />
248    &nbsp;&nbsp;<span id="div_css_status" style="color: #808080;">idle</span>
249    </form>
250    </action>
251    <action type='js'>
252    <!--
253    iwfLog('loaded CSS template');
254    -->
255    </action>
256    </response>
257                    };
258    
259                    exit;
260    
261  } else {  } else {
262          print qq{  
263            my $template_list_html = template_list_html($template_filename);
264    
265            print <<"_END_OF_HEAD_";
266  <html>  <html>
267  <head>  <head>
268  <title>WebPAC simple browse interface</title>  <title>WebPAC simple browse interface</title>
269    
270    <link id="user_css_link" href="user.css" type="text/css" rel="stylesheet">
271    
272  <script type='text/javascript' src='iwf/iwfcore.js'></script>  <script type='text/javascript' src='iwf/iwfcore.js'></script>
273  <script type='text/javascript' src='iwf/iwfgui.js'></script>  <script type='text/javascript' src='iwf/iwfgui.js'></script>
274  <script type='text/javascript' src='iwf/iwfxml.js'></script>  <script type='text/javascript' src='iwf/iwfxml.js'></script>
275  <script type='text/javascript' src='iwf/iwfajax.js'></script>  <script type='text/javascript' src='iwf/iwfajax.js'></script>
276    <script type='text/javascript' src='iwf/iwconfig.js'></script>
277  <script type='text/javascript'>  <script type='text/javascript'>
278    
279  var rec = $rec ;  var rec = $rec ;
280  var url = '$self';  var url = '$self';
281    var template_filename = '$template_filename';
282    
283    var css_rnd = 0;
284    
285  function update_status(text) {  function update_status(text) {
286          var el = iwfGetById('divRecordNr');          var el = iwfGetById('div_record_nr');
287          if (el) el.innerHTML = text;          if (el) el.innerHTML = text;
288  }  }
           
289    
290  function load_rec(nr) {  function load_rec(nr) {
291            if (nr == 1) {
292                    iwfHide('a_left_arr', 1);
293            } else {
294                    iwfShow('a_left_arr', 1);
295            }
296          update_status(nr+'...');          update_status(nr+'...');
297          iwfRequest( url+'/xml/?rec='+nr, 'divRecord' );          iwfRequest( url+'/xml/?template='+template_filename+'&rec='+nr, 'div_record' );
298          iwfOpacity('divRecord', 30);          iwfOpacity('div_record', 30);
299  }  }
300    
301  function inc_rec() {  function inc_rec() {
# Line 96  function inc_rec() { Line 305  function inc_rec() {
305  }  }
306    
307  function dec_rec() {  function dec_rec() {
308          rec--;          if (rec > 1) {
309                    rec--;
310                    load_rec(rec);
311            }
312            return false;
313    }
314    
315    function reload_rec() {
316          load_rec(rec);          load_rec(rec);
317          return false;          return false;
318  }  }
319    
320    function edit_template() {
321            iwfHideGently('div_css', 30, 1);
322            iwfShowGently('div_template', 30, 1);
323            return false;
324    }
325    
326    function edit_css() {
327            iwfHideGently('div_template', 30, 1);
328            iwfShowGently('div_css', 30, 1);
329            return false;
330    }
331    
332    function load_template(name) {
333            iwfLog('changing template to '+name);
334            iwfOpacity('div_template', 30);
335            template_filename = name;
336            // load template editor
337            iwfRequest( url+'/template/?template='+template_filename, 'div_template' );
338            load_rec(rec);
339    
340            // refresh template list
341            iwfRequest( url+'/template_list/?template='+template_filename, 'div_template_list' );
342    }
343    
344    function init_page() {
345            iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
346            iwfLog('div_template = ' + iwfX('div_template') + ':' + iwfY('div_template'));
347    
348            iwfX('div_css', iwfX('div_template'));
349            iwfY('div_css', iwfY('div_template'));
350    
351            iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
352    
353            // load template editor and record
354            load_template(template_filename);
355    
356            // load css editor
357            iwfRequest( url+'/css/', 'div_css' );
358    }
359    
360  </script>  </script>
361    
362  </head>  </head>
363  <body onload="load_rec($rec);">  <body onload="init_page();">
364    
365    <div id="iwfLog">
366    </div>
367    
368  db_path = <tt>$db_path</tt><br/>  db_path = <tt>$db_path</tt><br/>
369    css = <tt>$css_file</tt>
370    
371  <div style="background: #e0e0e0; padding: 0.5em; display: block;">  <div style="background: #e0e0e0; padding: 0.5em; display: block;">
372          <a href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>&nbsp;          <a id="a_left_arr" href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>
373          <span id="divRecordNr"> none </span>          <span id="div_record_nr"> none </span>
374          <a href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>&nbsp;  
375            <a id="a_right_arr" href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>
376            <a id="a_reload" href="$self?rec=}, $rec, qq{" onClick="return reload_rec();">&#8634;</a>
377            <a href="#" onClick="iwfShowLog(); return false;">&#9636;</a>
378    
379  </div>  </div>
380    
381  <div id="divRecord">  
382    <div id="div_template_list" style="display: inline; border: 2px solid #ffcc88; float: right; z-index: 10;">
383    $template_list_html
384    </div>
385    
386    <div>
387    
388    <div style="border: 2px solid #ffff80;">
389    
390    Editor
391    <a id="a_template" href="#" onClick="return edit_template();">template</a>
392    <a id="a_css" href="#" onClick="return edit_css();">css</a>
393    
394    
395    <div id="div_template">
396    <span style="color: #808080;"> no template loaded yet. </span>
397    </div>
398    
399    <div id="div_css" style="position: absolute; display: none;">
400    <span style="color: #808080;"> no CSS loaded yet. </span>
401    </div>
402    
403    </div>
404    
405    <div id="div_record" style="display: block;">
406  <span style="color: #808080;"> no record loaded yet. </span>  <span style="color: #808080;"> no record loaded yet. </span>
407  </div>  </div>
408    
409    
410  </body>  </body>
411  </html>};  </html>
412    _END_OF_HEAD_
413    
414  };  }

Legend:
Removed from v.46  
changed lines
  Added in v.1310

  ViewVC Help
Powered by ViewVC 1.1.26