<%

<!--#INCLUDE FILE="functions.nxscript"-->

var
  aSession: TnxSession;
  adb: TnxDatabase;
  aQuery: TnxQuery;
  aTable: TnxTable;
  IndexFieldName: string;
  TableName: string;
  DatabaseName: string;
  Back: String;
  RecordID: string;
  i: integer;
  isOk: Boolean;
begin
%>

<!--#INCLUDE FILE="header.html"-->

<h3>Edit Record Example</h3>

<%
  Back:=Request('BackLink');
  if Back='' then
    Back:=Server.Headers.Values['Referer'];
  // get the database name from the passed in params
  DatabaseName:=Request('DatabaseName');
  // if there's no database name report error and exit
  if DatabaseName='' then
  begin
    Output.Writeln('ERROR: No database name');
    exit;
  end;

  // get the indexfieldname from the passed in params
  TableName:=Request('TableName');
  // if there's no field name report error and exit
  if TableName='' then
  begin
    Output.Writeln('ERROR: No table name');
    exit;
  end;

  // get the indexfieldname from the passed in params
  IndexFieldName:=Request('IndexFieldName');
  // if there's no field name report error and exit
  if IndexFieldName='' then
  begin
    Output.Writeln('ERROR: No index field name');
    exit;
  end;

  // get the id from the passed in params
  RecordID:=Request('ID');
  // if there's no id report error and exit
  if RecordID='' then
  begin
    Output.Writeln('ERROR: No record ID');
    exit;
  end;

  // defined in functions.nxscript
  CreateNxAccessComponents(aSession, aDB, aQuery, aTable);
  // it's important to wrap that into a try/finally to make sure the 
  // created instances are freed again!
  try
    // set the db name
    adb.AliasName:=DatabaseName;
    aTable.TableName:=TableName;
    aTable.Open;
    if not aTable.Locate(IndexFieldName, RecordID, []) then
    begin
      Output.Writeln('ERROR: Record not found. Might have been deleted.');
      exit;
    end;

  // check if we're in post data mode; if yes then we try to update the table here
  // first we try to set to edit mode. if that fails report error and continue
  if Request('postdata')='1' then
  begin
    isOk:=true;
    try
      aTable.Edit;
    except
      Output.WriteLn('ERROR: '+ExceptionToString(ExceptionType, ExceptionParam));
      IsOk:=false;
    end;

    if IsOk then
    try
      // now since we've locked the record we check whether it still has the same data 
      for i:=0 to aTable.FieldCount-1 do
        if not SameText(aTable.Fields.Fields[i].AsString, ServerDecode(Request(aTable.Fields.Fields[i].FieldName+'_oldValue'))) 
          and not (aTable.Fields.Fields[i].DataType = ftBlob) 
          and not (aTable.Fields.Fields[i].DataType = ftGraphic) then
          IsOk:=false;
       if not IsOK then
       begin
         Output.WriteLn('ERROR: The data has been changed.');
         // make sure to release the lock
         aTable.Cancel;
       end;

       if IsOk then
       begin
         // all is ok, so lets update the data
         for i:=0 to aTable.FieldCount-1 do
           if not (aTable.Fields.Fields[i].DataType = ftBlob) and not (aTable.Fields.Fields[i].DataType = ftGraphic) then
             aTable.Fields.Fields[i].AsString:=ServerDecode(Request(aTable.Fields.Fields[i].FieldName+'_newValue'));
 
         // and finally post it
         aTable.Post;
         Output.WriteLn('Record successfully updated<br>');
         Output.WriteLn('<a href="'+ServerDecode(back)+'">Back</a>');
       end;
    except
      Output.WriteLn('ERROR: '+ExceptionToString(ExceptionType, ExceptionParam));
      // make sure to release the lock
      if aTable.State = dsEdit then
        aTable.Cancel;
    end;
  end;

%>

<form>
<table width="80%">
<%      
    Output.WriteLn('<tr class=header>');
    Output.WriteLn('<td>Field Name</td>');
    if Request('postdata')<>'1' then
    begin
      Output.WriteLn('<td>Old Value</td>');
      Output.WriteLn('<td>New Value</td>');
    end
    else
      Output.WriteLn('<td>Value</td>');
    Output.WriteLn('</tr>');

    // write the table headers
    for i:=0 to aTable.FieldCount-1 do
    begin
      Output.WriteLn('<tr>');
      Output.WriteLn('<td>'+aTable.Fields.Fields[i].FieldName+'</td>');
      Output.WriteLn('<td>'+aTable.Fields.Fields[i].AsString+'</td>');

      // write new value input fields if we're not posting data
      if Request('postdata')<>'1' then
      begin
        if SameText(aTable.Fields.Fields[i].FieldName, IndexFieldName) 
          or (aTable.Fields.Fields[i].DataType = ftBlob) 
          or (aTable.Fields.Fields[i].DataType = ftGraphic) 
          then
        begin
          Output.WriteLn('<td>'+aTable.Fields.Fields[i].AsString+' (readonly)</td>')
          Output.WriteLn('<td><input type=hidden name='+aTable.Fields.Fields[i].FieldName+'_newvalue value="'+aTable.Fields.Fields[i].AsString+'"></td>');
        end
        else
          Output.WriteLn('<td><input type=text name='+aTable.Fields.Fields[i].FieldName+'_newvalue size=80 value="'+aTable.Fields.Fields[i].AsString+'"></td>');
      end;

      Output.WriteLn('<td><input type=hidden name='+aTable.Fields.Fields[i].FieldName+'_oldvalue value="'+aTable.Fields.Fields[i].AsString+'"></td>');
      
      Output.WriteLn('</tr>');
    end;
%>

  </table>
<%
    Output.WriteLn('<input type=hidden name=fieldcount size=80 value="'+inttostr(aTable.FieldCount)+'"></td>');
    Output.WriteLn('<input type=hidden name=postdata value="1"></td>');
    Output.WriteLn('<input type=hidden name=databasename value="'+databasename+'"></td>');
    Output.WriteLn('<input type=hidden name=tablename value="'+tablename+'"></td>');
    Output.WriteLn('<input type=hidden name=indexfieldname value="'+indexfieldname+'"></td>');
    Output.WriteLn('<input type=hidden name=id value="'+recordid+'"></td>');
    Output.WriteLn('<input type=hidden name=backlink value="'+back+'"></td>');
  
    Output.WriteLn('<a href="'+ServerDecode(back)+'">Back</a>');

    if Request('postdata')<>'1' then
      Output.WriteLn('<input type=submit value="Submit"></td>');
  finally
    aQuery.Free;
    aTable.Free;
    adb.free;
    aSession.Free;
  end;
%>
  </form>

<!--#INCLUDE FILE="footer.html"-->

<%
end.
%>