r8879@llin: dpavlin | 2005-11-14 18:57:21 +0100
[webpac2] / web / browse.cgi
index 5f1b2c3..0a67b96 100755 (executable)
@@ -1,8 +1,13 @@
 #!/usr/bin/perl -w
 
+use strict;
+
 use Cwd qw/abs_path/;
 use CGI::Carp qw(fatalsToBrowser);
 use CGI::Simple;
+use File::Slurp;
+use Data::Dumper;
+use Text::Iconv;
 
 use lib '../lib';
 
@@ -13,6 +18,10 @@ my $abs_path = abs_path($0);
 $abs_path =~ s#/[^/]*$#/../#;
 
 my $db_path = $abs_path . '/db/';
+my $template = 'html_ffzg.tt';
+
+my $iconv_utf8 = new Text::Iconv('ISO-8859-2', 'UTF-8');
+my $iconv_loc = new Text::Iconv('UTF-8', 'ISO-8859-2');
 
 my $db = new WebPAC::DB(
        path => $db_path,
@@ -30,39 +39,104 @@ my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );
 
 my $rec = $q->param('rec') || 1;
 
-print $q->header;
+print $q->header( -charset    => 'utf-8' );
 
 if ($q->path_info =~ m#xml#) {
 
        my @ds = $db->load_ds($rec);
 
-       if (@ds) {
+       if (@ds && $#ds > 0) {
                print qq{<response>
-<action type='html' target='divRecord' errorCode='' errorMessage='' >
+<action type='html' target='div_record' errorCode='' errorMessage='' >
                }, $out->apply(
-                       template => 'html_ffzg.tt',
+                       template => $template,
                        data => \@ds,
                ), qq{
 
-<script type='text/javascript'>
+</action>
+<action type='javascript' errorCode='' errorMessage='' >
 <!--
-       var el = iwfGetById('divRecordNr');
+       var el = iwfGetById('div_record_nr');
        if (el) el.innerHTML = '# <b>$rec</b>';
-       //iwfShow('divRecord');
-       iwfOpacity('divRecord', 100);
+       //iwfShow('div_record');
+       iwfOpacity('div_record', 100);
 //-->
-</script>
-
 </action>
 </response>
 };
                exit;
        } else {
-               print qq{
-                       <b>Record $rec not found!</b>
-               };
+               print qq{<response>
+<action type='html' target='div_record' errorCode='' errorMessage='' >
+
+<b>Record $rec not found!</b>
+</action>
+<action type='javascript' errorCode='' errorMessage='' >
+<!--
+       var el = iwfGetById('div_record_nr');
+       if (el) el.innerHTML = '<strike>$rec</strike>';
+//-->
+</action>
+</response>
+};
+               exit;
        }
 
+} elsif ($q->path_info =~ m#template#) {
+
+               my @actions;
+
+               if ($q->param('save_template')) {
+
+                       my $tmpl = $q->param('tt_template') || die "no template?";
+                       my $tmpl_file = $out->{'include_path'} . '/' . $template;
+                       write_file($tmpl_file . '.new', $iconv_loc->convert($tmpl)) || die "can't save $tmpl_file: $!";
+                       rename $tmpl_file . '.new', $tmpl_file || die "can't rename to $tmpl_file: $!";
+
+                       print qq{<response>
+<action type='html' target='div_status' errorCode='' errorMessage='' >
+$template saved
+</action>
+<action type='js'>
+<!--
+reload_rec();
+-->
+</action>
+</response>
+                       };
+                       exit;
+
+               }
+
+               my $tmpl = read_file($out->{'include_path'} . '/' . $template) || die "can't read template $template: $!";
+               $tmpl = $q->escapeHTML($iconv_utf8->convert($tmpl));
+
+               print qq{<response>
+<action type='html' target='div_template' errorCode='' errorMessage='' >
+<pre>}, Dumper($q->Vars), qq{</pre>
+
+<form name="frmEditor" action="$self" method="post"
+iwfTarget="div_status"
+>
+
+<textarea name="tt_template" cols="80" rows="10" style="display: block;">
+$tmpl
+</textarea>
+
+<br/>
+<input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
+<input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" />
+<span id="div_status" style="color: #808080;">idle</span>
+
+<input type='hidden' value='hidden post value' name='hidValue' />
+
+</form>
+</action>
+</response>
+               };
+
+               exit;
+
 } else {
        print qq{
 <html>
@@ -78,15 +152,14 @@ var rec = $rec ;
 var url = '$self';
 
 function update_status(text) {
-       var el = iwfGetById('divRecordNr');
+       var el = iwfGetById('div_record_nr');
        if (el) el.innerHTML = text;
 }
-       
 
 function load_rec(nr) {
        update_status(nr+'...');
-       iwfRequest( url+'/xml/?rec='+nr, 'divRecord' );
-       iwfOpacity('divRecord', 30);
+       iwfRequest( url+'/xml/?rec='+nr, 'div_record' );
+       iwfOpacity('div_record', 30);
 }
 
 function inc_rec() {
@@ -101,22 +174,46 @@ function dec_rec() {
        return false;
 }
 
+function reload_rec() {
+       load_rec(rec);
+       return false;
+}
+
+function init_page() {
+       load_rec(rec);
+       // load template
+       iwfRequest( url+'/template/', 'div_template' );
+}
+
 </script>
 </head>
-<body onload="load_rec($rec);">
+<body onload="init_page();">
 
 db_path = <tt>$db_path</tt><br/>
+template = <tt>$template</tt><br/>
+
+<div id="iwfLog" style="display: none;">
+</div>
 
 <div style="background: #e0e0e0; padding: 0.5em; display: block;">
        <a href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>&nbsp;
-       <span id="divRecordNr"> none </span>
+       <span id="div_record_nr"> none </span>
+
        <a href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>&nbsp;
+       <a href="$self?rec=}, $rec, qq{" onClick="return reload_rec();">&#8634;</a>&nbsp;
+       <a href="#" onClick="iwfRefreshLog(); return false;">&#9636;</a>&nbsp;
+
 </div>
 
-<div id="divRecord">
+<div id="div_template">
+<span style="color: #808080;"> no template loaded yet. </span>
+</div>
+
+<div id="div_record" style="display: block;">
 <span style="color: #808080;"> no record loaded yet. </span>
 </div>
 
+
 </body>
 </html>};