/* form generation kit */

VAR fn_db : STRING
VAR form : INTEGER = rewrite('ramtext:form')

PROCEDURE html_base
VAR rt : INTEGER = rewrite('ramtext')
  writeln(rt,'<html><body bgcolor="#e8e8e8">')
  writeln(rt,'<h2><a href="http://www.tdb-engine.de">tdbengine</a>: form generation kit</h2>')
  writeln(rt,'<form action="'+paramstr(0)+'" method="post">')
  writeln(rt,'<b>database: </b><input type="submit" name="load" value="#database#">')
  writeln(rt,'</form>')
  writeln(rt,'#content#')
  writeln(rt,'</body></html>')
ENDPROC

PROCEDURE cgiwrite(s : STRING)
  write(form,s)
ENDPROC

PROCEDURE cgiwriteln(s : STRING)
  writeln(form,s)
ENDPROC

PROCEDURE PrintForm(db : INTEGER)
VAR i : INTEGER
  WHILE i++<=MaxLabel(db) DO
    cgiwrite('<input type="checkbox" name="c_'+str(i)+'" value="1"')
    IF cgigetparam('c_'+str(i)) THEN cgiwrite(' checked') END
    cgiwriteln('>&nbsp;'+tohtml(Label(db,i))+'<br>')
  END
  subst('#structure#','ramtext:form')
  form:=rewrite('ramtext:form')
ENDPROC

PROCEDURE GenForm(db : INTEGER)
VAR i,j,p : INTEGER
VAR s,t,l : STRING
  cgiwriteln('<!-- input-formular generated for tdbengine database: '+dbdir(db)+dbname(db)+' -->')
  cgiwriteln('<form action="#prg#" method="post">')
  cgiwriteln('<table border="0" bgcolor="white">')
  WHILE i++<=MaxLabel(db) DO
    IF cgigetparam('c_'+str(i)) THEN
      s:=getstructure(db,i); p:=pos(',',s); l:=s[1,p-1]; s:=s[p+1,255];
      p:=pos(',',s); t:=s[1,p-1]; s:=s[p+1,255];
      cgiwriteln('  <tr><td valign="top">'+tohtml(l)+'</td>')
      cgiwriteln('    <td>')
      cgiwrite('      <input type="hidden" name="input_'+tohtml(l)+'" value="1">')
      IF t like 'STRING' THEN
        cgiwriteln('<input type="text" name="'+tohtml(l)+'" value="#'+tohtml(l)+'#" maxchars="'+s+'">')
      ELSIF t IN ['NUMBER','DATE','TIME'] THEN
        cgiwriteln('<input type="text" name="'+tohtml(l)+'" value="#'+tohtml(l)+'#">')
      ELSIF t like 'AUTO' THEN
        cgiwriteln('!! no input field !!')
      ELSIF t like 'MEMO' THEN
        cgiwriteln('<textarea name="text:'+tohtml(l)+'" cols="40" rows="5">#'+tohtml(l)+'#</textarea>')
      ELSIF t like 'BOOLEAN' THEN
        cgiwriteln('<input type="checkbox" name="'+tohtml(l)+'" value="JA" #ck_'+tohtml(l)+'>')
      ELSIF t like 'SELECT'
        cgiwriteln('<select name="'+tohtml(l)+'">'); j:=0
        WHILE s DO
          IF p:=pos(',',s) THEN t:=s[1,p-1]; s:=s[p+1,255] ELSE t:=s; s:='' END
          cgiwriteln('        <option value="'+tohtml(t)+'" #sel_'+str(j++)+'#>'+tohtml(t)+'</option>')
        END
        cgiwriteln('      </select>')
      ELSE
        cgiwriteln('no supported: '+tohtml(t))
      END
      cgiwriteln('    </td>')
      cgiwriteln('  </tr>')
    END
  END
  cgiwriteln('</table>')
  cgiwriteln('</form>')
  subst('#formular#','ramtext:form')
  subst('#formular_txt#','ramtext:form',1)
ENDPROC

PROCEDURE Main
VAR db : INTEGER
  IF cgigetparam('load')
    system.cgiwriteln('location: '+exchange(paramstr(0),'genform','scanfile')+'?filter=*.dat&application='+paramstr(0))
    system.cgiwriteln('')
    HALT
  END
  system.cgiwriteln('content-type: text/html')
  system.cgiwriteln('')
  html_base
  IF fn_db:=getquerystring('filename') OR fn_db:=cgigetparam('filename') THEN
    subst('#database#',fn_db,1)
    setpara('ec 1'); db:=opendb(fn_db); setpara('ec 0')
    IF TDB_LastError THEN subst('#content#','error: '+TDB_ErrorStr(TDB_LastError),1)
    ELSE
      subst('#content#','extern:genform.frm')
      subst('#filename#',fn_db,1)
      PrintForm(db)
      GenForm(db)
    END
  ELSE
    subst('#database#','open'); subst('#content#','')
  END
  WHILE subst('#action#',paramstr(0)) DO END
  cgiwritetemplate
ENDPROC